RelationalDesign: kreiranje.sql

File kreiranje.sql, 8.7 KB (added by 201045, 2 years ago)
Line 
1
2drop table namirnica_pripagja_kategorija;
3drop table namirnica_e_dodatok_hrana;
4drop table hrana_pripagja_kategorija;
5drop table naracka_ima_paket;
6drop table hrana_e_napravena_namirnica;
7drop table paket_sodrzi_hrana;
8drop table naracka_sodrzi_hrana;
9drop table prodazhnomesto_nudi_paket;
10drop table cena;
11drop table zaliha;
12drop table paket;
13drop table hrana;
14drop table vrabotenpd;
15drop table kupon;
16drop table naracka;
17drop table naplata;
18drop table prodazhnomesto;
19drop table dostavuvac;
20drop table admin_table;
21drop table potrosuvac;
22drop table vozilo;
23drop table korisnik;
24drop table namirnica;
25drop table kategorija;
26drop table franshiza;
27drop table lokacija;
28
29
30
31
32create table lokacija(
33 lokacija_id serial,
34 adresa varchar(100) not null,
35 broj integer not null,
36 constraint pk_lokacija primary key(lokacija_id)
37);
38
39
40create table franshiza(
41 franshiza_id serial,
42 ime varchar(100) not null,
43 constraint pk_franshiza primary key(franshiza_id)
44);
45create table kategorija(
46 id_kategorija serial,
47 ime_kat varchar(100) not null,
48 constraint pk_kategorija primary key(id_kategorija)
49);
50create table namirnica(
51 id_namirnica serial,
52 ime_name varchar(100) not null,
53 dali_alergent boolean not null,
54 constraint pk_namirnica primary key(id_namirnica)
55);
56
57
58create table korisnik(
59 korisnik_id serial,
60 ime varchar(100),
61 datum_kreiranje date not null,
62 username varchar(100) not null,
63 constraint korisnik_pk primary key(korisnik_id)
64);
65create table vozilo(
66 vozilo_id serial,
67 tip_vozilo varchar(100) not null,
68 constraint vozilo_pk primary key(vozilo_id)
69);
70
71create table potrosuvac(
72 korisnik_id integer not null,
73 broj_naracki integer not null,
74 lokacija_id integer,
75 constraint potrosuvac_pk primary key(korisnik_id),
76 constraint korisnik_fk foreign key(korisnik_id)
77 references korisnik(korisnik_id),
78 constraint lokacija_fk foreign key(lokacija_id)
79 references lokacija(lokacija_id)
80
81);
82
83create table admin_table(
84 korisnik_id integer not null,
85 constraint admin_pk primary key(korisnik_id),
86 constraint korisnik_fk foreign key(korisnik_id)
87 references korisnik(korisnik_id)
88);
89
90
91create table dostavuvac(
92 korisnik_id integer not null,
93 admin_id integer not null,
94 vozilo_id integer,
95 constraint dostavuvac_pk primary key(korisnik_id),
96 constraint korisnik_fk foreign key(korisnik_id)
97 references korisnik(korisnik_id),
98 constraint admin_fk foreign key(admin_id)
99 references admin_table(korisnik_id),
100 constraint vozilo_fk foreign key(vozilo_id)
101 references vozilo(vozilo_id)
102);
103
104create table prodazhnomesto(
105 id_mesto serial,
106 ime varchar(100) not null,
107 ulica varchar(100) not null,
108 broj integer not null,
109 franshiza_id integer,
110 admin_id integer not null,
111 constraint pk_prodazhnomesto primary key(id_mesto),
112 constraint fk_franshiza foreign key(franshiza_id)
113 references franshiza(franshiza_id),
114 constraint admin_fk foreign key(admin_id)
115 references admin_table(korisnik_id)
116);
117
118create table naplata(
119 naplata_id serial,
120 potrosuvac_id integer not null,
121 dostavuvac_id integer not null,
122 iznos integer not null,
123 nplakjanje varchar(100) not null,
124 datum_naplata timestamp,
125 constraint naplata_pk primary key(naplata_id),
126 constraint potrosuvac_fk foreign key(potrosuvac_id)
127 references potrosuvac(korisnik_id),
128 constraint dostavuvac_fk foreign key(dostavuvac_id)
129 references dostavuvac(korisnik_id),
130 CONSTRAINT iznost_pog_0 CHECK (iznos > 0)
131
132);
133
134
135
136create table naracka(
137 naracka_id serial,
138 dostavuvac_id integer,
139 id_mesto integer not null,
140 status varchar(100),
141 potrosuvac_id integer not null,
142 naplata_id integer,
143 datum_naracka timestamp,
144 constraint naracka_pk primary key(naracka_id),
145 constraint dostavuvac_fk foreign key(dostavuvac_id)
146 references dostavuvac(korisnik_id),
147 constraint prodazhnomest_fk foreign key(id_mesto)
148 references prodazhnomesto(id_mesto),
149 constraint potrosuvac_fk foreign key(potrosuvac_id)
150 references potrosuvac(korisnik_id),
151 constraint naplata_fk foreign key(naplata_id)
152 references naplata(naplata_id)
153
154);
155create table kupon(
156 kupon_id serial,
157 iznos_kupon integer not null,
158 potrosuvac_id integer,
159 naracka_id integer,
160 naplata_id integer,
161 constraint kupon_pk primary key(kupon_id),
162 constraint potrosuvac_fk foreign key(potrosuvac_id)
163 references potrosuvac(korisnik_id),
164 constraint naracka_fk foreign key(naracka_id)
165 references naracka(naracka_id),
166 constraint naplata_fk foreign key(naplata_id)
167 references naplata(naplata_id),
168 CONSTRAINT iznos_kup_pog_0 CHECK (iznos_kupon > 0)
169
170);
171create table vrabotenpd(
172 korisnik_id integer,
173 id_mesto integer,
174 constraint vrabotenpd_pk primary key(korisnik_id),
175 constraint korisnik_fk foreign key(korisnik_id)
176 references korisnik(korisnik_id),
177 constraint prodaznomesto_fk foreign key(id_mesto)
178 references prodazhnomesto(id_mesto)
179);
180create table hrana(
181 id_stavka serial,
182 ime_hra varchar(100) not null,
183 vrabotenpd_id integer not null,
184 id_mesto integer,
185 constraint hrana_pk primary key(id_stavka),
186 constraint vraboten_fk foreign key(vrabotenpd_id)
187 references vrabotenpd(korisnik_id),
188 constraint prodaznomesto_fk foreign key(id_mesto)
189 references prodazhnomesto(id_mesto)
190);
191
192create table paket(
193 paket_id serial,
194 vraboten_id integer not null,
195 constraint paket_pk primary key(paket_id),
196 constraint vraboten_fk foreign key(vraboten_id)
197 references vrabotenpd(korisnik_id)
198);
199
200create table zaliha(
201 broj_zaliha serial,
202 id_stavka integer,
203 kolicina integer not null,
204 constraint zaliha_pk primary key(broj_zaliha, id_stavka),
205 constraint hrana_fk foreign key(id_stavka)
206 references hrana(id_stavka),
207 CONSTRAINT kolicina_pog_0 CHECK(kolicina > 0)
208);
209
210
211create table cena(
212 broj_cena serial,
213 id_stavka integer,
214 iznos integer not null,
215 vazi_od date not null,
216 vazi_do date,
217 constraint cena_pk primary key(broj_cena, id_stavka),
218 constraint hrana_fk foreign key(id_stavka)
219 references hrana(id_stavka),
220 CONSTRAINT iznos_pog_0 CHECK (iznos > 0),
221 CONSTRAINT vazi_od_po_vazi_do CHECK (vazi_od < vazi_do)
222);
223
224
225create table prodazhnomesto_nudi_paket(
226 id_mesto integer,
227 paket_id integer,
228 constraint pnp_pk primary key(id_mesto, paket_id),
229 constraint prodazhnomesto_fk foreign key(id_mesto)
230 references prodazhnomesto(id_mesto),
231 constraint paket_fk foreign key(paket_id)
232 references paket(paket_id)
233);
234
235create table naracka_sodrzi_hrana(
236 naracka_id integer,
237 id_stavka integer,
238 kolicina integer not null,
239 constraint nsh_pk primary key(naracka_id, id_stavka),
240 constraint naracka_fk foreign key(naracka_id)
241 references naracka(naracka_id),
242 constraint hrana_fk foreign key(id_stavka)
243 references hrana(id_stavka),
244 CONSTRAINT kolicina_pog_0 CHECK (kolicina > 0)
245);
246
247create table paket_sodrzi_hrana(
248 paket_id integer,
249 id_stavka integer,
250 constraint psh_pk primary key(paket_id, id_stavka),
251 constraint paket_fk foreign key(paket_id)
252 references paket(paket_id),
253 constraint hrana_fk foreign key(id_stavka)
254 references hrana(id_stavka)
255);
256
257create table hrana_e_napravena_namirnica(
258 id_stavka integer,
259 id_namirnica integer,
260 constraint hrana_e_nap_nam_pk primary key(id_stavka, id_namirnica),
261 constraint hrana_fk foreign key(id_stavka)
262 references hrana(id_stavka),
263 constraint namirnica_fk foreign key(id_namirnica)
264 references namirnica(id_namirnica)
265);
266
267create table naracka_ima_paket(
268 naracka_id integer,
269 paket_id integer,
270 kolicina integer not null,
271 constraint naracka_ima_paket_pk primary key(naracka_id, paket_id),
272 constraint naracaka_fk foreign key(naracka_id)
273 references naracka(naracka_id),
274 constraint paket_fk foreign key(paket_id)
275 references paket(paket_id),
276 CONSTRAINT kolicina_pog_0 CHECK (kolicina > 0)
277);
278
279create table hrana_pripagja_kategorija(
280 id_stavka integer,
281 id_kategorija integer,
282 constraint hrana_prp_kat_pk primary key(id_stavka, id_kategorija),
283 constraint hrana_fk foreign key(id_stavka)
284 references hrana(id_stavka),
285 constraint kategorija_fk foreign key(id_kategorija)
286 references kategorija(id_kategorija)
287);
288
289create table namirnica_e_dodatok_hrana(
290 id_namirnica integer,
291 id_stavka integer,
292 constraint namir_e_dot_hran_pk primary key(id_namirnica, id_stavka),
293 constraint namirnica_fk foreign key(id_namirnica)
294 references namirnica(id_namirnica),
295 constraint hrana_fk foreign key(id_stavka)
296 references hrana(id_stavka)
297);
298
299create table namirnica_pripagja_kategorija(
300 id_namirnica integer,
301 id_kategorija integer,
302 constraint namirnica_prip_kat_pk primary key(id_namirnica, id_kategorija),
303 constraint namirnica_fk foreign key(id_namirnica)
304 references namirnica(id_namirnica),
305 constraint kategorija_fk foreign key(id_kategorija)
306 references kategorija(id_kategorija)
307);
308
309
310
311
312
313
314
315