Advertisement
MagnusArias

DB2 | 4

Mar 26th, 2018
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 2.74 KB | None | 0 0
  1. -- <ScriptOptions statementTerminator=";" />
  2. CREATE TABLE PON.OCENY (
  3.     ID_OCENY INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
  4.     ID_STUDENTA INTEGER NOT NULL,
  5.     OCENA DB2ADMIN.OCENA NOT NULL,
  6.     PRIMARY KEY ( ID_OCENY )
  7.    );
  8.    
  9.    ALTER TABLE PON.STUDENCI ADD PRIMARY KEY (ID_STUDENTA);
  10.    ALTER TABLE PON.OCENY ADD CONSTRAINT FK_OCENY_STUD FOREIGN KEY (ID_STUDENTA) REFERENCES PON.STUDENCI(ID_STUDENTA);
  11.    
  12.    ALTER TABLE PON.OCENY ADD CONSTRAINT SPR_OCENE CHECK(CAST(OCENA AS DECIMAL(5,1)) BETWEEN 2 AND 5);
  13. ---ALTER TABLE APN.OCENY ADD CONSTRAINT SPR_OCENY CHECK(CAST(OCENA AS DECIMAL(5,1)) IN (2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0));
  14.    
  15.    INSERT INTO PON.STUDENCI(NAZWISKO) VALUES ('Kowalski');
  16.    INSERT INTO PON.STUDENCI(NAZWISKO) VALUES ('Nowak');
  17.    INSERT INTO PON.STUDENCI(NAZWISKO) VALUES ('Dgbioc');
  18.    
  19. SELECT * FROM PON.STUDENCI;
  20. UPDATE PON.STUDENCI SET IMIE='Jan';
  21.  
  22. INSERT INTO PON.OCENY (ID_STUDENTA, OCENA) VALUES(3, 5.0);
  23. INSERT INTO PON.OCENY (ID_STUDENTA, OCENA) VALUES(2, 4.0);
  24. INSERT INTO PON.OCENY (ID_STUDENTA, OCENA) VALUES(1, 3.0);
  25.  
  26. CREATE VIEW STUD_OCENA AS
  27. SELECT S.NAZWISKO, S.IMIE, AVG(O.OCENA) FROM PON.STUDENCI s INNER JOIN PON.OCENY O
  28. ON O.ID_STUDENTA=s.ID_STUDENTA;
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. TO samo ale po raz drugi XDDD
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. -- <ScriptOptions statementTerminator=";" />
  45. ALTER TABLE APN.OCENY ADD CONSTRAINT FK_OCENY_STUD
  46. FOREIGN KEY (ID_STUDENTA) REFERENCES APN.STUDENCI(ID_STUDENTA);
  47.  
  48. ALTER TABLE APN.OCENY DROP CONSTRAINT SPR_OCENY;
  49. ALTER TABLE APN.OCENY ADD CONSTRAINT SPR_OCENY
  50. CHECK(CAST(OCENA AS DECIMAL(5,1)) IN (2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0));
  51.  
  52. INSERT INTO APN.STUDENCI(NAZWISKO, IMIE) VALUES ('KOWALSKI', 'JAN');
  53. INSERT INTO APN.STUDENCI(NAZWISKO, IMIE) VALUES ('MARAS', 'SARAM');
  54.  
  55. INSERT INTO APN.OCENY (ID_STUDENTA, OCENA) VALUES (1, 2.0);
  56. INSERT INTO APN.OCENY (ID_STUDENTA, OCENA) VALUES (1, 4.5);
  57. INSERT INTO APN.OCENY (ID_STUDENTA, OCENA) VALUES (1, 3.5);
  58.  
  59. INSERT INTO APN.OCENY (ID_STUDENTA, OCENA) VALUES (2, 3.0);
  60. INSERT INTO APN.OCENY (ID_STUDENTA, OCENA) VALUES (2, 5.0);
  61. INSERT INTO APN.OCENY (ID_STUDENTA, OCENA) VALUES (2, 5.0);
  62.  
  63. SELECT S.NAZWISKO, O.OCENA FROM APN.STUDENCI S INNER JOIN APN.OCENY O
  64. ON S.ID_STUDENTA = O.ID_STUDENTA;
  65.  
  66. -- ZA POMOCĄ WIDOKU OBLICZYC SREDNIA OCENE DLA STUDENTA
  67. SELECT S.NAZWISKO, AVG(CAST(O.OCENA AS DECIMAL(5,1)))
  68. FROM APN.STUDENCI S
  69. INNER JOIN APN.OCENY O
  70. ON S.ID_STUDENTA = O.ID_STUDENTA
  71. GROUP BY S.NAZWISKO;
  72.  
  73. CREATE OR REPLACE VIEW SREDNIE AS
  74. SELECT S.NAZWISKO,
  75.     CAST
  76.     (
  77.         ROUND
  78.         (
  79.             AVG
  80.             (
  81.                 CAST
  82.                 (
  83.                     O.OCENA AS DECIMAL(5,1)
  84.                 )
  85.             )*2, 0
  86.         )/2 AS DECIMAL(5,1)
  87.     ) AS SREDNIA
  88. FROM APN.STUDENCI S
  89. INNER JOIN APN.OCENY O
  90. ON S.ID_STUDENTA = O.ID_STUDENTA
  91. GROUP BY S.NAZWISKO;
  92.  
  93. SELECT * FROM SREDNIE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement