RelationaDesign: Kreiranje-ddl.sql

File Kreiranje-ddl.sql, 5.4 KB (added by 193076, 3 years ago)
Line 
1--BRISENJE TABELI
2drop table if exists chovek cascade;
3drop table if exists authentication cascade;
4drop table if exists chlen cascade;
5drop table if exists bibliotekar cascade;
6drop table if exists avtor cascade;
7drop table if exists kategorija cascade;
8drop table if exists izdavac cascade;
9drop table if exists dobavuvac cascade;
10drop table if exists knigi cascade;
11drop table if exists izdava cascade;
12drop table if exists e_od cascade;
13drop table if exists ocena cascade;
14drop table if exists instanca_od_kniga cascade;
15drop table if exists pozajmica cascade;
16drop table if exists kazna cascade;
17
18
19--DODAVANJE TABELI
20-----------------------------------------
21
22create table chovek(
23id_chovek serial primary key,
24ime varchar(50) not null,
25prezime varchar(50) not null,
26email varchar(50) not null,
27tel_broj varchar(25) not null,
28adresa varchar(150) not null,
29embg char(13) not null,
30username varchar(50) not null,
31password varchar(100) not null
32);
33
34-----------------------------------------
35
36create table authentication(
37log_id serial primary key,
38timestamp_log timestamp not null,
39id_chovek integer not null,
40constraint fk_id_chovek foreign key (id_chovek) references chovek (id_chovek)
41);
42
43-----------------------------------------
44
45create table chlen(
46id_chovek serial primary key,
47broj integer not null,
48datum_na_zachlenuvanje timestamp not null,
49constraint fk_id_chovek foreign key (id_chovek) references chovek (id_chovek)
50);
51
52-----------------------------------------
53
54create table bibliotekar(
55id_chovek serial primary key,
56id_na_bibliotekar integer not null,
57constraint fk_id_chovek foreign key (id_chovek) references chovek (id_chovek)
58);
59
60-----------------------------------------
61
62create table avtor(
63id_na_avtor serial primary key,
64ime varchar(100) not null
65);
66
67-----------------------------------------
68
69create table kategorija(
70id_na_kategorija serial primary key,
71ime_kategorija varchar(30) not null
72);
73
74-----------------------------------------
75
76create table izdavac(
77id_na_izdavac serial primary key,
78email varchar(50) not null,
79lokacija varchar(200) not null,
80ime_na_izdavac varchar(100) not null
81);
82
83-----------------------------------------
84
85create table dobavuvac(
86id_na_dobavuvac serial primary key,
87ime_na_dobavuvac varchar(100) not null
88);
89
90-----------------------------------------
91
92create table knigi(
93id_na_kniga serial primary key,
94opis varchar(5500) not null,
95naslov varchar (100) not null,
96id_izdavac integer not null,
97constraint fk_id_izdavac foreign key (id_izdavac) references izdavac(id_na_izdavac)
98);
99
100-----------------------------------------
101
102create table izdava(
103id_avtor serial not null,
104id_knigi serial not null,
105constraint pk_izdadeno primary key (id_knigi, id_avtor),
106constraint fk_id_avtor foreign key (id_avtor) references avtor (id_na_avtor),
107constraint fk_id_knigi foreign key (id_knigi) references knigi (id_na_kniga)
108);
109
110-----------------------------------------
111
112create table e_od(
113id_na_kategorija serial not null,
114id_knigi serial not null,
115constraint pk_e_od primary key (id_knigi, id_na_kategorija),
116constraint fk_id_na_kategorija foreign key (id_na_kategorija) references kategorija (id_na_kategorija),
117constraint fk_id_knigi foreign key (id_knigi) references knigi(id_na_kniga)
118);
119
120-----------------------------------------
121
122create table ocena(
123id_na_ocena serial primary key,
124ocena integer not null,
125komentar varchar(500) not null,
126id_chovek_dava integer not null,
127ocena_id_kniga integer,
128ocena_id_avtor integer,
129constraint fk_id_chovek_dava foreign key (id_chovek_dava) references chlen (id_chovek),
130constraint fk_ocena_id_kniga foreign key (ocena_id_kniga) references knigi (id_na_kniga),
131constraint fk_ocena_id_avtor foreign key (ocena_id_avtor) references avtor (id_na_avtor)
132);
133
134-----------------------------------------
135
136create table instanca_od_kniga(
137unique_id serial,
138dali_e_ostetena boolean not null,
139status boolean not null,
140id_na_dobavuvac integer not null,
141id_na_kniga integer not null,
142constraint pk_id_na_kniga primary key (id_na_kniga,unique_id),
143constraint fk_id_na_kniga foreign key (id_na_kniga) references knigi (id_na_kniga),
144constraint fk_id_na_dobavuvac foreign key (id_na_dobavuvac) references dobavuvac (id_na_dobavuvac)
145);
146
147-----------------------------------------
148
149create table pozajmica(
150id_na_pozajmica serial primary key,
151datum_na_zemanje date not null,
152datum_na_vrakjanje date not null,
153status boolean not null,
154id_chovek_odobruva integer not null,
155id_chovek_prave integer not null,
156unique_id integer not null,
157id_na_kniga integer not null,
158constraint fk_id_chovek_odobruva foreign key (id_chovek_odobruva) references bibliotekar (id_chovek),
159constraint fk_id_chovek_prave foreign key (id_chovek_prave) references chlen (id_chovek),--,
160constraint fk_unique_id foreign key (id_na_kniga,unique_id) references instanca_od_kniga(id_na_kniga,unique_id)
161);
162
163-----------------------------------------
164
165create table kazna(
166id_kazna serial primary key,
167datum date not null,
168cena_vo_denari FLOAT not null,
169id_pozajmica_proverka integer not null,
170id_chovek_imakazna integer not null,
171constraint fk_id_pozajmica_proverka foreign key (id_pozajmica_proverka) references pozajmica (id_na_pozajmica),
172constraint fk_id_chovek_imakazna foreign key (id_chovek_imakazna) references chlen (id_chovek)
173);
174
175-----------------------------------------
176