简体   繁体   English

SQL Server截断表-删除并重新创建FK约束脚本

[英]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: 为了截断表,我想我需要这样做:

  • drop all FK constraints from table, 从表中删除所有FK约束,
  • truncate the table, 截断表,
  • recreate all previously deleted constraints. 重新创建所有先前删除的约束。

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: 好吧,您可以从您的应用程序中执行此操作:

  • run a SQL command on your existing database to find all foreign key constraints 在现有数据库上运行SQL命令以查找所有外键约束
  • from that list of foreign key constraints, create two scripts 从该外键约束列表中,创建两个脚本
    • one to drop all existing foreign key constraints (before you truncate the tables) 一个删除所有现有的外键约束(在截断表之前)
    • a second one to re-create the foreign key constraints after you've truncated the tables 删除表后,第二个重新创建外键约束

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: 对于这两个查询,这是一个两步过程:

  • first execute the query that I show using C# and ADO.NET against your database 首先执行我对您的数据库使用C#和ADO.NET显示的查询
  • this will produce an output which is a list of T-SQL commands (to DROP or re-create the FK relationships) 这将产生一个输出,该输出是T-SQL命令的列表(以DROP或重新创建FK关系)
  • take the output and in a second step, execute that output as a T-SQL command batch from your C#/ADO.NET application. 获取输出,然后在第二步中,从C#/ ADO.NET应用程序中以T-SQL命令批处理的形式执行该输出。

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: 在企业管理器中:

  1. Select the table, 选择表格,
  2. Right-click the table, choose All Tasks -> Generate SQL Scripts, 右键点击表格,选择所有任务->生成SQL脚本,
  3. Un-tick Generate the Drop and Generate the Create , 取消勾选生成放置生成创建
  4. Click over to the Options tab, 点击“选项”标签,
  5. Tick all of the items beneath Table Scripting Options 勾选表脚本选项下的所有项目
  6. Click back over to the General tab, 单击返回到“常规”选项卡,
  7. Click Preview, 点击预览,
  8. Copy the content to a Notepad, 将内容复制到记事本中,
  9. You now have all of your script for re-creating the indexes / keys / constraints. 现在,您拥有了用于重新创建索引/键/约束的所有脚本。

With a bit of massaging of this chunk of script, you can make your drop constraint and drop index statements. 稍微按摩一下这段脚本,就可以创建drop constraintdrop 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM