每种sql server或许不同.这是interbase下的 sql格式:ALTER TABLE <table_name> ADD CONSTRAINT <constraint_name>REFERENCES other_table [(other_col [, other_col …])][ON DELETE {NO ACTION|CASCADE|SET DEFAULT|SET NULL}][ON UPDATE {NO ACTION|CASCADE|SET DEFAULT|SET NULL}]其中: Used with REFERENCES: Changes a foreign key whenever the referenced primary key changes; valid options are:• [Default] NO ACTION: Does not change the foreign key; may cause the primary key update to fail due to referential integrity checks • CASCADE: For ON DELETE, deletes the corresponding foreign key; for ON UPDATE, updates the corresponding foreign key to the new value of the primary key• SET NULL: Sets all the columns of the corresponding foreign key to NULL• SET DEFAULT: Sets every column of the corresponding foreign key is set to its default value in effect when the referential integrity constraint is defined; when the default for a foreign column changes after the referential integrity constraint is defined, the change does not have an effect on the default value used in the referential integrity constraint
on delete cascade就表示当你删除父表数据时,同时删除有外键指向子表相关项。
建立外键关系的时候,选择
[对 INSERT 和 UPDATE 强制关系]
[级联更新相关的字段]
无论何时更新主键值,都指示 DBMS 自动更新该关系的外键值。
[级联删除相关的字段]
无论何时删除主表的被引用行,都指示 DBMS 自动删除外键表的行
Used with REFERENCES: Changes a foreign key whenever the referenced primary key changes; valid options are:• [Default] NO ACTION: Does not change the foreign key; may cause the primary key update to fail due to referential integrity checks • CASCADE: For ON DELETE, deletes the corresponding foreign key; for ON UPDATE, updates the corresponding foreign key to the new value of the primary key• SET NULL: Sets all the columns of the corresponding foreign key to NULL• SET DEFAULT: Sets every column of the corresponding foreign key is set to its default value in effect when the referential integrity constraint is defined; when the default for a foreign column changes after the referential integrity constraint is defined, the change does not have an effect on the default value used in the referential integrity constraint