DDL: kreiranje_v1.sql

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