MySQL(13)

-- on delete

create table `branch`(
   `branch_id` int primary key,
   `branch_name` varchar(10),
   `manager_id` int,
   foreign key(`manager_id`) references `employee`(`emp_id`) on delete set null
   );
-- 若对应不到,则填充null
create table `works_with`(
   `emp_id` int ,
   `client_id` int,
   `total_sales` int,
   primary key(`emp_id`,`client_id`),
   foreign key(`emp_id`) references `employee`(`emp_id`) on delete cascade,
   foreign key(`client_id`) references `client`(`client_id`) on delete cascade
   );
-- 若对应不到,则该资料删除(primary key不能设定为null)

delete from `employee` where `emp_id`=207;
select * from `branch`;
select * from `works_with`;

语法内容的总结:

1:on delete set null/on delete cascade

2:on delete set null/on delete cascade分别在什么情况下不能使用?

你可能感兴趣的:(sql,数据库,mysql)