DDL: kreiranje_v2.sql

File kreiranje_v2.sql, 9.5 KB (added by 211012, 8 months ago)
Line 
1CREATE SCHEMA IF NOT EXISTS final_eglas;
2
3SET SEARCH_PATH TO final_eglas;
4
5
6DROP TABLE IF EXISTS kodovi;
7DROP TABLE IF EXISTS oformuva;
8DROP TABLE IF EXISTS se_clenovi_na;
9DROP TABLE IF EXISTS e_kandidat_vo;
10DROP TABLE IF EXISTS glasovi_za_lista;
11DROP TABLE IF EXISTS glasovi_za_kandidat;
12DROP TABLE IF EXISTS glasanja;
13DROP TABLE IF EXISTS komisii;
14DROP TABLE IF EXISTS kandidatski_listi;
15DROP TABLE IF EXISTS kandidaturi;
16DROP TABLE IF EXISTS realizacii_so_kandidaturi;
17DROP TABLE IF EXISTS realizacii_so_kandidatska_lista;
18DROP TABLE IF EXISTS koalicii;
19DROP TABLE IF EXISTS prigovori_od_gragjani;
20DROP TABLE IF EXISTS prigovori_od_kandidati;
21DROP TABLE IF EXISTS prigovori;
22DROP TABLE IF EXISTS partii;
23DROP TABLE IF EXISTS realizacii_na_izbori;
24DROP TABLE IF EXISTS clenovi_na_komisija;
25DROP TABLE IF EXISTS kandidati;
26DROP TABLE IF EXISTS lokacija_izbiracko_mesto;
27DROP TABLE IF EXISTS korisnicki_profil;
28DROP TABLE IF EXISTS dokumenti_za_identifikacija;
29DROP TABLE IF EXISTS gragjani;
30DROP TABLE IF EXISTS adresi;
31DROP TABLE IF EXISTS kandidatski_listi;
32DROP TABLE IF EXISTS izbiracki_mesta;
33DROP TABLE IF EXISTS izborni_edinici;
34DROP TABLE IF EXISTS izbori;
35DROP TABLE IF EXISTS opstini;
36
37CREATE TABLE opstini (
38 o_id SERIAL PRIMARY KEY,
39 o_ime VARCHAR(255) NOT NULL
40);
41
42CREATE TABLE izbori (
43 i_id SERIAL PRIMARY KEY,
44 i_naziv VARCHAR(255) NOT NULL
45);
46
47CREATE TABLE izborni_edinici (
48 ie_id SERIAL PRIMARY KEY,
49 ie_ime VARCHAR(255) NOT NULL,
50 ie_broj_pratenici INT NOT NULL
51);
52
53CREATE TABLE izbiracki_mesta (
54 im_id SERIAL PRIMARY KEY,
55 im_mesto VARCHAR(255) NOT NULL,
56 ie_id INT NOT NULL,
57 a_id BIGINT NOT NULL,
58
59 CONSTRAINT fk_izbiracki_mesta_izborni_edinici FOREIGN KEY (ie_id) REFERENCES izborni_edinici(ie_id)
60);
61
62CREATE TABLE adresi (
63 a_id BIGSERIAL PRIMARY KEY,
64 a_ulica VARCHAR(255) NOT NULL,
65 a_broj INT NOT NULL,
66 a_vlez VARCHAR(255),
67 a_stan VARCHAR(255),
68 o_id INT NOT NULL,
69 im_id INT,
70 CONSTRAINT fk_adresi_opstini FOREIGN KEY (o_id) REFERENCES opstini(o_id),
71 CONSTRAINT fk_adresi_izbiracki_mesta FOREIGN KEY (a_id) REFERENCES izbiracki_mesta(im_id)
72);
73
74CREATE TABLE gragjani (
75 g_id BIGSERIAL PRIMARY KEY,
76 g_ime VARCHAR(255) NOT NULL,
77 g_prezime VARCHAR(255) NOT NULL,
78 g_embg VARCHAR(13) NOT NULL,
79 g_datum_ragjanje DATE NOT NULL,
80 g_godini INT NOT NULL,
81 a_id BIGINT NOT NULL,
82 CONSTRAINT fk_gragjani_adresi FOREIGN KEY (a_id) REFERENCES adresi(a_id)
83);
84
85CREATE TABLE korisnicki_profil (
86 kp_id BIGSERIAL PRIMARY KEY,
87 kp_korisnicko_ime VARCHAR(255) NOT NULL,
88 kp_lozinka VARCHAR(255) NOT NULL,
89 kp_uloga VARCHAR(255) NOT NULL,
90 g_id BIGINT NOT NULL,
91 CONSTRAINT fk_profili_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id)
92);
93
94
95CREATE TABLE dokumenti_za_identifikacija (
96 d_broj BIGSERIAL PRIMARY KEY,
97 d_vazhi_od DATE NOT NULL,
98 d_vazhi_do DATE NOT NULL,
99 d_izdaden DATE NOT NULL,
100 g_id BIGINT NOT NULL,
101 CONSTRAINT fk_dokumenti_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id)
102);
103
104CREATE TABLE kandidati (
105 g_id BIGSERIAL PRIMARY KEY,
106 CONSTRAINT fk_kandidati_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id)
107);
108
109CREATE TABLE clenovi_na_komisija (
110 g_id BIGSERIAL PRIMARY KEY,
111 CONSTRAINT fk_clenovi_na_komisija_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id)
112);
113
114CREATE TABLE realizacii_na_izbori (
115 ri_id SERIAL PRIMARY KEY,
116 ri_ime VARCHAR(255) NOT NULL,
117 ri_datum DATE NOT NULL,
118 i_id SERIAL NOT NULL,
119 CONSTRAINT fk_realizacii_na_izbori_izbori FOREIGN KEY (i_id) REFERENCES izbori(i_id)
120);
121
122CREATE TABLE partii (
123 p_id SERIAL PRIMARY KEY,
124 p_ime VARCHAR(255) NOT NULL,
125 a_id BIGINT NOT NULL,
126 CONSTRAINT fk_partii_adresi FOREIGN KEY (a_id) REFERENCES adresi(a_id)
127);
128
129CREATE TABLE prigovori (
130 pr_id SERIAL PRIMARY KEY,
131 pr_opis VARCHAR(255) NOT NULL,
132 ri_id INT NOT NULL,
133 im_id INT NOT NULL,
134 CONSTRAINT fk_prigovori_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id),
135 CONSTRAINT fk_prigovori_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id)
136);
137
138CREATE TABLE prigovori_od_kandidati (
139 pr_id SERIAL PRIMARY KEY,
140 g_id BIGINT NOT NULL,
141 CONSTRAINT fk_prigovori_gragjani_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
142 CONSTRAINT fk_prigovori_gragjani_prigovori FOREIGN KEY (pr_id) REFERENCES prigovori(pr_id)
143);
144
145CREATE TABLE prigovori_od_gragjani (
146 pr_id SERIAL PRIMARY KEY,
147 g_id BIGINT NOT NULL,
148 CONSTRAINT fk_prigovori_gragjani_kandidati FOREIGN KEY (g_id) REFERENCES kandidati(g_id),
149 CONSTRAINT fk_prigovori_gragjani_prigovori FOREIGN KEY (pr_id) REFERENCES prigovori(pr_id)
150);
151
152CREATE TABLE koalicii (
153 koa_id SERIAL PRIMARY KEY,
154 koa_ime VARCHAR(255) NOT NULL,
155 koa_moto VARCHAR(255) NOT NULL,
156 ri_id INT NOT NULL,
157 o_id INT,
158 ie_id INT,
159 CONSTRAINT fk_koalicii_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id),
160 CONSTRAINT fk_koalicii_opstini FOREIGN KEY (o_id) REFERENCES opstini(o_id),
161 CONSTRAINT fk_koalicii_izborni_edinici FOREIGN KEY (ie_id) REFERENCES izborni_edinici(ie_id)
162);
163
164CREATE TABLE realizacii_so_kandidatska_lista (
165 ri_id SERIAL PRIMARY KEY,
166 CONSTRAINT fk_realizacii_so_kandidatska_lista_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id)
167);
168
169CREATE TABLE realizacii_so_kandidaturi (
170 ri_id SERIAL PRIMARY KEY,
171 CONSTRAINT fk_realizacii_so_kandidaturi_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id)
172);
173
174CREATE TABLE kandidaturi (
175 kan_id SERIAL PRIMARY KEY,
176 kan_opis VARCHAR(255) NOT NULL,
177 g_id BIGINT NOT NULL,
178 ri_id INT NOT NULL,
179 p_id INT,
180 o_id INT,
181 CONSTRAINT fk_kandidaturi_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
182 CONSTRAINT fk_kandidaturi_realizacii_so_kandidat FOREIGN KEY (ri_id) REFERENCES realizacii_so_kandidaturi(ri_id),
183 CONSTRAINT fk_kandidaturi_partii FOREIGN KEY (p_id) REFERENCES partii(p_id),
184 CONSTRAINT fk_kandidaturi_opstini FOREIGN KEY (o_id) REFERENCES opstini(o_id)
185);
186
187CREATE TABLE kandidatski_listi (
188 kl_id SERIAL PRIMARY KEY,
189 kl_opis VARCHAR(255) NOT NULL,
190 ri_id INT NOT NULL,
191 p_id INT,
192 ie_id INT NOT NULL,
193 CONSTRAINT fk_kandidatski_listi_realizacii_so_kandidatska_lista FOREIGN KEY (ri_id) REFERENCES realizacii_so_kandidatska_lista(ri_id),
194 CONSTRAINT fk_kandidatski_listi_partii FOREIGN KEY (p_id) REFERENCES partii(p_id),
195 CONSTRAINT fk_kandidatski_listi_izborni_edinici FOREIGN KEY (ie_id) REFERENCES izborni_edinici(ie_id)
196);
197
198CREATE TABLE komisii (
199 kom_id SERIAL PRIMARY KEY,
200 ri_id INT NOT NULL,
201 im_id INT NOT NULL,
202 CONSTRAINT fk_komisii_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id),
203 CONSTRAINT fk_komisii_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id)
204);
205
206CREATE TABLE glasanja (
207 ug_id SERIAL PRIMARY KEY,
208 ug_vreme TIMESTAMP NOT NULL,
209 g_id BIGINT NOT NULL,
210 im_id INT NOT NULL,
211 ri_id INT NOT NULL,
212 CONSTRAINT fk_glasanja_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
213 CONSTRAINT fk_glasanja_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id),
214 CONSTRAINT fk_glasanja_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id)
215);
216
217CREATE TABLE glasovi_za_kandidat (
218 gk_id BIGSERIAL PRIMARY KEY,
219 im_id INT NOT NULL,
220 ri_id INT NOT NULL,
221 kan_id INT NOT NULL,
222 CONSTRAINT fk_glas_za_kandidat_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id),
223 CONSTRAINT fk_glas_za_kandidat_realizacii_so_kandidate FOREIGN KEY (ri_id) REFERENCES realizacii_so_kandidaturi(ri_id),
224 CONSTRAINT fk_glas_za_kandidat_kandidaturi FOREIGN KEY (kan_id) REFERENCES kandidaturi(kan_id)
225);
226
227CREATE TABLE glasovi_za_lista (
228 gl_id BIGSERIAL PRIMARY KEY,
229 im_id INT NOT NULL,
230 ri_id INT NOT NULL,
231 kl_id INT NOT NULL,
232 CONSTRAINT fk_glas_za_kandidat_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id),
233 CONSTRAINT fk_glas_za_kandidat_realizacii_so_kandidate FOREIGN KEY (ri_id) REFERENCES realizacii_so_kandidaturi(ri_id),
234 CONSTRAINT fk_glas_za_lista_kandidatski_listi FOREIGN KEY (kl_id) REFERENCES realizacii_so_kandidatska_lista(ri_id)
235);
236
237CREATE TABLE e_kandidat_vo (
238 g_id BIGSERIAL NOT NULL,
239 kl_id INT NOT NULL,
240 PRIMARY KEY (g_id, kl_id),
241 CONSTRAINT fk_e_kandidat_vo_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
242 CONSTRAINT fk_e_kandidat_vo_kandidatski_listi FOREIGN KEY (kl_id) REFERENCES kandidatski_listi(kl_id)
243);
244
245CREATE TABLE se_clenovi_na (
246 g_id BIGSERIAL NOT NULL,
247 kom_id INT NOT NULL,
248 PRIMARY KEY (g_id, kom_id),
249 CONSTRAINT fk_se_clenovi_na_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
250 CONSTRAINT fk_se_clenovi_na_komisii FOREIGN KEY (kom_id) REFERENCES komisii(kom_id)
251);
252
253CREATE TABLE oformuva(
254 p_id SERIAL NOT NULL,
255 koa_id SERIAL NOT NULL,
256 PRIMARY KEY (p_id, koa_id),
257 CONSTRAINT fk_oformuva_partii FOREIGN KEY (p_id) REFERENCES partii(p_id),
258 CONSTRAINT fk_oformuva_koalicii FOREIGN KEY (koa_id) REFERENCES koalicii(koa_id)
259);
260
261CREATE TABLE kodovi(
262 ug_id SERIAL NOT NULL,
263 kod_kod VARCHAR(255) NOT NULL UNIQUE,
264 kod_vazi_do DATE NOT NULL,
265 PRIMARY KEY(kod_kod, ug_id),
266 CONSTRAINT fk_kodovi_glasanja FOREIGN KEY (ug_id) REFERENCES glasanja(ug_id)
267);