Advertisement
dlozic

PL/SQL Baza sahovski turniri

Dec 26th, 2021 (edited)
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 8.64 KB | None | 0 0
  1. SET SERVEROUTPUT ON;
  2.  
  3. BEGIN
  4.    FOR cur_rec IN (SELECT object_name, object_type
  5.                      FROM user_objects
  6.                     WHERE object_type IN
  7.                              ('TABLE',
  8.                               'VIEW',
  9.                               'PACKAGE',
  10.                               'PROCEDURE',
  11.                               'FUNCTION',
  12.                               'SEQUENCE',
  13.                               'SYNONYM',
  14.                               'PACKAGE BODY'
  15.                              ))
  16.    LOOP
  17.       BEGIN
  18.          IF cur_rec.object_type = 'TABLE'
  19.          THEN
  20.             EXECUTE IMMEDIATE    'DROP '
  21.                               || cur_rec.object_type
  22.                               || ' "'
  23.                               || cur_rec.object_name
  24.                               || '" CASCADE CONSTRAINTS';
  25.          ELSE
  26.             EXECUTE IMMEDIATE    'DROP '
  27.                               || cur_rec.object_type
  28.                               || ' "'
  29.                               || cur_rec.object_name
  30.                               || '"';
  31.          END IF;
  32.       EXCEPTION
  33.          WHEN OTHERS
  34.          THEN
  35.             DBMS_OUTPUT.put_line (   'FAILED: DROP '
  36.                                   || cur_rec.object_type
  37.                                   || ' "'
  38.                                   || cur_rec.object_name
  39.                                   || '"'
  40.                                  );
  41.       END;
  42.    END LOOP;
  43. END;
  44.  
  45. /
  46.  
  47. CREATE TABLE kontinenti (
  48.     id INT PRIMARY KEY,
  49.     naziv VARCHAR(50) UNIQUE,
  50.     broj_stanovnika INT DEFAULT 0
  51. );
  52.  
  53. INSERT INTO kontinenti VALUES (1, 'Europa', 747636026);
  54. INSERT INTO kontinenti VALUES (2, 'Azija', 4641054775);
  55. INSERT INTO kontinenti VALUES (3, 'Sjeverna Amerika', NULL);
  56. INSERT INTO kontinenti VALUES (4, 'Južna Amerika', NULL);
  57. INSERT INTO kontinenti VALUES (5, 'Australija', 0);
  58.  
  59. /
  60.  
  61. CREATE TABLE zemlje (
  62.     id INT PRIMARY KEY,
  63.     naziv VARCHAR(50) NOT NULL,
  64.     broj_stanovnika INT,
  65.     kontinent_id INT,
  66.  
  67.     CONSTRAINT fk_zemlje_kontinent_id
  68.     FOREIGN KEY (kontinent_id)
  69.     REFERENCES kontinenti(id)
  70.     ON DELETE CASCADE
  71. );
  72.  
  73. INSERT INTO zemlje VALUES (1, 'Hrvatska', 4000000, 1);
  74. INSERT INTO zemlje VALUES (2, 'Njemačka', 82000000, 1);
  75. INSERT INTO zemlje VALUES (3, 'Kina', 1386000000, 2);
  76. INSERT INTO zemlje VALUES (4, 'Indija', 1339000000, 2);
  77. INSERT INTO zemlje VALUES (5, 'USA', 327000000, 3);
  78. INSERT INTO zemlje VALUES (6, 'Kanada', 38000000, 3);
  79. INSERT INTO zemlje VALUES (7, 'Afganistan', NULL, NULL);
  80. INSERT INTO zemlje VALUES (8, 'Čile', 18000000, 4);
  81.  
  82. CREATE TABLE gradovi (
  83.     id INT PRIMARY KEY,
  84.     naziv VARCHAR(50) NOT NULL,
  85.     zemlja_id INT,
  86.  
  87.     CONSTRAINT fk_zemlje_zemlja_id
  88.     FOREIGN KEY (zemlja_id)
  89.     REFERENCES zemlje(id)
  90.     ON DELETE CASCADE
  91. );
  92.  
  93. INSERT INTO gradovi VALUES (1, 'Zagreb', 1);
  94. INSERT INTO gradovi VALUES (2, 'Osijek', 1);
  95. INSERT INTO gradovi VALUES (3, 'Berlin', 2);
  96. INSERT INTO gradovi VALUES (4, 'Hamburg', 2);
  97. INSERT INTO gradovi VALUES (5, 'Peking', 3);
  98. INSERT INTO gradovi VALUES (6, 'Mumbai', 4);
  99. INSERT INTO gradovi VALUES (7, 'Washington', 5);
  100. INSERT INTO gradovi VALUES (8, 'Toronto', 6);
  101.  
  102. CREATE TABLE turniri (
  103.     id INT PRIMARY KEY,
  104.     naziv VARCHAR(50) NOT NULL,
  105.     datum_pocetka DATE,
  106.     datum_zavrsetka DATE
  107. );
  108.  
  109. INSERT INTO turniri VALUES (1, 'Šahovski turnir Zagreb', DATE '2018-05-05', DATE '2018-05-10');
  110. INSERT INTO turniri VALUES (2, 'Šahovski turnir Osijek', DATE '2019-01-01', DATE '2019-01-08');
  111. INSERT INTO turniri VALUES (3, 'Chess tournament Berlin', DATE '2020-01-05', NULL);
  112. INSERT INTO turniri VALUES (4, 'Chess tournament Hamburg', DATE '2020-01-03', DATE '2020-01-15');
  113. INSERT INTO turniri VALUES (5, 'Chess tournament Peking', DATE '2015-07-01', DATE '2015-07-18');
  114. INSERT INTO turniri VALUES (6, 'Šahovski turnir Zagreb', DATE '2019-07-03', DATE '2019-07-06');
  115. INSERT INTO turniri VALUES (7, 'Chess tournament Washington', NULL, NULL);
  116. INSERT INTO turniri VALUES (8, 'Chess tournament Washington', DATE '2019-09-09', DATE '2019-09-17');
  117.  
  118. CREATE TABLE igraci (
  119.     id INT PRIMARY KEY,
  120.     ime VARCHAR(30),
  121.     prezime VARCHAR(30),
  122.     datum_rodenja DATE,
  123.     datum_prvog_igranja DATE,
  124.     prebivaliste_id INT,
  125.     boraviste_id INT,
  126.  
  127.     CONSTRAINT fk_igraci_boraviste_id
  128.     FOREIGN KEY (boraviste_id)
  129.     REFERENCES gradovi(id)
  130.     ON DELETE CASCADE,
  131.  
  132.     CONSTRAINT fk_igraci_prebivaliste_id
  133.     FOREIGN KEY (prebivaliste_id)
  134.     REFERENCES gradovi(id)
  135.     ON DELETE CASCADE
  136. );
  137.  
  138. INSERT INTO igraci VALUES (1, 'Marko', 'Marić', DATE '1990-01-01', NULL, 1, 2);
  139. INSERT INTO igraci VALUES (2, 'Ivana', 'Ivanić', DATE '1989-01-01', NULL, 2, 1);
  140. INSERT INTO igraci VALUES (3, 'Petra', 'Petrić', DATE '1996-01-01', DATE '2015-01-01', 1, 1);
  141. INSERT INTO igraci VALUES (4, 'Johan', 'Muller', DATE '1998-01-01', DATE '2014-01-01', 3, 4);
  142. INSERT INTO igraci VALUES (5, 'Heinrich', 'Lide', DATE '1990-05-05', DATE '2011-01-01', 3, 3);
  143. INSERT INTO igraci VALUES (6, 'Lee', 'Moon', DATE '1979-10-05', NULL, 5, NULL);
  144. INSERT INTO igraci VALUES (7, 'Anu', 'Barwaktar', DATE '1983-11-20', DATE '1999-01-01', 6, 6);
  145. INSERT INTO igraci VALUES (8, 'Igor', 'Cheese', DATE '2008-11-20', DATE '2011-01-01', 6, 5);
  146.  
  147. CREATE TABLE igre (
  148.     id INT PRIMARY KEY,
  149.     turnir_id INT,
  150.     igrac1_id INT,
  151.     igrac2_id INT,
  152.  
  153.     CONSTRAINT fk_igre_turnir_id
  154.     FOREIGN KEY (turnir_id)
  155.     REFERENCES turniri(id)
  156.     ON DELETE CASCADE,
  157.  
  158.     CONSTRAINT fk_igre_igrac1_id
  159.     FOREIGN KEY (igrac1_id)
  160.     REFERENCES igraci(id)
  161.     ON DELETE CASCADE,
  162.  
  163.     CONSTRAINT fk_igre_igrac2_id
  164.     FOREIGN KEY (igrac2_id)
  165.     REFERENCES igraci(id)
  166.     ON DELETE CASCADE
  167. );
  168.  
  169. INSERT INTO igre VALUES (1, 1, 1, 2);
  170. INSERT INTO igre VALUES (2, 1, 2, 3);
  171. INSERT INTO igre VALUES (3, 2, 3, 4);
  172. INSERT INTO igre VALUES (4, 2, 4, 5);
  173. INSERT INTO igre VALUES (5, 2, 5, 6);
  174. INSERT INTO igre VALUES (6, 2, 1, 2);
  175. INSERT INTO igre VALUES (7, 3, 2, 6);
  176. INSERT INTO igre VALUES (8, 3, 7, 6);
  177. INSERT INTO igre VALUES (9, 3, 5, 1);
  178. INSERT INTO igre VALUES (10, 4, 1, 2);
  179. INSERT INTO igre VALUES (11, 4, 3, 4);
  180. INSERT INTO igre VALUES (12, 4, 5, 6);
  181. INSERT INTO igre VALUES (13, 4, 7, 5);
  182. INSERT INTO igre VALUES (14, 4, 5, 1);
  183. INSERT INTO igre VALUES (15, 4, 5, 2);
  184. INSERT INTO igre VALUES (16, 5, 1, 3);
  185. INSERT INTO igre VALUES (17, 5, 5, 4);
  186. INSERT INTO igre VALUES (18, 5, 4, 6);
  187. INSERT INTO igre VALUES (19, 6, 2, 5);
  188. INSERT INTO igre VALUES (20, 6, 6, 1);
  189.  
  190. CREATE TABLE sponzori (
  191.     id INT PRIMARY KEY,
  192.     naziv VARCHAR(255)
  193. );
  194.  
  195. INSERT INTO sponzori VALUES (1, 'Kraš');
  196. INSERT INTO sponzori VALUES (2, 'HGK');
  197. INSERT INTO sponzori VALUES (3, 'Ured za promicanje znanosti');
  198. INSERT INTO sponzori VALUES (4, 'Konzulat');
  199. INSERT INTO sponzori VALUES (5, 'Sponzor 5');
  200. INSERT INTO sponzori VALUES (6, 'Sponzor 6');
  201. INSERT INTO sponzori VALUES (7, 'Sponzor 7');
  202. INSERT INTO sponzori VALUES (8, 'Sponzor 8');
  203.  
  204. CREATE TABLE sponzorstva (
  205.     id INT PRIMARY KEY,
  206.     turnir_id INT,
  207.     sponzor_id INT,
  208.  
  209.     CONSTRAINT fk_sponzorstva_turnir_id
  210.     FOREIGN KEY (turnir_id)
  211.     REFERENCES turniri(id)
  212.     ON DELETE CASCADE,
  213.  
  214.     CONSTRAINT fk_sponzorstva_sponzor_id
  215.     FOREIGN KEY (sponzor_id)
  216.     REFERENCES sponzori(id)
  217.     ON DELETE CASCADE
  218. );
  219.  
  220. INSERT INTO sponzorstva VALUES (1, 1, 1);
  221. INSERT INTO sponzorstva VALUES (2, 1, 2);
  222. INSERT INTO sponzorstva VALUES (3, 1, 3);
  223. INSERT INTO sponzorstva VALUES (4, 2, 3);
  224. INSERT INTO sponzorstva VALUES (5, 2, 4);
  225. INSERT INTO sponzorstva VALUES (6, 3, 1);
  226. INSERT INTO sponzorstva VALUES (7, 4, 5);
  227. INSERT INTO sponzorstva VALUES (8, 5, 5);
  228. INSERT INTO sponzorstva VALUES (9, 5, 6);
  229. INSERT INTO sponzorstva VALUES (10, 6, 7);
  230.  
  231. CREATE TABLE rang_liste (
  232.     id INT PRIMARY KEY,
  233.     turnir_id INT NOT NULL,
  234.     igrac_id INT NOT NULL,
  235.     rang_mjesto NUMBER(7, 0) NOT NULL,
  236.  
  237.     CONSTRAINT fk_rl_turnir_id
  238.     FOREIGN KEY (turnir_id)
  239.     REFERENCES turniri(id)
  240.     ON DELETE CASCADE,
  241.  
  242.     CONSTRAINT fk_rl_igrac_id
  243.     FOREIGN KEY (igrac_id)
  244.     REFERENCES igraci(id)
  245.     ON DELETE CASCADE
  246. );
  247.  
  248. INSERT INTO rang_liste VALUES (1, 1, 1, 1);
  249. INSERT INTO rang_liste VALUES (2, 1, 2, 2);
  250. INSERT INTO rang_liste VALUES (3, 1, 3, 3);
  251. INSERT INTO rang_liste VALUES (4, 1, 4, 4);
  252. INSERT INTO rang_liste VALUES (5, 2, 2, 1);
  253. INSERT INTO rang_liste VALUES (6, 2, 3, 2);
  254. INSERT INTO rang_liste VALUES (7, 2, 4, 3);
  255. INSERT INTO rang_liste VALUES (8, 2, 5, 4);
  256. INSERT INTO rang_liste VALUES (9, 2, 6, 5);
  257. INSERT INTO rang_liste VALUES (10, 3, 1, 1);
  258. INSERT INTO rang_liste VALUES (11, 3, 2, 2);
  259. INSERT INTO rang_liste VALUES (12, 4, 1, 1);
  260. INSERT INTO rang_liste VALUES (13, 4, 2, 2);
  261. INSERT INTO rang_liste VALUES (14, 5, 3, 1);
  262.  
  263. COMMIT;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement