Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. lab1
  2. select * from filmy where rok_produkcji = 1998 or rok_produkcji = 1999;
  3. select * from filmy where cena > 9 order by cena;
  4. select nazwisko from klienci where imie = 'Jan';
  5. select imie, nazwisko from klienci where length(imie) > length(nazwisko);
  6. select nazwisko from aktorzy where imie = 'Arnold' or imie = 'Tom' or imie = 'Jodie' order by nazwisko;
  7. select id_kopii, data_zwrotu - data_wypozyczenia as czas from wypozyczenia where data_zwrotu - data_wypozyczenia > 1;
  8. select id_kopii from wypozyczenia where data_wypozyczenia >= '2005-07-15' and data_wypozyczenia <= '2005-07-22' order by id_kopii;
  9. select substr(imie, 1, 1)|| '. ' ||nazwisko as aktor from aktorzy
  10. select tytul from filmy order by char_length(tytul)
  11. select tytul, cena from filmy order by rok_produkcji desc limit 3;
  12. select imie, substr(imie, 1 ,1) as pierwsza, substr (imie, length(imie),1) as ostatnia from klienci
  13. select imie from klienci where lower(substr(imie, 1,1)) = substr(imie, length(imie),1)
  14. select tytul from filmy where substr(tytul, (length(tytul)-1),1) = 'o'
  15. select lower(imie)|| '.' ||lower(nazwisko)|| '@wsb.pl' as email from klienci
  16. select distinct id_filmu from kopie cross join wypozyczenia where czy_dostepna = 'T' order by id_filmu;
  17.  
  18. lab2
  19. select id_kopii, tytul from kopie natural join filmy;
  20. select distinct tytul from kopie natural join filmy cross join wypozyczenia where czy_dostepna = 'T' order by tytul;
  21. select id_kopii from kopie natural join filmy where ROK_PRODUKCJI = 1984;
  22. select data_wypozyczenia, data_zwrotu, nazwisko from wypozyczenia natural join klienci;
  23. select nazwisko, tytul from klienci natural join wypozyczenia natural join kopie natural join filmy;
  24. select tytul, rok_produkcji from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Kowalski';
  25. select nazwisko from filmy natural join kopie natural join wypozyczenia natural join klienci order by data_wypozyczenia limit 1;
  26. select nazwisko from aktorzy natural join obsada natural join filmy where tytul = 'Terminator';
  27. select tytul from aktorzy natural join obsada natural join filmy where nazwisko = 'De Niro';
  28. select tytul from filmy natural join kopie natural join wypozyczenia order by data_wypozyczenia limit 1;
  29. select nazwisko from klienci natural join wypozyczenia where data_wypozyczenia >= '2005-07-15' and data_wypozyczenia <= '2005-07-20';
  30. 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;
  31. select klienci.imie, klienci.nazwisko, aktorzy.nazwisko from klienci join aktorzy on klienci.imie = aktorzy.imie order by klienci.imie;
  32.  
  33. lab3
  34. -----Funkcje grupowe
  35. select rok_produkcji, avg(cena) from filmy group by rok_produkcji order by rok_produkcji desc;
  36. select max(cena) from filmy;
  37. select count(filmy) from filmy where rok_produkcji = '1984';
  38. select count(filmy) from filmy natural join kopie natural join wypozyczenia where tytul = 'Taksowkarz'
  39. select round(avg(data_zwrotu - data_wypozyczenia),1) from filmy natural join kopie natural join wypozyczenia where tytul = 'Ronin'
  40. 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
  41.  
  42. 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
  43. FROM filmy natural join kopie natural join wypozyczenia group by tytul
  44. order by razy desc
  45.  
  46. SELECT imie, nazwisko, count(id_kopii) from klienci natural join wypozyczenia group by imie, nazwisko
  47.  
  48. select nazwisko, count(id_filmu) from aktorzy natural join obsada group by nazwisko having count(nazwisko) > 1
  49.  
  50. select nazwisko, sum(cena)
  51. from filmy natural join kopie natural join wypozyczenia natural join klienci
  52. group by nazwisko;
  53.  
  54. -----Funckje mnogościowe
  55. 1.select nazwisko from klienci union select nazwisko from aktorzy order by nazwisko;
  56.  
  57. 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'
  58.  
  59. 3.select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Kowalski'
  60. intersect
  61. select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Nowak'
  62.  
  63. 4.select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Kowalski'
  64. except
  65. select tytul from filmy natural join kopie natural join wypozyczenia natural join klienci where nazwisko = 'Nowak'
  66.  
  67. 5.select nazwisko from filmy natural join obsada natural join aktorzy where tytul = 'Terminator'
  68. except
  69. select nazwisko from filmy natural join obsada natural join aktorzy where tytul = 'Ghostbusters'
  70. order by nazwisko
  71.  
  72. Lab.4
  73. 1.select tytul from filmy
  74. where cena = (select max(cena) from filmy)
  75.  
  76. 2.select nazwisko from klienci natural join wypozyczenia
  77. where data_wypozyczenia = (select min(data_wypozyczenia) from wypozyczenia)
  78.  
  79.  
  80.  
  81. Lab.6
  82. 1.
  83. insert into FILMY values (11, 'Komornik', '2005', '10.5');
  84. select * from filmy order by id_filmu
  85.  
  86. 2.
  87. delete from filmy where rok_produkcji = 2005
  88. select * from filmy order by id_filmu
  89.  
  90. 3.
  91. update filmy set cena = cena + 0.5 where rok_produkcji <= 1980;
  92. select * from filmy order by id_filmu;
  93.  
  94. 4.
  95. insert into filmy values (12, 'Nikofor', '2004', '9.5')
  96. select * from filmy order by id_filmu
  97.  
  98. 5.
  99. delete from filmy where id_filmu not in (select id_filmu from obsada)
  100. select * from filmy order by id_filmu
  101.  
  102. 6.update filmy set cena = 5 where tytul = 'Taksowkarz'
  103. select * from filmy order by id_filmu
  104.  
  105.  
  106. Lab.7
  107. 1.
  108. select column_name, data_type, character_maximum_length chml, numeric_precision np
  109. from information_schema.columns
  110. where table_name = 'filmy'
  111.  
  112. 2.
  113. CREATE TABLE KSIAZKI (
  114. ID_KSIAZKI INTEGER,
  115. TYTUL VARCHAR(30),
  116. AUTOR VARCHAR(30),
  117. ROK_WYDANIA INTEGER )
  118.  
  119. 3.
  120. INSERT INTO KSIAZKI values (1, 'Pan Tadeusz', 'Adam Mickiewicz', 1995)
  121. INSERT INTO KSIAZKI values (2, 'Krzyzacy', 'Henryk Sienkiewicz', 1990)
  122.  
  123. 4.
  124. select column_name, data_type, character_maximum_length chml, numeric_precision np
  125. from information_schema.columns
  126. where table_name='ksiazki'
  127.  
  128. 5.
  129. drop table ksiazki
  130.  
  131. 6.
  132. CREATE TABLE OSOBY (
  133. PESEL CHAR(11) PRIMARY KEY
  134. CHECK (length(PESEL) = '11')
  135. CHECK (substring (PESEL, 1,2)= to_char(DATA_URODZENIA, 'YY')),
  136. CHECK (substring (PESEL, 3,2)= to_char(DATA_URODZENIA, 'MM')),
  137. CHECK (substring (PESEL, 5,2)= to_char(DATA_URODZENIA, 'DD')),
  138. IMIE VARCHAR(15) not null,
  139. NAZWISKO VARCHAR(15) not null,
  140. DATA_URODZENIA DATE
  141. )
  142.  
  143. 8.
  144. I SPOSÓB
  145. CREATE TABLE FAKTURY (
  146. NUMER SERIAL PRIMARY KEY,
  147. PESEL CHAR(11) REFERENCES OSOBY NOT NULL,
  148. KWOTA NUMERIC(8,2)
  149. CHECK (KWOTA > 0)
  150. )
  151.  
  152. II SPOSÓB
  153. CREATE TABLE FAKTURA (
  154. NUMER SERIAL PRIMARY KEY,
  155. PESEL CHAR(11),
  156. KWOTA NUMERIC(8,2),
  157. CHECK (KWOTA > 0),
  158. FOREIGN KEY (PESEL) REFERENCES OSOBY(PESEL)
  159. )
  160.  
  161. 9.
  162. INSERT INTO FAKTURY (PESEL, KWOTA) values ('39090100001', '123.45')
  163. INSERT INTO FAKTURY (PESEL) values ('75021800123')
  164.  
  165. 10.select * from faktury
  166.  
  167. 11.select numer, coalesce(kwota, 0) as kwota from faktury
  168.  
  169. 12.select numer, pesel from faktury where kwota is null
  170.  
  171. 13.delete from osoby where imie = 'Jan'
  172.  
  173. --PERSPEKTYWA
  174. 1.CREATE VIEW STATYSTYKA_KLIENTOW AS
  175. SELECT imie, nazwisko, count(id_klienta) as l_wyp from klienci natural join wypozyczenia group by imie, nazwisko;
  176.  
  177. 2.select * from statystyka_klientow where l_wyp > 2
  178.  
  179. 3.drop view statystyka_klientow;
  180.  
  181. 4.begin work;
  182. delete from filmy;
  183. rollback work;
  184.  
  185. 5.
  186. begin work;
  187. insert into filmy values (11, 'Komornik', 2005, 10.5)
  188. savepoint P1;
  189. delete from filmy;
  190. savepoint P2;
  191. delete from kopie;
  192. select * from filmy;
  193. select * from kopie;
  194. rollback work to savepoint P2;
  195. select * from filmy;
  196. select * from kopie;
  197. rollback work to savepoint P1;
  198. select * from filmy;
  199. select * from kopie;
  200. rollback work;
  201. select * from filmy;
  202. select * from kopie;
  203.  
  204. 6.create user name with password text;
  205.  
  206. //Users
  207. 8.
  208. create user jacek with password '123';
  209. create user placek with password '456';
  210. 9.create table telefony(
  211. ID_OSOBY SERIAL,
  212. IMIE VARCHAR(15),
  213. NUMER_TEL INTEGER
  214. )
  215. insert into telefony (imie, numer_tel) values ('Kacper', 788165453)
  216. insert into telefony (imie, numer_tel) values ('Andrzej', 521463798)
  217. insert into telefony (imie, numer_tel) values ('Wojtek', 633598765)
  218.  
  219. //grant all privileges on table telefony to placek;
  220. grant select on table telefony to placek;
  221. grant insert on table telefony to placek;
  222.  
  223. insert into telefony (imie, numer_tel) values ('Bartek', 655355455);
  224.  
  225.  
  226. create table telefony (
  227. ID_OSOBY INTEGER,
  228. IMIE VARCHAR(15),
  229. NUMER_TEL INTEGER)
  230.  
  231. insert into telefony values (1, 'Adam', 725555333);
  232. insert into telefony values (2, 'Andrzej', 817666333);
  233. insert into telefony values (3, 'Wojtek', 805234154);
  234.  
  235. grant select, insert on table telefony to placek;
  236.  
  237. update telefony set numer_tel = 456312728 where id_osoby = 3;
  238.  
  239. revoke insert on table telefony from placek;
  240.  
  241. //Indexy
  242.  
  243. 7.
  244. CREATE TABLE PIASEK (
  245. ID_ZIARENKA SERIAL,
  246. MASA_ZIARENKA REAL,
  247. KOLOR_ZIARENKA VARCHAR(10)
  248. )
  249.  
  250. insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.025 , 'szary');
  251. insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.021 , 'czarny');
  252. insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.018 , 'bialy');
  253. insert into piasek (masa_ziarenka, kolor_ziarenka) values (0.031 , 'czarny');
  254.  
  255. insert into piasek (id_ziarenka, masa_ziarenka, kolor_ziarenka)
  256. select piasek.id_ziarenka, piasek.masa_ziarenka, piasek.kolor_ziarenka
  257. from piasek;
  258.  
  259. 17.
  260. select kolor_ziarenka from piasek where id_ziarenka = 123456;
  261. 113msec
  262.  
  263. 18.
  264. select count(*) from piasek where id_ziarenka > 500000 and id_ziarenka < 500005;
  265. 133 msec
  266. 19.
  267. select count(*) from piasek where masa_ziarenka = '0.025'
  268. 166msec
  269. 20.
  270. Create index piasek_ind on piasek(id_ziarenka);
  271. 21.
  272. select kolor_ziarenka from piasek where id_ziarenka = 123456;
  273. 16msec
  274.  
  275. 22.
  276. 18msec,
  277.  
  278. 23.
  279. 219msec
  280. Nie uległ,
  281.  
  282. 24.
  283. drop index piasek_ind
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement