-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sql
73 lines (73 loc) · 2.41 KB
/
setup.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
DROP DATABASE IF EXISTS `imdb`;
CREATE DATABASE `imdb` /*!40100 COLLATE 'utf8mb4_unicode_ci' */;
USE `imdb`;
CREATE TABLE `users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` CHAR(50) NOT NULL,
`username` CHAR(15) NOT NULL,
`email` CHAR(255) NOT NULL,
`password` CHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX (`username`),
UNIQUE INDEX (`email`)
);
CREATE TABLE `tokens` (
`token` CHAR(255) NOT NULL,
`user` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`token`),
FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `movies` (
`id` CHAR(15) NOT NULL,
`title` CHAR(255) NOT NULL,
`description` VARCHAR(1000) NOT NULL,
`avatar` TEXT NOT NULL,
`year` YEAR NOT NULL,
`duration` CHAR(7) NOT NULL,
`rate` FLOAT UNSIGNED NOT NULL,
`rank` TINYINT(3) UNSIGNED NOT NULL,
`directors` VARCHAR(1000) NOT NULL,
`actors` VARCHAR(1000) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX (`rank`)
);
CREATE TABLE `thumbnails` (
`url` VARCHAR(1000) NOT NULL,
`movie` CHAR(15) NOT NULL,
FOREIGN KEY (`movie`) REFERENCES `movies` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `comments` (
`author` INT(10) UNSIGNED NOT NULL,
`movie` CHAR(15) NOT NULL,
`time` DATETIME NOT NULL,
`content` CHAR(255) NOT NULL,
FOREIGN KEY (`author`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (`movie`) REFERENCES `movies` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `rates` (
`user` INT(10) UNSIGNED NOT NULL,
`movie` CHAR(15) NOT NULL,
`rate` FLOAT NOT NULL,
FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (`movie`) REFERENCES `movies` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `favorites` (
`user` INT(10) UNSIGNED NOT NULL,
`movie` CHAR(15) NOT NULL,
PRIMARY KEY (`user`, `movie`),
FOREIGN KEY (`movie`) REFERENCES `movies` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `genres` (
`genre` CHAR(15) NOT NULL,
`movie` CHAR(15) NOT NULL,
PRIMARY KEY (`genre`, `movie`),
FOREIGN KEY (`movie`) REFERENCES `movies` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `top_week` (
`movie` CHAR(15) NOT NULL,
`rank` TINYINT(3) UNSIGNED NOT NULL,
PRIMARY KEY (`movie`),
UNIQUE INDEX (`rank`),
FOREIGN KEY (`movie`) REFERENCES `movies` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
);