sqlserver truncate清空表时候,无法删除 'B表',因为该表正由一个 FOREIGN KEY 约束引用。

时间:2023-03-09 02:08:57
sqlserver truncate清空表时候,无法删除 'B表',因为该表正由一个 FOREIGN KEY 约束引用。

外键:

查询:select object_name(a.parent_object_id) 'tables'  from sys.foreign_keys a  where a.referenced_object_id=object_id('B表')

格式(名称):FK_A表_B表

A表是主表,引用B表的id字段作为外键,删除B表时候,“无法截断表B,因为该表正由 FOREIGN KEY 约束引用。”

删除外键:

alert table A表 drop constraint FK_A表_B表

清空表B

TRUNCATE TABLE B表

新增外键:

alert table A表 add constraint FK_A表_B表 foreign key (A表的id) references B表(B表的id)