-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTable.sql
212 lines (177 loc) · 5.57 KB
/
createTable.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
-- CREATE TABLE SCRIPT FOR HOMESTAY CO. DATABASE
-- Family (5 Tables)
CREATE TABLE Family (
FamilyID INT,
Bio VARCHAR(1500),
PrimaryContact_FirstName CHAR(20) NOT NULL,
PrimaryContact_Email CHAR(200) NOT NULL,
PrimaryContact_PhoneNumber BIGINT NOT NULL,
PrimaryContact_Ocupation CHAR(30),
PRIMARY KEY (FamilyID),
CHECK(PrimaryContact_PhoneNumber > 999999999 AND PrimaryContact_PhoneNumber <= 9999999999)
);
CREATE TABLE FamilyMembers_LiveTogether (
FamilyID INT,
FirstName CHAR(25),
LastName CHAR(25),
BirthDate DATE NOT NULL,
Gender CHAR(1) NOT NULL,
PhoneNumber BIGINT,
isPrimaryContact CHAR(1) NOT NULL,
PRIMARY KEY (FamilyID, FirstName),
FOREIGN KEY (FamilyID) REFERENCES Family(FamilyID) ON DELETE CASCADE,
CHECK(PhoneNumber >= 1000000000 AND PhoneNumber <= 9999999999)
);
CREATE TABLE FamilyMember_Language (
FamilyID INT,
FirstName CHAR(25),
Languages CHAR(25),
PRIMARY KEY (FamilyID, FirstName, Languages),
FOREIGN KEY (FamilyID, FirstName) REFERENCES FamilyMembers_LiveTogether(FamilyID, FirstName) ON DELETE CASCADE
);
CREATE TABLE FamilyMember_Interest (
FamilyID INT,
FirstName CHAR(25),
Interest CHAR(25),
PRIMARY KEY (FamilyID, FirstName, Interest),
FOREIGN KEY (FamilyID, FirstName) REFERENCES FamilyMembers_LiveTogether(FamilyID, FirstName) ON DELETE CASCADE
);
CREATE TABLE FamilyMember_Hobby (
FamilyID INT,
FirstName CHAR(25),
Hobby CHAR(25),
PRIMARY KEY (FamilyID, FirstName, Hobby),
FOREIGN KEY (FamilyID, FirstName) REFERENCES FamilyMembers_LiveTogether(FamilyID, FirstName) ON DELETE CASCADE
);
-- House (2 Tables)
CREATE TABLE Family_Owns_House (
FamilyID INT UNIQUE,
UnitNumber INT,
AppartmentNumber INT,
Street CHAR (50) NOT NULL,
City CHAR(30) NOT NULL,
Province CHAR(2) NOT NULL,
PostalCode CHAR(6) NOT NULL,
HouseType CHAR(10),
NumberOfRooms TINYINT NOT NULL,
NumberOfBathrooms TINYINT NOT NULL,
SmokingAllowed CHAR(1) NOT NULL,
PetAllowed CHAR(1) NOT NULL,
Size INT,
PRIMARY KEY (FamilyID, UnitNumber),
FOREIGN KEY (FamilyID) REFERENCES Family(FamilyID)
);
CREATE TABLE House_Img (
FamilyID INT,
UnitNumber INT,
ImgURL VARCHAR (500),
PRIMARY KEY (FamilyID, UnitNumber, ImgURL)
);
-- Room (2 Tables)
CREATE TABLE House_Has_RoomsForRent (
FamilyID INT,
UnitNumber INT,
RoomNumber TINYINT,
Size INT,
BedSize CHAR(10),
FloorNumber TINYINT,
IsAvailable CHAR(1),
AvailibilityDate DATE,
HasPrivateBathroom CHAR(1),
Price INT NOT NULL,
PRIMARY KEY (FamilyID, UnitNumber, RoomNumber),
FOREIGN KEY (FamilyID, UnitNumber) REFERENCES Family_Owns_House(FamilyID, UnitNumber) ON DELETE CASCADE,
CHECK(Price > 0)
);
CREATE TABLE Room_Img (
FamilyID INT,
UnitNumber INT,
RoomNumber TINYINT,
ImgURL VARCHAR(500),
PRIMARY KEY (FamilyID, UnitNumber, RoomNumber, ImgURL),
FOREIGN KEY (FamilyID, UnitNumber, RoomNumber) REFERENCES House_Has_RoomsForRent(FamilyID, UnitNumber, RoomNumber) ON DELETE CASCADE
);
-- School (1 Table)
CREATE TABLE School (
SchoolName CHAR(50),
UnitNumber INT,
Street CHAR (50),
City CHAR(30),
Province CHAR(2),
PostalCode CHAR(6),
PRIMARY KEY (SchoolName)
);
-- Student (4 Tables)
CREATE TABLE Student (
StudentID INT,
FirstName CHAR(25) NOT NULL,
LastName CHAR(25) NOT NULL,
BirthDate DATE NOT NULL,
Gender CHAR(1) NOT NULL,
Email CHAR(200) NOT NULL,
PhoneNumber BIGINT NOT NULL,
ExpectedGraduationDate DATE,
SchoolName CHAR (50) NOT NULL,
PRIMARY KEY (StudentID),
FOREIGN KEY (SchoolName) REFERENCES School(SchoolName) ON DELETE SET NULL,
CHECK(PhoneNumber >= 1000000000 AND PhoneNumber <= 9999999999)
);
CREATE TABLE Student_Interest (
StudentID INT,
Interest CHAR(25),
PRIMARY KEY (StudentID, Interest),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
CREATE TABLE Student_Language (
StudentID INT,
Languages CHAR(25),
PRIMARY KEY (StudentID, Languages),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
CREATE TABLE Student_Hobby (
StudentID INT,
Hobby CHAR(25),
PRIMARY KEY (StudentID, Hobby),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
-- Contracts (1 Table)
CREATE TABLE Student_Room_Contract(
TrackingNumber INT,
StudentID INT,
FamilyID INT,
UnitNumber INT,
RoomNumber TINYINT,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
Price INT NOT NULL,
SignedDocURL VARCHAR(500) NOT NULL,
PRIMARY KEY (TrackingNumber),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (FamilyID, UnitNumber, RoomNumber) REFERENCES House_Has_RoomsForRent(FamilyID, UnitNumber, RoomNumber),
CHECK(EndDate > StartDate),
CHECK(Price > 0)
);
-- Reviews (2 Tables)
CREATE TABLE Student_Review (
ReviewID INT,
StudentID INT,
TrackingNumber INT,
ReviewDate DATE,
Rating TINYINT,
CommentOnHome VARCHAR(1500),
CommentOnFamily VARCHAR(1500),
PRIMARY KEY (ReviewID, StudentID, TrackingNumber),
FOREIGN KEY (TrackingNumber) REFERENCES Student_Room_Contract(TrackingNumber),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
CREATE TABLE Family_Review (
ReviewID INT,
FamilyID INT,
TrackingNumber INT,
ReviewDate DATE,
Rating TINYINT,
CommentOnStudent VARCHAR(1500),
PRIMARY KEY (ReviewID, FamilyID, TrackingNumber),
FOREIGN KEY (FamilyID) REFERENCES Family(FamilyID),
FOREIGN KEY (TrackingNumber) REFERENCES Student_Room_Contract(TrackingNumber)
);