Advertisement
vallec

Untitled

Mar 20th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1.  
  2. /* zad 1 */
  3. DROP DATABASE IF EXISTS cinema_city;
  4. CREATE DATABASE cinema_city;
  5. USE cinema_city;
  6. CREATE TABLE Cinema (
  7. id int primary key auto_increment,
  8. name VARCHAR(100) NOT NULL,
  9. town VARCHAR(100) NOT NULL
  10. );
  11.  
  12. CREATE TABLE Hall (
  13. id int primary key auto_increment,
  14. cinemaID int,
  15. number INT NOT NULL,
  16. type ENUM('Normal', 'VIP', 'Deluxe'),
  17. foreign key (cinemaID) REFERENCES Cinema(id)
  18. );
  19.  
  20. CREATE TABLE Film (
  21. id int primary key auto_increment,
  22. name VARCHAR(255) NOT NULL,
  23. publish_year DATE NOT NULL,
  24. country VARCHAR(255) NOT NULL
  25. );
  26.  
  27. CREATE TABLE Projection (
  28. id int primary key auto_increment,
  29. hallID int,
  30. filmID int,
  31. projection_time DATETIME NOT NULL,
  32. viewers int NOT NULL,
  33. FOREIGN KEY (hallID) REFERENCES Hall(id),
  34. FOREIGN KEY (filmID) REFERENCES Film(id)
  35. );
  36.  
  37. INSERT INTO Cinema (name, town) VALUES
  38. ('Arena Mladost', 'Sofia'),
  39. ('Cineplex', 'Plovdiv'),
  40. ('Hollywood Cinema', 'Varna');
  41.  
  42. INSERT INTO Hall (number, type, cinemaID) VALUES
  43. (1, 'Normal', 1),
  44. (2, 'VIP', 1),
  45. (3, 'Deluxe', 1),
  46. (1, 'Normal', 2),
  47. (2, 'VIP', 2),
  48. (3, 'Deluxe', 2),
  49. (4, 'Deluxe', 2);
  50.  
  51. INSERT INTO Film (name, publish_year, country) VALUES
  52. ('Final Destination 7', '2023-10-12', "USA"),
  53. ("Test", '2024-10-12', "Bulgaria");
  54.  
  55. INSERT INTO Projection (projection_time, viewers, hallID, filmID) VALUES
  56. ('2024-03-20 18:00:00', 100, 1, 1),
  57. ('2024-03-20 20:00:00', 50, 2, 1),
  58. ('2024-03-20 22:00:00', 20, 1, 2),
  59. ('2024-03-21 10:00:00', 150, 2, 2),
  60. ('2024-03-21 15:00:00', 30, 1, 1),
  61. ('2024-03-23 10:00:00', 50, 6, 1),
  62. ('2024-03-21 15:00:00', 150, 7, 1);
  63.  
  64. /* 2 */
  65. SELECT Cinema.name, Hall.number, Projection.projection_time
  66. FROM Cinema
  67. JOIN Hall ON Cinema.id = Hall.cinemaID
  68. JOIN Projection ON Hall.id = Projection.hallID
  69. JOIN Film ON Projection.filmID = Film.id
  70. WHERE Film.name = 'Final Destination 7'
  71. AND Hall.type IN ('VIP', 'Deluxe')
  72. ORDER BY Cinema.name, Hall.number;
  73.  
  74. /* 3 */
  75. SELECT sum(Projection.viewers) as Viewers
  76. FROM Cinema
  77. JOIN Hall ON Cinema.id = Hall.cinemaID
  78. JOIN Projection ON Hall.id = Projection.hallID
  79. JOIN Film ON Projection.filmID = Film.id
  80. WHERE Film.name = 'Final Destination 7'
  81. AND Hall.type = 'VIP'
  82. AND Cinema.name = 'Arena Mladost';
  83.  
  84. /* 4 */
  85. USE school_sport_clubs;
  86. SELECT DISTINCT st1.name AS student1, st2.name AS student2
  87. FROM student_sport AS ss1
  88. JOIN student_sport AS ss2 ON ss1.sportGroup_id = ss2.sportGroup_id
  89. AND ss1.student_id < ss2.student_id
  90. JOIN students AS st1 ON ss1.student_id = st1.id
  91. JOIN students AS st2 ON ss2.student_id = st2.id
  92. JOIN sports AS s1 ON ss1.sportGroup_id = s1.id
  93. JOIN sports AS s2 ON ss2.sportGroup_id = s2.id
  94. WHERE s1.name = 'Football' AND s2.name = 'Football';
  95.  
  96. /* 5 */
  97. CREATE VIEW TrainingsAt8 AS
  98. SELECT s.name AS student_name, s.class, sg.location, sg.dayOfWeek, sg.hourOfTraining, c.name AS coach_name
  99. FROM students AS s
  100. JOIN student_sport AS ss ON s.id = ss.student_id
  101. JOIN sportGroups AS sg ON ss.sportGroup_id = sg.id
  102. JOIN coaches AS c ON sg.coach_id = c.id
  103. WHERE TIME(sg.hourOfTraining) = '08:00:00';
  104.  
  105. /* 6 */
  106. SELECT s.name AS sport, COUNT(ss.student_id) AS students
  107. FROM sports AS s
  108. JOIN sportGroups AS sg ON s.id = sg.sport_id
  109. JOIN student_sport AS ss ON sg.id = ss.sportGroup_id
  110. GROUP BY s.name;
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement