/*==============================================================*/ /* DBMS name: Microsoft SQL Server 2008 */ /* Created on: 11/19/2014 9:47:13 PM */ /*==============================================================*/ /* Ciscenja generisana od strane PowerDesigner -a nisu potrebna if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('AUTOR') and o.name = 'FK_AUTOR_AUTOR_P_D_POZORISN') alter table AUTOR drop constraint FK_AUTOR_AUTOR_P_D_POZORISN go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('AUTOR') and o.name = 'FK_AUTOR_SZERZO2_LICE') alter table AUTOR drop constraint FK_AUTOR_SZERZO2_LICE go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('LICE') and o.name = 'FK_LICE_BORAVI_MESTO') alter table LICE drop constraint FK_LICE_BORAVI_MESTO go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('LICE_U_ULOZI_PREDST_PROJ') and o.name = 'FK_LICE_U_U_LICE_ULOG_LICE') alter table LICE_U_ULOZI_PREDST_PROJ drop constraint FK_LICE_U_U_LICE_ULOG_LICE go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('LICE_U_ULOZI_PREDST_PROJ') and o.name = 'FK_LICE_U_U_P_P_ULOGA_PREDST_P') alter table LICE_U_ULOZI_PREDST_PROJ drop constraint FK_LICE_U_U_P_P_ULOGA_PREDST_P go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('MESTO') and o.name = 'FK_MESTO_PRIPADA_ZEMLJA') alter table MESTO drop constraint FK_MESTO_PRIPADA_ZEMLJA go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('POZORISNO_DELO') and o.name = 'FK_POZORISN_PREVODILA_LICE') alter table POZORISNO_DELO drop constraint FK_POZORISN_PREVODILA_LICE go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('POZORISNO_DELO') and o.name = 'FK_POZORISN_ZANR_POZ__ZANR') alter table POZORISNO_DELO drop constraint FK_POZORISN_ZANR_POZ__ZANR go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('POZORISTE') and o.name = 'FK_POZORIST_RADI_MESTO') alter table POZORISTE drop constraint FK_POZORIST_RADI_MESTO go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('POZ_PREDST_PROJ') and o.name = 'FK_POZ_PRED_POZ_PREDS_POZORIST') alter table POZ_PREDST_PROJ drop constraint FK_POZ_PRED_POZ_PREDS_POZORIST go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('POZ_PREDST_PROJ') and o.name = 'FK_POZ_PRED_SZ_H_E_P2_PREDSTAV') alter table POZ_PREDST_PROJ drop constraint FK_POZ_PRED_SZ_H_E_P2_PREDSTAV go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('PREDSTAVA_NA_REPERTOARU') and o.name = 'FK_PREDSTAV_PREDSTAVA_PREDSTAV') alter table PREDSTAVA_NA_REPERTOARU drop constraint FK_PREDSTAV_PREDSTAVA_PREDSTAV go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('PREDSTAVA_NA_REPERTOARU') and o.name = 'FK_PREDSTAV_PREDSTAVA_PROSTORI') alter table PREDSTAVA_NA_REPERTOARU drop constraint FK_PREDSTAV_PREDSTAVA_PROSTORI go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('PREDSTAVA_PROJEKT') and o.name = 'FK_PREDSTAV_P_D_PREDS_POZORISN') alter table PREDSTAVA_PROJEKT drop constraint FK_PREDSTAV_P_D_PREDS_POZORISN go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('PREDST_PROJ_ULOGA') and o.name = 'FK_PREDST_P_E_P_SZERE_PREDSTAV') alter table PREDST_PROJ_ULOGA drop constraint FK_PREDST_P_E_P_SZERE_PREDSTAV go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('PREDST_PROJ_ULOGA') and o.name = 'FK_PREDST_P_PREDST_PR_ULOGA') alter table PREDST_PROJ_ULOGA drop constraint FK_PREDST_P_PREDST_PR_ULOGA go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('PROSTORIJA') and o.name = 'FK_PROSTORI_PROSTORIJ_POZORIST') alter table PROSTORIJA drop constraint FK_PROSTORI_PROSTORIJ_POZORIST go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('UCESTVUJE_U_PREDSTAVI') and o.name = 'FK_UCESTVUJ_LICE_UCES_LICE_U_U') alter table UCESTVUJE_U_PREDSTAVI drop constraint FK_UCESTVUJ_LICE_UCES_LICE_U_U go if exists (select 1 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') where r.fkeyid = object_id('UCESTVUJE_U_PREDSTAVI') and o.name = 'FK_UCESTVUJ_UCESTVUJE_PREDSTAV') alter table UCESTVUJE_U_PREDSTAVI drop constraint FK_UCESTVUJ_UCESTVUJE_PREDSTAV go if exists (select 1 from sysindexes where id = object_id('AUTOR') and name = 'AUTOR_P_D_FK' and indid > 0 and indid < 255) drop index AUTOR.AUTOR_P_D_FK go if exists (select 1 from sysindexes where id = object_id('AUTOR') and name = 'SZERZO2_FK' and indid > 0 and indid < 255) drop index AUTOR.SZERZO2_FK go if exists (select 1 from sysobjects where id = object_id('AUTOR') and type = 'U') drop table AUTOR go if exists (select 1 from sysindexes where id = object_id('LICE') and name = 'BORAVI_FK' and indid > 0 and indid < 255) drop index LICE.BORAVI_FK go if exists (select 1 from sysobjects where id = object_id('LICE') and type = 'U') drop table LICE go if exists (select 1 from sysindexes where id = object_id('LICE_U_ULOZI_PREDST_PROJ') and name = 'P_P_ULOGA_LICE_U_P_P_ULOZI_FK' and indid > 0 and indid < 255) drop index LICE_U_ULOZI_PREDST_PROJ.P_P_ULOGA_LICE_U_P_P_ULOZI_FK go if exists (select 1 from sysindexes where id = object_id('LICE_U_ULOZI_PREDST_PROJ') and name = 'LICE_ULOGA_P_P_FK' and indid > 0 and indid < 255) drop index LICE_U_ULOZI_PREDST_PROJ.LICE_ULOGA_P_P_FK go if exists (select 1 from sysobjects where id = object_id('LICE_U_ULOZI_PREDST_PROJ') and type = 'U') drop table LICE_U_ULOZI_PREDST_PROJ go if exists (select 1 from sysindexes where id = object_id('MESTO') and name = 'PRIPADA_FK' and indid > 0 and indid < 255) drop index MESTO.PRIPADA_FK go if exists (select 1 from sysobjects where id = object_id('MESTO') and type = 'U') drop table MESTO go if exists (select 1 from sysindexes where id = object_id('POZORISNO_DELO') and name = 'PREVODILAC_FK' and indid > 0 and indid < 255) drop index POZORISNO_DELO.PREVODILAC_FK go if exists (select 1 from sysindexes where id = object_id('POZORISNO_DELO') and name = 'ZANR_POZ_DELA_FK' and indid > 0 and indid < 255) drop index POZORISNO_DELO.ZANR_POZ_DELA_FK go if exists (select 1 from sysobjects where id = object_id('POZORISNO_DELO') and type = 'U') drop table POZORISNO_DELO go if exists (select 1 from sysindexes where id = object_id('POZORISTE') and name = 'RADI_FK' and indid > 0 and indid < 255) drop index POZORISTE.RADI_FK go if exists (select 1 from sysobjects where id = object_id('POZORISTE') and type = 'U') drop table POZORISTE go if exists (select 1 from sysindexes where id = object_id('POZ_PREDST_PROJ') and name = 'POZ_PREDST_PROJ_FK' and indid > 0 and indid < 255) drop index POZ_PREDST_PROJ.POZ_PREDST_PROJ_FK go if exists (select 1 from sysindexes where id = object_id('POZ_PREDST_PROJ') and name = 'SZ_H_E_P2_FK' and indid > 0 and indid < 255) drop index POZ_PREDST_PROJ.SZ_H_E_P2_FK go if exists (select 1 from sysobjects where id = object_id('POZ_PREDST_PROJ') and type = 'U') drop table POZ_PREDST_PROJ go if exists (select 1 from sysindexes where id = object_id('PREDSTAVA_NA_REPERTOARU') and name = 'PREDSTAVA_PROJEKT_NA_REPERTOARU_FK' and indid > 0 and indid < 255) drop index PREDSTAVA_NA_REPERTOARU.PREDSTAVA_PROJEKT_NA_REPERTOARU_FK go if exists (select 1 from sysindexes where id = object_id('PREDSTAVA_NA_REPERTOARU') and name = 'PREDSTAVA_U_PROSTORIJI_FK' and indid > 0 and indid < 255) drop index PREDSTAVA_NA_REPERTOARU.PREDSTAVA_U_PROSTORIJI_FK go if exists (select 1 from sysobjects where id = object_id('PREDSTAVA_NA_REPERTOARU') and type = 'U') drop table PREDSTAVA_NA_REPERTOARU go if exists (select 1 from sysindexes where id = object_id('PREDSTAVA_PROJEKT') and name = 'P_D_PREDST_PROJ_FK' and indid > 0 and indid < 255) drop index PREDSTAVA_PROJEKT.P_D_PREDST_PROJ_FK go if exists (select 1 from sysobjects where id = object_id('PREDSTAVA_PROJEKT') and type = 'U') drop table PREDSTAVA_PROJEKT go if exists (select 1 from sysindexes where id = object_id('PREDST_PROJ_ULOGA') and name = 'PREDST_PROJ_ULOGA_FK' and indid > 0 and indid < 255) drop index PREDST_PROJ_ULOGA.PREDST_PROJ_ULOGA_FK go if exists (select 1 from sysindexes where id = object_id('PREDST_PROJ_ULOGA') and name = 'E_P_SZEREPKOR2_FK' and indid > 0 and indid < 255) drop index PREDST_PROJ_ULOGA.E_P_SZEREPKOR2_FK go if exists (select 1 from sysobjects where id = object_id('PREDST_PROJ_ULOGA') and type = 'U') drop table PREDST_PROJ_ULOGA go if exists (select 1 from sysindexes where id = object_id('PROSTORIJA') and name = 'PROSTORIJA_POZORISTA_FK' and indid > 0 and indid < 255) drop index PROSTORIJA.PROSTORIJA_POZORISTA_FK go if exists (select 1 from sysobjects where id = object_id('PROSTORIJA') and type = 'U') drop table PROSTORIJA go if exists (select 1 from sysindexes where id = object_id('UCESTVUJE_U_PREDSTAVI') and name = 'LICE_UCESTVUJE_FK' and indid > 0 and indid < 255) drop index UCESTVUJE_U_PREDSTAVI.LICE_UCESTVUJE_FK go if exists (select 1 from sysindexes where id = object_id('UCESTVUJE_U_PREDSTAVI') and name = 'UCESTVUJE_U_PREDSTAVI_FK' and indid > 0 and indid < 255) drop index UCESTVUJE_U_PREDSTAVI.UCESTVUJE_U_PREDSTAVI_FK go if exists (select 1 from sysobjects where id = object_id('UCESTVUJE_U_PREDSTAVI') and type = 'U') drop table UCESTVUJE_U_PREDSTAVI go if exists (select 1 from sysobjects where id = object_id('ULOGA') and type = 'U') drop table ULOGA go if exists (select 1 from sysobjects where id = object_id('ZANR') and type = 'U') drop table ZANR go if exists (select 1 from sysobjects where id = object_id('ZEMLJA') and type = 'U') drop table ZEMLJA go Ciscenja generisana od strane PowerDesigner -a nisu potrebna - kraj */ -- POCETAK -- POCETAK -- POCETAK create database PozorisniPortal; go use PozorisniPortal; go /*==============================================================*/ /* Table: ZEMLJA */ /*==============================================================*/ create table ZEMLJA ( SIF_ZEMLJE numeric identity, 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 numeric identity, SIF_ZEMLJE numeric 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 /* spoljni kljuc fk_zemlja je unesen u tabelu mesto alter table MESTO add constraint FK_MESTO_PRIPADA_ZEMLJA foreign key (SIF_ZEMLJE) references ZEMLJA (SIF_ZEMLJE) go spoljni kljuc fk_zemlja je unesen u tabelu mesto - kraj */ /*==============================================================*/ /* Table: ZANR */ /*==============================================================*/ create table ZANR ( SIF_ZANRA numeric identity, ZANR varchar(50) not null, constraint PK_ZANR primary key nonclustered (SIF_ZANRA) ) go /*==============================================================*/ /* Table: ULOGA */ /*==============================================================*/ create table ULOGA ( SIF_ULOGE numeric identity, ULOGA varchar(30) null, constraint PK_ULOGA primary key nonclustered (SIF_ULOGE) ) go /*==============================================================*/ /* Table: POZORISTE */ /*==============================================================*/ create table POZORISTE ( SIF_POZORISTA numeric identity, SIF_MESTA numeric 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 /* spoljni kljuc fk_mesto je unesen u tabelu pozoriste alter table POZORISTE add constraint FK_POZORIST_RADI_MESTO foreign key (SIF_MESTA) references MESTO (SIF_MESTA) go spoljni kljuc fk_mesto je unesen u tabelu pozoriste - kraj */ /*==============================================================*/ /* Table: LICE */ /*==============================================================*/ create table LICE ( SIF_LICA numeric identity, SIF_MESTA numeric 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 /* spoljni kljuc fk_mesto je unesen u tabelu lice alter table LICE add constraint FK_LICE_BORAVI_MESTO foreign key (SIF_MESTA) references MESTO (SIF_MESTA) go spoljni kljuc fk_mesto je unesen u tabelu pozoriste - kraj */ /*==============================================================*/ /* Table: PROSTORIJA */ /*==============================================================*/ create table PROSTORIJA ( SIF_POZORISTA numeric not null foreign key references POZORISTE (SIF_POZORISTA), SIF_PROSTORIJE numeric identity, NAZIV_PROSTORIJE varchar(50) not null, PR_BROJ_SEDISTA numeric(3) not null, constraint PK_PROSTORIJA primary key nonclustered (SIF_POZORISTA, SIF_PROSTORIJE) ) go /* spoljni kljuc fk_pozoriste je unesen u tabelu prostorija alter table PROSTORIJA add constraint FK_PROSTORI_PROSTORIJ_POZORIST foreign key (SIF_POZORISTA) references POZORISTE (SIF_POZORISTA) go spoljni kljuc fk_pozoriste je unesen u tabelu prostorija - kraj */ /*==============================================================*/ /* Table: POZORISNO_DELO */ /*==============================================================*/ create table POZORISNO_DELO ( SIF_POZ_DELA numeric identity, SIF_ZANRA numeric not null foreign key references ZANR (SIF_ZANRA), SIF_LICA numeric not null 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 /* spoljni kljuc fk_lice i fk_zanr su uneseni u tabelu pozorisno_delo alter table POZORISNO_DELO add constraint FK_POZORISN_PREVODILA_LICE foreign key (SIF_LICA) references LICE (SIF_LICA) go alter table POZORISNO_DELO add constraint FK_POZORISN_ZANR_POZ__ZANR foreign key (SIF_ZANRA) references ZANR (SIF_ZANRA) go spoljni kljuc fk_lice i fk_zanr su uneseni u tabelu pozorisno_delo - kraj */ /*==============================================================*/ /* Table: AUTOR */ /*==============================================================*/ create table AUTOR ( SIF_POZ_DELA numeric not null foreign key references POZORISNO_DELO (SIF_POZ_DELA), SIF_LICA numeric not null foreign key references LICE (SIF_LICA), constraint PK_AUTOR primary key nonclustered (SIF_POZ_DELA, SIF_LICA) ) go /* spoljni kljuc fk_pozorisno+delo i fk_lice su uneseni u tabelu autor alter table AUTOR add constraint FK_AUTOR_AUTOR_P_D_POZORISN foreign key (SIF_POZ_DELA) references POZORISNO_DELO (SIF_POZ_DELA) go alter table AUTOR add constraint FK_AUTOR_SZERZO2_LICE foreign key (SIF_LICA) references LICE (SIF_LICA) go spoljni kljuc fk_pozorisno+delo i fk_lice su uneseni u tabelu autor - kraj */ /*==============================================================*/ /* Table: PREDSTAVA_PROJEKT */ /*==============================================================*/ create table PREDSTAVA_PROJEKT ( SIF_POZ_DELA numeric 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 /* spoljni kljuc fk_pozorisno_delo je unesen u tabelu predstava_projekt alter table PREDSTAVA_PROJEKT add constraint FK_PREDSTAV_P_D_PREDS_POZORISN foreign key (SIF_POZ_DELA) references POZORISNO_DELO (SIF_POZ_DELA) go spoljni kljuc fk_pozorisno_delo je unesen u tabelu predstava_projekt - kraj */ /*==============================================================*/ /* Table: POZ_PREDST_PROJ */ /*==============================================================*/ create table POZ_PREDST_PROJ ( SIF_POZ_DELA numeric not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA numeric 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 /* spoljni kljuc fk_pozorisTE i slozeni spoljni kljuc fk_predstava_projekt su uneseni u tabelu poz_predst_proj alter table POZ_PREDST_PROJ add constraint FK_POZ_PRED_POZ_PREDS_POZORIST foreign key (SIF_POZORISTA) references POZORISTE (SIF_POZORISTA) go alter table POZ_PREDST_PROJ add constraint FK_POZ_PRED_SZ_H_E_P2_PREDSTAV foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE) go spoljni kljuc fk_pozorisTE i slozeni spoljni kljuc fk_predstava_projekt su uneseni u tabelu poz_predst_proj - kraj */ /*==============================================================*/ /* Table: PREDSTAVA_NA_REPERTOARU */ /*==============================================================*/ create table PREDSTAVA_NA_REPERTOARU ( SIF_POZ_DELA numeric not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA numeric not null, SIF_PROSTORIJE numeric 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 /* slozeni spoljni kljucevi fk_predstava_projekt i fk_postorija su uneseni u tabelu predstava_na_repertoaru alter table PREDSTAVA_NA_REPERTOARU add constraint FK_PREDSTAV_PREDSTAVA_PREDSTAV foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE) go alter table PREDSTAVA_NA_REPERTOARU add constraint FK_PREDSTAV_PREDSTAVA_PROSTORI foreign key (SIF_POZORISTA, SIF_PROSTORIJE) references PROSTORIJA (SIF_POZORISTA, SIF_PROSTORIJE) go slozeni spoljni kljucevi fk_predstava_projekt i fk_postorija su uneseni u tabelu predstava_na_repertoaru - kraj */ /*==============================================================*/ /* Table: PREDST_PROJ_ULOGA */ /*==============================================================*/ create table PREDST_PROJ_ULOGA ( SIF_POZ_DELA numeric not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_ULOGE numeric 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 /* slozeni spoljni kljuc fk_predstava_projekt i prost spoljni kljuc fk_sif_uloge su uneseni u tabelu predstava_proj_uloga alter table PREDST_PROJ_ULOGA add constraint FK_PREDST_P_E_P_SZERE_PREDSTAV foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE) references PREDSTAVA_PROJEKT (SIF_POZ_DELA, NASLOV_PREDSTAVE) go alter table PREDST_PROJ_ULOGA add constraint FK_PREDST_P_PREDST_PR_ULOGA foreign key (SIF_ULOGE) references ULOGA (SIF_ULOGE) go slozeni spoljni kljuc fk_predstava_projekt i prost spoljni kljuc fk_sif_uloge su uneseni u tabelu predstava_proj_uloga - kraj */ /*==============================================================*/ /* Table: LICE_U_ULOZI_PREDST_PROJ */ /*==============================================================*/ create table LICE_U_ULOZI_PREDST_PROJ ( SIF_LICA numeric not null foreign key references LICE (SIF_LICA), SIF_POZ_DELA numeric not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_ULOGE numeric 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 /* slozeni spoljni kljuc fk_predstava_proj_uloga i prost spoljni kljuc fk_sif_lica su uneseni u tabelu lice_u_ulozi_predst_proj alter table LICE_U_ULOZI_PREDST_PROJ add constraint FK_LICE_U_U_LICE_ULOG_LICE foreign key (SIF_LICA) references LICE (SIF_LICA) go alter table LICE_U_ULOZI_PREDST_PROJ add constraint FK_LICE_U_U_P_P_ULOGA_PREDST_P foreign key (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) references PREDST_PROJ_ULOGA (SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) go slozeni spoljni kljuc fk_predstava_proj_uloga i prost spoljni kljuc fk_sif_lica su uneseni u tabelu lice_u_ulozi_predst_proj - kraj */ /*==============================================================*/ /* Table: UCESTVUJE_U_PREDSTAVI */ /*==============================================================*/ create table UCESTVUJE_U_PREDSTAVI ( SIF_POZ_DELA numeric not null, NASLOV_PREDSTAVE varchar(50) not null, SIF_POZORISTA numeric not null, SIF_PROSTORIJE numeric not null, POCETAK_PREDSTAVE datetime not null, SIF_LICA numeric not null, SIF_ULOGE numeric 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 /* slozeni spoljni kljucevi fk_lice_u_ulozi_predst_proj i fk_predstava_na_repertoaru su uneseni u tabelu ucestvuje_u_predstavi alter table UCESTVUJE_U_PREDSTAVI add constraint FK_UCESTVUJ_LICE_UCES_LICE_U_U foreign key (SIF_LICA, LIC_SIF_POZ_DELA, LIC_NASLOV_PREDSTAVE, SIF_ULOGE) references LICE_U_ULOZI_PREDST_PROJ (SIF_LICA, SIF_POZ_DELA, NASLOV_PREDSTAVE, SIF_ULOGE) go alter table UCESTVUJE_U_PREDSTAVI add constraint FK_UCESTVUJ_UCESTVUJE_PREDSTAV 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 slozeni spoljni kljucevi fk_lice_u_ulozi_predst_proj i fk_predstava_na_repertoaru su uneseni u tabelu ucestvuje_u_predstavi - kraj */ -- KRAJ KRAJ KRAJ KRAJ -- KRAJ KRAJ KRAJ KRAJ -- KRAJ KRAJ KRAJ KRAJ /* Generisanje indeksa nece biti odradjeno - pocetak /*==============================================================*/ /* Index: SZERZO2_FK */ /*==============================================================*/ create index SZERZO2_FK on AUTOR ( SIF_LICA ASC ) go /*==============================================================*/ /* Index: AUTOR_P_D_FK */ /*==============================================================*/ create index AUTOR_P_D_FK on AUTOR ( SIF_POZ_DELA ASC ) go /*==============================================================*/ /* Index: BORAVI_FK */ /*==============================================================*/ create index BORAVI_FK on LICE ( SIF_MESTA ASC ) go /*==============================================================*/ /* Index: LICE_ULOGA_P_P_FK */ /*==============================================================*/ create index LICE_ULOGA_P_P_FK on LICE_U_ULOZI_PREDST_PROJ ( SIF_LICA ASC ) go /*==============================================================*/ /* Index: P_P_ULOGA_LICE_U_P_P_ULOZI_FK */ /*==============================================================*/ create index P_P_ULOGA_LICE_U_P_P_ULOZI_FK on LICE_U_ULOZI_PREDST_PROJ ( SIF_POZ_DELA ASC, NASLOV_PREDSTAVE ASC, SIF_ULOGE ASC ) go /*==============================================================*/ /* Index: PRIPADA_FK */ /*==============================================================*/ create index PRIPADA_FK on MESTO ( SIF_ZEMLJE ASC ) go /*==============================================================*/ /* Index: ZANR_POZ_DELA_FK */ /*==============================================================*/ create index ZANR_POZ_DELA_FK on POZORISNO_DELO ( SIF_ZANRA ASC ) go /*==============================================================*/ /* Index: PREVODILAC_FK */ /*==============================================================*/ create index PREVODILAC_FK on POZORISNO_DELO ( SIF_LICA ASC ) go /*==============================================================*/ /* Index: RADI_FK */ /*==============================================================*/ create index RADI_FK on POZORISTE ( SIF_MESTA ASC ) go /*==============================================================*/ /* Index: SZ_H_E_P2_FK */ /*==============================================================*/ create index SZ_H_E_P2_FK on POZ_PREDST_PROJ ( SIF_POZ_DELA ASC, NASLOV_PREDSTAVE ASC ) go /*==============================================================*/ /* Index: POZ_PREDST_PROJ_FK */ /*==============================================================*/ create index POZ_PREDST_PROJ_FK on POZ_PREDST_PROJ ( SIF_POZORISTA ASC ) go /*==============================================================*/ /* Index: PREDSTAVA_U_PROSTORIJI_FK */ /*==============================================================*/ create index PREDSTAVA_U_PROSTORIJI_FK on PREDSTAVA_NA_REPERTOARU ( SIF_POZORISTA ASC, SIF_PROSTORIJE ASC ) go /*==============================================================*/ /* Index: PREDSTAVA_PROJEKT_NA_REPERTOARU_FK */ /*==============================================================*/ create index PREDSTAVA_PROJEKT_NA_REPERTOARU_FK on PREDSTAVA_NA_REPERTOARU ( SIF_POZ_DELA ASC, NASLOV_PREDSTAVE ASC ) go /*==============================================================*/ /* Index: P_D_PREDST_PROJ_FK */ /*==============================================================*/ create index P_D_PREDST_PROJ_FK on PREDSTAVA_PROJEKT ( SIF_POZ_DELA ASC ) go /*==============================================================*/ /* Index: E_P_SZEREPKOR2_FK */ /*==============================================================*/ create index E_P_SZEREPKOR2_FK on PREDST_PROJ_ULOGA ( SIF_POZ_DELA ASC, NASLOV_PREDSTAVE ASC ) go /*==============================================================*/ /* Index: PREDST_PROJ_ULOGA_FK */ /*==============================================================*/ create index PREDST_PROJ_ULOGA_FK on PREDST_PROJ_ULOGA ( SIF_ULOGE ASC ) go /*==============================================================*/ /* Index: PROSTORIJA_POZORISTA_FK */ /*==============================================================*/ create index PROSTORIJA_POZORISTA_FK on PROSTORIJA ( SIF_POZORISTA ASC ) go /*==============================================================*/ /* Index: UCESTVUJE_U_PREDSTAVI_FK */ /*==============================================================*/ create index UCESTVUJE_U_PREDSTAVI_FK on UCESTVUJE_U_PREDSTAVI ( SIF_POZ_DELA ASC, NASLOV_PREDSTAVE ASC, SIF_POZORISTA ASC, SIF_PROSTORIJE ASC, POCETAK_PREDSTAVE ASC ) go /*==============================================================*/ /* Index: LICE_UCESTVUJE_FK */ /*==============================================================*/ create index LICE_UCESTVUJE_FK on UCESTVUJE_U_PREDSTAVI ( SIF_LICA ASC, LIC_SIF_POZ_DELA ASC, LIC_NASLOV_PREDSTAVE ASC, SIF_ULOGE ASC ) go Generisanje indeksa nece biti odradjeno - kraj */ /* Osma nedelja - pocetak Osma nedelja - pocetak Osma nedelja - pocetak Osma nedelja - pocetak */ use PozorisniPortal; go insert ZEMLJA values ('Srbija', 'SRB'); go select * from ZEMLJA; go insert MESTO values (1, 'Beograd', 11000); go select * from MESTO; go insert POZORISTE values(1, 'Pozoriste na Terazijama', 11000, 'Beograd', 'Terazije 1', '+38111578-9814', 'www.pozoristenaterazijama.rs','info@pozoristenatrerazijama.rs', 'false', 'false'); go select * from POZORISTE; go insert POZORISTE (SIF_MESTA,NAZIV_POZORISTA,P_POSTANSKI_BROJ,P_MESTO, P_IZ_VOJVODINE,P_AMATERSKO) values(1, 'Okruzno Pozoriste Becej', 21220, 'Becej', 'true', 'true'); go insert POZORISTE ( values(1, 'Pozoriste na Terazijama', 11000, 'Beograd', 'Terazije 1', '+38111578-9814', 'www.pozoristenaterazijama.rs','info@pozoristenatrerazijama.rs', 'false', 'false'); go insert POZORISTE (SIF_MESTA,NAZIV_POZORISTA,P_POSTANSKI_BROJ,P_MESTO, P_IZ_VOJVODINE,P_AMATERSKO) values(1, 'Vojovdjansko Pozoriste Becej', 21220, 'Becej', 'true', 'false'); go select * from POZORISTE; go -- Listati nazive i ulicu-broj za sva pozorista select NAZIV_POZORISTA,P_ULICA_BROJ from POZORISTE; go -- Listati nazive i ulicu-broj za amaterska pozorista select NAZIV_POZORISTA,P_ULICA_BROJ from POZORISTE where P_AMATERSKO='true'; go -- Listati nazive i ulicu-broj za amaterska pozorista i uveriti se u tacnost -- (da budu prikazani podaci da je pozoriste amatersko) select NAZIV_POZORISTA,P_ULICA_BROJ,P_AMATERSKO from POZORISTE where P_AMATERSKO='true'; go -- Listati nazive i ulicu-broj za amaterska pozorista, i za ona pozorista -- za koja nemamo podatke o emailu i dokazati tacnost select NAZIV_POZORISTA,P_ULICA_BROJ,P_AMATERSKO,P_EMAIL from POZORISTE where P_AMATERSKO='true' or P_EMAIL is null; go -- Koliko imamo amaterskih i profesionalnih pozorista select P_AMATERSKO, COUNT(*) as 'Broj pozorista' from POZORISTE group by P_AMATERSKO; go -- Koliko imamo amaterskih i profesionalnih pozorista -- kod kojih su nam nepoznate www adrese select P_AMATERSKO, COUNT(*) as 'Broj pozorista' from POZORISTE where P_WWW_ADRESA is null group by P_AMATERSKO; go -- Koliko imamo amaterskih i profesionalnih pozorista -- prikazati samo one grupe koje imaju broj pozorista veci od 2 select P_AMATERSKO, COUNT(*) as 'Broj pozorista' from POZORISTE group by P_AMATERSKO having COUNT(*)>2; go -- Koliko imamo amaterskih i pozorista select COUNT(*) as 'Broj amaterskih pozorista' from POZORISTE where P_AMATERSKO = 'true'; go select P_AMATERSKO, COUNT(*) as 'Broj amaterskih pozorista' from POZORISTE group by P_AMATERSKO having P_AMATERSKO='true'; go select COUNT(*) as 'Broj amaterskih pozorista' from POZORISTE group by P_AMATERSKO having P_AMATERSKO='true'; go -- Broj amaterskih pozorista koja imaju e/mail select COUNT(*) as 'Broj amaterskih pozorista' from POZORISTE where P_EMAIL is not null group by P_AMATERSKO having P_AMATERSKO='true'; go -- Koliko iznosi suma postanskih brojeva amaterskih pozorista -- sa sedistem u Beogradu select SUM(P_POSTANSKI_BROJ) from POZORISTE where P_AMATERSKO='true' and P_MESTO='Beograd'; go -- Koliko iznosi suma postanskih brojeva amaterskih pozorista -- sa sedistem u Beceju select SUM(P_POSTANSKI_BROJ) from POZORISTE where P_AMATERSKO='true' and P_MESTO='Becej'; go -- Koliko iznosi suma postanskih brojeva Vojvodjanskih profesionalnih -- pozorista select SUM(P_POSTANSKI_BROJ) from POZORISTE where P_AMATERSKO='false' and P_IZ_VOJVODINE = 'true'; go select * from POZORISTE; go /* Osma nedelja - kraj Osma nedelja - kraj Osma nedelja - kraj Osma nedelja - kraj */ -- deveta nedelja - pocetak -- deveta nedelja - pocetak -- deveta nedelja - pocetak -- deveta nedelja - pocetak -- create #naziv_lokalne_teporary_datoteke -- from ... -- create ##naziv_lokalne_teporary_datoteke -- from ... -- kopiranje strukture i sadrzaja tabele use PozorisniPortal; go select * into POZORISTE1 from pozoriste; go select * from POZORISTE1; go -- kopiranje strukture tabele select * into POZORISTE2 from pozoriste where 5=6; go sp_help pozoriste2; go select * from POZORISTE2; go -- punjenje tabele iz druge postojece tabele insert POZORISTE2 select SIF_MESTA,NAZIV_POZORISTA,P_POSTANSKI_BROJ,P_MESTO,P_ULICA_BROJ, P_TELEFON,P_WWW_ADRESA,P_EMAIL,P_IZ_VOJVODINE,P_AMATERSKO from POZORISTE; go select * from POZORISTE2; go -- slozeni uslov u where klauzuli -- Listati nazive pozorista koji pocinju os slova 'G' -- do slova 'P' select NAZIV_POZORISTA from POZORISTE where NAZIV_POZORISTA >= 'G' and NAZIV_POZORISTA < 'R'; go select * from POZORISTE; go -- Listati nazive pozorista koji pocinju os slova 'G' -- do slova 'P' ili profesionalna pozorista select NAZIV_POZORISTA, P_AMATERSKO from POZORISTE where (NAZIV_POZORISTA >= 'G' and NAZIV_POZORISTA < 'R') or P_AMATERSKO=0; go -- Listati nazive pozorista koji pocinju od slova 'G' -- do slova 'P' ili profesionalna pozorista, ali samo ona -- koja nemaju evidentirani telefon select NAZIV_POZORISTA, P_AMATERSKO from POZORISTE where ((NAZIV_POZORISTA >= 'G' and NAZIV_POZORISTA < 'R') or P_AMATERSKO=0) and P_TELEFON is null; go -- Listati amaterska pozorista -- koja nemaju evidentirani telefon select NAZIV_POZORISTA, P_AMATERSKO, P_TELEFON from POZORISTE where P_AMATERSKO=1 and P_TELEFON is null; go -- Listati profesionalna pozorista -- u novosadskom okrugu (pomocu between-a) select NAZIV_POZORISTA, P_AMATERSKO, P_TELEFON,P_POSTANSKI_BROJ from POZORISTE where P_AMATERSKO=0 and P_POSTANSKI_BROJ between 21000 and 21999; go select * from POZORISTE; go -- Listati profesionalna pozorista -- u novosadskom okrugu (pomocu between-a) koji nemaju telefon select NAZIV_POZORISTA, P_AMATERSKO, P_TELEFON,P_POSTANSKI_BROJ from POZORISTE where P_AMATERSKO=0 and P_POSTANSKI_BROJ between 21000 and 21999 and P_TELEFON is null; go -- in operator -- listati pozorista koja nisu iz beceja ili beograda pomocu IN operatora select NAZIV_POZORISTA, P_AMATERSKO, P_TELEFON,P_POSTANSKI_BROJ from POZORISTE where P_MESTO not in ('Becej', 'Beograd'); go -- listati pozorista koja su iz beceja ili beograda pomocu IN operatora select NAZIV_POZORISTA, P_AMATERSKO, P_TELEFON,P_POSTANSKI_BROJ,P_MESTO from POZORISTE where P_MESTO in ('Becej', 'Beograd'); go -- izbrojati koliko ima naznacenih mesta u tabeli pozoriste, i koliko ima -- razlicitih naziva mesta u istoj tabeli select COUNT(P_MESTO) as 'Broj popunjenih redova sa nazivom mesta', COUNT(distinct P_MESTO) as 'Broj razlicitih naziva mesta' from POZORISTE; go select * from POZORISTE; go -- izbrojati koliko ima evidentiranih telefona u tabeli pozoriste, i koliko ima -- razlicitih brojeva telefona u istoj tabeli select COUNT(P_TELEFON) as 'Broj popunjenih redova sa brojem telefona', COUNT(distinct P_TELEFON) as 'Broj razlicitih brojeva telefona' from POZORISTE; go -- + operator -- Ispisati naziv pozorista, mesto i adresu sa znakom ' - ' izmedju njih select NAZIV_POZORISTA + ' - ' + P_MESTO + ' - ' + P_ULICA_BROJ as 'Naziv+Mesto+Adresa' from POZORISTE; go -- Izabrati prvih deset znakova naziva pozorista select SUBSTRING(NAZIV_POZORISTA, 1, 10) as 'Prvih 10 znakova naziva pozorista' from POZORISTE; go -- Ispisati velikim/malim slovima nazive pozorista select UPPER(NAZIV_POZORISTA)as 'Veliki nazivi' from POZORISTE; go -- Zameniti Prvo slovo u nazivu pozorista, ako je ono 'S' -- sa slovom 'Z' select NAZIV_POZORISTA, replace (SUBSTRING(NAZIV_POZORISTA,1,1),'S','Z')+ SUBSTRING(NAZIV_POZORISTA,2, LEN(NAZIV_POZORISTA)) from POZORISTE; go -- Kraj 9 nedelje -- deveta nedelja - kraj -- deveta nedelja - kraj -- deveta nedelja - kraj -- deveta nedelja - kraj /* Priprema za deseti susret - pocetak Priprema za deseti susret - pocetak Priprema za deseti susret - pocetak -- REPLACE() funkcija -- LEN() funkcija, DATALENGTH(), COL_LENGTH() -- LTRIM(), RTRIM() funkcija -- LTRIM(), RTRIM() funkcio */ use PozorisniPortal; go select * from POZORISTE; go select NAZIV_POZORISTA + REPLACE(left(NAZIV_POZORISTA, 1), 'S', 'G')+ SUBSTRING(NAZIV_POZORISTA, 2, col_length('POZORISTE', 'NAZIV_POZORISTA')) as 'Prosta zamena', ltrim(NAZIV_POZORISTA) + REPLACE(left(NAZIV_POZORISTA, 1), 'S', 'G')+ SUBSTRING(NAZIV_POZORISTA, 2, col_length('POZORISTE', 'NAZIV_POZORISTA')) as 'sa ltrim funkcijom', rtrim(NAZIV_POZORISTA) + REPLACE(left(NAZIV_POZORISTA, 1), 'S', 'G')+ SUBSTRING(NAZIV_POZORISTA, 2, col_length('POZORISTE', 'NAZIV_POZORISTA')) as 'sa rtrim funkcijom' from POZORISTE; go -- CURRENT_TIMESTAMP (CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP)operator -- GETDATE() --??? 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 -- CURRENT_TIMESTAMP operator -- SYSTEM_USER, CURRENT_USER SELECT CURRENT_USER AS "User"; go SELECT SYSTEM_USER AS "User"; go -- CAST() funkcija -- CASE struktura -- Duplirajmo postanske brojeve za pozorista iz Vojvodine! select NAZIV_POZORISTA, P_MESTO, P_POSTANSKI_BROJ, case P_IZ_VOJVODINE when 1 then P_POSTANSKI_BROJ*2 else P_POSTANSKI_BROJ end as 'Ispravljeni postanski brojevi' from POZORISTE; go -- COALESCE() funkcija -- NULLIF() funkcija -- CONVERT() -- GETDATE() -- DATEDIFF() -- DATEPART() -- ISNULL() -- Finally, for adding a default value, the ADD CONSTRAINT syntax is very simple. /* ADD CONSTRAINT constraint_name DEFAULT default_value FOR column_to_receive_the_value The only part of the preceding syntax that requires further explanation is the default_value area. default_value can be a string, a numeric, NULL, or a system function (for example, GETDATE(), which would insert the current date and time). So the default value does not have to be fixed; it can be dynamic. -- • LTRIM/RTRIM: These perform similar functionality. If you have a string with leading spaces, and you wish to remove those leading spaces, you would use LTRIM so that the returned varchar value would have a nonspace character as its first value. If you have trailing spaces, you would use RTRIM. You can only use this function with a data type of varchar or nvarchar, or a data type that can be implicitly converted to these two data types, or with a data type converted to varchar or nvarchar using the CAST SQL Server function. -- • CAST: This specialized function converts one data type to another data type. I don’t cover this within the book. If you wish to convert data types, check on the command in Books Online, which can be found by selecting Help in Query Editor. -- • LEFT/RIGHT: This function returns the leftmost or rightmost characters from a string. Passing in a second parameter to the function will determine the number of characters to return from whichever side of the string. The LEFT and RIGHT functions accept any data type except text or ntext expressions to perform the string manipulation, implicitly converting any noncharacter data type or varchar or nvarchar, and returning a varchar or nvarchar data type as a result. -- 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 */ -- Timestamp za 50 casova select GETDATE(), DATEADD(hh, 50, getdate()) as 'Timestamp za 50 casova'; go -- 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, 50, getdate()) as 'Timestamp za 50 dana', DATEDIFF(WW, getdate(), DATEADD(DD, 50, getdate())) as 'Razlika u nedeljama'; go /* -- DATENAME() Returning the name of the part of the date is great for using with things such as customer statements. Changing the number 6 to the word June makes for more pleasant reading. The syntax is DATENAME(datepart, datetoinspect) We will also see this in action in DATEPART(). Try It Out: DATENAME -- 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())-1 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 /* -- GETDATE()/SYSDATETIME() GETDATE() is a great function for returning the exact date and time from the system. You have seen this in action when setting up a table with a default value, and at a couple of other points in the book. There are no parameters to the syntax. If you need greater accuracy to nanoseconds and further, then use SYSDATETIME(). String This next section will look at some functions that can act on those data types that are characterbased, such as varchar and char. -- LEFT() When it is necessary to return the first n left characters from a string-based variable, you can achieve this through the use of LEFT(n), replacing n with the number of characters you wish to return. -- LOWER() To change alphabetic characters within a string, ensuring that all characters are in lowercase, you can use the LOWER() function. -- LTRIM() There will be times that leading spaces will occur in a string and you’ll want to remove them. LTRIM() will trim these spaces on the left. -- RIGHT() The opposite of LEFT() is, of course, RIGHT(), and this function returns a set of characters from the right-hand side. -- RTRIM() When you have a CHAR() data type, no matter how many characters you enter, the variable will be filled on the right, known as right-padded, with spaces. To remove these, use RTRIM. This changes the data from a fixed-length CHAR() to a variable-length value. -- STR() Some data types have implicit conversions. We will see later how to complete explicit conversions, but a simple conversion that will take any numeric value and convert it to a variable-length string is STR(), which we look at next. -- SUBSTRING() As you have seen, you can take a number of characters from the left and from the right of a string. To retrieve a number of characters that do not start with the first or last character, you need to use the function SUBSTRING(). This has three parameters: the variable or column, which character to start the retrieval from, and the number of characters to return. -- System Functions System functions are functions that provide extra functionality outside of the boundaries that can be defined as string, numeric, or date related. Three of these functions will be used extensively throughout our code, and therefore you should pay special attention to CASE, CAST, and ISNULL. CASE WHEN. . .THEN. . .ELSE. . .END The first function is when we wish to test a condition. WHEN that condition is true THEN we can do further processing, ELSE if it is false, then we can do something else. What happens in the WHEN section and the THEN section can range from another CASE statement to providing a value that sets a column or a variable. The CASE WHEN statement can be used to return a value or, if on the right-hand side of an equality statement, to set a value. Both of these scenarios are covered in the following examples. -- CAST()/CONVERT() These are two functions used to convert from one data type to another. The main difference between them is that CAST() is ANSI SQL–92 compliant, but CONVERT() has more functionality. The syntax for CAST() is CAST(variable_or_column AS datatype) This is opposed to the syntax for CONVERT(), which is CONVERT(datatype,variable_or_column) Not all data types can be converted between each other, such as converting a datetime to a text data type, and some conversions need neither a CAST() nor a CONVERT(). There is a grid in Books Online that provides the necessary information. If you wish to CAST() from numeric to decimal or vice versa, then you need to use CAST(); otherwise, you will lose precision. -- 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-29') go select ISDATE(GETDATE()) go /* -- ISNULL() Many times so far, you have seen NULL values within a column of returned data. As a value, NULL is very useful, as you have seen. However, you may wish to test whether a column contains a NULL or not. If there were a value, you would retain it, but if there were a NULL, you would convert it to a value. This function could be used to cover a NULL value in an aggregation, for example. The syntax is ISNULL(value_to_test,new_value) where the first parameter is the column or variable to test if there is a NULL value, and the second option defines what to change the value to if there is a NULL value. This change only occurs in the results and doesn’t change the underlying data that the value came from. */ select NAZIV_POZORISTA, P_POSTANSKI_BROJ, P_MESTO, P_ULICA_BROJ, ISNULL (P_ULICA_BROJ, 'N.A.') as 'Primenom ISNULL-a' from POZORISTE; go /* -- ISNUMERIC() This final system function tests the value within a column or variable and ascertains whether it is numeric or not. The value returned is 0, or false, for an invalid number, or 1 for true if the test is okay and can convert to a numeric. */ -- -- Visetablicni upiti --Dekartov proizvod/cross join select * from dbo.ZEMLJA, dbo.MESTO; go select * from dbo.ZEMLJA cross join dbo.MESTO; go -- Filtrirano/uslovno povezivanje dve tabele select * from dbo.ZEMLJA z, dbo.MESTO m where z.SIF_ZEMLJE=m.SIF_ZEMLJE; go select * from dbo.ZEMLJA z inner join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE; go /* Kraj desetog druzenja - 04-12-2014 Kraj desetog druzenja - 04-12-2014 Kraj desetog druzenja - 04-12-2014 */ use PozorisniPortal; go insert ZEMLJA values ('Makedonija','MK'); go -- Listati zemlje i njima pripadajuca mesta -- ako se za zemlju nije vezano mesto null vrednosti ce se pojaviti umesto podataka za mesto select * from dbo.ZEMLJA z left outer join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE; go -- Listati zemlje kojima pripadaju mesta i podatke o pripadajucim mestima select * from dbo.ZEMLJA z left outer join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE where NAZIV_MESTA is not null; go -- Listati zemlje kojima ne pripadaju mesta select * from dbo.ZEMLJA z left outer join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE where NAZIV_MESTA is null; go -- Kojim razlicitim drzavama/zemljama ne pripada ni jedno mesto select distinct OZNAKA_DRZAVE from dbo.ZEMLJA z left outer join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE where NAZIV_MESTA is null; go -- Za koliko drzava nije vezano ni jedno mesto select COUNT (distinct OZNAKA_DRZAVE) from dbo.ZEMLJA z left outer join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE where NAZIV_MESTA is null; go -- right outer join -- Listati mesta i zemlje koje im pripadaju select * from dbo.ZEMLJA z right outer join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE; go -- full outer join select * from dbo.ZEMLJA z full outer join dbo.MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE; go select * from ZEMLJA z full outer join MESTO m on z.SIF_ZEMLJE=m.SIF_ZEMLJE where z.SIF_ZEMLJE is null or m.SIF_ZEMLJE is null; go -- Unutrasnji SELCT -- -- Unutrašnji SELECT vraća jednu vrednost -- where select * from dbo.POZORISTE; go -- Listati nazive pozorista za najveci postanski broj u tabeli pozoriste select NAZIV_POZORISTA, P_POSTANSKI_BROJ from dbo.POZORISTE where P_POSTANSKI_BROJ = (select MAX(P_POSTANSKI_BROJ) from dbo.POZORISTE); go -- Koliko ima pozorista za mesto sa najvecim postanski brojem u tabeli pozoriste select count(NAZIV_POZORISTA) from dbo.POZORISTE where P_POSTANSKI_BROJ = (select MAX(P_POSTANSKI_BROJ) from dbo.POZORISTE); go -- Koliko ima pozorista mesto sa nazivom Becej -- IN select count(NAZIV_POZORISTA) from dbo.POZORISTE where P_POSTANSKI_BROJ in (select P_POSTANSKI_BROJ from dbo.POZORISTE where P_MESTO = 'Becej'); go -- Listati nazive pozorista za najveci postanski broj iz tabele mesto select * from MESTO; go select * from dbo.POZORISTE; go insert MESTO values (1, 'Subotica', 24000); go select NAZIV_POZORISTA, P_POSTANSKI_BROJ from dbo.POZORISTE where P_POSTANSKI_BROJ=(select min(POST_BROJ) from dbo.MESTO); go -- Koliko ima pozorista mesto sa nazivom Becej -- ANY select count(NAZIV_POZORISTA) from dbo.POZORISTE where P_POSTANSKI_BROJ = ANY (select P_POSTANSKI_BROJ from dbo.POZORISTE where P_MESTO = 'Becej'); go -- Listati nazive pozorista iz tabele pozoriste za postanski broj manji od bilo kojeg postanskog broja -- iz tabele mesto update dbo.MESTO set POST_BROJ=12000 where NAZIV_MESTA='beograd'; go select * from dbo.MESTO; go -- Listati nazive pozorista iz tabele pozoriste za postanski broj manji od bilo kojeg postanskog broja -- iz tabele mesto -- sledi pogresno resenje sa ANY select NAZIV_POZORISTA, P_POSTANSKI_BROJ from dbo.POZORISTE where P_POSTANSKI_BROJ < ANY (select POST_BROJ from dbo.MESTO); go -- a sada tacno resenje sa ALL: select NAZIV_POZORISTA, P_POSTANSKI_BROJ from dbo.POZORISTE where P_POSTANSKI_BROJ < All (select POST_BROJ from dbo.MESTO); go -- Listati nazive pozorista iz tabele pozoriste za postanski broj veci od bilo kojeg postanskog broja -- iz tabele mesto -- sledi pogresno resenje sa ANY select NAZIV_POZORISTA, P_POSTANSKI_BROJ from dbo.POZORISTE where P_POSTANSKI_BROJ > ANY (select POST_BROJ from dbo.MESTO); go -- a sada tacno resenje sa ALL: update dbo.MESTO set POST_BROJ=23000 where NAZIV_MESTA='subotica'; go select * from dbo.MESTO; go select NAZIV_POZORISTA, P_POSTANSKI_BROJ from dbo.POZORISTE where P_POSTANSKI_BROJ > All (select POST_BROJ from dbo.MESTO); go -- kraj 11/og druzenja -- kraj 11/og druzenja -- kraj 11/og druzenja -- kraj 11/og druzenja -- pocetak 12-og druzenja -- pocetak 12-og druzenja -- pocetak 12-og druzenja -- pocetak 12-og druzenja -- Listati nazive pozorista i nazive mesta iz tabele pozoriste u slucaju da u tabeli prostojija -- postoji bar jedan red podataka -- EXIST use PozorisniPortal; go select NAZIV_POZORISTA, P_MESTO from dbo.POZORISTE where exists (select NAZIV_PROSTORIJE from dbo.PROSTORIJA); go select * from dbo.PROSTORIJA; go -- Listati nazive pozorista i nazive mesta iz tabele pozoriste u slucaju da u tabeli prostojija -- ne postoji ni jedan jedan red podataka -- EXIST select NAZIV_POZORISTA, P_MESTO from dbo.POZORISTE where not exists (select NAZIV_PROSTORIJE from dbo.PROSTORIJA); go -- Listati nazive mesta i postanske brojeve bez ponavljanja iz tabele pozoriste -- za ona mesta iz tabele mseto koja imaju evidentirano pozoriste, povezivanje dve tabele -- raditi po postanskom broju -- bez exists - Dekartov proizvod select P_MESTO, P_POSTANSKI_BROJ from dbo.POZORISTE p, dbo.MESTO m where p.P_POSTANSKI_BROJ=m.POST_BROJ; go select * from dbo.MESTO; go update dbo.MESTO set POST_BROJ=24000 where NAZIV_MESTA='Subotica'; go -- Listati nazive mesta i postanske brojeve bez ponavljanja iz tabele pozoriste -- za ona mesta iz tabele mseto koja imaju evidentirano pozoriste, povezivanje dve tabele -- raditi po postanskom broju -- bez exists - inner join select P_MESTO, P_POSTANSKI_BROJ from dbo.POZORISTE p join dbo.MESTO m on p.P_POSTANSKI_BROJ=m.POST_BROJ; go -- Listati nazive mesta i postanske brojeve bez ponavljanja iz tabele pozoriste -- za ona mesta iz tabele mseto koja imaju evidentirano pozoriste, povezivanje dve tabele -- raditi po postanskom broju -- bez exists - outer join select P_MESTO, P_POSTANSKI_BROJ from dbo.POZORISTE p left join dbo.MESTO m on p.P_POSTANSKI_BROJ=m.POST_BROJ where NAZIV_MESTA is not null; go -- Listati nazive mesta i postanske brojeve bez ponavljanja iz tabele pozoriste -- za ona mesta iz tabele mseto koja imaju evidentirano pozoriste, povezivanje dve tabele -- raditi po postanskom broju -- exists select P_MESTO, P_POSTANSKI_BROJ from dbo.POZORISTE p where exists (select NAZIV_MESTA from dbo.MESTO m where m.POST_BROJ=p.P_POSTANSKI_BROJ); go -- Listati nazive mesta i postanske brojeve bez ponavljanja iz tabele pozoriste -- za ona mesta iz tabele mseto koja imaju evidentirano pozoriste, povezivanje dve tabele -- raditi po kljucu-spoljnom kljucu -- exists select P_MESTO, P_POSTANSKI_BROJ from dbo.POZORISTE p where exists (select NAZIV_MESTA from dbo.MESTO m where m.SIF_MESTA=p.SIF_MESTA); go -- unutrasnji select u from-u -- U proseku koliko ima pozorista po mestima? --(8+21+4+4=37/4=9 kerekitve)? select AVG(boj_pozorista_po_mestima) from (select cast(COUNT(NAZIV_POZORISTA) as numeric (5,2))as boj_pozorista_po_mestima from dbo.POZORISTE group by P_MESTO) as d; go select COUNT(NAZIV_POZORISTA) as boj_pozorista_po_mestima from dbo.POZORISTE group by P_MESTO; go -- Unutrasnji select u having-u -- Izbrojati koliko ima pozorista po mestima, a ispisati samo -- podatke za mesto sa majvecim postanskim brojem select P_POSTANSKI_BROJ, P_MESTO, COUNT(NAZIV_POZORISTA) from dbo.POZORISTE group by P_POSTANSKI_BROJ, P_MESTO having P_POSTANSKI_BROJ=(select MAX(P_POSTANSKI_BROJ) from dbo.POZORISTE); go -- Unutrasnji select u having-u -- Izbrojati koliko ima pozorista bez ponavljanja imena pozorista po mestima, a ispisati samo -- podatke za mesto sa majvecim postanskim brojem iz tabele mesto select P_POSTANSKI_BROJ, P_MESTO, COUNT(distinct NAZIV_POZORISTA) from dbo.POZORISTE group by P_POSTANSKI_BROJ, P_MESTO having P_POSTANSKI_BROJ=(select MAX(POST_BROJ) from dbo.MESTO); go -- Unutrasnji select u SELECT-u -- listati postanske brojeve, nazive mesta i nazive pozorista -- iz tabele pozoriste i pored njih najmanji postanski broj i njemu pripadajuci naziv mesta iz tabele mesto select P_POSTANSKI_BROJ, P_MESTO, NAZIV_POZORISTA, (select MIN(POST_BROJ) from dbo.MESTO) as 'Najmanji post.br. iz MESTO', (select distinct NAZIV_MESTA from dbo.MESTO where POST_BROJ=(select MIN(POST_BROJ) from dbo.MESTO)) as 'Naziv mesta koji odgovara najmanjem post br. iz MESTO' from dbo.POZORISTE; go select * from dbo.MESTO; go /* Zadnji sastanak */ /* 1. Listázzuk az országneveket és országjelzéseket! */ use PozorisniPortal; go select distinct DRZAVA, OZNAKA_DRZAVE from dbo.ZEMLJA order by DRZAVA desc; go /* 4. Listati drzave sa pocetnim slovom M ! */ select distinct DRZAVA from dbo.ZEMLJA where SUBSTRING(DRZAVA,1,1)='M'; go select distinct DRZAVA from dbo.ZEMLJA where DRZAVA like 'M%'; go /* 5. Potraziti zemlje/drzave koje imaju u nazivu slovo k */ select DRZAVA from dbo.ZEMLJA where DRZAVA like '%k%'; go /* 5. Listti malim slovima nazive drzava od 6 karaktera! */ select distinct LOWER(DRZAVA) from dbo.ZEMLJA where LEN(DRZAVA)=6; go /* 6. Koliko ima zemalja u bazi podatata? */ select COUNT(distinct drzava) as 'Broj drzava' from dbo.ZEMLJA; go /* 7. Listati drzave sa pocetnim slovima od M do S */ select distinct DRZAVA from dbo.ZEMLJA where substring(DRZAVA, 1, 1) >= 'M' and substring(DRZAVA, 1, 1) <= 'S'; go select DRZAVA from dbo.ZEMLJA where substring(DRZAVA, 1, 1) between 'M' and 'S'; go /* 10. Koliko karaktera sadrzi najduzi naziv drzave? */ select max (len(DRZAVA)) from dbo.ZEMLJA; go /* 11. Koji su nazivi drzave koji imaju najveci broj karaktera? */ select DRZAVA, LEN(DRZAVA) as 'Broj karaktera ' from dbo.ZEMLJA; go select distinct DRZAVA, LEN(DRZAVA) from dbo.ZEMLJA where LEN(DRZAVA)=(select max (len(DRZAVA)) from dbo.ZEMLJA) ; go /* 12. Listati nazive pozorista koja imaju tacno dve prostorije za predstave */ select NAZIV_POZORISTA from POZORISTE where SIF_POZORISTA in (select SIF_POZORISTA from (select SIF_POZORISTA, COUNT(SIF_PROSTORIJE) from dbo.PROSTORIJA group by SIF_POZORISTA; go select p.NAZIV_POZORISTA, COUNT(pr.SIF_PROSTORIJE) from dbo.POZORISTE p join dbo.PROSTORIJA pr on pr.SIF_POZORISTA = p.SIF_POZORISTA group by p.NAZIV_POZORISTA having COUNT(pr.SIF_PROSTORIJE)=2; go /* 14. Koje nazive prostorija imamo evidentirane iz Srbije? */ select NAZIV_PROSTORIJE from dbo.PROSTORIJA pr join dbo.POZORISTE po on po.SIF_POZORISTA = pr.SIF_POZORISTA join dbo.MESTO m on m.SIF_MESTA=po.SIF_MESTA join dbo.ZEMLJA z on z.SIF_ZEMLJE=m.SIF_ZEMLJE where z.DRZAVA='Srbija'; go select NAZIV_PROSTORIJE from dbo.PROSTORIJA where SIF_POZORISTA in (select SIF_POZORISTA from dbo.POZORISTE where SIF_MESTA in (select SIF_MESTA from dbo.MESTO where SIF_ZEMLJE in (select SIF_ZEMLJE from dbo.ZEMLJA where DRZAVA='Srbija'))); go /* 15. Listati drzave, mesta u tim drzavama, pozorista u tim mestima i prostorije koje pripadaju tim pozoristima. */ select DRZAVA, NAZIV_MESTA, NAZIV_POZORISTA, NAZIV_PROSTORIJE from dbo.ZEMLJA join dbo.MESTO on mesto.SIF_ZEMLJE=ZEMLJA.SIF_ZEMLJE join dbo.POZORISTE on POZORISTE.SIF_MESTA=MESTO.SIF_MESTA left join dbo.PROSTORIJA on prostorija.SIF_POZORISTA=POZORISTE.SIF_POZORISTA order by DRZAVA, NAZIV_MESTA, NAZIV_POZORISTA, NAZIV_PROSTORIJE; go