Advertisement
alexarcan

lab2_dbd(done)

Oct 10th, 2016
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. DROP table STUDENT;
  2. DROP table GRADES;
  3. DROP table COURSE;
  4.  
  5. CREATE table STUDENT
  6. (
  7. CODS int primary key,
  8. name varchar2(20),
  9. birthdate date
  10. );
  11.  
  12.  
  13. CREATE table COURSE
  14. (
  15. CODC int primary key,
  16. name varchar2(20),
  17. professor varchar2(20)
  18. );
  19.  
  20. CREATE table GRADES
  21. (
  22. CODS int,
  23. CODC int,
  24. CONSTRAINT fk_cods FOREIGN KEY(CODS) REFERENCES STUDENT(CODS),
  25. CONSTRAINT fk_codc FOREIGN KEY(CODC) REFERENCES COURSE(CODC),
  26. grade FLOAT,
  27. prezentare int
  28. );
  29.  
  30. INSERT INTO STUDENT(CODS, name, birthdate)
  31. VALUES(1,'John Doe', TO_DATE('2003/05/03', 'yyyy/mm/dd'));
  32.  
  33. INSERT INTO STUDENT(CODS, name, birthdate)
  34. VALUES(2,'Alice Doe', TO_DATE('2004/05/03', 'yyyy/mm/dd'));
  35.  
  36. INSERT INTO STUDENT(CODS, name, birthdate)
  37. VALUES(3,'Jim Carrey', TO_DATE('1998/05/03', 'yyyy/mm/dd'));
  38.  
  39.  
  40. INSERT INTO STUDENT(CODS, name, birthdate)
  41. VALUES(4,'Jimmy Stoya', TO_DATE('1995/06/03', 'yyyy/mm/dd'));
  42.  
  43. INSERT INTO COURSE(CODC, name, professor)
  44. VALUES(1,'Assembly','Ron Wisley');
  45.  
  46. INSERT INTO COURSE(CODC, name, professor)
  47. VALUES(2,'Web Development','Johnny Wonka');
  48.  
  49. INSERT INTO COURSE(CODC, name, professor)
  50. VALUES(3,'Databases','Richard Long');
  51.  
  52. INSERT INTO GRADES(CODC, CODS, grade, prezentare)
  53. VALUES(1,1,10,1);
  54.  
  55. INSERT INTO GRADES(CODC, CODS, grade, prezentare)
  56. VALUES(1,2,9,1);
  57.  
  58. INSERT INTO GRADES(CODC, CODS, grade, prezentare)
  59. VALUES(2,1,9,1);
  60.  
  61. INSERT INTO GRADES(CODC, CODS, grade, prezentare)
  62. VALUES(2,3,4,3);
  63.  
  64. INSERT INTO GRADES(CODC, CODS, grade, prezentare)
  65. VALUES(1,4,3,3);
  66.  
  67. --Extract a table containing the following information:
  68. --STUDENT_NAME, COURSE_NAME, GRADE
  69.  
  70. DROP VIEW STUDENTS_VIEW;
  71. CREATE VIEW STUDENTS_VIEW(STUDENT, COURSE, GRADE) AS
  72. SELECT DISTINCT S.NAME NUME_STUDENT, C.NAME NUME_CURS, G.GRADE GRADE
  73. FROM GRADES G, STUDENT S, COURSE C
  74. WHERE G.CODC=C.CODC AND G.CODS=S.CODS;
  75. SELECT * FROM STUDENTS_VIEW;
  76.  
  77.  
  78. --Extract a table containing all students which failed at certain courses and the
  79. --corresponfing course names.
  80.  
  81. DROP VIEW FAILED_VIEW;
  82. CREATE VIEW FAILED_VIEW(STUDENT, COURSE, GRADE) AS
  83. SELECT DISTINCT S.NAME NUME_STUDENT, C.NAME NUME_CURS, G.GRADE GRADE
  84. FROM GRADES G, STUDENT S, COURSE C
  85. WHERE G.CODS=S.CODS AND
  86. G.CODC=C.CODC AND
  87. GRADE < 5 AND
  88. G.PREZENTARE >= 3;
  89. SELECT * FROM FAILED_VIEW;
  90.  
  91.  
  92. --Count all the students with "failed" courses
  93.  
  94. SELECT COUNT(*) failed_students FROM FAILED_VIEW;
  95.  
  96.  
  97.  
  98. --Extract a table with all the students signed for (received at least a grade for
  99.  
  100. --the exam corresponfing to that course in our case) a certain proffesor's course
  101.  
  102. --(specified through the PROFFESOR field).
  103.  
  104. CREATE VIEW SIGNED_STUDENTS(STUDENT, COURSE, PROFESSOR, GRADE)
  105. AS SELECT S.NAME, C.NAME, C.PROFESSOR, G.GRADE
  106. FROM GRADES G, STUDENT S, COURSE C
  107. WHERE G.CODS=S.CODS AND G.CODC=C.CODC
  108. GROUP BY C.PROFESSOR, C.NAME, S.NAME, G.GRADE;
  109.  
  110. SELECT DISTINCT P.STUDENT
  111. FROM SIGNED_STUDENTS P
  112. WHERE P.PROFESSOR='Ron Wisley';
  113.  
  114.  
  115. -- Update the GRADES table by promoting all the students not receiving passing marks (grade < 5)
  116. UPDATE GRADES
  117. SET grade = 8
  118. WHERE grade < 5;
  119.  
  120.  
  121. --Extract a Student top based on their marks average (all marks, even if a student
  122. --received multiple marks for a certain course).
  123. CREATE VIEW STUDENTS_TOP_MARK
  124. AS SELECT S.NAME, MAX(G.GRADE) HIGHEST_MARK, C.CODC
  125. FROM student S, grades G, course C
  126. WHERE S.CODS=G.CODS and G.CODC=C.CODC GROUP BY S.NAME, C.CODC;
  127. SELECT * FROM STUDENTS_TOP_MARK;
  128.  
  129. CREATE VIEW STUDENT_AVERAGE(STUDENT, GRADE)
  130. AS SELECT b.NAME STUDENT, AVG(b.HIGHEST_MARK)
  131. FROM STUDENTS_TOP_MARK b
  132. GROUP BY b.NAME ORDER BY AVG(b.HIGHEST_MARK);
  133. SELECT * FROM STUDENT_AVERAGE WHERE GRADE IS NOT NULL ORDER BY GRADE DESC;
  134.  
  135. --Similar to h. - but you have to extract only the students without failed exams
  136. --and with an average grade above 8.5
  137. SELECT * FROM STUDENT_AVERAGE
  138. WHERE STUDENT NOT IN
  139. (SELECT STUDENT FROM FAILED_VIEW)
  140. AND GRADE >= 8.5;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement