/*==============================================================*/ /* DBMS name: Microsoft SQL Server 2008 */ /* Created on: 11/19/2014 9:47:13 PM */ /*==============================================================*/ /* Brisanje baze ako postoji */ use master; go drop database PPortal; go /* Brisanje baze ako postoji - KRAJ */ /* Generisanje baze: PPortal*/ create database PPortal; go use PPortal; go /* Generisanje baze: PPortal - KRAJ */ /*==============================================================*/ /* Table: ZEMLJA */ /*==============================================================*/ create table ZEMLJA ( SIF_ZEMLJE int, DRZAVA varchar(30) not null, OZNAKA_DRZAVE char(5) null, constraint PK_ZEMLJA primary key nonclustered (SIF_ZEMLJE) ) go /*==============================================================*/ /* Table: MESTO */ /*==============================================================*/ create table MESTO ( SIF_MESTA int, SIF_ZEMLJE int not null foreign key references ZEMLJA (SIF_ZEMLJE), NAZIV_MESTA varchar(30) not null, POST_BROJ numeric(5) not null, constraint PK_MESTO primary key nonclustered (SIF_MESTA) ) go /*==============================================================*/ /* Table: ZANR */ /*==============================================================*/ create table ZANR ( SIF_ZANRA int, ZANR varchar(50) not null, constraint PK_ZANR primary key nonclustered (SIF_ZANRA) ) go /*==============================================================*/ /* Table: ULOGA */ /*==============================================================*/ create table ULOGA ( SIF_ULOGE int, ULOGA varchar(30) null, constraint PK_ULOGA primary key nonclustered (SIF_ULOGE) ) go /*==============================================================*/ /* Table: POZORISTE */ /*==============================================================*/ create table POZORISTE ( SIF_POZORISTA int, SIF_MESTA int not null foreign key references MESTO (SIF_MESTA), NAZIV_POZORISTA varchar(100) not null, P_POSTANSKI_BROJ numeric(5) not null, P_MESTO varchar(30) not null, P_ULICA_BROJ varchar(30) null, P_TELEFON varchar(20) null, P_WWW_ADRESA varchar(100) null, P_EMAIL varchar(50) null, P_IZ_VOJVODINE bit not null, P_AMATERSKO bit not null, constraint PK_POZORISTE primary key nonclustered (SIF_POZORISTA) ) go /*==============================================================*/ /* Table: LICE */ /*==============================================================*/ create table LICE ( SIF_LICA int, SIF_MESTA int not null foreign key references MESTO (SIF_MESTA), L_PREZIME varchar(20) not null, L_IME varchar(20) not null, L_BIOGRAFIJA varchar(5000) null, L_IZ_VOJVODINE bit not null, L_AMATER bit not null, L_AKTIVNO bit not null, constraint PK_LICE primary key nonclustered (SIF_LICA) ) go /*==============================================================*/ /* Table: PROSTORIJA */ /*==============================================================*/ create table PROSTORIJA ( SIF_POZORISTA int not null foreign key references POZORISTE (SIF_POZORISTA), SIF_PROSTORIJE int, NAZIV_PROSTORIJE varchar(50) not null, PR_BROJ_SEDISTA numeric(3) not null, constraint PK_PROSTORIJA primary key nonclustered (SIF_POZORISTA, SIF_PROSTORIJE) ) go /*==============================================================*/ /* Table: POZORISNO_DELO */ /*==============================================================*/ create table POZORISNO_DELO ( SIF_POZ_DELA int, SIF_ZANRA int not null foreign key references ZANR (SIF_ZANRA), SIF_LICA_PREVOD int foreign key references LICE (SIF_LICA), NASLOV_POZ_DELA varchar(50) not null, POZ_DELO_BR_CINOVA numeric(1) not null, constraint PK_POZORISNO_DELO primary key nonclustered (SIF_POZ_DELA) ) go /*==============================================================*/ /* Table: AUTOR */ /*==============================================================*/ create table AUTOR ( SIF_POZ_DELA int not null foreign key references POZORISNO_DELO (SIF_POZ_DELA), SIF_LICA int not null foreign key references LICE (SIF_LICA), constraint PK_AUTOR primary key nonclustered (SIF_POZ_DELA, SIF_LICA) ) go /*==============================================================*/ /* Table: PREDSTAVA_PROJEKT */ /*==============================================================*/ create table PREDSTAVA_PROJEKT ( SIF_POZ_DELA int not null foreign key references POZORISNO_DELO (SIF_POZ_DELA), NASLOV_PREDSTAVE varchar(50) not null, PREDST_BR_CINOVA numeric(1) not null, PREDST_SADRZAJ varchar(5000) null, PREDST_DAT_I_VREME_PREMIJERE datetime not null, PREDST_DUZINA_U_MIN numeric(3) not null, NA_REPERTOARU bit not null, constraint PK_PREDSTAVA_PROJEKT primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE) ) go /*==============================================================*/ /* Table: POZ_PREDST_PROJ */ /*==============================================================*/ create table POZ_PREDST_PROJ ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA int not null foreign key references POZORISTE (SIF_POZORISTA), constraint PK_POZ_PREDST_PROJ primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA), constraint FK_PREDSTAVA_PROJEKT foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE) ) go /*==============================================================*/ /* Table: PREDSTAVA_NA_REPERTOARU */ /*==============================================================*/ create table PREDSTAVA_NA_REPERTOARU ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA int not null, SIF_PROSTORIJE int not null, POCETAK_PREDSTAVE datetime not null, constraint PK_PREDSTAVA_NA_REPERTOARU primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE), constraint FK_PREDSTAVA_PROJEKT1 foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE), constraint FK_PROSTORIJA foreign key (SIF_POZORISTA, SIF_PROSTORIJE) references PROSTORIJA (SIF_POZORISTA, SIF_PROSTORIJE) ) go /*==============================================================*/ /* Table: PREDST_PROJ_ULOGA */ /*==============================================================*/ create table PREDST_PROJ_ULOGA ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_ULOGE int not null foreign key references ULOGA (SIF_ULOGE), constraint PK_PREDST_PROJ_ULOGA primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE), constraint FK_PREDSTAVA_PROJEKT2 foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE) ) go /*==============================================================*/ /* Table: LICE_U_ULOZI_PREDST_PROJ */ /*==============================================================*/ create table LICE_U_ULOZI_PREDST_PROJ ( SIF_LICA int not null foreign key references LICE (SIF_LICA), SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_ULOGE int not null, constraint PK_LICE_U_ULOZI_PREDST_PROJ primary key nonclustered (SIF_LICA, SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE), constraint FK_PREDST_PROJ_ULOGA_ foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) references PREDST_PROJ_ULOGA (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) ) go /*==============================================================*/ /* Table: UCESTVUJE_U_PREDSTAVI */ /*==============================================================*/ create table UCESTVUJE_U_PREDSTAVI ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA int not null, SIF_PROSTORIJE int not null, POCETAK_PREDSTAVE datetime not null, SIF_LICA int not null, SIF_ULOGE int not null, constraint PK_UCESTVUJE_U_PREDSTAVI primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE, SIF_LICA, SIF_ULOGE), constraint FK_LICE_U_ULOZI_PREDST_PROJ foreign key (SIF_LICA, SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) references LICE_U_ULOZI_PREDST_PROJ (SIF_LICA, SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE), constraint FK_PREDSTAVA_NA_REPERTOARU foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE) references PREDSTAVA_NA_REPERTOARU (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE) ) go use PPortal; go /* PUNJENJE PODATAKA U TABELE */ /* PUNJENJE PODATAKA U TABELE */ /* 1 - Punjenje tabele ZEMLJA */ /* create table ZEMLJA ( SIF_ZEMLJE int, DRZAVA varchar(30) not null, OZNAKA_DRZAVE char(5) null, constraint PK_ZEMLJA primary key nonclustered (SIF_ZEMLJE) ) go */ insert ZEMLJA values (1, 'Republika Srbija', 'SRB'); insert ZEMLJA values (2, 'Crna Gora', 'MNE'); insert ZEMLJA values (3, 'Madjarska', 'H'); insert ZEMLJA values (4, 'Sjedinjene Americke Drzave', 'USA'); /* 2 - Punjenje tabele MESTO */ /* create table MESTO ( SIF_MESTA int, SIF_ZEMLJE int not null foreign key references ZEMLJA (SIF_ZEMLJE), NAZIV_MESTA varchar(30) not null, POST_BROJ numeric(5) not null, constraint PK_MESTO primary key nonclustered (SIF_MESTA) ) go */ insert MESTO values (1, 1, 'Novi Sad', 21000); insert MESTO values(2, 1, 'Beograd',11000); insert MESTO values(3, 2, 'Budva',2350); insert MESTO values(4, 3, 'Budimpesta',1111); insert MESTO values(5, 4, 'New York',13572); insert MESTO values(6, 3, 'Chichago',23657); go /* 3 - Punjenje tabele ZANR */ /* create table ZANR ( SIF_ZANRA int, ZANR varchar(50) not null, constraint PK_ZANR primary key nonclustered (SIF_ZANRA) ) go */ insert ZANR values (1, 'Mjuzikl'); insert ZANR values(2, 'Drama'); go /* 4 - Punjenje tabele ULOGA */ /* create table ULOGA ( SIF_ULOGE int, ULOGA varchar(30) null, constraint PK_ULOGA primary key nonclustered (SIF_ULOGE) ) go */ insert ULOGA values (1, 'Reditelj'); insert ULOGA values(2, 'Domar'); insert ULOGA values(3, 'Bela'); insert ULOGA values(4, 'Ana'); insert ULOGA values(5, 'Tevje, mlekadzija'); insert ULOGA values(6, 'Golde, njegova zena'); insert ULOGA values(7, 'Cajtel, najstarija kci'); go /* 5 - Punjenje tabele POZORISTE */ /* create table POZORISTE ( SIF_POZORISTA int, SIF_MESTA int not null foreign key references MESTO (SIF_MESTA), NAZIV_POZORISTA varchar(100) not null, P_POSTANSKI_BROJ numeric(5) not null, P_MESTO varchar(30) not null, P_ULICA_BROJ varchar(30) null, P_TELEFON varchar(20) null, P_WWW_ADRESA varchar(100) null, P_EMAIL varchar(50) null, P_IZ_VOJVODINE bit not null, P_AMATERSKO bit not null, constraint PK_POZORISTE primary key nonclustered (SIF_POZORISTA) ) go */ insert POZORISTE values(1, 3, 'Budva grad teatar', 2355, 'Budva', 'Put pozorista 1', '+38611578-9814', 'www.budvagradteatar.mne', 'info@budvagradteatar.mne', 'false', 'false'); insert POZORISTE values(2, 2,'Smart studio',11150,'Beograd', 'Kalemegdanska 2','+38111/8123-344','http://www.smartstudio.rs', 'info@smartstudio.rs','false','false'); insert POZORISTE values(3, 2,'Dom omladine Beograda',11110,'Beograd', 'Omladinska 5-7','+38111/8698-344','http://www.domomladinebeograda.rs', 'info@domomladinebeograda.rs','false','false'); insert POZORISTE values(4, 1,'Srpsko narodno pozoriste',21000,'Novi Sad', 'Pozorisni trg 1','+38121/8288-389','http://www.srpskonarodnopozoriste.rs', 'info@srpskonarodnopozoriste.rs','true','false'); insert POZORISTE values(5, 1,'Novosadsko pozoriste',21000,'Novi Sad', 'Jovan Subotic 3-5','+38121/2689-389','http://www.novosadskopozoriste.rs', 'info@novosadskopozoriste.rs','true','false'); go /* 6 - Punjenje tabele LICE */ /* create table LICE ( SIF_LICA int, SIF_MESTA int not null foreign key references MESTO (SIF_MESTA), L_PREZIME varchar(20) not null, L_IME varchar(20) not null, L_BIOGRAFIJA varchar(5000) null, L_IZ_VOJVODINE bit not null, L_AMATER bit not null, L_AKTIVNO bit not null, constraint PK_LICE primary key nonclustered (SIF_LICA) ) go */ insert LICE values(1, 2, 'Maksimovic', 'Danica', 'Biografijaaaaa Danice Maksimovic...', 'false', 'false', 'true'); insert LICE values(2, 2,'Lecic','Branislav','Biografija Branislava Lecica...', 'false','false','true'); insert LICE values(3, 2, 'Vickovic', 'Marija', 'Biografijaaaaa Marije Vickovic...', 'false', 'false', 'true'); insert LICE values(4, 2,'Premovic','Dusko','Biografija Duska Premovica...', 'false','false','true'); insert LICE values(5, 1,'Beres','Atila','Biografija Atile Beresa...', 'false','false','true'); insert LICE values(6, 2,'Jurisa','Ivo','Biografija Ive Jurise...', 'false','false','true'); insert LICE values(7, 1,'Balaz','Aron','Biografija Arona Balaza...', 'false','false','true'); insert LICE values(8, 1,'Josic Gajin','Gordana','Biografija Gordane Josic Gajin...', 'false','false','true'); insert LICE values(9, 1,'Elor','Emina','Biografija Emine Elor...', 'false','false','true'); insert LICE values(10, 5,'Stein','Joseph','Biografija Josepha Steina...', 'false','false','true'); insert LICE values(11, 6,'Bock','Jerry','Biografija Jerryja Bocka...', 'false','false','true'); go /* 7 - Punjenje tabele PROSTORIJA */ /* create table PROSTORIJA ( SIF_POZORISTA int not null foreign key references POZORISTE (SIF_POZORISTA), SIF_PROSTORIJE int, NAZIV_PROSTORIJE varchar(50) not null, PR_BROJ_SEDISTA numeric(3) not null, constraint PK_PROSTORIJA primary key nonclustered (SIF_POZORISTA, SIF_PROSTORIJE) ) go */ insert PROSTORIJA values(1, 1, 'Pera Dobrinovic', 156); insert PROSTORIJA values(3, 1,'Bina 1',150); go /* 8 - Punjenje tabele POZORISNO_DELO */ /* create table POZORISNO_DELO ( SIF_POZ_DELA int, SIF_ZANRA int not null foreign key references ZANR (SIF_ZANRA), SIF_LICA_PREVOD int foreign key references LICE (SIF_LICA), NASLOV_POZ_DELA varchar(50) not null, POZ_DELO_BR_CINOVA numeric(1) not null, constraint PK_POZORISNO_DELO primary key nonclustered (SIF_POZ_DELA) ) go */ insert POZORISNO_DELO values(1, 1, 6, 'Violinista na krovu', 2); insert POZORISNO_DELO values(2, 2, null, 'Indigo',4); go /* 9 - Punjenje tabele AUTOR */ /* create table AUTOR ( SIF_POZ_DELA int not null foreign key references POZORISNO_DELO (SIF_POZ_DELA), SIF_LICA int not null foreign key references LICE (SIF_LICA), constraint PK_AUTOR primary key nonclustered (SIF_POZ_DELA, SIF_LICA) ) go */ insert AUTOR values(2, 4); insert AUTOR values(1, 10); insert AUTOR values(1, 11); go /* 10 - Punjenje tabele PREDSTAVA_PROJEKT */ /* create table PREDSTAVA_PROJEKT ( SIF_POZ_DELA int not null foreign key references POZORISNO_DELO (SIF_POZ_DELA), NASLOV_PREDSTAVE varchar(50) not null, PREDST_BR_CINOVA numeric(1) not null, PREDST_SADRZAJ varchar(5000) null, PREDST_DAT_I_VREME_PREMIJERE datetime not null, PREDST_DUZINA_U_MIN numeric(3) not null, NA_REPERTOARU bit not null, constraint PK_PREDSTAVA_PROJEKT primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE) ) go */ insert PREDSTAVA_PROJEKT values( 1, 'Violinista na krovu', 2, 'Sadrzaj predstaveeeee Violinista.......', '2015.05.23 19:30', 150, 'true'); insert PREDSTAVA_PROJEKT values(2,'Indigo',4,'Sadrzaj predstave Indigo...', '9/23/2012 20:30',90,'true'); go /* 11 - Punjenje tabele POZ_PREDST_PROJ */ /* create table POZ_PREDST_PROJ ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA int not null foreign key references POZORISTE (SIF_POZORISTA), constraint PK_POZ_PREDST_PROJ primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA), constraint FK_PREDSTAVA_PROJEKT foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE) ) go */ insert POZ_PREDST_PROJ values( 1, 'Violinista na krovu', 4); insert POZ_PREDST_PROJ values( 1, 'Violinista na krovu', 5); insert POZ_PREDST_PROJ values(2,'Indigo', 1); insert POZ_PREDST_PROJ values(2,'Indigo', 2); insert POZ_PREDST_PROJ values(2,'Indigo', 3); go /* 12 - Punjenje tabele PREDSTAVA_NA_REPERTOARU */ /* create table PREDSTAVA_NA_REPERTOARU ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA int not null, SIF_PROSTORIJE int not null, POCETAK_PREDSTAVE datetime not null, constraint PK_PREDSTAVA_NA_REPERTOARU primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE), constraint FK_PREDSTAVA_PROJEKT1 foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE), constraint FK_PROSTORIJA foreign key (SIF_POZORISTA, SIF_PROSTORIJE) references PROSTORIJA (SIF_POZORISTA, SIF_PROSTORIJE) ) go */ insert PREDSTAVA_NA_REPERTOARU values( 1, 'Violinista na krovu', 1, 1, '2015.06.13 19:30'); insert PREDSTAVA_NA_REPERTOARU values( 1, 'Violinista na krovu', 1, 1, '2015.11.26 19:30'); insert PREDSTAVA_NA_REPERTOARU values(2,'Indigo',3,1,'9/23/2012 20:30'); insert PREDSTAVA_NA_REPERTOARU values(2,'Indigo',3,1,'9/25/2012 20:00'); go /* 13 - Punjenje tabele PREDST_PROJ_ULOGA */ /* create table PREDST_PROJ_ULOGA ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_ULOGE int not null foreign key references ULOGA (SIF_ULOGE), constraint PK_PREDST_PROJ_ULOGA primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE), constraint FK_PREDSTAVA_PROJEKT2 foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE) ) go */ insert PREDST_PROJ_ULOGA values( 1, 'Violinista na krovu', 1); insert PREDST_PROJ_ULOGA values( 1, 'Violinista na krovu', 5); insert PREDST_PROJ_ULOGA values( 1, 'Violinista na krovu', 6); insert PREDST_PROJ_ULOGA values( 1, 'Violinista na krovu', 7); insert PREDST_PROJ_ULOGA values(2,'Indigo',1); insert PREDST_PROJ_ULOGA values(2,'Indigo',2); insert PREDST_PROJ_ULOGA values(2,'Indigo',3); insert PREDST_PROJ_ULOGA values(2,'Indigo',4); go /* 14 - Punjenje tabele LICE_U_ULOZI_PREDST_PROJ */ /* create table LICE_U_ULOZI_PREDST_PROJ ( SIF_LICA int not null foreign key references LICE (SIF_LICA), SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_ULOGE int not null, constraint PK_LICE_U_ULOZI_PREDST_PROJ primary key nonclustered (SIF_LICA, SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE), constraint FK_PREDST_PROJ_ULOGA_ foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) references PREDST_PROJ_ULOGA (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) ) go */ insert LICE_U_ULOZI_PREDST_PROJ values( 5, 1, 'Violinista na krovu', 1); insert LICE_U_ULOZI_PREDST_PROJ values( 7, 1, 'Violinista na krovu', 5); insert LICE_U_ULOZI_PREDST_PROJ values( 8, 1, 'Violinista na krovu', 6); insert LICE_U_ULOZI_PREDST_PROJ values( 9, 1, 'Violinista na krovu', 7); insert LICE_U_ULOZI_PREDST_PROJ values(2,2,'Indigo',1); insert LICE_U_ULOZI_PREDST_PROJ values(2,2,'Indigo',2); insert LICE_U_ULOZI_PREDST_PROJ values(1,2,'Indigo',3); insert LICE_U_ULOZI_PREDST_PROJ values(3,2,'Indigo',4); go /* 15 - Punjenje tabele UCESTVUJE_U_PREDSTAVI */ /* create table UCESTVUJE_U_PREDSTAVI ( SIF_POZ_DELA int not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA int not null, SIF_PROSTORIJE int not null, POCETAK_PREDSTAVE datetime not null, SIF_LICA int not null, SIF_ULOGE int not null, constraint PK_UCESTVUJE_U_PREDSTAVI primary key nonclustered (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE, SIF_LICA, SIF_ULOGE), constraint FK_LICE_U_ULOZI_PREDST_PROJ foreign key (SIF_LICA, SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) references LICE_U_ULOZI_PREDST_PROJ (SIF_LICA, SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE), constraint FK_PREDSTAVA_NA_REPERTOARU foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE) references PREDSTAVA_NA_REPERTOARU (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_POZORISTA, SIF_PROSTORIJE, POCETAK_PREDSTAVE) ) go */ insert UCESTVUJE_U_PREDSTAVI values( 1, 'Violinista na krovu', 1, 1, '2015.06.13 19:30', 7, 5); insert UCESTVUJE_U_PREDSTAVI values( 1, 'Violinista na krovu', 1, 1, '2015.06.13 19:30', 8, 6); insert UCESTVUJE_U_PREDSTAVI values( 1, 'Violinista na krovu', 1, 1, '2015.06.13 19:30', 9, 7); insert UCESTVUJE_U_PREDSTAVI values( 1, 'Violinista na krovu', 1, 1, '2015.11.26 19:30', 7, 5); insert UCESTVUJE_U_PREDSTAVI values( 1, 'Violinista na krovu', 1, 1, '2015.11.26 19:30', 8, 6); insert UCESTVUJE_U_PREDSTAVI values( 1, 'Violinista na krovu', 1, 1, '2015.11.26 19:30', 9, 7); insert UCESTVUJE_U_PREDSTAVI values(2,'Indigo',3,1,'9/23/2012 20:30',2,2); insert UCESTVUJE_U_PREDSTAVI values(2,'Indigo',3,1,'9/23/2012 20:30',1,3); insert UCESTVUJE_U_PREDSTAVI values(2,'Indigo',3,1,'9/23/2012 20:30',3,4); insert UCESTVUJE_U_PREDSTAVI values(2,'Indigo',3,1,'9/25/2012 20:00',2,2); insert UCESTVUJE_U_PREDSTAVI values(2,'Indigo',3,1,'9/25/2012 20:00',1,3); insert UCESTVUJE_U_PREDSTAVI values(2,'Indigo',3,1,'9/25/2012 20:00',3,4); go /* Vezba dana 09.12.2015. - grupa 3 */ -- Izbrojati pozorista / Koliko ima pozorista u evidenciji? select COUNT(SIF_POZORISTA) from dbo.POZORISTE; go -- Koliko ima pozorista po mestima u evidenciji? select SIF_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE group by SIF_MESTA; go -- Koliko ima pozorista po nazivima mesta u evidenciji? select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p, dbo.MESTO m where p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA; go -- Koliko ima pozorista po nazivima mesta u evidenciji? -- Urediti tabelu rezultata po opadajucem ABC-dnom redosledu select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p, dbo.MESTO m where p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA order by NAZIV_MESTA desc; go -- Koliko ima pozorista po nazivima mesta u evidenciji? -- Urediti tabelu broju pozorisata u rastucem redosledu select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p, dbo.MESTO m where p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA order by COUNT(SIF_POZORISTA); go -- Koliko ima pozorista po nazivima mesta u evidenciji? -- Urediti tabelu broju pozorisata u opadajucem redosledu select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p, dbo.MESTO m where p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA order by COUNT(SIF_POZORISTA) desc; go -- Koliko ima pozorista po nazivima mesta u evidenciji? -- Urediti tabelu broju pozorista u opadajucem redosledu, -- a po obrnutom ABC-dnmom redosledu kao druga osnova uredjivanja select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p, dbo.MESTO m where p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA order by COUNT(SIF_POZORISTA) desc, NAZIV_MESTA desc; go select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p join dbo.MESTO m on p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA order by COUNT(SIF_POZORISTA) desc, NAZIV_MESTA desc; go -- Koliko ima pozorista po nazivima mesta u evidenciji? -- Zadrzati samo one redove u tabeli rezultata u kojima je taj broj -- veci od 1 select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p join dbo.MESTO m on p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA having COUNT(SIF_POZORISTA)>1; go -- sledece resenje ne radi zbog nepostojanja vrednosti agregacione funkcije select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p join dbo.MESTO m on p.SIF_MESTA=m.SIF_MESTA where COUNT(SIF_POZORISTA)>1 group by NAZIV_MESTA; go -- Koliko ima pozorista po nazivima mesta u evidenciji? -- Zadrzati samo red koji sadzi Beograd/zadrzati samo pozorista -- iz Beograda select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p join dbo.MESTO m on p.SIF_MESTA=m.SIF_MESTA group by NAZIV_MESTA having NAZIV_MESTA='Beograd'; go select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p join dbo.MESTO m on p.SIF_MESTA=m.SIF_MESTA where NAZIV_MESTA='Beograd' group by NAZIV_MESTA; go -- Koliko ima amaterskih pozorista po Beogradu (join)? select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p join dbo.MESTO m on p.SIF_MESTA=m.SIF_MESTA where NAZIV_MESTA='Beograd' and P_AMATERSKO='true' group by NAZIV_MESTA; go -- Koliko ima amaterskih pozorista po Beogradu (Dekartov proizvod)? select NAZIV_MESTA, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE p, dbo.MESTO m where p.SIF_MESTA=m.SIF_MESTA and NAZIV_MESTA='Beograd' and P_AMATERSKO='true' group by NAZIV_MESTA; go -- Listati nazive pozorista iz mesta sa najmanjim postanskim brojem select NAZIV_POZORISTA from dbo.POZORISTE where P_POSTANSKI_BROJ=(select MIN(P_POSTANSKI_BROJ) from dbo.POZORISTE); go select NAZIV_POZORISTA, POST_BROJ from dbo.POZORISTE p join dbo.MESTO m on m.SIF_MESTA=p.SIF_MESTA where POST_BROJ=(select MIN(POST_BROJ) from dbo.POZORISTE p join dbo.MESTO m on m.SIF_MESTA=p.SIF_MESTA); go select NAZIV_POZORISTA, POST_BROJ from dbo.POZORISTE p join dbo.MESTO m on m.SIF_MESTA=p.SIF_MESTA where POST_BROJ=(select MIN(POST_BROJ) from dbo.MESTO); go /* 16-12-2015 */ use PPortal; go /* Koliko ima amaterskih a koliko profesionalnih pozorista po evidenciji? */ select P_AMATERSKO, COUNT(SIF_POZORISTA) from dbo.POZORISTE group by P_AMATERSKO; go /* Koliko imao vojvodjanskih profesionalnih pozorista iz Novog Sada po evidenciji? */ select COUNT(SIF_POZORISTA) from dbo.POZORISTE where P_AMATERSKO='false' and P_IZ_VOJVODINE=1 and P_MESTO='Novi Sad'; go select P_MESTO, COUNT(SIF_POZORISTA) 'Broj pozorista' from dbo.POZORISTE where P_AMATERSKO='false' group by P_MESTO having P_MESTO='Novi Sad'; go -- Listati imena i prezimena lica iz Srbije select L_IME, L_PREZIME from dbo.LICE l, dbo.ZEMLJA z, dbo.MESTO m where l.SIF_MESTA=m.SIF_MESTA and m.SIF_ZEMLJE=z.SIF_ZEMLJE and DRZAVA='Republika Srbija'; go select L_IME, L_PREZIME from dbo.LICE l join dbo.MESTO m on l.SIF_MESTA=m.SIF_MESTA join dbo.ZEMLJA z on m.SIF_ZEMLJE=z.SIF_ZEMLJE where DRZAVA='Republika Srbija'; go -- Listati imena i prezimena lica iz Srbije -- resenje pomocu ugradjenog SELECT-a select L_IME, L_PREZIME from dbo.LICE where SIF_MESTA in (select SIF_MESTA from dbo.MESTO where SIF_ZEMLJE=(select SIF_ZEMLJE from dbo.ZEMLJA where DRZAVA='Republika Srbija')); go -- Listati imena i prezimena lica kao i naziv mesta u kojem stanuju -- iz Srbije -- resenje pomocu ugradjenog SELECT-a select L_IME, L_PREZIME, NAZIV_MESTA from dbo.LICE l join dbo.MESTO m on m.SIF_MESTA=l.SIF_MESTA where SIF_ZEMLJE=(select SIF_ZEMLJE from dbo.ZEMLJA where DRZAVA='Republika Srbija'); go -- Listati imena, prezimena lica kao i naziv mesta -- u kojem stanuju kao i oznake drzave -- iz Srbije select L_IME, L_PREZIME, NAZIV_MESTA, OZNAKA_DRZAVE from dbo.LICE l join dbo.MESTO m on l.SIF_MESTA=m.SIF_MESTA join dbo.ZEMLJA z on m.SIF_ZEMLJE=z.SIF_ZEMLJE where DRZAVA='Republika Srbija'; go -- Listati lica iz Novog Sada i Beograda koja su aktivna, -- bave se profesionalno pozoristem a duzina biografije prevazilazi -- 5 znakova select L_IME, L_PREZIME from dbo.LICE where SIF_MESTA in (select SIF_MESTA from dbo.MESTO where NAZIV_MESTA='Beograd' or NAZIV_MESTA='Novi Sad') and L_AMATER='false' and LEN(L_BIOGRAFIJA)>5; go select L_IME, L_PREZIME from dbo.LICE where SIF_MESTA in (select SIF_MESTA from dbo.MESTO where NAZIV_MESTA in ('Beograd', 'Novi Sad')) and L_AMATER='false' and LEN(L_BIOGRAFIJA)>5; go select L_IME, L_PREZIME from dbo.LICE l join dbo.MESTO m on m.SIF_MESTA=l.SIF_MESTA where (NAZIV_MESTA='Beograd' or NAZIV_MESTA='Novi Sad') and L_AMATER='false' and LEN(L_BIOGRAFIJA)>5; go -- Listati lica iz Novog Sada cije ime pocinje slovom 'A', koja su aktivna select L_IME, L_PREZIME from dbo.LICE l join dbo.MESTO m on m.SIF_MESTA=l.SIF_MESTA where NAZIV_MESTA='Novi Sad' and substring(L_IME,1,1)='A'; go select L_IME, L_PREZIME from dbo.LICE l join dbo.MESTO m on m.SIF_MESTA=l.SIF_MESTA where NAZIV_MESTA='Novi Sad' and L_IME like 'A%'; go select L_IME, L_PREZIME from dbo.LICE l join dbo.MESTO m on m.SIF_MESTA=l.SIF_MESTA where NAZIV_MESTA='Novi Sad' and L_IME >= 'A%' and L_IME <= 'B%'; go /* Prikazite nazive mesta iz VOJVODJANSKOG okruga (post.broj izmedju 21000 i 25000 */ select NAZIV_MESTA, POST_BROJ from dbo.MESTO where POST_BROJ>=21000 and POST_BROJ<=25000; go select NAZIV_MESTA, POST_BROJ from dbo.MESTO where POST_BROJ between 21000 and 23657; go go select * from MESTO; go select * from LICE; go /* 23.12.2015. */ use PPortal; go /*Prikazati trenutni datum i trenutno vreme */ SELECT substring(cast(CURRENT_TIMESTAMP as character), 1, 11) AS "Date", substring(cast(CURRENT_TIMESTAMP as character), 12,8) AS "Time", CURRENT_TIMESTAMP AS "Timestamp" go SELECT substring(cast(GETDATE() as character), 1, 11) AS "Date", substring(cast(GETDATE() as character), 12,8) AS "Time", GETDATE() AS "GetDate"; go SELECT substring(cast(Sysdatetime() as character), 1, 11) AS "Date", substring(cast(Sysdatetime() as character), 12,8) AS "Time", Sysdatetime() AS "Sysdatetime"; go /* -- DATEADD() If you want to add or subtract an amount of time to a column or a variable, then display a new value in a rowset or set a variable with that new value, DATEADD() will do this. The syntax for DATEADD() is DATEADD(datepart, number, date) The datepart option applies to all of the date functions and details what you want to add from milliseconds to years. These are defined as reserved words and therefore are not surrounded by quotation marks. There are a number of possible values, as detailed in Table 11-1. Taking the second option of the datepart function, to add the value, make the number positive, and to subtract a number, make it negative. Moving to the final option of the datepart function, this can be either a value, a variable, or a column date type holding the date and time you wish to change. Table 11-1. Potential Values for datepart datepart Definition Meaning isowk, isoww ISOWeek is a numbering system used to give every week in the calendar a unique, ascending number. An ISO week starts on a Monday, and Week 1 is the week containing the first Thursday of that year. For example, in 2008, the first Thursday occurred on January 3, so Week 1 ran from December 31, 2007, through January 6, 2008. tz Timezone offset ns Nanosecond mcs Microsecond ms Millisecond ss, s Second mi, n Minute hh Hour dw, w Weekday wk, ww Week dd, d Day dy, y Day of year mm, n Month qq, q Quarter yy, yyyy Year */ -- Prikazati Timestamp za 50 casova select GETDATE(), DATEADD(hh, 50, getdate()) as 'Timestamp za 50 casova'; go -- Prikazati Timestamp za trenutak od pre 50 casova select GETDATE() as 'Trenutni Timestamp', DATEADD(hh, -50, getdate()) as 'Timestamp pre 50 casova'; go /* -- DATEDIFF() To find the difference between two dates, you would use the function DATEDIFF(). The syntax for this function is DATEDIFF(datepart, startdate, enddate) The first option contains the same options as for DATEADD(), and startdate and enddate are the two days you wish to compare. A negative number shows that the enddate is before the startdate. */ -- Timestamp za 50 dana, a razlika u nedeljama select GETDATE() as 'Trenutni Timestamp', DATEADD(DD, 55, getdate()) as 'Timestamp za 55 dana', DATEDIFF(WW, getdate(), DATEADD(DD, 55, getdate())) as 'Razlika u nedeljama'; go /* -- DATEPART() If you wish to achieve returning part of a date from a date variable, column, or value, you can use DATEPART() within a SELECT statement. As you may be expecting by now, the syntax has datepart as the first option, and then the datetoinspect as the second option, which returns the numerical day of the week from the date inspected. DATEPART(datepart, datetoinspect) 1. We need to set only one local variable to a date and time. After that, we find the day of the month. DECLARE @WhatsTheDay datetime SET @WhatsTheDay = '24 March 2008 3:00 PM' SELECT DATEPART(dd, @WhatsTheDay) Figure 11-24 shows the results after executing this code. Figure 11-24. Finding part of a date 2. To produce a more pleasing date and time for a statement, we can combine DATEPART() and DATENAME() to have a meaningful output. The function CAST(), which we will look at in detail shortly, is needed here, as it is a data type conversion function. DECLARE @WhatsTheDay datetime SET @WhatsTheDay = '24 March 2008 3:00 PM' SELECT DATENAME(dw, @WhatsTheDay) + ', ' + CAST(DATEPART(dd,@WhatsTheDay) AS varchar(2)) + ' ' + DATENAME(mm,@WhatsTheDay) + ' ' + CAST(DATEPART(yyyy,@WhatsTheDay) AS char(4)) 3. When this is executed, it will produce the more meaningful date shown in Figure 11-25. Figure 11-25. Finding and concatenating to provide a useful date */ -- Ispisati timestamp, koji je dan u nedelji u trenutnom datumu -- naziv dana nedelje u trenutnom datumu -- Timestamp za 50 dana, redni broj dana u nedelji u tom datumu i naziv dana select GETDATE() as 'Trenutni Timestamp', datepart(DW, GETDATE()) as 'Redni broj dana u nedelji', DATENAME(dw, GETDATE()) as 'Naziv dana', DATEADD(DD, 50, getdate()) as 'Timestamp za 50 dana', datepart(DW, DATEADD(DD, 50, getdate())) as 'Redni br. dana iz Timestampa+50', DATENAME(dw, DATEADD(DD, 50, getdate())) as 'Naziv dana za Timestampa+50', DATEDIFF(WW, getdate(), DATEADD(DD, 50, getdate())) as 'Razlika u nedeljama'; go /* -- ISDATE() Although ISDATE() is a function that works with dates and times, this system function takes a value in a column or a variable and confirms whether it contains a valid date or time. The value returned is 0, or false, for an invalid date, or 1 for true if the date is okay. The formatting of the date for testing within the ISDATE() function has to be in the same regional format as you have set with SET DATEFORMAT or SET LANGUAGE. If you are testing in a European format but have your database set to US format, then you will get a false value returned. -- 1. The first example demonstrates where a date is invalid. There are only 30 days in September. DECLARE @IsDate char(15) SET @IsDate = '31 Sep 2008' SELECT ISDATE(@IsDate) - FALSE */ select ISDATE('2001-02-28') go select GETDATE(),ISDATE(GETDATE()) go /* Koliko lica su autori i koliko puta u nasoj evidenciji? */ select SIF_LICA, COUNT(SIF_LICA) from dbo.AUTOR group by SIF_LICA; go /* Koliko lica (po imenu i prezimenu) koji su autori i koliko puta se pojavljuju u nasoj evidenciji? */ select L_PREZIME, L_IME, COUNT(a.SIF_LICA) from dbo.AUTOR a, dbo.LICE l where a.SIF_LICA=l.SIF_LICA group by L_PREZIME, L_IME; go /* Koliko lica (po imenu i prezimenu) koji su autori i koliko puta se pojavljuju u nasoj evidenciji? Tabelu rezultata urediti po prezimenu po abecedi */ select L_PREZIME, L_IME, COUNT(a.SIF_LICA) from dbo.AUTOR a, dbo.LICE l where a.SIF_LICA=l.SIF_LICA group by L_PREZIME, L_IME order by L_PREZIME; go select L_PREZIME, L_IME, COUNT(a.SIF_LICA) from dbo.AUTOR a join dbo.LICE l on a.SIF_LICA=l.SIF_LICA group by L_PREZIME, L_IME order by L_PREZIME; go /* Koliko ima pozorisnih dela po zanru (ZANR u tabeli Zanr) */ select ZANR, COUNT(SIF_POZ_DELA) from dbo.ZANR z, dbo.POZORISNO_DELO pd where z.SIF_ZANRA=pd.SIF_ZANRA group by ZANR; go select ZANR, COUNT(SIF_POZ_DELA) from dbo.ZANR z join dbo.POZORISNO_DELO pd on z.SIF_ZANRA=pd.SIF_ZANRA group by ZANR; go /* Koja su ona lica (po imenu i prezimenu) koja nisu autori ni jednog pozorisnog dela? */ -- 1. resenje podSELECT - unutrasnji SELECT /* Naredno resenje ne daje dobar rezultat - verovatno zbog postojanja null vrednosti za SIF_LICA_PREVOD */ select L_IME, L_PREZIME from dbo.LICE where SIF_LICA not in (select SIF_LICA_PREVOD from dbo.POZORISNO_DELO); go /* Sledece resenje daje dobar rezultat jer su izuzeti redovi u kojima polje SIF_LICA_PREVOD ima null vrednosti */ select L_IME, L_PREZIME from dbo.LICE where SIF_LICA not in (select SIF_LICA_PREVOD from dbo.POZORISNO_DELO where SIF_LICA_PREVOD is not null); go /* Naredno resenje daje dobar rezultat jer su iz tabele Lice izabrana ona lica koja su autori (u unutrasnjem SELECT-u), a onda njih izuzimamo od ostalih lica uz tabele Lice */ select L_IME, L_PREZIME from dbo.LICE where SIF_LICA not in (select SIF_LICA from dbo.LICE l join dbo.POZORISNO_DELO pd on pd.SIF_LICA_PREVOD=l.SIF_LICA); go select SIF_LICA_PREVOD from dbo.POZORISNO_DELO; go select L_IME, L_PREZIME from dbo.LICE; go -- 2. resenje OUTER JOIN -- OUTER JOIN uvek daje dobro resenje select L_IME, L_PREZIME from dbo.LICE l left join dbo.POZORISNO_DELO pd on pd.SIF_LICA_PREVOD=l.SIF_LICA where NASLOV_POZ_DELA is null; go select L_IME, L_PREZIME from dbo.LICE l left join dbo.POZORISNO_DELO pd on pd.SIF_LICA_PREVOD=l.SIF_LICA where SIF_LICA_PREVOD is null; go