-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_cars_database.sql
118 lines (105 loc) · 2.63 KB
/
create_cars_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
--
-- Установка кодировки, с использованием которой клиент будет посылать запросы на сервер
--
SET NAMES 'utf8';
-- Удаляем базу данных если она есть
DROP DATABASE IF EXISTS cars;
-- И создаём её заново
CREATE DATABASE cars;
-- Установка базы данных по умолчанию
USE cars;
CREATE TABLE vendor (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 4
AVG_ROW_LENGTH = 5461
CHARACTER SET utf8
COLLATE utf8_general_ci;
CREATE TABLE model (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
vendor_id INT(11) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT FK_model_vendor_id FOREIGN KEY (vendor_id)
REFERENCES vendor (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = INNODB
AUTO_INCREMENT = 4
AVG_ROW_LENGTH = 5461
CHARACTER SET utf8
COLLATE utf8_general_ci;
--
-- Описание для таблицы car
--
CREATE TABLE car (
id INT(11) NOT NULL AUTO_INCREMENT,
number VARCHAR(255) NOT NULL,
model_id INT(11) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT FK_car_model_id FOREIGN KEY (model_id)
REFERENCES model (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = INNODB
AUTO_INCREMENT = 5
AVG_ROW_LENGTH = 5461
CHARACTER SET utf8
COLLATE utf8_general_ci;
CREATE TABLE service (
id INT(11) NOT NULL AUTO_INCREMENT,
name TEXT NOT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 3
AVG_ROW_LENGTH = 8192
CHARACTER SET utf8
COLLATE utf8_general_ci;
--
-- Описание для таблицы car_service
--
CREATE TABLE car_service (
car_id INT(11) DEFAULT NULL,
service_id INT(11) DEFAULT NULL,
CONSTRAINT FK_car_service_car_id FOREIGN KEY (car_id)
REFERENCES car (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT FK_car_service_service_id FOREIGN KEY (service_id)
REFERENCES service (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = INNODB
AVG_ROW_LENGTH = 4096
CHARACTER SET utf8
COLLATE utf8_general_ci;
--
-- Вывод данных для таблицы vendor
--
INSERT INTO vendor VALUES
(1, 'BMW'),
(2, 'Lada'),
(3, 'General Motors');
INSERT INTO model VALUES
(1, 'X5', 1),
(2, 'X4', 1),
(3, 'X6', 1);
INSERT INTO car VALUES
(1, '345432', 1),
(3, '33543253', 2),
(4, '122312', 3);
INSERT INTO service VALUES
(1, 'Сервис 1'),
(2, 'Сервис 2');
INSERT INTO car_service VALUES
(1, 1),
(1, 1),
(3, 1),
(3, 2);