免费看《Windows CE 嵌入式系统开发
从基础到实践》
创建外键时的选项
创建测试表
drop table student;
drop table class;
drop table student_class;
Create table student(student_id integer not nullstudent_name varchar() CONSTRAINT P_KEY_ primary key (student_id)) in luzl_k_tb index in luzl_k_tb ;
Create table class(class_id integer not nullclass_name varchar() CONSTRAINT P_KEY_ primary key (class_id)) in luzl_k_tb index in luzl_k_tb ;
Create table student_class(student_class_id integerstudent_id integerclass_id integer) in luzl_k_tb index in luzl_k_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_idstudent_name) values(luzl);
Insert into class(class_idclass_name) values(db);
Insert into student_class(student_class_idstudent_idclass_id) values();
On Delete 的选项有
Restrict/no action/cascade/set null其中cascade选项指定的话如果删除父记录依赖于他的子记录也会自动删除相当于级联删除如果指定no action和cascade都会报错因为还有子记录所以无法删除该记录set nul允许删除父记录并且l会将子表中与父表关联的字段设置为null
On Update 只有两个选项 no action/restrict它们在更新和删除时并没有区别:如果与子表关联不允许删除
另外还需要注意一点父表中的字段必须是主键才能做为子表的外键