-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example4_W4.sql
44 lines (44 loc) · 1.37 KB
/
Example4_W4.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-- Drop existing tables in reverse order of dependencies
DROP TABLE IF EXISTS Tour CASCADE;
DROP TABLE IF EXISTS Facility CASCADE;
DROP TABLE IF EXISTS Park_Ranger CASCADE;
DROP TABLE IF EXISTS Visitor CASCADE; -- Assuming you need this
DROP TABLE IF EXISTS National_Park CASCADE;
-- Create National_Park table
CREATE TABLE National_Park (
Park_ID INT PRIMARY KEY,
Name VARCHAR(255),
Location VARCHAR(255)
);
-- Create Visitor table
CREATE TABLE Visitor (
Visitor_ID INT PRIMARY KEY,
Visitor_Name VARCHAR(255)
);
-- Create Park_Ranger table
CREATE TABLE Park_Ranger (
Ranger_ID INT PRIMARY KEY,
Name VARCHAR(255),
Park_ID INT,
Intern_Ranger_ID INT,
FOREIGN KEY (Park_ID) REFERENCES National_Park(Park_ID),
FOREIGN KEY (Intern_Ranger_ID) REFERENCES Park_Ranger(Ranger_ID) -- Recursive relationship
);
-- Create Facility table
CREATE TABLE Facility (
Facility_ID INT PRIMARY KEY,
Name VARCHAR(255),
Park_ID INT,
FOREIGN KEY (Park_ID) REFERENCES National_Park(Park_ID)
);
-- Create Tour table
CREATE TABLE Tour (
Tour_ID INT PRIMARY KEY,
Description TEXT,
Park_ID INT,
Ranger_ID INT,
Visitor_ID INT,
FOREIGN KEY (Park_ID) REFERENCES National_Park(Park_ID),
FOREIGN KEY (Ranger_ID) REFERENCES Park_Ranger(Ranger_ID),
FOREIGN KEY (Visitor_ID) REFERENCES Visitor(Visitor_ID)
);