-
Notifications
You must be signed in to change notification settings - Fork 4
/
Animals.SQL
175 lines (137 loc) · 3.48 KB
/
Animals.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
172
173
174
/*
Created: 11.04.2023
Modified: 11.04.2023
Model: MySQL 8.0
Database: MySQL 8.0
*/
-- Table animal
CREATE TABLE `animal`
(
`id` Int NOT NULL AUTO_INCREMENT,
`animal_name` Varchar(20) NOT NULL,
PRIMARY KEY (`id`)
)
;
-- Table home_animal
CREATE TABLE `home_animal`
(
`id` Int NOT NULL AUTO_INCREMENT,
`home_name` Varchar(20) NOT NULL,
`livePlace` Varchar(20),
`id_animal` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `animal_home` ON `home_animal` (`id_animal`)
;
-- Table cat
CREATE TABLE `cat`
(
`id` Int NOT NULL AUTO_INCREMENT,
`cat_name` Varchar(30) NOT NULL,
`date_birth` Date NOT NULL,
`commands` Varchar(200),
`color` Varchar(20),
`id_home` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `home_cat` ON `cat` (`id_home`)
;
-- Table dog
CREATE TABLE `dog`
(
`id` Int NOT NULL AUTO_INCREMENT,
`dog_name` Varchar(30) NOT NULL,
`date_birth` Date NOT NULL,
`commands` Varchar(200),
`color` Varchar(20),
`id_home` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `home_dog` ON `dog` (`id_home`)
;
-- Table hamster
CREATE TABLE `hamster`
(
`id` Int NOT NULL AUTO_INCREMENT,
`hamster_name` Varchar(30) NOT NULL,
`date_birth` Date NOT NULL,
`commands` Varchar(200),
`color` Varchar(20),
`id_home` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `home_hamster` ON `hamster` (`id_home`)
;
-- Table pack_animal
CREATE TABLE `pack_animal`
(
`id` Int NOT NULL AUTO_INCREMENT,
`pack_name` Varchar(20) NOT NULL,
`livePlace` Varchar(20),
`id_animal` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `animal_pack` ON `pack_animal` (`id_animal`)
;
-- Table camel
CREATE TABLE `camel`
(
`id` Int NOT NULL AUTO_INCREMENT,
`name` Varchar(30) NOT NULL,
`date_birth` Date NOT NULL,
`commands` Varchar(200),
`color` Varchar(20),
`id_pack` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `pack_camel` ON `camel` (`id_pack`)
;
-- Table horse
CREATE TABLE `horse`
(
`id` Int NOT NULL AUTO_INCREMENT,
`name` Varchar(30) NOT NULL,
`date_birth` Date NOT NULL,
`commands` Varchar(200),
`color` Varchar(20),
`id_pack` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `pack_hourse` ON `horse` (`id_pack`)
;
-- Table mule
CREATE TABLE `mule`
(
`id` Int NOT NULL AUTO_INCREMENT,
`name` Varchar(30) NOT NULL,
`date_birth` Date NOT NULL,
`commands` Varchar(200),
`color` Varchar(20),
`id_pack` Int,
PRIMARY KEY (`id`)
)
;
CREATE INDEX `pack_mule` ON `mule` (`id_pack`)
;
-- Create foreign keys (relationships) section -------------------------------------------------
ALTER TABLE `home_animal` ADD CONSTRAINT `animal_to_home` FOREIGN KEY (`id_animal`) REFERENCES `animal` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
;
ALTER TABLE `cat` ADD CONSTRAINT `home_to_cat` FOREIGN KEY (`id_home`) REFERENCES `home_animal` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
;
ALTER TABLE `dog` ADD CONSTRAINT `home_to_dog` FOREIGN KEY (`id_home`) REFERENCES `home_animal` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
;
ALTER TABLE `hamster` ADD CONSTRAINT `home_to_hamster` FOREIGN KEY (`id_home`) REFERENCES `home_animal` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
;
ALTER TABLE `camel` ADD CONSTRAINT `pack_to_camel` FOREIGN KEY (`id_pack`) REFERENCES `pack_animal` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
;
ALTER TABLE `horse` ADD CONSTRAINT `pack_to_horse` FOREIGN KEY (`id_pack`) REFERENCES `pack_animal` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
;
ALTER TABLE `mule` ADD CONSTRAINT `pack_to_mule` FOREIGN KEY (`id_pack`) REFERENCES `pack_animal` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
;