RelationalDesign: kreiranje_v1.sql

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