-
Notifications
You must be signed in to change notification settings - Fork 0
/
criar.sql
executable file
·360 lines (317 loc) · 12.1 KB
/
criar.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
PRAGMA foreign_keys = ON;
PRAGMA encoding="UTF-8";
DROP TABLE IF EXISTS Staff;
DROP TABLE IF EXISTS MaintenanceStaff;
DROP TABLE IF EXISTS ManagementStaff;
DROP TABLE IF EXISTS Caretaker;
DROP TABLE IF EXISTS Feeder;
DROP TABLE IF EXISTS Veterinarian;
DROP TABLE IF EXISTS Structure;
DROP TABLE IF EXISTS WorkSpace;
DROP TABLE IF EXISTS CleaningRepairs;
DROP TABLE IF EXISTS StructureCleaning;
DROP TABLE IF EXISTS Upkeep;
DROP TABLE IF EXISTS ServicesBuilding;
DROP TABLE IF EXISTS ServicesWorker;
DROP TABLE IF EXISTS Ticket;
DROP TABLE IF EXISTS Purchase;
DROP TABLE IF EXISTS Visitor;
DROP TABLE IF EXISTS Acquisition;
DROP TABLE IF EXISTS Habitat;
DROP TABLE IF EXISTS Aquarium;
DROP TABLE IF EXISTS Terrarium;
DROP TABLE IF EXISTS AnimalClass;
DROP TABLE IF EXISTS Species;
DROP TABLE IF EXISTS Habitation;
DROP TABLE IF EXISTS Animal;
DROP TABLE IF EXISTS Relationship;
DROP TABLE IF EXISTS Event;
DROP TABLE IF EXISTS StructureHabitat;
DROP TABLE IF EXISTS StructureServicesBuilding;
DROP TABLE IF EXISTS EventStructure;
DROP TABLE IF EXISTS EventVisitor;
DROP TABLE IF EXISTS EventAnimal;
DROP TABLE IF EXISTS EventServiceWorker;
DROP TABLE IF EXISTS EventManagementWorker;
DROP TABLE IF EXISTS Food;
DROP TABLE IF EXISTS Diet;
DROP TABLE IF EXISTS DietFood;
DROP TABLE IF EXISTS Feeding;
DROP TABLE IF EXISTS Appointment;
DROP TABLE IF EXISTS VetAppointment;
DROP TABLE IF EXISTS AppointmentReport;
CREATE TABLE Staff(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
address TEXT NOT NULL,
contact INTEGER NOT NULL UNIQUE,
salary REAL CHECK(salary>0.0)
);
CREATE TABLE MaintenanceStaff(
id INTEGER PRIMARY KEY,
occupation TEXT NOT NULL,
FOREIGN KEY(id) REFERENCES Staff ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE ManagementStaff(
id INTEGER PRIMARY KEY,
rank TEXT NOT NULL,
FOREIGN KEY(id) REFERENCES Staff ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE Caretaker(
id INTEGER PRIMARY KEY,
training TEXT,
FOREIGN KEY(id) REFERENCES Staff ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE Feeder(
id INTEGER PRIMARY KEY,
safetyFormation TEXT DEFAULT "Basic",
FOREIGN KEY(id) REFERENCES Caretaker ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE Veterinarian(
id INTEGER PRIMARY KEY,
medicSpecialization TEXT DEFAULT "General",
FOREIGN KEY(id) REFERENCES Caretaker ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE Structure(
id INTEGER PRIMARY KEY,
name TEXT,
structureNumber INTEGER CHECK(structureNumber>=0) NOT NULL UNIQUE,
size REAL NOT NULL
);
CREATE TABLE Workspace(
staff INTEGER NOT NULL,
structure INTEGER NOT NULL,
FOREIGN KEY(staff) REFERENCES Staff ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(structure) REFERENCES Structure ON DELETE RESTRICT ON UPDATE RESTRICT,
PRIMARY KEY(staff, structure)
);
CREATE TABLE CleaningRepairs(
id INTEGER PRIMARY KEY,
dateTime TEXT NOT NULL
);
CREATE TABLE StructureCleaning(
cleaning INTEGER NOT NULL,
structure INTEGER NOT NULL,
FOREIGN KEY(cleaning) REFERENCES CleaningRepairs ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(structure) REFERENCES Structure ON DELETE RESTRICT ON UPDATE RESTRICT,
PRIMARY KEY(cleaning, structure)
);
CREATE TABLE Upkeep(
cleaning INTEGER NOT NULL,
staff INTEGER NOT NULL,
FOREIGN KEY(cleaning) REFERENCES CleaningRepairs ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(staff) REFERENCES MaintenanceStaff ON DELETE RESTRICT ON UPDATE RESTRICT,
PRIMARY KEY(cleaning, staff)
);
CREATE TABLE ServicesBuilding(
id INTEGER PRIMARY KEY
);
CREATE TABLE ServicesWorker(
id INTEGER NOT NULL UNIQUE,
building INTEGER NOT NULL,
partTime INTEGER CHECK(partTime==0 or partTime==1) NOT NULL,
FOREIGN KEY(id) REFERENCES Staff ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(building) REFERENCES ServicesBuilding ON DELETE RESTRICT ON UPDATE RESTRICT,
PRIMARY KEY(id, building)
);
CREATE TABLE Ticket(
id INTEGER PRIMARY KEY,
ticketNumber INTEGER NOT NULL UNIQUE,
type TEXT DEFAULT "Adult",
price REAL CHECK(price>=0.0) DEFAULT 20
);
CREATE TABLE Purchase(
id INTEGER PRIMARY KEY,
product TEXT NOT NULL,
amount REAL CHECK(amount>=0.0) NOT NULL
);
CREATE TABLE Visitor(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER CHECK(age >=0) NOT NULL
);
CREATE TABLE Acquisition(
purchase INTEGER NOT NULL,
servicesBuilding INTEGER NOT NULL,
ticket INTEGER NOT NULL,
visitor INTEGER NOT NULL,
PRIMARY KEY(purchase, servicesBuilding, ticket),
FOREIGN KEY(purchase) REFERENCES Purchase ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(servicesBuilding) REFERENCES ServicesBuilding ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(ticket) REFERENCES Ticket ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(visitor) REFERENCES Visitor ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE Habitat(
id INTEGER PRIMARY KEY,
type TEXT NOT NULL,
temperature REAL NOT NULL
);
CREATE TABLE Aquarium(
id INTEGER PRIMARY KEY,
salinity REAL CHECK(salinity>=0.0) NOT NULL,
ph REAL CHECK(ph>=0.0 and ph<=14.0) NOT NULL,
FOREIGN KEY(id) REFERENCES Habitat ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE Terrarium(
id INTEGER PRIMARY KEY,
humidity REAL CHECK(humidity>=0.0 and humidity<=100.0) NOT NULL,
vegetation REAL NOT NULL,
FOREIGN KEY(id) REFERENCES Habitat ON DELETE RESTRICT ON UPDATE CASCADE
);
CREATE TABLE AnimalClass(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE Species(
id INTEGER PRIMARY KEY,
scientificName TEXT NOT NULL UNIQUE,
commonName TEXT,
habitatPreference TEXT NOT NULL,
classId INTEGER NOT NULL,
FOREIGN KEY(classId) REFERENCES AnimalClass ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE Habitation(
species INTEGER NOT NULL,
habitat INTEGER NOT NULL,
minTemp REAL NOT NULL,
maxTemp REAL NOT NULL,
minSal REAL,
maxSal REAL,
minPh REAL,
maxPh REAL,
minHum REAL,
maxHum REAL,
PRIMARY KEY(species, habitat),
FOREIGN KEY(species) REFERENCES Species ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(habitat) REFERENCES Habitat ON DELETE RESTRICT ON UPDATE RESTRICT,
CHECK(minSal>=0.0 and minSal<=maxSal),
CHECK(maxSal>=0.0)
CHECK(minPh>=0.0 and minPh<=14.0 and minPh<=maxPh),
CHECK(maxPh>=0.0 and maxPh<=14.0),
CHECK(minHum>=0.0 and minHum<=100 and minHum<=maxHum),
CHECK(maxHum>=0.0 and maxHum<=100),
CHECK(minTEMP<=maxTemp)
);
CREATE TABLE Animal(
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER CHECK(age >= 0) NOT NULL,
sex CHAR NOT NULL,
weight REAL NOT NULL,
species INTEGER NOT NULL,
diet INTEGER NOT NULL,
adopter INTEGER,
FOREIGN KEY(species) REFERENCES Species ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(diet) REFERENCES Diet ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(adopter) REFERENCES Visitor ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE Relationship(
child INTEGER PRIMARY KEY,
mother INTEGER,
father INTEGER,
FOREIGN KEY(mother) REFERENCES Animal ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(father) REFERENCES Animal ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE Event(
id INTEGER PRIMARY KEY,
type TEXT NOT NULL,
dateTime TEXT NOT NULL
);
CREATE TABLE StructureHabitat(
id INTEGER PRIMARY KEY,
habitat INTEGER,
structure INTEGER,
FOREIGN KEY(habitat) REFERENCES Habitat ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(structure) REFERENCES Structure ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE StructureServicesBuilding(
id INTEGER PRIMARY KEY,
servicesbuilding INTEGER,
structure INTEGER,
FOREIGN KEY(servicesbuilding) REFERENCES ServicesBuilding ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(structure) REFERENCES Structure ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE EventStructure(
event INTEGER NOT NULL,
structure INTEGER NOT NULL,
PRIMARY KEY(event,structure),
FOREIGN KEY(event) REFERENCES Event ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(structure) REFERENCES Structure ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE EventVisitor(
event INTEGER NOT NULL,
visitor INTEGER NOT NULL,
PRIMARY KEY(event, visitor),
FOREIGN KEY(event) REFERENCES Event ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(visitor) REFERENCES Visitor ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE EventAnimal(
event INTEGER NOT NULL,
animal INTEGER NOT NULL,
PRIMARY KEY(event, animal),
FOREIGN KEY(event) REFERENCES Event ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(animal) REFERENCES Animal ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE EventServiceWorker(
event INTEGER NOT NULL,
worker INTEGER NOT NULL,
building INTEGER NOT NULL,
PRIMARY KEY(event, worker),
FOREIGN KEY(event) REFERENCES Event ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(worker,building) REFERENCES ServicesWorker(id,building) ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE EventManagementWorker(
event INTEGER NOT NULL,
admin INTEGER NOT NULL,
PRIMARY KEY(event, admin),
FOREIGN KEY(event) REFERENCES Event ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(admin) REFERENCES ManagementStaff ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE Food(
id INTEGER PRIMARY KEY,
type TEXT NOT NULL,
price INTEGER CHECK(price >=0) NOT NULL,
expirationDate TEXT NOT NULL
);
CREATE TABLE Diet(
id INTEGER PRIMARY KEY,
diet TEXT NOT NULL
);
CREATE TABLE DietFood(
diet INTEGER NOT NULL,
food INTEGER NOT NULL,
PRIMARY KEY(diet, food),
FOREIGN KEY(diet) REFERENCES Diet ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(food) REFERENCES Food ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE Feeding(
id INTEGER PRIMARY KEY,
dateTime TEXT NOT NULL,
quantity REAL CHECK(quantity>=0.0) NOT NULL,
food INTEGER NOT NULL,
feeder INTEGER NOT NULL,
animal INTEGER NOT NULL,
FOREIGN KEY(food) REFERENCES Food ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(feeder) REFERENCES Feeder ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(animal) REFERENCES Animal ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE Appointment(
id INTEGER PRIMARY KEY,
dateTime TEXT NOT NULL,
animal INTEGER NOT NULL,
FOREIGN KEY(animal) REFERENCES Animal ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE VetAppointment(
vet INTEGER NOT NULL,
appointment INTEGER NOT NULL,
PRIMARY KEY(vet, appointment),
FOREIGN KEY(vet) REFERENCES Veterinarian ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY(appointment) REFERENCES Appointment ON DELETE RESTRICT ON UPDATE RESTRICT
);
CREATE TABLE AppointmentReport(
id INTEGER PRIMARY KEY,
dateTime TEXT NOT NULL,
condition TEXT DEFAULT "Healthy",
appointment INTEGER NOT NULL,
FOREIGN KEY(appointment) REFERENCES Appointment ON DELETE RESTRICT ON UPDATE RESTRICT
);