DDL: eglas_kreiranje_corrected.sql

File eglas_kreiranje_corrected.sql, 9.3 KB (added by 212005, 5 months ago)
Line 
1drop schema final_eglas cascade;
2CREATE SCHEMA IF NOT EXISTS final_eglas;
3
4SET SEARCH_PATH TO final_eglas;
5
6
7DROP TABLE IF EXISTS kodovi;
8DROP TABLE IF EXISTS oformuva;
9DROP TABLE IF EXISTS se_clenovi_na;
10DROP TABLE IF EXISTS e_kandidat_vo;
11DROP TABLE IF EXISTS glasovi_za_lista;
12DROP TABLE IF EXISTS glasovi_za_kandidat;
13DROP TABLE IF EXISTS glasanja;
14DROP TABLE IF EXISTS komisii;
15DROP TABLE IF EXISTS kandidatski_listi;
16DROP TABLE IF EXISTS kandidaturi;
17DROP TABLE IF EXISTS realizacii_so_kandidaturi;
18DROP TABLE IF EXISTS realizacii_so_kandidatska_lista;
19DROP TABLE IF EXISTS koalicii;
20DROP TABLE IF EXISTS prigovori_od_gragjani;
21DROP TABLE IF EXISTS prigovori_od_kandidati;
22DROP TABLE IF EXISTS prigovori;
23DROP TABLE IF EXISTS partii;
24DROP TABLE IF EXISTS realizacii_na_izbori;
25DROP TABLE IF EXISTS clenovi_na_komisija;
26DROP TABLE IF EXISTS kandidati;
27DROP TABLE IF EXISTS lokacija_izbiracko_mesto;
28DROP TABLE IF EXISTS korisnicki_profil;
29DROP TABLE IF EXISTS dokumenti_za_identifikacija;
30DROP TABLE IF EXISTS gragjani;
31DROP TABLE IF EXISTS adresi;
32DROP TABLE IF EXISTS kandidatski_listi;
33DROP TABLE IF EXISTS izbiracki_mesta;
34DROP TABLE IF EXISTS izborni_edinici;
35DROP TABLE IF EXISTS izbori;
36DROP TABLE IF EXISTS opstini;
37
38CREATE TABLE opstini (
39 o_id SERIAL PRIMARY KEY,
40 o_ime VARCHAR(255) NOT NULL
41);
42
43CREATE TABLE izbori (
44 i_id SERIAL PRIMARY KEY,
45 i_naziv VARCHAR(255) NOT NULL
46);
47
48CREATE TABLE izborni_edinici (
49 ie_id SERIAL PRIMARY KEY,
50 ie_ime VARCHAR(255) NOT NULL,
51 ie_broj_pratenici INT NOT NULL
52);
53
54CREATE TABLE izbiracki_mesta (
55 im_id SERIAL PRIMARY KEY,
56 im_mesto VARCHAR(255) NOT NULL,
57 ie_id INT NOT NULL,
58 a_id BIGINT NOT NULL,
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 (im_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_od 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 pr_status INT NOT NULL,
135 CONSTRAINT fk_prigovori_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id),
136 CONSTRAINT fk_prigovori_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id)
137);
138
139CREATE TABLE prigovori_od_gragjanin (
140 pr_id SERIAL PRIMARY KEY,
141 g_id BIGINT NOT NULL,
142 CONSTRAINT fk_prigovori_gragjanin_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
143 CONSTRAINT fk_prigovori_gragjanin_prigovori FOREIGN KEY (pr_id) REFERENCES prigovori(pr_id)
144);
145
146CREATE TABLE prigovori_od_uchesnik (
147 pr_id SERIAL PRIMARY KEY,
148 g_id BIGINT NOT NULL,
149 CONSTRAINT fk_prigovori_uchesnik_kandidati FOREIGN KEY (g_id) REFERENCES kandidati(g_id),
150 CONSTRAINT fk_prigovori_uchesnik_prigovori FOREIGN KEY (pr_id) REFERENCES prigovori(pr_id)
151);
152
153CREATE TABLE koalicii (
154 koa_id SERIAL PRIMARY KEY,
155 koa_ime VARCHAR(255) NOT NULL,
156 koa_moto VARCHAR(255) NOT NULL,
157 ri_id INT NOT NULL,
158 CONSTRAINT fk_koalicii_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id)
159);
160
161CREATE TABLE realizacii_so_kandidatska_lista (
162 ri_id SERIAL PRIMARY KEY,
163 CONSTRAINT fk_realizacii_so_kandidatska_lista_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id)
164);
165
166CREATE TABLE realizacii_so_kandidat (
167 ri_id SERIAL PRIMARY KEY,
168 CONSTRAINT fk_realizacii_so_kandidat_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id)
169);
170
171CREATE TABLE kandidaturi (
172 kan_id SERIAL PRIMARY KEY,
173 kan_opis VARCHAR(255) NOT NULL,
174 g_id BIGINT NOT NULL,
175 ri_id INT NOT NULL,
176 p_id INT,
177 o_id INT,
178 CONSTRAINT fk_kandidaturi_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
179 CONSTRAINT fk_kandidaturi_realizacii_so_kandidat FOREIGN KEY (ri_id) REFERENCES realizacii_so_kandidat(ri_id),
180 CONSTRAINT fk_kandidaturi_partii FOREIGN KEY (p_id) REFERENCES partii(p_id),
181 CONSTRAINT fk_kandidaturi_opstini FOREIGN KEY (o_id) REFERENCES opstini(o_id)
182);
183
184CREATE TABLE kandidatski_listi (
185 kl_id SERIAL PRIMARY KEY,
186 kl_opis VARCHAR(255) NOT NULL,
187 ri_id INT NOT NULL,
188 p_id INT,
189 ie_id INT NOT NULL,
190 CONSTRAINT fk_kandidatski_listi_realizacii_so_kandidatska_lista FOREIGN KEY (ri_id) REFERENCES realizacii_so_kandidatska_lista(ri_id),
191 CONSTRAINT fk_kandidatski_listi_partii FOREIGN KEY (p_id) REFERENCES partii(p_id),
192 CONSTRAINT fk_kandidatski_listi_izborni_edinici FOREIGN KEY (ie_id) REFERENCES izborni_edinici(ie_id)
193);
194
195CREATE TABLE komisii (
196 k_id SERIAL PRIMARY KEY,
197 ri_id INT NOT NULL,
198 im_id INT NOT NULL,
199 CONSTRAINT fk_komisii_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id),
200 CONSTRAINT fk_komisii_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id)
201);
202
203CREATE TABLE glasanja (
204 ug_id SERIAL PRIMARY KEY,
205 ug_vreme TIMESTAMP NOT NULL,
206 g_id BIGINT NOT NULL,
207 im_id INT ,
208 ri_id INT NOT NULL,
209 CONSTRAINT fk_glasanja_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
210 CONSTRAINT fk_glasanja_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id),
211 CONSTRAINT fk_glasanja_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id)
212);
213
214CREATE TABLE kodovi_za_identifikacija (
215 ki_id_kod BIGSERIAL PRIMARY KEY,
216 ki_dodelen BOOLEAN,
217 ki_vazi_do DATE NOT NULL
218);
219
220CREATE TABLE glas (
221 gl_id BIGSERIAL PRIMARY KEY,
222 im_id INT NOT NULL,
223 ri_id INT NOT NULL,
224 ki_id_kod BIGSERIAL NOT NULL,
225 CONSTRAINT fk_glas_izbiracki_mesta FOREIGN KEY (im_id) REFERENCES izbiracki_mesta(im_id),
226 CONSTRAINT fk_glas_realizacii_na_izbori FOREIGN KEY (ri_id) REFERENCES realizacii_na_izbori(ri_id),
227 CONSTRAINT fk_glas_kod_za_identifikacija FOREIGN KEY (ki_id_kod) REFERENCES kodovi_za_identifikacija(ki_id_kod)
228);
229
230CREATE TABLE glasovi_za_kandidat (
231 gl_id BIGSERIAL PRIMARY KEY,
232 kan_id INT NOT NULL,
233 CONSTRAINT fk_glas_za_kandidat_glas FOREIGN KEY (gl_id) REFERENCES glas(gl_id),
234 CONSTRAINT fk_glas_za_kandidat_kandidaturi FOREIGN KEY (kan_id) REFERENCES kandidaturi(kan_id)
235);
236
237CREATE TABLE glasovi_za_lista (
238 gl_id BIGSERIAL PRIMARY KEY,
239 kl_id INT NOT NULL,
240 CONSTRAINT fk_glas_za_lista_glas FOREIGN KEY (gl_id) REFERENCES glas(gl_id),
241 CONSTRAINT fk_glas_za_lista_kandidatski_listi FOREIGN KEY (kl_id) REFERENCES realizacii_so_kandidatska_lista(ri_id)
242);
243
244CREATE TABLE e_kandidat_vo (
245 g_id BIGSERIAL NOT NULL,
246 kl_id INT NOT NULL,
247 PRIMARY KEY (g_id, kl_id),
248 CONSTRAINT fk_e_kandidat_vo_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
249 CONSTRAINT fk_e_kandidat_vo_kandidatski_listi FOREIGN KEY (kl_id) REFERENCES kandidatski_listi(kl_id)
250);
251
252CREATE TABLE se_clenovi_na (
253 g_id BIGSERIAL NOT NULL,
254 k_id INT NOT NULL,
255 PRIMARY KEY (g_id, k_id),
256 CONSTRAINT fk_se_clenovi_na_gragjani FOREIGN KEY (g_id) REFERENCES gragjani(g_id),
257 CONSTRAINT fk_se_clenovi_na_komisii FOREIGN KEY (k_id) REFERENCES komisii(k_id)
258);
259
260CREATE TABLE oformuva(
261 p_id SERIAL NOT NULL,
262 koa_id SERIAL NOT NULL,
263 PRIMARY KEY (p_id, koa_id),
264 CONSTRAINT fk_oformuva_partii FOREIGN KEY (p_id) REFERENCES partii(p_id),
265 CONSTRAINT fk_oformuva_koalicii FOREIGN KEY (koa_id) REFERENCES koalicii(koa_id)
266);
267
268CREATE TABLE kodovi(
269 ug_id SERIAL NOT NULL,
270 kod_kod VARCHAR(255) NOT NULL UNIQUE,
271 kod_vazi_do DATE NOT NULL,
272 PRIMARY KEY(kod_kod, ug_id),
273 CONSTRAINT fk_kodovi_glasanja FOREIGN KEY (ug_id) REFERENCES glasanja(ug_id)
274);