RelationalDesign: kreiranje.sql

File kreiranje.sql, 5.1 KB (added by 191041, 2 years ago)
Line 
1create table korisnik(
2 user_id serial primary key,
3 user_ime varchar(50) not null,
4 user_prezime varchar(50) not null,
5 user_password varchar(300) not null,
6 user_admin_id integer not null
7 --treba fk da se dodade do sistemski_admin so alter table
8);
9
10create table sistemski_admin(
11 user_id integer primary key,
12 constraint fk_user foreign key (user_id) references korisnik (user_id)
13 on delete cascade on update cascade
14);
15
16alter table korisnik add constraint fk_korisnik_administriran foreign key (user_admin_id) references sistemski_admin(user_id)
17on delete cascade on update cascade;
18
19create table kupuvac(
20 user_id serial primary key,
21 kupuvac_tel_broj varchar(50) not null,
22 constraint fk_user foreign key (user_id) references korisnik (user_id)
23 on delete cascade on update cascade
24);
25
26create table dostavuvac(
27 user_id serial primary key,
28 dostavuvac_vraboten_od date not null,
29 constraint fk_user foreign key (user_id) references korisnik (user_id)
30 on delete cascade on update cascade
31);
32
33create table menadzer(
34 user_id serial primary key,
35 menadzer_vraboten_od date not null,
36 constraint fk_user foreign key (user_id) references korisnik (user_id)
37 on delete cascade on update cascade
38);
39
40create table restoran(
41 restoran_id serial primary key,
42 restoran_ime varchar(50) not null,
43 restoran_rejting integer not null,
44 restoran_adresa varchar(50) not NULL,
45 user_id integer NOT null,
46 constraint fk_restoran_menadzer foreign key(user_id) references menadzer(user_id) on delete cascade on update cascade
47);
48
49create table tip(
50 tip_id serial primary key,
51 tip_ime varchar(50) not null
52);
53
54create table meni(
55 meni_id serial primary key,
56 tip_id integer not null,
57 restoran_id integer not null,
58 constraint fk_meni_tip foreign key (tip_id) references tip(tip_id) on delete cascade on update cascade,
59 constraint fk_meni_restoran foreign key (restoran_id) references restoran(restoran_id) on delete cascade on update cascade
60);
61
62create table obrok(
63 obrok_id serial primary key,
64 obrok_opis varchar(10000) not null,
65 obrok_ime varchar(50) not null,
66 meni_id integer not null,
67 constraint fk_obrok_meni foreign key(meni_id) references meni(meni_id) on delete cascade on update cascade
68);
69
70create table rezervacija(
71 rezervacija_id serial primary key,
72 rezervacija_vreme timestamp not null,
73 br_lugje integer not null,
74 rezervacija_status varchar(50) not null,
75 rezervacija_opis varchar(10000) not null,
76 restoran_id integer not null,
77 user_id integer not null,
78 constraint fk_rezervacija_restoran foreign key(restoran_id) references restoran(restoran_id) on delete cascade on update cascade,
79 constraint fk_rezervacija_kupuvac foreign key (user_id) references kupuvac(user_id) on delete cascade on update cascade
80);
81
82create table naracka(
83 naracka_id serial primary key,
84 naracana_na timestamp not null,
85 naracka_status varchar(50) not null,
86 naracka_cena_vkupna integer not null,
87 user_id_kupuvac integer not null,
88 user_id_dostavuvac integer not null,
89 constraint fk_naracka_kupuvac foreign key (user_id_kupuvac) references kupuvac(user_id) on delete cascade on update cascade,
90 constraint fk_naracka_dostavuvac foreign key(user_id_dostavuvac) references dostavuvac(user_id)on delete cascade on update cascade
91);
92
93create table restoran_tel_broj(
94 restoran_id integer not null,
95 broj varchar(50),
96 constraint pk_restoran_tel_broj primary key (restoran_id,broj),
97 constraint fk_restoran_tel_broj_restoran foreign key(restoran_id) references restoran(restoran_id) on delete cascade on update cascade
98);
99
100create table cena(
101 obrok_id integer not null,
102 cena_id integer not null,
103 cena_iznos integer not null,
104 cena_vazi_od date not null,
105 cena_vazi_do date not null,
106 constraint pk_cena primary key (obrok_id,cena_id),
107 constraint fk_cena_obrok foreign key (obrok_id) references obrok(obrok_id) on delete cascade on update cascade
108);
109
110create table rezervira(
111 rezervacija_id integer not null,
112 obrok_id integer not null,
113 constraint pk_rezervira primary key(rezervacija_id,obrok_id),
114 constraint fk_rezervira_rezervacija foreign key (rezervacija_id) references rezervacija(rezervacija_id) on delete cascade on update cascade,
115 constraint fk_rezervira_obrok foreign key (obrok_id) references obrok(obrok_id) on delete cascade on update cascade
116);
117
118create table se_sostoi_od(
119 naracka_id integer not null,
120 obrok_id integer not null,
121 kolicina integer not null,
122 cena integer not null,
123 constraint pk_se_sostoi_od primary key (naracka_id,obrok_id),
124 constraint fk_se_sostoi_od_naracka foreign key (naracka_id) references naracka(naracka_id) on delete cascade on update cascade,
125 constraint fk_se_sostoi_od_obrok foreign key (obrok_id) references obrok(obrok_id) on delete cascade on update cascade
126);
127
128-- drop tables
129drop table korisnik;
130drop table sistemski_admin;
131drop table kupuvac;
132drop table dostavuvac;
133drop table menadzer;
134drop table restoran;
135drop table tip;
136drop table meni;
137drop table obrok;
138drop table rezervacija;
139drop table naracka;
140drop table restoran_tel_broj;
141drop table cena;
142drop table rezervira;
143drop table se_sostoi_od;