RelationalDesign: kreiranje.sql

File kreiranje.sql, 5.2 KB (added by 201051, 2 years ago)
Line 
1create table studenti (
2 s_id serial,
3 s_ime varchar(20) not null,
4 s_prezime varchar(20) not null,
5 s_korisnicko_ime varchar(50) not null,
6 s_email varchar(100) not null,
7 s_lozinka varchar(50) not null,
8 s_index varchar(10) not null,
9 constraint pk_studenti primary key (s_id)
10);
11
12create table administratori (
13 a_id serial,
14 a_ime varchar(20) not null,
15 a_prezime varchar(20) not null,
16 a_korisnicko_ime varchar(50) not null,
17 a_email varchar(100) not null,
18 a_lozinka varchar(50) not null,
19 constraint pk_administratori primary key (a_id)
20);
21
22create table profesori (
23 p_id serial,
24 p_ime varchar(20) not null,
25 p_prezime varchar(20) not null,
26 p_korisnicko_ime varchar(50) not null,
27 p_email varchar(100) not null,
28 p_lozinka varchar(50) not null,
29 constraint pk_profesori primary key (p_id)
30);
31
32create table predmeti (
33 pr_id serial,
34 pr_ime varchar(200) not null,
35 a_id integer not null,
36 constraint pk_predmeti primary key (pr_id),
37 constraint fk_predmeti_administratori foreign key (a_id)
38 references administratori (a_id)
39);
40
41create table kursevi(
42 ku_id serial,
43 ku_ime varchar(200) not null,
44 ku_opis varchar(500),
45 pr_id integer not null,
46 constraint pk_kursevi primary key (ku_id),
47 constraint fk_kursevi_predmeti foreign key (pr_id)
48 references predmeti (pr_id)
49);
50
51create table kategorii(
52 ka_id serial,
53 ka_ime varchar(40) not null,
54 ka_opis varchar(150),
55 a_id integer not null,
56 constraint pk_kategorii primary key (ka_id),
57 constraint fk_kategorii_administratori foreign key (a_id)
58 references administratori (a_id)
59);
60
61create table materijali(
62 m_id serial,
63 m_ime varchar(150) not null,
64 m_bytes text,
65 p_id integer not null,
66 ka_id integer not null,
67 ku_id integer not null,
68 constraint pk_materijali primary key (m_id),
69 constraint fk_materijali_profesori foreign key (p_id)
70 references profesori (p_id),
71 constraint fk_materijali_kategorii foreign key (ka_id)
72 references kategorii (ka_id),
73 constraint fk_materijali_kursevi foreign key (ku_id)
74 references kursevi (ku_id)
75);
76
77create table prasanja(
78 pra_id serial,
79 pra_naslov varchar(100) not null,
80 pra_sodrzina text not null,
81 pra_datum timestamp default now(),
82 ku_id integer not null,
83 s_id integer not null,
84 constraint pk_prasanja primary key (pra_id),
85 constraint fk_prasanja_kursevi foreign key (ku_id)
86 references kursevi (ku_id),
87 constraint fk_prasanja_studenti foreign key (s_id)
88 references studenti (s_id)
89);
90
91create table odgovori(
92 o_id serial,
93 o_sodrzina text not null ,
94 o_validen boolean default false,
95 o_datum timestamp default now(),
96 pra_id integer not null,
97 s_id integer ,
98 p_id integer,
99 constraint pk_odgovori primary key (o_id),
100 constraint fk_odgovori_prasanja foreign key (pra_id)
101 references prasanja (pra_id),
102 constraint fk_odgovori_studenti foreign key (s_id)
103 references studenti (s_id),
104 constraint fk_odgovori_profesori foreign key (p_id)
105 references profesori (p_id),
106 constraint chk_not_null_avtor check (s_id is not null or p_id is not null)
107);
108
109create table prof_term_za_kons(
110 p_id integer not null,
111 termin timestamp not null,
112 constraint pk_prof_term_za_kons primary key (p_id, termin),
113 constraint fk_prof_term_za_kons_profesori foreign key (p_id)
114 references profesori (p_id)
115);
116
117create table e_vo_vrska_so(
118 pra_id integer not null,
119 m_id integer not null,
120 constraint fk_e_vp_vrska_so_prasanja foreign key (pra_id)
121 references prasanja (pra_id),
122 constraint fk_e_vp_vrska_so_materijali foreign key (m_id)
123 references materijali (m_id),
124 constraint pk_e_vo_vrska_so primary key (pra_id, m_id)
125);
126
127create table moze_da_slusa(
128 s_id integer not null,
129 ku_id integer not null,
130 constraint fk_moze_da_slusa_studenti foreign key (s_id)
131 references studenti (s_id),
132 constraint fk_moze_da_slusa_kursevi foreign key (ku_id)
133 references kursevi (ku_id),
134 constraint pk_moze_da_slusa primary key (s_id, ku_id)
135);
136
137create table e_prof_na(
138 p_id integer not null,
139 ku_id integer not null,
140 constraint fk_e_prof_na_profesori foreign key (p_id)
141 references profesori (p_id),
142 constraint fk_e_prof_na_kursevi foreign key (ku_id)
143 references kursevi (ku_id),
144 constraint pk_e_prof_na primary key (p_id, ku_id)
145);
146
147create table e_oznaceno_so(
148 pra_id integer not null,
149 ka_id integer not null,
150 constraint fk_e_oznaceno_so_prasanja foreign key (pra_id)
151 references prasanja (pra_id),
152 constraint fk_e_oznaceno_so_kategorii foreign key (ka_id)
153 references kategorii (ka_id),
154 constraint pk_e_oznacneo_so primary key (pra_id, ka_id)
155);
156
157create table dava_reakcija(
158 p_id integer not null,
159 o_id integer not null,
160 reakcija boolean not null,
161 constraint fk_dava_reakcija_profesori foreign key (p_id)
162 references profesori (p_id),
163 constraint fk_dava_reakcija_odgovori foreign key (o_id)
164 references odgovori (o_id),
165 constraint pk_dava_reakcija primary key (p_id, o_id)
166);
167
168create table reagira_na(
169 s_id integer not null,
170 o_id integer not null,
171 reakcija boolean not null,
172 constraint fk_reagira_na_studenti foreign key (s_id)
173 references studenti (s_id),
174 constraint fk_reagira_na_odgovori foreign key (o_id)
175 references odgovori (o_id),
176 constraint pk_reagira_na primary key (s_id, o_id)
177);