create table mytab(id int not null primary key,
name char(10)
)
注意,not null 不可省略,必须与primary key 同时指定!
免费看《Windows CE 嵌入式系统开发 从基础到实践》创建外键时的选项
创建测试表
drop table studentdrop table classdrop table student_classCreate table student(student_id integer not null student_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 null class_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 integer student_id integer class_id integer) in luzl_ k_tb index in luzl_ k_tbalter table student_class add constraint if_class foreign key(class_id) references class(class_id) ON DELETE cascade ON UPDATE RESTRICTalter table student_class add constraint if_student foreign key(student_id) references student(student_id) ON DELETE cascade ON UPDATE RESTRICTInsert into student(student_id student_name) values( luzl )Insert into class(class_id class_name) values( db )Insert into student_class(student_class_id student_id class_id) values( )
On Delete 的选项有
Restrict/no action/cascade/set null 其中cascade选项指定的话 如果删除父记录 依赖于他的子记录也会自动删除 相当于级联删除 如果指定no action和cascade都会报错 因为还有子记录所以无法删除该记录 set nul允许删除父记录并且l会将子表中与父表关联的字段设置为null
On Update 只有两个选项 no action/restrict 它们在更新和删除时并没有区别:如果与子表关联不允许删除
lishixinzhi/Article/program/DB2/201311/21928
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)