-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL_Foreign key.sql
More file actions
112 lines (83 loc) · 2.79 KB
/
Copy pathMySQL_Foreign key.sql
File metadata and controls
112 lines (83 loc) · 2.79 KB
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
-- Foreign Key
create database social_media_app;
use social_media_app;
create table users(
id int not null primary key auto_increment,
user_name varchar(200)
);
-- insert data
insert into users(user_name)
values
('Amanda'), ('Brian'), ('Cally'), ('Daniel'), ('Edward');
select * from users;
create table photos(
id int not null primary key auto_increment,
photo_url varchar(200),
user_id int,
foreign key (user_id) references users(id)
);
-- check table settings
desc photos;
-- insert data
insert into photos(photo_url, user_id)
values('https://123456.png', 1);
select * from users;
select * from photos;
insert into photos(photo_url, user_id)
values('https://7777777.png', 567);
show warnings;
# Cannot add or update a child row: a foreign key constraint fails (`social_media_app`.`photos`, CONSTRAINT `photos_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))
insert into photos(photo_url, user_id)
values('https://999999.png', null);
-- on delete restrict
# delete id = 1
delete from users where id = 1;
show warnings;
# Cannot delete or update a parent row: a foreign key constraint fails (`social_media_app`.`photos`, CONSTRAINT `photos_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))
drop table users;
show warnings;
# Cannot drop table 'users' referenced by a foreign key constraint 'photos_ibfk_1' on table 'photos'.
-- how to drop users
drop table photos;
#delete id = 1 or drop table
delete from users where id = 1;
drop table users;
-- on delete cascade
create table photos_02(
id int primary key auto_increment,
photo_url varchar(200),
user_id int,
foreign key(user_id) references users(id) on delete cascade
);
desc photos_02;
insert into photos_02(photo_url, user_id)
values
('https://50vO5C2qYeQBPfvV.png', 1), ('https://9939P61ncLk0zT7l.png', 1),
('https://IDiRYiItNd5TC2h9.png', 2), ('https://LsrdCdC0dhjrjteg.png', 2),
('https://TKHN7Fnmeoepeahw.png', 3), ('https://ajG9183iiGYHoReq.png', 3),
('https://edJKy1VdLkZ8wv5W.png', 4), ('https://nbiLUDgfCwI4ubWE.png', 4),
('https://pfhednPD67rDnreQ.png', 5), ('https://uxlInX2oBS6YtBiG.png', 5);
select * from photos_02;
select * from users;
-- delete users_id = 3
delete from users where id = 3;
select * from users;
select * from photos_02;
-- on delete set null
create table photos_03(
id int primary key auto_increment,
photo_url varchar(200),
user_id int,
foreign key(user_id) references users(id) on delete set null
);
insert into photos_03(photo_url, user_id)
values
('https://50vO5C2qYeQBPfvV.png', 1), ('https://9939P61ncLk0zT7l.png', 1),
('https://IDiRYiItNd5TC2h9.png', 2), ('https://LsrdCdC0dhjrjteg.png', 2),
('https://pfhednPD67rDnreQ.png', 5), ('https://uxlInX2oBS6YtBiG.png', 5);
select * from users;
select * from photos_03;
-- delete users_id = 4
delete from users where id = 4;
select * from users;
select * from photos_03;