查询:
SELECT 主键列ID=b.rkey ,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid) ,外键表ID=b.fkeyid ,外键表名称=object_name(b.fkeyid) ,外键列ID=b.fkey ,外键列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid) ,级联更新=ObjectProperty(a.id,'CnstIsUpdateCascade') ,级联删除=ObjectProperty(a.id,'CnstIsDeleteCascade') FROM sysobjects a join sysforeignkeys b on a.id=b.constid join sysobjects c on a.parent_obj=c.id
删除:
declare @sql varchar(max), @tab_name varchar(128), @fk_name varchar(128); declare c cursor for select OBJECT_NAME(parent_object_id), name from sys.objects where type='F' open c fetch next from c into @tab_name, @fk_name while @@FETCH_STATUS=0 begin set @sql=''; set @sql='alter table ' + @tab_name + ' drop constraint ' + @fk_name print @sql exec(@sql) fetch next from c into @tab_name, @fk_name end close c deallocate c
使用方法:
打开数据库,转到要操作的那一个库,执行上面的代码即可