-
Notifications
You must be signed in to change notification settings - Fork 1
/
script2.sql
171 lines (153 loc) · 6.24 KB
/
script2.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema learn_app
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `learn_app` ;
-- -----------------------------------------------------
-- Schema learn_app
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `learn_app` DEFAULT CHARACTER SET utf8 ;
USE `learn_app` ;
-- -----------------------------------------------------
-- Table `learn_app`.`roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `learn_app`.`roles` (
`id` INT NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NOT NULL,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `learn_app`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `learn_app`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(100) NOT NULL,
`last_name` VARCHAR(100) NOT NULL,
`birth_date` DATE NOT NULL,
`email` VARCHAR(150) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`role_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_users_roles1_idx` (`role_id` ASC) VISIBLE,
CONSTRAINT `fk_users_roles1`
FOREIGN KEY (`role_id`)
REFERENCES `learn_app`.`roles` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `learn_app`.`categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `learn_app`.`categories` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(80) NOT NULL,
`description` VARCHAR(255) NOT NULL,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`image` VARCHAR(255) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `learn_app`.`courses`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `learn_app`.`courses` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(200) NULL,
`description` TEXT(10000) NULL,
`instructor_id` INT NOT NULL,
`category_id` INT NOT NULL,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`image` VARCHAR(255) NULL,
PRIMARY KEY (`id`),
INDEX `fk_courses_users1_idx` (`instructor_id` ASC) VISIBLE,
INDEX `fk_courses_categories1_idx` (`category_id` ASC) VISIBLE,
CONSTRAINT `fk_courses_users1`
FOREIGN KEY (`instructor_id`)
REFERENCES `learn_app`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_courses_categories1`
FOREIGN KEY (`category_id`)
REFERENCES `learn_app`.`categories` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `learn_app`.`records`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `learn_app`.`records` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`course_id` INT NOT NULL,
`description` VARCHAR(255) NOT NULL,
`record` VARCHAR(255) NOT NULL,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `fk_records_courses1_idx` (`course_id` ASC) VISIBLE,
CONSTRAINT `fk_records_courses1`
FOREIGN KEY (`course_id`)
REFERENCES `learn_app`.`courses` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `learn_app`.`comments`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `learn_app`.`comments` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`comment` VARCHAR(200) NULL,
`rate` TINYINT(1) NOT NULL,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_id` INT NOT NULL,
`course_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_comments_users1_idx` (`user_id` ASC) VISIBLE,
INDEX `fk_comments_courses1_idx` (`course_id` ASC) VISIBLE,
CONSTRAINT `fk_comments_users1`
FOREIGN KEY (`user_id`)
REFERENCES `learn_app`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_comments_courses1`
FOREIGN KEY (`course_id`)
REFERENCES `learn_app`.`courses` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `learn_app`.`users_has_courses`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `learn_app`.`users_has_courses` (
`user_id` INT NOT NULL,
`course_id` INT NOT NULL,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`, `course_id`),
INDEX `fk_users_has_courses_courses1_idx` (`course_id` ASC) VISIBLE,
INDEX `fk_users_has_courses_users1_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `fk_users_has_courses_users1`
FOREIGN KEY (`user_id`)
REFERENCES `learn_app`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_courses_courses1`
FOREIGN KEY (`course_id`)
REFERENCES `learn_app`.`courses` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
INSERT INTO roles(role) VALUES('admin'),('instructor'),('student');