-
Notifications
You must be signed in to change notification settings - Fork 0
/
Init.sql
130 lines (112 loc) · 3.46 KB
/
Init.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Master list of of all the SQL statements that we call to intialize the database.
# We will leave the dynamic SQL calls in the other code files. No need to include them here.
CREATE TABLE Accounts (
Username CHAR(50),
Password CHAR(70) NOT NULL,
Email CHAR(60) NOT NULL UNIQUE,
PRIMARY KEY (Username),
CHECK (LEN(Password) > 6)
);
CREATE TABLE Locations (
LocationId INT,
CityName CHAR(50),
Country CHAR(30) NOT NULL,
State CHAR(20),
PRIMARY KEY(LocationId),
CHECK (LocationId > 0)
);
CREATE TABLE Reviews (
ReviewId INT,
Rating INT NOT NULL,
AttractionId INT NOT NULL,
ReviewText VARCHAR(15000) NOT NULL,
PostDate Date NOT NULL,
PRIMARY KEY (ReviewId),
CHECK (ReviewId > 0)
);
CREATE TABLE WritesReview (
ReviewId INT,
Username CHAR(50),
PRIMARY KEY (ReviewId, Username),
FOREIGN KEY (ReviewId) REFERENCES Reviews(ReviewId),
FOREIGN KEY (Username) REFERENCES Accounts(Username)
);
CREATE TABLE Trips (
TripId INT,
Username CHAR(50),
FromLocationId INT,
ToLocationId INT,
StartDate DATE,
EndDate DATE,
TransportationId INT,
AccommodationId INT,
PaymentId INT,
NumPeople INT,
PRIMARY KEY (TripId),
FOREIGN KEY (Username) REFERENCES Accounts(Username),
FOREIGN KEY (FromLocationId) REFERENCES Locations(LocationId),
FOREIGN KEY (ToLocationId) REFERENCES Locations(LocationId),
FOREIGN KEY (TransportationId) REFERENCES Transportation(TransportationId),
FOREIGN KEY (AccommodationId) REFERENCES Accommodation(AccommodationId),
FOREIGN KEY (PaymentId) REFERENCES Payment(PaymentId),
CHECK (NumPeople > 0)
);
CREATE TABLE TripAttractions(
TripId INT,
AttractionId INT,
PRIMARY KEY (TripId, AttractionId),
FOREIGN KEY (TripId) REFERENCES Trips(TripId),
FOREIGN KEY (AttractionId) REFERENCES Attractions(AttractionId)
);
CREATE TABLE Attractions (
AttractionId INT,
Name CHAR(100),
LocationId INT,
Description VARCHAR(2500),
PRIMARY KEY (AttractionId),
FOREIGN KEY (LocationId) REFERENCES Locations (LocationId),
CHECK (Cost >= 0)
);
CREATE TABLE Accommodation (
AccommodationId INT,
AccommodationType CHAR(10) NOT NULL,
RatePerNight DECIMAL(20, 2) NOT NULL,
Facilities CHAR(50),
PRIMARY KEY (AccommodationId),
CHECK (AccommodationType='Hotel' OR AccommodationType='Airbnb'),
CHECK (AccommodationId > 0),
CHECK (RatePerNight >= 0)
);
CREATE TABLE Payment (
PaymentId INT AUTO_INCREMENT,
CardNumber INT,
CardSecurityCode INT,
Amount DECIMAL(20, 2) NOT NULL,
ExpirationDate Date,
PaymentDate DATETIME NOT NULL,
CardName CHAR(60),
PRIMARY KEY (PaymentId),
CHECK (CardNumber > 0 AND CardSecruityCode > 0),
CHECK (Amount >= 0)
);
CREATE TABLE Transportation(
TransportationId INTEGER,
TransportationType CHAR(15),
Cost DECIMAL(20, 2) NOT NULL,
BeginTime CHAR(10),
EndTime CHAR(10),
PRIMARY KEY(TransportationId),
CHECK(TransportationType = 'Train' OR TransportationType = 'Flight' OR TransportationType = 'Car' OR TransportationType = 'Cruise')
);
# Run cities and attractions script
INSERT INTO Transportation VALUES (1, 'Flight', 300);
INSERT INTO Transportation VALUES (2, 'Cruise', 150);
INSERT INTO Transportation VALUES (3, 'Train', 100);
INSERT INTO Transportation VALUES (4, 'Car', 80);
INSERT INTO Accommodation VALUES (1, 'Hotel', 150, 'Free wifi, Free breakfast, TV, Swimming Pool');
INSERT INTO Accommodation VALUES (2, 'Airbnb', 75, 'Free wifi, TV');
# For dropping tables to test:
DROP TABLE TripAttractions;
DROP TABLE Trips;
DROP TABLE Attractions;
DROP TABLE Locations;