Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lab1
- select * from filmy where rok_produkcji = 1998 or rok_produkcji = 1999;
- select * from filmy where cena > 9 order by cena;
- select nazwisko from klienci where imie = 'Jan';
- select imie, nazwisko from klienci where length(imie) > length(nazwisko);
- select nazwisko from aktorzy where imie = 'Arnold' or imie = 'Tom' or imie = 'Jodie' order by nazwisko;
- select id_kopii, data_zwrotu - data_wypozyczenia as czas from wypozyczenia where data_zwrotu - data_wypozyczenia > 1;
- select id_kopii from wypozyczenia where data_wypozyczenia >= '2005-07-15' and data_wypozyczenia <= '2005-07-22' order by id_kopii;
- select substr(imie, 1, 1)|| '. ' ||nazwisko as aktor from aktorzy
- select tytul from filmy order by char_length(tytul)
- select tytul, cena from filmy order by rok_produkcji desc limit 3;
- select imie, substr(imie, 1 ,1) as pierwsza, substr (imie, length(imie),1) as ostatnia from klienci
- select imie from klienci where lower(substr(imie, 1,1)) = substr(imie, length(imie),1)
- select tytul from filmy where substr(tytul, (length(tytul)-1),1) = 'o'
- select lower(imie)|| '.' ||lower(nazwisko)|| '@wsb.pl' as email from klienci
- select distinct id_filmu from kopie cross join wypozyczenia where czy_dostepna = 'T' order by id_filmu;
- lab2
- select id_kopii, tytul from kopie natural join filmy;
- select distinct tytul from kopie natural join filmy cross join wypozyczenia where czy_dostepna = 'T' order by tytul;
- select id_kopii from kopie natural join filmy where ROK_PRODUKCJI = 1984;
- select data_wypozyczenia, data_zwrotu, nazwisko from wypozyczenia natural join klienci;
- select nazwisko, tytul from klienci natural join wypozyczenia natural join kopie natural join filmy;
- select tytul, rok_produkcji from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Kowalski';
- select nazwisko from filmy natural join kopie natural join wypozyczenia natural join klienci order by data_wypozyczenia limit 1;
- select nazwisko from aktorzy natural join obsada natural join filmy where tytul = 'Terminator';
- select tytul from aktorzy natural join obsada natural join filmy where nazwisko = 'De Niro';
- select tytul from filmy natural join kopie natural join wypozyczenia order by data_wypozyczenia limit 1;
- select nazwisko from klienci natural join wypozyczenia where data_wypozyczenia >= '2005-07-15' and data_wypozyczenia <= '2005-07-20';
- select distinct tytul from filmy natural join wypozsyczenia natural join kopie where data_wypozyczenia >= '2005-07-15' and data_wypozyczenia <= '2005-07-25' order by tytul;
- select klienci.imie, klienci.nazwisko, aktorzy.nazwisko from klienci join aktorzy on klienci.imie = aktorzy.imie order by klienci.imie;
- lab3
- -----Funkcje grupowe
- select rok_produkcji, avg(cena) from filmy group by rok_produkcji order by rok_produkcji desc;
- select max(cena) from filmy;
- select count(filmy) from filmy where rok_produkcji = '1984';
- select count(filmy) from filmy natural join kopie natural join wypozyczenia where tytul = 'Taksowkarz'
- select round(avg(data_zwrotu - data_wypozyczenia),1) from filmy natural join kopie natural join wypozyczenia where tytul = 'Ronin'
- select tytul, min(data_zwrotu - data_wypozyczenia), max(data_zwrotu - data_wypozyczenia), round(avg(data_zwrotu - data_wypozyczenia),1), count(filmy) from filmy natural join kopie natural join wypozyczenia group by tytul order by count desc
- SELECT tytul, min(data_zwrotu - data_wypozyczenia), max(data_zwrotu - data_wypozyczenia), round(avg(data_zwrotu - data_wypozyczenia),1) as sre, count(filmy) as razy
- FROM filmy natural join kopie natural join wypozyczenia group by tytul
- order by razy desc
- SELECT imie, nazwisko, count(id_kopii) from klienci natural join wypozyczenia group by imie, nazwisko
- select nazwisko, count(id_filmu) from aktorzy natural join obsada group by nazwisko having count(nazwisko) > 1
- select nazwisko, sum(cena)
- from filmy natural join kopie natural join wypozyczenia natural join klienci
- group by nazwisko;
- -----Funckje mnogościowe
- 1.select nazwisko from klienci union select nazwisko from aktorzy order by nazwisko;
- 2.select tytul from filmy natural join obsada natural join aktorzy where nazwisko = 'De Niro' intersect select tytul from filmy natural join obsada natural join aktorzy where nazwisko = 'Reno'
- 3.select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Kowalski'
- intersect
- select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Nowak'
- 4.select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Kowalski'
- except
- select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Nowak'
- 5.select nazwisko from filmy natural join obsada natural join aktorzy where tytul = 'Terminator'
- except
- select nazwisko from filmy natural join obsada natural join aktorzy where tytul = 'Ghostbusters'
- order by nazwisko
- Lab.4
- 1.select tytul from filmy
- where cena = (select max(cena) from filmy)
- 2.select nazwisko from klienci natural join wypozyczenia
- where data_wypozyczenia = (select min(data_wypozyczenia) from wypozyczenia)
- Lab.6
- 1.
- insert into FILMY values (11, 'Komornik', '2005', '10.5');
- select * from filmy order by id_filmu
- 2.
- delete from filmy where rok_produkcji = 2005
- select * from filmy order by id_filmu
- 3.
- update filmy set cena = cena + 0.5 where rok_produkcji <= 1980;
- select * from filmy order by id_filmu;
- 4.
- insert into filmy values (12, 'Nikofor', '2004', '9.5')
- select * from filmy order by id_filmu
- 5.
- delete from filmy where id_filmu not in (select id_filmu from obsada)
- select * from filmy order by id_filmu
- 6.update filmy set cena = 5 where tytul = 'Taksowkarz'
- select * from filmy order by id_filmu
- Lab.7
- 1.
- select column_name, data_type, character_maximum_length chml, numeric_precision np
- from information_schema.columns
- where table_name = 'filmy'
- 2.
- CREATE TABLE KSIAZKI (
- ID_KSIAZKI INTEGER,
- TYTUL VARCHAR(30),
- AUTOR VARCHAR(30),
- ROK_WYDANIA INTEGER )
- 3.
- INSERT INTO KSIAZKI values (1, 'Pan Tadeusz', 'Adam Mickiewicz', 1995)
- INSERT INTO KSIAZKI values (2, 'Krzyzacy', 'Henryk Sienkiewicz', 1990)
- 4.
- select column_name, data_type, character_maximum_length chml, numeric_precision np
- from information_schema.columns
- where table_name='ksiazki'
- 5.
- drop table ksiazki
- 6.
- CREATE TABLE OSOBY (
- PESEL CHAR(11) PRIMARY KEY
- CHECK (length(PESEL) = '11')
- CHECK (substring (PESEL, 1,2)= to_char(DATA_URODZENIA, 'YY')),
- CHECK (substring (PESEL, 3,2)= to_char(DATA_URODZENIA, 'MM')),
- CHECK (substring (PESEL, 5,2)= to_char(DATA_URODZENIA, 'DD')),
- IMIE VARCHAR(15) not null,
- NAZWISKO VARCHAR(15) not null,
- DATA_URODZENIA DATE
- )
- 8.
- I SPOSÓB
- CREATE TABLE FAKTURY (
- NUMER SERIAL PRIMARY KEY,
- PESEL CHAR(11) REFERENCES OSOBY NOT NULL,
- KWOTA NUMERIC(8,2)
- CHECK (KWOTA > 0)
- )
- II SPOSÓB
- CREATE TABLE FAKTURA (
- NUMER SERIAL PRIMARY KEY,
- PESEL CHAR(11),
- KWOTA NUMERIC(8,2),
- CHECK (KWOTA > 0),
- FOREIGN KEY (PESEL) REFERENCES OSOBY(PESEL)
- )
- 9.
- INSERT INTO FAKTURY (PESEL, KWOTA) values ('39090100001', '123.45')
- INSERT INTO FAKTURY (PESEL) values ('75021800123')
- 10.select * from faktury
- 11.select numer, coalesce(kwota, 0) as kwota from faktury
- 12.select numer, pesel from faktury where kwota is null
- 13.delete from osoby where imie = 'Jan'
- --PERSPEKTYWA
- 1.CREATE VIEW STATYSTYKA_KLIENTOW AS
- SELECT imie, nazwisko, count(id_klienta) as l_wyp from klienci natural join wypozyczenia group by imie, nazwisko;
- 2.select * from statystyka_klientow where l_wyp > 2
- 3.drop view statystyka_klientow;
- 4.begin work;
- delete from filmy;
- rollback work;
- 5.
- begin work;
- insert into filmy values (11, 'Komornik', 2005, 10.5)
- savepoint P1;
- delete from filmy;
- savepoint P2;
- delete from kopie;
- select * from filmy;
- select * from kopie;
- rollback work to savepoint P2;
- select * from filmy;
- select * from kopie;
- rollback work to savepoint P1;
- select * from filmy;
- select * from kopie;
- rollback work;
- select * from filmy;
- select * from kopie;
- 6.create user name with password text;
- //Users
- 8.
- create user jacek with password '123';
- create user placek with password '456';
- 9.create table telefony(
- ID_OSOBY SERIAL,
- IMIE VARCHAR(15),
- NUMER_TEL INTEGER
- )
- insert into telefony (imie, numer_tel) values ('Kacper', 788165453)
- insert into telefony (imie, numer_tel) values ('Andrzej', 521463798)
- insert into telefony (imie, numer_tel) values ('Wojtek', 633598765)
- //grant all privileges on table telefony to placek;
- grant select on table telefony to placek;
- grant insert on table telefony to placek;
- insert into telefony (imie, numer_tel) values ('Bartek', 655355455);
- create table telefony (
- ID_OSOBY INTEGER,
- IMIE VARCHAR(15),
- NUMER_TEL INTEGER)
- insert into telefony values (1, 'Adam', 725555333);
- insert into telefony values (2, 'Andrzej', 817666333);
- insert into telefony values (3, 'Wojtek', 805234154);
- grant select, insert on table telefony to placek;
- update telefony set numer_tel = 456312728 where id_osoby = 3;
- revoke insert on table telefony from placek;
- //Indexy
- 7.
- CREATE TABLE PIASEK (
- ID_ZIARENKA SERIAL,
- MASA_ZIARENKA REAL,
- KOLOR_ZIARENKA VARCHAR(10)
- )
- insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.025 , 'szary');
- insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.021 , 'czarny');
- insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.018 , 'bialy');
- insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.031 , 'czarny');
- insert into piasek (id_ziarenka, masa_ziarenka, kolor_ziarenka)
- select piasek.id_ziarenka, piasek.masa_ziarenka, piasek.kolor_ziarenka
- from piasek;
- 17.
- select kolor_ziarenka from piasek where id_ziarenka = 123456;
- 113msec
- 18.
- select count(*) from piasek where id_ziarenka > 500000 and id_ziarenka < 500005;
- 133 msec
- 19.
- select count(*) from piasek where masa_ziarenka = '0.025'
- 166msec
- 20.
- Create index piasek_ind on piasek(id_ziarenka);
- 21.
- select kolor_ziarenka from piasek where id_ziarenka = 123456;
- 16msec
- 22.
- 18msec,
- 23.
- 219msec
- Nie uległ,
- 24.
- drop index piasek_ind
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement