Spoiler: --tworzenie bazy
Create database biblioteka; GO
create table biblioteka.dbo.czytelnicy( id varchar(5) primary key, nazwisko varchar(15) not null, imie varchar(15) not null, pesel varchar(11) not null, data_ur datetime not null, plec varchar(1), telefon varchar(15)); go
alter table biblioteka.dbo.czytelnicy with nocheck add constraint id check(id like '[a-zA-Z][a-zA-Z][0-9][0-9][0-9]');
alter table biblioteka.dbo.czytelnicy with nocheck add constraint sex check(plec in ('K','M'));
alter table biblioteka.dbo.czytelnicy with nocheck add constraint cyfry check(pesel like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]');
alter table biblioteka.dbo.czytelnicy drop constraint sex; go
create table biblioteka.dbo.pracownicy( id int identity(1,1) primary key, nazwisko varchar(15) not null, imie varchar(15) not null, data_ur datetime not null, data_zatr datetime not null) go
alter table biblioteka.dbo.pracownicy with nocheck add constraint kolejnosc check(data_ur < data_zatr);
create table biblioteka.dbo.wydawnictwa( id int identity(1,1) primary key, nazwa varchar(50) not null, miasto varchar(50) not null, telefon varchar(15)) go
create table biblioteka.dbo.ksiazki( sygn int primary key, id_wyd int references wydawnictwa(id), tytul varchar(40) not null, cena money not null, strony int, gatunek varchar(30)); go
alter table biblioteka.dbo.ksiazki with nocheck add constraint gatunki check(gatunek in ('powieść','powieść historyczna','dla dzieci','wiersze','kryminał','powieść science fiction','książka naukowa'));
create table biblioteka.dbo.wypozyczenia( id_w int identity(1,1) primary key, sygn int references ksiazki(sygn), id_cz varchar(5) references czytelnicy(id), id_p int references pracownicy(id), data_w datetime not null, data_z datetime, kara int default 0); go
Spoiler:--4 ALTER TABLE biblioteka.dbo.pracownicy ADD plec varchar(1); go
--5 alter table biblioteka.dbo.pracownicy with nocheck add constraint sex2 check(plec in ('K','M'));
--6 alter table biblioteka.dbo.czytelnicy with nocheck add constraint jedenPesel unique(pesel); go
--7 alter table biblioteka.dbo.wypozyczenia with nocheck add constraint unikalne unique(sygn,data_w); go
--8 CREATE TABLE biblioteka.dbo.wypoz_lato (imie_nazwisko VARCHAR(30) NOT NULL, tytul VARCHAR(40) NOT NULL, data_w DATETIME NOT NULL, data_z DATETIME, );
INSERT INTO biblioteka.dbo.wypoz_lato SELECT c.imie+' '+c.nazwisko, k.tytul, w.data_w, w.data_z FROM biblioteka.dbo.wypozyczenia AS w left join biblioteka.dbo.czytelnicy AS c ON w.id_cz=c.id left join biblioteka.dbo.ksiazki AS k ON k.sygn=w.sygn WHERE (DATEPART(month, w.data_w) IN (6,7,8)) ORDER BY w.data_w ASC;
SELECT COUNT(*) FROM biblioteka.dbo.wypoz_lato; SELECT * FROM biblioteka.dbo.wypoz_lato;
|