-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_tables.sql
204 lines (175 loc) · 4.9 KB
/
02_tables.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
USE vetdb;
CREATE TABLE animals (
a_animalid BIGINT NOT NULL,
a_name VARCHAR(255),
a_species VARCHAR(255),
a_subspecies VARCHAR(255),
a_breed VARCHAR(255),
a_type VARCHAR(255),
a_region VARCHAR(255),
a_sex INTEGER,
a_birthdate DATE,
a_status INTEGER,
a_requeststatus INTEGER,
a_ownerid BIGINT,
a_profilepic BIGINT,
a_tattoonum INTEGER,
a_citytattoo VARCHAR(255),
a_rfidnumber VARCHAR(255),
a_microchipnumber VARCHAR(255),
a_coatcolor VARCHAR(255),
a_continuousmedication VARCHAR(255),
a_distinctfeature VARCHAR(255),
PRIMARY KEY (a_animalid)
);
CREATE TABLE users (
u_userid BIGINT NOT NULL,
u_joiningdate DATE NOT NULL,
u_activationdate DATE,
u_terminationdate DATE,
u_firstname VARCHAR(255),
u_middlename VARCHAR(255),
u_lastname VARCHAR(255),
u_role INTEGER,
u_emailid VARCHAR(255),
u_passwordhash TEXT,
u_passwordsalt VARCHAR(255),
u_status INTEGER NOT NULL,
PRIMARY KEY (u_userid)
);
CREATE TABLE owners (
o_ownerid BIGINT NOT NULL,
o_firstname VARCHAR(255),
o_middlename VARCHAR(255),
o_lastname VARCHAR(255),
o_contactnumber VARCHAR(255),
o_emailid VARCHAR(255),
o_address VARCHAR(255),
PRIMARY KEY (o_ownerid)
);
CREATE TABLE weights (
w_weightid BIGINT NOT NULL,
w_massinkg DOUBLE PRECISION NOT NULL,
w_recorddate DATE NOT NULL,
w_recordedby BIGINT,
w_animalid BIGINT NOT NULL,
PRIMARY KEY (w_weightid)
);
CREATE TABLE photos (
p_photoid BIGINT NOT NULL,
p_photodesc VARCHAR(255),
p_animalid BIGINT NOT NULL,
p_photolink VARCHAR(255),
p_alttext VARCHAR(255),
p_uploader BIGINT,
p_uploaddate DATE NOT NULL,
PRIMARY KEY (p_photoid)
);
CREATE TABLE comments (
c_commentid BIGINT NOT NULL,
c_commentdesc VARCHAR(255),
c_animalid BIGINT NOT NULL,
c_commentdate DATE NOT NULL,
c_commenter BIGINT,
PRIMARY KEY (c_commentid)
);
CREATE TABLE issues (
i_issueid BIGINT NOT NULL,
i_issuedesc VARCHAR(255),
i_detecteddate DATE NOT NULL,
i_animalid BIGINT NOT NULL,
i_raisedby BIGINT,
i_isresolved BIT,
PRIMARY KEY (i_issueid)
);
CREATE TABLE treatments (
t_treatmentid BIGINT NOT NULL,
t_treatmentdesc VARCHAR(255),
t_drugname VARCHAR(255),
t_drugdose VARCHAR(255),
t_deliverymethod VARCHAR(255),
t_animalid BIGINT NOT NULL,
t_treatmentdate DATE NOT NULL,
t_treatedby BIGINT,
PRIMARY KEY (t_treatmentid)
);
CREATE TABLE sequence_animals (
next_val BIGINT
);
CREATE TABLE sequence_users (
next_val BIGINT
);
CREATE TABLE sequence_owners (
next_val BIGINT
);
CREATE TABLE sequence_weights (
next_val BIGINT
);
CREATE TABLE sequence_photos (
next_val BIGINT
);
CREATE TABLE sequence_comments (
next_val BIGINT
);
CREATE TABLE sequence_issues (
next_val BIGINT
);
CREATE TABLE sequence_treatments (
next_val BIGINT
);
INSERT INTO sequence_animals VALUES (1);
INSERT INTO sequence_users VALUES (1);
INSERT INTO sequence_owners VALUES (1);
INSERT INTO sequence_weights VALUES (1);
INSERT INTO sequence_photos VALUES (1);
INSERT INTO sequence_comments VALUES (1);
INSERT INTO sequence_issues VALUES (1);
INSERT INTO sequence_treatments VALUES (1);
ALTER TABLE animals
ADD CONSTRAINT fk_a_ownerid_animals
FOREIGN KEY (a_ownerid)
REFERENCES owners (o_ownerid);
ALTER TABLE animals
ADD CONSTRAINT fk_a_profilepic_animals
FOREIGN KEY (a_profilepic)
REFERENCES photos (p_photoid);
ALTER TABLE weights
ADD CONSTRAINT fk_w_animalid_weights
FOREIGN KEY (w_animalid)
REFERENCES animals (a_animalid) ON DELETE CASCADE;
ALTER TABLE weights
ADD CONSTRAINT fk_w_recordedby_weights
FOREIGN KEY (w_recordedby)
REFERENCES users (u_userid) ON DELETE SET NULL;
ALTER TABLE photos
ADD CONSTRAINT fk_p_animalid_photos
FOREIGN KEY (p_animalid)
REFERENCES animals (a_animalid) ON DELETE CASCADE;
ALTER TABLE photos
ADD CONSTRAINT fk_p_uploader_photos
FOREIGN KEY (p_uploader)
REFERENCES users (u_userid) ON DELETE SET NULL;
ALTER TABLE comments
ADD CONSTRAINT fk_c_commenter_comments
FOREIGN KEY (c_commenter)
REFERENCES users (u_userid) ON DELETE SET NULL;
ALTER TABLE comments
ADD CONSTRAINT fk_c_animalid_comments
FOREIGN KEY (c_animalid)
REFERENCES animals (a_animalid) ON DELETE CASCADE;
ALTER TABLE issues
ADD CONSTRAINT fk_i_raisedby_issues
FOREIGN KEY (i_raisedby)
REFERENCES users (u_userid) ON DELETE SET NULL;
ALTER TABLE issues
ADD CONSTRAINT fk_i_animalid_issues
FOREIGN KEY (i_animalid)
REFERENCES animals (a_animalid) ON DELETE CASCADE;
ALTER TABLE treatments
ADD CONSTRAINT fk_t_animalid_treatments
FOREIGN KEY (t_animalid)
REFERENCES animals (a_animalid) ON DELETE CASCADE;
ALTER TABLE treatments
ADD CONSTRAINT fk_t_treatedby_treatments
FOREIGN KEY (t_treatedby)
REFERENCES users (u_userid) ON DELETE SET NULL;