RelationalDesign: kreiranje.sql

File kreiranje.sql, 6.1 KB (added by 201155, 2 years ago)
Line 
1--drop schema if exists project cascade;
2--create schema project;
3
4drop table vozaci_ima_kategorii;
5drop table rezervacii_ima_za_vozila;
6drop table vozaci_e_na_klienti;
7drop table Avtomobili;
8drop table Motori;
9drop table Kombinja;
10drop table MSteti;
11drop table Parkirani;
12drop table Servisi;
13drop table ServisKompBr;
14drop table ServisKomp;
15drop table Vozila;
16drop table Kategorii;
17drop table IzdavSaloniBr;
18drop table Lokacii;
19drop table FizickiLBr;
20drop table FizickiL;
21drop table PravniL;
22drop table Rezervacii;
23drop table Naplati;
24drop table Smetki;
25drop table Dogovori;
26drop table Klienti;
27drop table VraboteniBr;
28drop table Prodavachi;
29drop table Menadzeri;
30drop table Vraboteni;
31drop table IzdavSaloni;
32drop table VozaciBr;
33drop table Vozaci;
34
35-------------------------------
36
37create table Kategorii (
38 idKat serial primary key,
39 tip varchar(3)
40);
41
42create table Vozila (
43 idVozi integer primary key,
44 idKat integer,
45 model varchar(255),
46 marka varchar(255),
47 regTab varchar(255),
48 cenaVozi decimal,
49 constraint fk_idKat foreign key (idKat) references Kategorii (idKat)
50);
51
52create table Avtomobili (
53 idVoziA integer primary key,
54 brSedista integer,
55 constraint fk_idVozi foreign key (idVoziA) references Vozila(idVozi)
56);
57
58create table Motori (
59 idVoziM integer primary key,
60 kubikaza integer,
61 constraint fk_idVozi foreign key (idVoziM) references Vozila(idVozi)
62);
63
64create table Kombinja (
65 idVoziK integer primary key,
66 brSedista integer,
67 nosivost integer,
68 constraint fk_idVozi foreign key (idVoziK) references Vozila(idVozi)
69);
70
71create table IzdavSaloni (
72 idIS serial primary key
73);
74
75create table IzdavSaloniBr (
76 idIS integer,
77 telBrIs char(9),
78 constraint pk_IzdavSaloniBr primary key (idIs, telBrIs),
79 constraint fk_IzdavSaloniBr foreign key (idIS) references IzdavSaloni(idIS)
80);
81
82create table Klienti (
83 EMBGK char(13) primary key,
84 imeK varchar(255),
85 adresaK varchar(255),
86 primiumK boolean
87);
88
89create table FizickiL (
90 EMBGKFL char(13) primary key,
91 constraint fk_FizickiL foreign key (EMBGKFL) references Klienti (EMBGK)
92);
93
94create table FizickiLBr (
95 EMBGKFL char(13),
96 telBrFL char(9),
97 constraint pk_FizickiLBr primary key (EMBGKFL, telBrFL),
98 constraint fk_FizickiLBr foreign key (EMBGKFL) references FizickiL(EMBGKFL)
99);
100
101create table PravniL (
102 EMBGKPL char(13) primary key,
103 direktor varchar(255),
104 constraint fk_PravniL foreign key (EMBGKPL) references Klienti (EMBGK)
105);
106
107create table Dogovori (
108 idDog serial primary key,
109 EMBGK char(13) references Klienti(EMBGK),
110 idIS integer references IzdavSaloni(idIS),
111 cenaDog decimal,
112 sklucenNa date,
113 vaziDo date
114);
115
116create table Lokacii (
117 idLok serial primary key,
118 idIS integer references IzdavSaloni(idIS),
119 adresaLok varchar(255),
120 gradLok varchar(255)
121);
122
123create table Vozaci (
124 EMBGVoza char(13) primary key,
125 imeVoza varchar(255),
126 adresaVoza varchar(255),
127 vozackaD char(8)
128);
129
130create table VozaciBr (
131 EMBGVoza char(13) references Vozaci(EMBGVoza),
132 telBrVoza char(9),
133 constraint pk_VozaciBr primary key (EMBGVoza, telBrVoza)
134);
135
136create table MSteti (
137 idMS serial primary key,
138 idVozi integer references Vozila(idVozi),
139 EMBGVoza char(13) references Vozaci(EMBGVoza),
140 cenaMS decimal
141);
142
143create table Vraboteni (
144 EMBGV char(13) primary key,
145 idIS integer references IzdavSaloni(idIS),
146 imeV varchar(255)
147);
148
149create table VraboteniBr (
150 EMBGVBr char(13) references Vraboteni(EMBGV),
151 telBrV char(9),
152 constraint pk_VraboteniBr primary key (EMBGVBr, telBrV)
153);
154
155create table Menadzeri (
156 EMBGVM char(13) primary key,
157 constraint fk_Menadzeri foreign key (EMBGVM) references Vraboteni(EMBGV)
158);
159
160create table Prodavachi (
161 EMBGVP char(13) primary key,
162 EMBGVM char(13) references Menadzeri(EMBGVM),
163 constraint fk_Prodavachi foreign key (EMBGVP) references Vraboteni(EMBGV)
164);
165
166create table Rezervacii (
167 idRez serial primary key,
168 EMBGVoza char(13) references Vozaci(EMBGVoza),
169 idDog integer references Dogovori(idDog),
170 naDatumRez date,
171 doDatumRez date
172);
173
174create table ServisKomp (
175 brSK integer primary key,
176 imeSK varchar(255),
177 adresaSK varchar(255)
178);
179
180create table ServisKompBr (
181 brSK integer references ServisKomp(brSK),
182 telBrSK char(9),
183 constraint pk_ServisKompBr primary key (brSK, telBrSK)
184);
185
186create table Servisi (
187 brS serial primary key,
188 brSk integer references ServisKomp(brSK),
189 idVozi integer references Vozila(idVozi),
190 cenaS decimal,
191 naDatumS date
192);
193
194create table Smetki (
195 brSm serial primary key,
196 idDog integer references Dogovori(idDog),
197 cenaSm decimal,
198 datumSm date
199);
200
201create table Parkirani (
202 od date,
203 doD date,
204 idVozi integer references Vozila(idVozi),
205 idIS integer references IzdavSaloni(idIS),
206 constraint pk_Parkirani primary key (od, idVozi, idIS)
207);
208
209create table Naplati (
210 idNap serial,
211 brSM integer references Smetki(brSm),
212 EMBGVP char(13) references Prodavachi(EMBGVP),
213 kesh boolean,
214 kartica boolean,
215 datumNap date,
216 constraint pk_Naplati primary key (idNap, brSM)
217);
218
219create table rezervacii_ima_za_vozila (
220 idRez integer references Rezervacii(idRez),
221 idVozi integer references Vozila (idVozi),
222 constraint pk_rez_ima_za_vozi primary key (idRez, IdVozi)
223);
224
225create table vozaci_e_na_klienti (
226 EMBGVoza char(13) references Vozaci(EMBGVoza),
227 EMBGK char(13) references Klienti(EMBGK),
228 constraint pk_voz_e_na_klienti primary key (EMBGVoza, EMBGK)
229);
230
231create table vozaci_ima_kategorii (
232 EMBGVoza char(13) references Vozaci(EMBGVoza),
233 idKat integer references Kategorii(idKat),
234 constraint pk_voz_ima_kat primary key (EMBGVoza, idKat)
235);
236
237alter table izdavsaloni
238add imeSal varchar(255);
239
240alter table fizickilbr
241alter column telbrfl type varchar(20);
242
243alter table izdavsalonibr
244alter column telbris type varchar(20);
245
246alter table serviskompbr
247alter column telbrsk type varchar(20);
248
249alter table vrabotenibr
250alter column telbrv type varchar(20);
251
252alter table vozacibr
253alter column telbrvoza type varchar(20);