惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
J
Java Code Geeks
博客园 - 聂微东
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
腾讯CDC
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
美团技术团队
博客园 - Franky
Google DeepMind News
Google DeepMind News
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
The Cloudflare Blog

博客园 - 空空儿

硬盘分区、寻址和系统启动过程 JavaScript里String.Format方法的实现 asp.net异步页 获取SQL Server数据行的物理地址信息(%%lockress%% & %%physloc%%) 利用Spire.DataExport将数据(Database/DataTable)导成各种文件 使用XML的value()方法将多行数据合并成一列 ASP.NET application and page life cycle SQL Server 2005 CLR 调用Web Service需要注意的几个问题 SQL SERVRE 2005 CLR TVF错误:从用户定义的表值函数获取新行时出错:Data access is not allowed in this context. PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 在C#里使用属性 - 空空儿 - 博客园 Read text file (txt, csv, log, tab, fixed length) 批量写数据---将XML数据批量写入数据库 对对象类型和调用方法属性进行存储以提升反射性能 过程 sp_addlinkedsrvlogin,第 91 行解密过程中出错的解决办法 生成大量随机字符串不同实现方式的效率对比 用SQL SERVER 2005新提供的命令实现行列转换 APM (异步编程模型) 利用宏让ERStudio生成代码文件
获取所有的外键信息
空空儿 · 2011-03-03 · via 博客园 - 空空儿

通过查询系统表,可以得到一个数据库里的所有外键信息,得到这些信息后就可以生成一些脚本,比如删除某张表的所有外键,根据现有外键信息生成新的外键.

    select fk.name fkname,constable.name constablename,conscol.name conscolname,reftable.name reftablename,refcol.name refcolname
        
from sys.foreign_keys fk
        
join sys.objects constable on fk.parent_object_id=constable.object_id
        
join sys.objects reftable on fk.referenced_object_id=reftable.object_id
        
join sys.foreign_key_columns fkc on fkc.constraint_object_id=fk.object_id 
        
join sys.columns conscol on fkc.parent_column_id =conscol.column_id and fkc.parent_object_id=conscol.object_id
        
join sys.columns refcol on fkc.referenced_column_id=refcol.column_id and fkc.referenced_object_id=refcol.object_id

以上查询得到外键名称,外键基表,外键列,外键引用表,外键引用列.

以下语句删除数据库db里关于tblname的外键:

use db
godeclare @references_name nvarchar(100),
        
@table_name nvarchar(100)declare cursor_references cursor for
    
select fk.name fkname,constable.name constable from sys.foreign_keys fk
        
join sys.objects constable on fk.parent_object_id=constable.object_id
        
join sys.objects reftable on fk.referenced_object_id=reftable.object_id
        
join sys.foreign_key_columns fkc on fkc.constraint_object_id=fk.object_id 
        
join sys.columns conscol on fkc.parent_column_id =conscol.column_id and fkc.parent_object_id=conscol.object_id
        
join sys.columns refcol on fkc.referenced_column_id=refcol.column_id and fkc.referenced_object_id=refcol.object_id
        
where reftable.name='tblname'
open cursor_references
fetch next from cursor_references into @references_name,@table_name
while @@fetch_status = 0
begin
    
exec('alter table '+@table_name+' drop constraint '+@references_name)
    
fetch next from cursor_references into @references_name,@table_name
end
close cursor_references
deallocate cursor_references