DB2创建外键时选项如何快速掌握?


  本文标签:DB2创建外键

  此文章主要讲述的是掌握DB2创建外键时选项的实际操作描述,你如果对快速掌握DB2创建外键时选项的实际操作描述有兴趣的话你就可以点击以下的文章进行观看了,以下就是相关内容的具体描述  。

  

  创建外键时的选项:

  1.创建测试表:

  

  1. drop table student;  
  2. drop table class;  
  3. drop table student_class;  
  4. 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 ;  
  5. 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 ;  
  6. Create table student_class(student_class_id integer,student_id integer,class_id integer) in luzl_32k_tb index in luzl_32k_tb;  
  7. alter table student_class add constraint if_class foreign key(class_id) references class(class_id) ON DELETE cascade ON UPDATE RESTRICT;  
  8. alter table student_class add constraint if_student foreign key(student_id) references student(student_id) ON DELETE cascade ON UPDATE RESTRICT;  
  9. Insert into student(student_id,student_name) values(1,luzl);  
  10. Insert into class(class_id,class_name) values(1,db2);  
  11. 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.另外还需要注意一点,父表中的字段必须是主键,才能做为子表的外键  。以上的相关内容就是对快速掌握DB2创建外键时的选项的介绍,望你能有所收获  。

快速掌握DB2创建外键时的选项

  以上的相关内容就是对快速掌握DB2创建外键时的选项的介绍,望你能有所收获  。