[英]SQL Server truncate table - drop and recreate FK constraints script
I'm writing small application (in c#) which helps me to truncate tables in SQL Server 2005/08. 我正在编写一个小型应用程序(用C#语言编写),它可以帮助我截断SQL Server 2005/08中的表。 In order to truncate table I think I need to do this: 为了截断表,我想我需要这样做:
Can someone help me to create such a script, or point me where I can find some clues? 有人可以帮助我创建这样的脚本,或者指向我在哪里可以找到一些线索?
Regards 问候
Well, you could do this from your application: 好吧,您可以从您的应用程序中执行此操作:
You can do this by inspecting the system catalog view. 您可以通过检查系统目录视图来执行此操作。
This query here will give you a list of all foreign key constraints: 此查询将为您提供所有外键约束的列表:
select
fk.name,
object_name(fk.parent_object_id) 'Parent table',
c1.name 'Parent column',
object_name(fk.referenced_object_id) 'Referenced table',
c2.name 'Referenced column'
from
sys.foreign_keys fk
inner join
sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
inner join
sys.columns c1 ON fkc.parent_column_id = c1.column_id and c1.object_id = fkc.parent_object_id
inner join
sys.columns c2 ON fkc.referenced_column_id = c2.column_id and c2.object_id = fkc.referenced_object_id
By combining these elements, you can create the list of DROP CONSTRAINT
commands to be run before the truncation of the tables: 通过组合这些元素,可以创建要在截断表之前运行的DROP CONSTRAINT
命令的列表:
select
'ALTER TABLE dbo.' + object_name(fk.parent_object_id) +
' DROP CONSTRAINT ' + fk.name
from
sys.foreign_keys fk
and you can also create the ALTER TABLE
scripts to be run after the truncating to restore the foreign key relationships. 您还可以创建截断后运行的ALTER TABLE
脚本以恢复外键关系。
select
'ALTER TABLE dbo.' + object_name(fk.parent_object_id) +
' ADD CONSTRAINT ' + fk.name +
' FOREIGN KEY(' + c1.name + ') REFERENCES dbo.' +
object_name(fk.referenced_object_id) + '(' + c2.name + ')'
from
sys.foreign_keys fk
inner join
sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
inner join
sys.columns c1 ON fkc.parent_column_id = c1.column_id and c1.object_id = fkc.parent_object_id
inner join
sys.columns c2 ON fkc.referenced_column_id = c2.column_id and c2.object_id = fkc.referenced_object_id
For these two queries, it's a two-step process: 对于这两个查询,这是一个两步过程:
Limitation: right now, the script assumes and works only if you have single-column foreign keys; 限制:目前,该脚本仅在您具有单列外键时才假定并起作用; if you don't have that, you might need to tweak the scripts a bit. 如果没有,可能需要稍微调整一下脚本。
In Enterprise Manager: 在企业管理器中:
With a bit of massaging of this chunk of script, you can make your drop constraint
and drop index
statements. 稍微按摩一下这段脚本,就可以创建drop constraint
和drop index
语句。 Drop any clustered index last, or else you'll be rebuilding the other indexes as you go along. 最后删除任何聚集索引,否则,您将继续重建其他索引。
Execute your drops, truncate the table, then execute the creates that you scripted out. 执行删除操作,截断表,然后执行脚本编写的创建。
Also: perform a database backup right after you've done this, as any truncate table
statement puts the transaction log into an uncertain state, as truncate table
statements aren't logged. 另外:完成此操作后立即执行数据库备份,因为任何truncate table
语句都会使事务日志进入不确定状态,因为不会记录truncate table
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.