Advertisement
Hasan1026

dbtask1

Sep 10th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. 1)
  2. Single:
  3. CREATE VIEW high_credit_students AS
  4. SELECT
  5. ID,
  6. name,
  7. dept_name,
  8. tot_cred
  9. FROM
  10. student
  11. WHERE
  12. tot_cred > 50;
  13.  
  14.  
  15.  
  16. Multiple:
  17. CREATE VIEW student_course_instructor AS
  18. SELECT
  19. s.name AS student_name,
  20. c.title AS course_title,
  21. i.name AS instructor_name
  22. FROM student s
  23. JOIN takes tk ON s.ID = tk.ID
  24. JOIN course c ON tk.course_id = c.course_id
  25. JOIN teaches te ON tk.course_id = te.course_id
  26. JOIN instructor i ON te.ID = i.ID;
  27.  
  28. 2)
  29. With Condition:
  30. SELECT *
  31. FROM student_course_instructor
  32. WHERE instructor_name = 'Katz';
  33.  
  34. student_name course_title instructor_name
  35. Zhang Intro. to Computer Science Katz
  36. Shankar Intro. to Computer Science Katz
  37. Levy Intro. to Computer Science Katz
  38. Levy Intro. to Computer Science Katz
  39. Levy Image Processing Katz
  40. Williams Intro. to Computer Science Katz
  41. Brown Intro. to Computer Science Katz
  42. Brown Image Processing Katz
  43. Bourikas Intro. to Computer Science Katz
  44.  
  45.  
  46. With aggregrate function:
  47. SELECT student_name, COUNT(course_title) AS num_courses
  48. FROM student_course_instructor
  49. GROUP BY student_name;
  50.  
  51. student_name num_courses
  52. Aoi 1
  53. Bourikas 3
  54. Brandt 1
  55. Brown 4
  56. Chavez 1
  57. Levy 6
  58. Peltier 1
  59. Sanchez 1
  60. Shankar 6
  61. Tanaka 2
  62. Williams 4
  63. Zhang 3
  64.  
  65.  
  66. 3)
  67. INSERT INTO student_course_instructor (student_name, course_title, instructor_name)
  68. VALUES ('Michael Scott', 'Management 101', 'David Wallace');
  69. Uncaught Error: cannot modify student_course_instructor because it is a view
  70.  
  71. UPDATE student_course_instructor
  72. SET instructor_name = 'Jan Levinson'
  73. WHERE course_title = 'Management 101' AND instructor_name = 'David Wallace';
  74. Uncaught Error: cannot modify student_course_instructor because it is a view
  75.  
  76. DELETE FROM student_course_instructor
  77. WHERE student_name = 'Michael Scott';
  78. Uncaught Error: cannot modify student_course_instructor because it is a view
  79.  
  80. 4)
  81. INSERT INTO student (ID, name) VALUES ('1001', 'Michael Scott');
  82. INSERT INTO course (course_id, title, dept_name, credits) VALUES ('MGT101', 'Management 101', 'Business', 3);
  83. INSERT INTO instructor (ID, name, dept_name, salary) VALUES ('2001', 'David Wallace', 'Business', 50000);
  84. INSERT INTO takes (ID, course_id, sec_id, semester, year) VALUES ('1001', 'MGT101', '01', 'Fall', 2023);
  85. INSERT INTO teaches (ID, course_id, sec_id, semester, year) VALUES ('2001', 'MGT101', '01', 'Fall', 2023);
  86. SELECT * FROM student_course_instructor WHERE student_name = 'Michael Scott';
  87.  
  88. student_name course_title instructor_name
  89. Michael Scott Management 101 David Wallace
  90.  
  91. UPDATE instructor SET name = 'Enamul Hassan' WHERE ID = '2001';
  92. SELECT * FROM student_course_instructor WHERE course_title = 'Management 101';
  93.  
  94. student_name course_title instructor_name
  95. Michael Scott Management 101 Enamul Hassan
  96.  
  97. DELETE FROM student WHERE name = 'Zhang';
  98. SELECT * FROM student_course_instructor WHERE student_name = 'Zhang';
  99.  
  100. No rows returned.
  101.  
  102. So, it can be observed that the views are updated accordingly.
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement