drop table if exists artysta, eksponat, instytucja, historia; drop function if exists art(), hist(); create table artysta( id serial primary key, imie varchar(20) not null, nazwisko varchar(20) not null, rok_urodzenia integer not null, rok_smierci integer ); create table eksponat( id serial primary key, tytul varchar(40) not null, typ varchar(20) not null, wysokosc numeric(4,3) not null, szerokosc numeric(4,3) not null, waga numeric(7,3) not null, id_artysta integer references artysta, cenny boolean not null ); create table instytucja( id serial primary key, nazwa varchar(40) not null, miasto varchar(20) not null ); create table historia( id serial primary key, id_eksponat integer not null references eksponat, galeria varchar(40), sala varchar(20), id_instytucja integer references instytucja, data_rozpoczecia date not null, data_zakonczenia date ); create function art() returns trigger as $$ begin if (new.rok_urodzenia > new.rok_smierci) then raise exception 'Rok urodzenia nie moze byc wiekszy od roku smierci'; end if; return new; end $$ language 'plpgsql'; create trigger art_trig after insert or update on artysta for each row execute procedure art(); create function hist() returns trigger as $$ begin if (new.galeria is not null or new.id_instytucja is not null) and TRUE in (select cenny from eksponat where eksponat.id = new.id_eksponat) then raise exception 'Nie mozna wypozyczyc cennego dziela'; elsif (new.id_instytucja is not null) and (select count(id) from eksponat where id_artysta in (select id_artysta from eksponat where id = new.id_eksponat) and id_artysta is not null) = 1 then raise exception 'Nie mozna wypozyczyc jedynego dziela artysty'; elsif (new.data_zakonczenia is not null) and (new.data_zakonczenia < new.data_rozpoczecia) then raise exception 'Data zakonczenia nie moze byc wczesniej niz data rozpoczecia'; end if; return new; end $$ language 'plpgsql'; create trigger hist_trig after insert or update on historia for each row execute procedure hist(); insert into artysta (imie, nazwisko, rok_urodzenia, rok_smierci) values ('Leonardo', 'da Vinci', 1452, 1519), ('Vincent', 'van Gogh', 1853, 1890), ('Pablo', 'Picasso', 1881, 1973), ('Claude', 'Monet', 1840, 1926), ('Salvador', 'Dali', 1904, 1989), ('Michelangelo', 'Buonarroti', 1475, 1564), ('Rembrandt', 'van Rijn', 1606, 1669), ('Edvard', 'Munch', 1863, 1944); insert into eksponat (tytul, typ, wysokosc, szerokosc, waga, id_artysta, cenny) values ('Mona Lisa', 'obraz', 0.77, 0.53, 8, 1, TRUE), ('Dama z łasiczka', 'obraz', 0.5, 0.3, 4, 1, FALSE), ('Gwiaździsta noc', 'obraz', 0.74, 0.92, 9, 2, FALSE), ('Guernica', 'obraz', 3.494, 7.77, 100, 3, FALSE), ('Impresja', 'obraz', 0.48, 0.63, 6.5, 4, TRUE), ('Trwałość pamięci', 'obraz', 0.24, 0.33, 5, 5, FALSE), ('Dawid', 'rzezba', 5.17, 1.95, 6000, 6, TRUE), ('Straż nocna', 'obraz', 3.63, 4.37, 50, 7, FALSE), ('Krzyk', 'obraz', 0.91, 0.73, 8, 8, FALSE); insert into instytucja (nazwa, miasto) values ('Luwr', 'Siemianowice Śląskie'), ('Galeria Narodowa', 'Sosnowiec'); insert into historia (id_eksponat, galeria, sala, id_instytucja, data_rozpoczecia, data_zakonczenia) values (1, null, null, null, '1900-01-01', null), (2, null, null, 1, '2025-01-15', '2025-01-22'), (2, null, null, 2, '2025-01-30', null); grant usage on schema kw448846 to scott; grant all on artysta to scott; grant all on eksponat to scott; grant all on instytucja to scott; grant all on historia to scott;