constraint foreign_key_name foreign key
(cloumn_name)
references database_name
(cloumn_name)
on update[on delete]
[no action]
[set null ]
[set default
][cascade]
on update 和 on delete 後面可以跟的詞語有四個
no action , set null , set default ,cascade
no action 表示 不做任何操作,
set null 表示在外鍵表中將相應字段設定為null
set default 表示設定為預設值(restrict)
cascade 表示級聯操作,就是說,如果主鍵表中被參考字段更新,外來鍵表中也更新,主鍵表中的記錄被刪除,外來鍵表中改行也相應刪除
select * from springcloud.payment;
insert into payment
(serial)
values
('serial001');
# 方法一 外來鍵測試刪除
create table sys_user
(id bigint(20
)auto_increment primary key,
username varchar(50
),password varchar(50
));select * from sys_user;
select * from roles;
create table roles
(id int primary key not null,
role varchar(5
) not null);
insert into roles
(id,role)
value(1
,'admin');
insert into roles
(id,role)
values(2
,'user'),
(3,'view');
alter table sys_user add roleid int after password;
alter table sys_user add constraint roleid foreign key
(roleid) references roles
(id)
;update sys_user set roleid=
4 where id =1;
delete from sys_user where roleid =1;
delete from roles where roles.id=1;
insert into sys_user
(id,username,password,roleid)
value(1
,'admin'
,'000000',1
);# 方法二 級聯測試主表刪除同時附表刪除該記錄
select * from employee;
create table employee
(id bigint(20
)auto_increment primary key,
empname varchar(50
));insert into employee values(1
,'張三軟體開發部',1
);insert into employee values(2
,'李四軟體開發部',2
);insert into employee values(3
,'王五應用維護部',3
);select * from salary;
# 員工是主表,其他副表有員工的關聯屬性外來鍵,新增級聯效應刪除員工其他表一同刪除;
# 員工是主表,只有刪除了其他關聯了的屬性才可以刪除主表資訊;
create table salary
(id bigint(20
)auto_increment primary key,
number bigint(20
));insert into salary values(1
,5000);
insert into salary values(2
,6000);
insert into salary values(3
,7000);
alter table employee change column stuname empname varchar(30
); alter table employee change column salaryid salaryid bigint;# 型別修改一致
alter table employee add salaryid int after empname;#新增外來鍵字段
alter table employee add constraint employee_salaryid foreign key
(salaryid) references salary
(id) on delete cascade;#新增外來鍵約束並進行級聯關係
delete from employee where id =1;
delete from salary where id =
1;# 沒有這個人也就沒有相應工資表,工資表外來鍵員工id
# 方法三 級聯測試外來鍵直接建立約束foreign key
select * from student;
create table student
(id bigint(20
)auto_increment primary key,
stuname varchar(50
),resultid bigint(11
),constraint stu_resultid foreign key
(resultid) references result
(id));
select s.id,s.stuname,r.number from student s natural join result r where s.id = r.id;
insert into student values(1
,'張三',1
);insert into student values(2
,'李四',2
);insert into student values(3
,'王五',3
);select * from result;
create table result
(id bigint(20
)auto_increment primary key,
number int(5
));insert into result values(1
,70);
insert into result values(2
,80);
insert into result values(3
,90);
delete from student where id =1;
# 方法四 外來鍵約束foreign key
select * from course2;
create table course2
(id bigint(20
)auto_increment primary key,
coursename varchar(50
),studentid bigint,
constraint stuid foreign key
(studentid) references student2
(id));
delete from course2 where id=1;
insert into course2 values(1
,'語文',1
);insert into course2 values(2
,'數學',2
);insert into course2 values(3
,'化學',3
);select * from student2;
create table student2
(id bigint(20
)auto_increment primary key,
stuname varchar(50
));insert into student2 values(1
,'張三');
insert into student2 values(2
,'李四');
insert into student2 values(3
,'王五');
delete from student2 where id =
1;
mysql約束與外來鍵 MySQL 外來鍵與約束
外來鍵的建立 建表如下 create table parent id int not null,primary key id type innodb create table child id int,parent id int,foreign key parent id references pa...
MySQL 外來鍵約束
建立測試主表.id 是主鍵.create table test main id int,value varchar 10 primary key id 建立測試子表.create table test sub id int,main id int,value varchar 10 primary k...
MySQL外來鍵約束
innodb型別表有乙個其他儲存引擎不支援的特性 外來鍵約束。當b表的外來鍵關聯到a表的主鍵時 表b是父表a表的子表 如果刪除了a表,那麼b中的外來鍵則仍然關聯著乙個不存在的表的主鍵。而外鍵約束則設定了當約束被破壞時應該應用的的規則,包括防止約束破壞。建立乙個外來鍵約束的語法是 foreign ke...