1.创建测试表:
drop table student;
drop table class;
drop table student_class;
Create table student(student_id integer not null,student_name varchar(200), CONSTRAINT P_KEY_1 primary key (student_id)) in luzl_32k_tb index in luzl_32k_tb ;
Create table class(class_id integer not null,class_name varchar(200), CONSTRAINT P_KEY_2 primary key (class_id)) in luzl_32k_tb index in luzl_32k_tb ;
Create table student_class(student_class_id integer,student_id integer,class_id integer) in luzl_32k_tb index in luzl_32k_tb;
alter table student_class add constraint if_class foreign key(class_id) references class(class_id) ON DELETE cascade ON UPDATE RESTRICT;
alter table student_class add constraint if_student foreign key(student_id) references student(student_id) ON DELETE cascade ON UPDATE RESTRICT;
Insert into student(student_id,student_name) values(1,'luzl');
Insert into class(class_id,class_name) values(1,'db2');
Insert into student_class(student_class_id,student_id,class_id) values(1,1,1);
2.On Delete 的选项有
Restrict/no action/cascade/set null.其中cascade选项指定的话,如果删除父记录,依赖于他的子记录也会自动删除.相当于级联删除.如果指定no action和cascade都会报错,因为还有子记录所以无法删除该记录.set nul允许删除父记录并且l会将子表中与父表关联的字段设置为null.
3.On Update 只有两个选项 no action/restrict.它们在更新和删除时并没有区别:如果与子表关联不允许删除.
4.另外还需要注意一点,父表中的字段必须是主键,才能做为子表的外键。
评论 {{userinfo.comments}}
{{child.content}}
{{question.question}}
提交