-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie_database.sql
124 lines (93 loc) · 3.11 KB
/
movie_database.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
create database Movie
use Movie;
CREATE TABLE genres (
genre_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE directors (
director_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE actors (
actor_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE movies (
movie_id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
release_year INT,
genre_id INT,
director_id INT,
FOREIGN KEY (genre_id) REFERENCES genres(genre_id),
FOREIGN KEY (director_id) REFERENCES directors(director_id)
);
CREATE TABLE movie_actors (
movie_id INT,
actor_id INT,
PRIMARY KEY (movie_id, actor_id),
FOREIGN KEY (movie_id) REFERENCES movies(movie_id),
FOREIGN KEY (actor_id) REFERENCES actors(actor_id)
);
CREATE TABLE ratings (
rating_id INT PRIMARY KEY,
movie_id INT,
user_id INT,
rating DECIMAL(3, 2),
rating_date DATE,
FOREIGN KEY (movie_id) REFERENCES movies(movie_id)
);
CREATE TABLE movies_list (
movie_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
release_year INT,
genre_id INT,
director_id INT,
FOREIGN KEY (genre_id) REFERENCES genres(genre_id),
FOREIGN KEY (director_id) REFERENCES directors(director_id)
);
CREATE TABLE genres (
genre_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);
CREATE TABLE directors (
director_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL
);
CREATE TABLE actors (
actor_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL
);
CREATE TABLE movie_actors (
movie_id INT,
actor_id INT,
PRIMARY KEY (movie_id, actor_id),
FOREIGN KEY (movie_id) REFERENCES movies(movie_id),
FOREIGN KEY (actor_id) REFERENCES actors(actor_id)
);
CREATE TABLE ratings (
rating_id INT PRIMARY KEY AUTO_INCREMENT,
movie_id INT,
user_id INT,
rating DECIMAL(2, 1) CHECK (rating >= 0.0 AND rating <= 10.0),
FOREIGN KEY (movie_id) REFERENCES movies(movie_id)
);
CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(100) NOT NULL
);
INSERT INTO genres (genre_id, name) VALUES (1, 'Action'), (2, 'Drama');
INSERT INTO directors (director_id, name) VALUES (1, 'Christopher Nolan'), (2, 'Steven Spielberg');
INSERT INTO actors (actor_id, name) VALUES (1, 'Leonardo DiCaprio'), (2, 'Brad Pitt');
INSERT INTO movies (movie_id, title, release_year, genre_id, director_id) VALUES (1, 'Inception', 2010, 1, 1);
INSERT INTO movies (movie_id, title, release_year, genre_id, director_id) VALUES (2, 'Fight Club', 1999, 2, 2);
INSERT INTO movie_actors (movie_id, actor_id) VALUES (1, 1), (2, 2);
INSERT INTO ratings (rating_id, movie_id, user_id, rating, rating_date) VALUES (1, 1, 1, 9.0, '2023-06-01');
INSERT INTO ratings (rating_id, movie_id, user_id, rating, rating_date) VALUES (2, 2, 1, 8.5, '2023-06-02');
-- Retrieve top-rated movies in the Action genre
SELECT m.title, AVG(r.rating) AS average_rating
FROM movies m
JOIN ratings r ON m.movie_id = r.movie_id
JOIN genres g ON m.genre_id = g.genre_id
WHERE g.name = 'Action'
GROUP BY m.title
ORDER BY average_rating DESC;