SQL中如何为表添加和删除外键约束
定义格式
[CONSTRAINT <约束名>][FOREIGN KEY][<列名>]REFERENCES<被参照表名>(<主键列名>)
例如
--删除外键
alter table AdItem drop constraint AdOrder_AdItem_FK1
www.2cto.com
--增加外键
alter table AdItem
add constraint AdOrder_AdItem_FK1 foreign key (AI_nOrderNo) references AdOrder(AO_nOrderNo)
--不检查约束
alter table AdItem nocheck constraint AdOrder_AdItem_FK1
--检查约束
alter table AdItem check constraint AdOrder_AdItem_FK1
下面是添加外键的例子参考:
alter table cc_record
add constraint cc_account_cc_record_FK1 foreign key (aId) references cc_account(aId)
add constraint cc_account_cc_user_FK1 foreign key (uId) references cc_cc_user(uId)
alter table cc_refresh
add constraint cc_task_cc_refresh_FK1 foreign key (tId) references cc_task(tId)
alter table cc_task
add constraint cc_account_cc_task_FK1 foreign key (aId) references cc_account(aId)