外键 和 关系
设计E-R模型时就应设计表之间的关系,而确定好表关系模型后,再通过外键将表关系提现
1.建立表时可以直接创建约束
create table socres (
id int primary key auto_increment,
stuid int,
subid int,
score decimal(5,2),
foreign key(stuid) references students(id),
foreign key(subid) references subjects(id)
);
2.修改表结构 插入外键约束

插入或者修改数据时 若subid的值在classes表中不存在则会报错


外键的级联操作
在删除students表的数据时,如果这个id值在scores 表中已经存在,则会抛出异常
sososo推荐使用逻辑删除,
可以创建表时,指定级联操作,

网友评论