<tfoot id='g4fXU'></tfoot>

<i id='g4fXU'><tr id='g4fXU'><dt id='g4fXU'><q id='g4fXU'><span id='g4fXU'><b id='g4fXU'><form id='g4fXU'><ins id='g4fXU'></ins><ul id='g4fXU'></ul><sub id='g4fXU'></sub></form><legend id='g4fXU'></legend><bdo id='g4fXU'><pre id='g4fXU'><center id='g4fXU'></center></pre></bdo></b><th id='g4fXU'></th></span></q></dt></tr></i><div id='g4fXU'><tfoot id='g4fXU'></tfoot><dl id='g4fXU'><fieldset id='g4fXU'></fieldset></dl></div>

      <bdo id='g4fXU'></bdo><ul id='g4fXU'></ul>

    <small id='g4fXU'></small><noframes id='g4fXU'>

    1. <legend id='g4fXU'><style id='g4fXU'><dir id='g4fXU'><q id='g4fXU'></q></dir></style></legend>
    2. 如何使用 T-SQL 临时禁用外键约束?

      How can foreign key constraints be temporarily disabled using T-SQL?(如何使用 T-SQL 临时禁用外键约束?)
      • <bdo id='2YPMd'></bdo><ul id='2YPMd'></ul>

            <tfoot id='2YPMd'></tfoot>

            <small id='2YPMd'></small><noframes id='2YPMd'>

                <tbody id='2YPMd'></tbody>
              <i id='2YPMd'><tr id='2YPMd'><dt id='2YPMd'><q id='2YPMd'><span id='2YPMd'><b id='2YPMd'><form id='2YPMd'><ins id='2YPMd'></ins><ul id='2YPMd'></ul><sub id='2YPMd'></sub></form><legend id='2YPMd'></legend><bdo id='2YPMd'><pre id='2YPMd'><center id='2YPMd'></center></pre></bdo></b><th id='2YPMd'></th></span></q></dt></tr></i><div id='2YPMd'><tfoot id='2YPMd'></tfoot><dl id='2YPMd'><fieldset id='2YPMd'></fieldset></dl></div>
              1. <legend id='2YPMd'><style id='2YPMd'><dir id='2YPMd'><q id='2YPMd'></q></dir></style></legend>
                本文介绍了如何使用 T-SQL 临时禁用外键约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 SQL Server 中是否支持禁用和启用外键约束?或者是我删除然后重新创建约束的唯一选择?

                Are disabling and enabling foreign key constraints supported in SQL Server? Or is my only option to drop and then re-create the constraints?

                推荐答案

                如果您想禁用数据库中的所有约束,只需运行以下代码:

                If you want to disable all constraints in the database just run this code:

                -- disable all constraints
                EXEC sp_MSforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
                

                要重新打开它们,请运行:(打印当然是可选的,它只是列出表格)

                To switch them back on, run: (the print is optional of course and it is just listing the tables)

                -- enable all constraints
                exec sp_MSforeachtable @command1="print '?'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
                

                我发现将数据从一个数据库填充到另一个数据库时很有用.这是比删除约束更好的方法.正如您所提到的,在删除数据库中的所有数据并重新填充它时(例如在测试环境中),它会派上用场.

                I find it useful when populating data from one database to another. It is much better approach than dropping constraints. As you mentioned it comes handy when dropping all the data in the database and repopulating it (say in test environment).

                如果您要删除所有数据,您可能会发现 这个解决方案很有帮助.

                If you are deleting all the data you may find this solution to be helpful.

                有时禁用所有触发器也很方便,您可以查看完整的解决方案 此处.

                Also sometimes it is handy to disable all triggers as well, you can see the complete solution here.

                这篇关于如何使用 T-SQL 临时禁用外键约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                SQL query to group by day(按天分组的 SQL 查询)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                sql group by versus distinct(sql group by 与不同)
                How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)
                Count number of records returned by group by(统计分组返回的记录数)
                SQL GROUP BY CASE statement with aggregate function(带聚合函数的 SQL GROUP BY CASE 语句)

                    <tbody id='x8C9p'></tbody>
                      <i id='x8C9p'><tr id='x8C9p'><dt id='x8C9p'><q id='x8C9p'><span id='x8C9p'><b id='x8C9p'><form id='x8C9p'><ins id='x8C9p'></ins><ul id='x8C9p'></ul><sub id='x8C9p'></sub></form><legend id='x8C9p'></legend><bdo id='x8C9p'><pre id='x8C9p'><center id='x8C9p'></center></pre></bdo></b><th id='x8C9p'></th></span></q></dt></tr></i><div id='x8C9p'><tfoot id='x8C9p'></tfoot><dl id='x8C9p'><fieldset id='x8C9p'></fieldset></dl></div>
                        <bdo id='x8C9p'></bdo><ul id='x8C9p'></ul>
                        1. <small id='x8C9p'></small><noframes id='x8C9p'>

                        2. <tfoot id='x8C9p'></tfoot>
                          <legend id='x8C9p'><style id='x8C9p'><dir id='x8C9p'><q id='x8C9p'></q></dir></style></legend>