php大神 发表于 2023-5-16 17:42

php三种外键操作

php三种外键操作

1、严格限制(参见主表和从表)2、置空操作(set null):如果主表记录删除,或关联字段更新,则从表外键字段被设置为null。3、级联操作(cascade):如果主表记录删除,则从表记录也被删除。主表更新,从表外键字段也更新。语法:foreign key (外键字段) references 主表名 (关联字段) [主表记录删除时的动作] [主表记录更新时的动作]。一般说删除时置空,更新时级联。代码如下:drop table if exists stuinfo;
create table stuinfo(
       id tinyint primary key comment '学号,主键',
       name varchar(20) comment '姓名'
)engine=innodb;

drop table if exists stuscore;
create table stuscore(
       id int auto_increment primary key comment '主键',
       sid tinyint comment '学号,外键',
       score tinyint unsigned comment '成绩',
       foreign key(sid) references stuinfo(id) on delete set null on update cascade
)engine=innodb;置空、级联操作中外键不能是从表的主键
页: [1]
查看完整版本: php三种外键操作