RelationalDesign: kreiranje_v2.sql

File kreiranje_v2.sql, 6.0 KB (added by 216049, 4 weeks ago)
Line 
1set search_path = project_new;
2
3drop table if exists Se_simnuva_na;
4--drop table if exists Se_validira;
5drop table if exists KaznaZaRegistriran;
6drop table if exists KaznaZaNeregistriran;
7drop table if exists Kazna;
8drop table if exists Kontroli;
9drop table if exists Vozenje;
10drop table if exists Bilet;
11drop table if exists TipBilet;
12drop table if exists PostojkaNaLinija;
13drop table if exists Postojka;
14drop table if exists Mesto;
15drop table if exists InstancaNaLinija;
16drop table if exists Linija;
17drop table if exists Avtobus;
18--drop table if exists Dokument;
19drop table if exists Kondukter;
20drop table if exists Vozac;
21drop table if exists Vraboten;
22drop table if exists Patnik;
23drop table if exists Korisnik;
24
25drop domain if exists string_dolg;
26drop domain if exists string_kratok;
27
28drop schema if exists project_new;
29
30create schema project_new;
31
32set search_path = project_new;
33
34create domain string_dolg AS character varying(4000);
35create domain string_kratok AS character varying(500);
36
37create table Korisnik(
38 k_id bigserial primary key,
39 k_ime string_dolg not null,
40 k_adresa string_dolg,
41 k_telefon string_kratok not null,
42 k_email string_kratok not null,
43 k_embg varchar(13),
44 k_is_admin bool not null,
45 k_lozinka string_kratok not null
46);
47
48create table Patnik(
49 k_id bigint primary key references Korisnik(k_id)
50);
51-- *k_id referencira od Korisnik(k_id)
52
53
54create table Vraboten(
55 k_id bigint primary key references Korisnik(k_id),
56 v_plata float8 not null,
57 v_datum_na_vrabotuvanje date not null,
58 v_datum_prekin_vrabotuvanje date
59);
60-- *k_id referencira od Korisnik(k_id)
61
62
63create table Vozac(
64 k_id bigint primary key references Vraboten(k_id)
65);
66-- *k_id referencira od Korisnik(k_id)
67
68create table Kondukter(
69 k_id bigint primary key references Vraboten(k_id)
70);
71-- *k_id referencira od Korisnik(k_id)
72
73--create table Dokument(
74-- d_broj_na_dokumnet string_kratok primary key,
75-- d_datum_na_izdavanje date not null,
76-- d_datum_ist date not null,
77-- d_koj_go_izdal string_kratok not null,
78-- k_id bigint not null,
79-- constraint dokument_za_covek foreign key (k_id) references Korisnik(k_id)
80--);
81
82create table Avtobus(
83 a_registracija varchar(8) primary key,
84 a_seriski_broj string_kratok not null,
85 a_broj_sedista smallint
86);
87
88create table Linija(
89 li_id integer primary key,
90 li_ime string_kratok not null,
91 li_pravec string_kratok not null
92);
93
94create table InstancaNaLinija (
95 inl_id bigserial primary key,
96 inl_datum_start timestamp not null,
97 inl_datum_end timestamp,
98 vozac_k_id bigint not null,
99 a_registracija varchar(8),
100 li_id integer not null,
101 foreign key (vozac_k_id) references Vozac(k_id),
102 foreign key (a_registracija) references Avtobus(a_registracija),
103 foreign key (li_id) references Linija(li_id)
104);
105-- *k_id referencira od Korisnik(k_id)
106-- ^a_registracija referncira od Avtobus(a_registracija)
107-- #li_id referencira od Linija(li_id)
108
109create table Mesto(
110 m_id serial primary key,
111 m_grad string_kratok not null,
112 m_opstina string_kratok not null,
113 m_ulica string_kratok not null
114);
115
116create table Postojka(
117 p_id serial primary key,
118 p_ime string_kratok not null,
119 m_id integer,
120 constraint postojka_na_mesto foreign key (m_id) references Mesto(m_id)
121);
122 --*m_id referncira od Mesto(m_id)
123
124
125create table PostojkaNaLinija(
126 pnl_id bigserial primary key,
127 pnl_reden_broj smallint not null,
128 li_id integer not null,
129 p_id integer not null,
130 constraint linija_na_postojka foreign key (li_id) references Linija(li_id),
131 constraint postojka_na_linija foreign key (p_id) references Postojka(p_id)
132);
133-- *li_id referencira od Linija(li_id)
134-- ^p_id referencira od Postojka(p_id)
135
136create table TipBilet (
137 tb_id serial primary key,
138 tb_trajnost bigint not null,
139 tb_ime string_kratok not null
140);
141
142
143create table Bilet (
144 b_id bigserial primary key,
145 b_datum_na_kupuvanje timestamp NOT NULL,
146 b_status string_kratok not null,
147 patnik_k_id bigint,
148 tb_id bigint,
149 foreign key (patnik_k_id) references Patnik(k_id),
150 foreign key (tb_id) references TipBilet(tb_id)
151);
152
153
154create table Vozenje (
155 vozenje_id bigserial primary key,
156 vozenje_start timestamp not null,
157 vozenje_end timestamp,
158 vozenje_status string_kratok not null,
159 patnik_k_id bigint,
160 kacuva_pnl_id bigint not null,
161 inl_id bigint not null,
162 b_id bigint not null,
163 foreign key (b_id) references Bilet(b_id),
164 foreign key (patnik_k_id) references Patnik(k_id),
165 foreign key (kacuva_pnl_id) references PostojkaNaLinija(pnl_id),
166 foreign key (inl_id) references InstancaNaLinija(inl_id)
167);
168
169create table Kontroli (
170 kontrola_id bigserial primary key,
171 kontrola_datum timestamp NOT NULL,
172 kondukter_k_id bigint,
173 inl_id bigint,
174 foreign key (kondukter_k_id) references Kondukter(k_id),
175 foreign key (inl_id) references InstancaNaLinija(inl_id)
176);
177
178create table Kazna (
179 kz_id bigserial primary key,
180 kz_iznos float8 NOT NULL,
181 kz_plateno boolean NOT NULL,
182 kz_datum timestamp NOT NULL,
183 kz_datum_plateno timestamp,
184 kz_dokument string_kratok not null,
185 kondukter_k_id bigint,
186 kontrola_id bigint,
187 foreign key (kondukter_k_id) references Kondukter(k_id),
188 foreign key (kontrola_id) references Kontroli(kontrola_id)
189);
190
191create table KaznaZaRegistriran(
192 kz_id bigint primary key references Kazna(kz_id),
193 patnik_k_id bigint,
194 foreign key (patnik_k_id) references Patnik(k_id)
195);
196
197create table KaznaZaNeregistriran(
198 kz_id bigint primary key references Kazna(kz_id),
199 kzn_telefon string_kratok,
200 kzn_ime string_dolg not null,
201 kzn_adresa string_dolg not null
202);
203
204
205
206--create table Se_validira (
207-- vozenje_id bigint not null,
208-- b_id bigint not null,
209-- primary key (vozenje_id, b_id),
210-- foreign key (vozenje_id) references Vozenje(vozenje_id),
211-- foreign key (b_id) references Bilet(b_id)
212--);
213---- * vozenje_id referencira od Vozenje(vozenje_id)
214---- ^ b_id referencira od Bilet(b_id)
215
216
217create table Se_simnuva_na (
218 pnl_id bigint,
219 vozenje_id bigint,
220 primary key (pnl_id, vozenje_id),
221 foreign key (pnl_id) references PostojkaNaLinija(pnl_id),
222 foreign key (vozenje_id) references Vozenje(vozenje_id)
223);
224