如何删除 SQL Server 表列表,忽略约束?
我有一个包含六个 MSSQL 2008 表的列表,我想立即从数据库中删除它们。数据已完全迁移到新表。 新表中没有对旧表的引用。
问题是旧表带有大量由工具(实际上是 aspnet_regsql)自动生成的内部 FK 约束。因此,手动删除所有约束是一件非常痛苦的事情。
我怎样才能删除旧表而忽略所有内部约束?
I have a list of half a dozen MSSQL 2008 tables that I would like to remove at once from my database. The data has been entirely migrated to new tables. There is no reference in the new tables to the old tables.
The problem being that old tables comes with loads of inner FK constraints that have been autogenerated by a tool (aspnet_regsql actually). Hence dropping manually all constraints is a real pain.
How can I can drop the old tables ignoring all inner constraints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这取决于您想要如何删除表。如果需要删除的表列表几乎涵盖了数据库下 20% 以上的表。
然后,我将在脚本下禁用该数据库中的所有约束,并删除表并在同一脚本下启用约束。
最后,要检查约束的状态,请启动此查询。
如果您不想禁用数据库级别的约束,请列出要删除的表。
步骤 1:检查与这些表关联的约束 步骤
2:禁用与这些表关联的约束。
Step3:删除表
It depends on how you want to drop the tables. If list of tables need to drop covers almost above 20 % of tables under your DB.
Then I will disable all the constraints in that DB under my script and drop the tables and Enable the constraints under the same script.
Finally to check the Status of your constraints fire up this Query.
If you dont want to disable the constraints at Database level then make a list of tables which you want to drop.
Step1 : Check the Constraints associated with thos tables
Step2 : Disable the Constraints which are associated with these tables.
Step3 : Drop the tables
一个简单的
删除表 dbo.MyTable
将忽略除外键之外的所有约束(和触发器)(除非您首先删除子表/引用表),您可能必须先删除它们。编辑:评论后:
没有自动的方法。您必须迭代
sys.foreign_keys
< /a> 并生成一些 ALTER TABLE 语句。A simple
DROP TABLE dbo.MyTable
will ignore all constraints (and triggers) except foreign keys (unless you drop the child/referencing table first) where you may have to drop these first.Edit: after comment:
There is no automatic way. You'll have to iterate through
sys.foreign_keys
and generate some ALTER TABLE statements.执行以下脚本,删除当前DB下所有表的所有约束,然后执行drop table语句。
Run the following script to delete all the constraints in all tables under current DB and then run the drop table statements.
我找到了一种合理的方法来做到这一点,即让 SQL 编写 SQL 来删除约束:
您将需要修改表名以满足您的需要,或者可能选择其他列/值。
此外,这将选择任何非主键约束,这可能太大了。也许您需要将其设置为=?
我不是 DBA。可能有更好的方法来做到这一点,但它对于我的目的来说已经足够好了。
I found a reasonable(ish) way to do it by making SQL write the SQL to drop the constraints:
You will want to modify the table name to suit your needs, or possibly select against other column/values.
Also, this would select any non primary key constraint, which might be too big of a sledgehammer. Maybe you need to just set it to =?
I am not a DBA. there may be better ways to do this, but it worked well enough for my purposes.
我终于找到了基于 脚本由 Jason Presley 提供。该脚本会自动删除数据库中的所有约束。添加 WHERE 子句很容易,以便它仅适用于相关表集。之后,删除所有表就很简单了。
I finally found the solution based on the script provided by Jason Presley. This script automatically removes all constraints in the DB. It's easy to add a WHERE clause so that it only applies to the set of concerned tables. After that, dropping all tables is a straightforward.
请务必小心以下脚本、数据库中的所有表、视图、函数、存储过程和用户定义类型,忽略所有约束。
Be very careful with the following script, all tables, views, functions, stored procedures and user defined types from a database ignoring all constraints.
我怀疑您必须在删除之前对有问题的表执行“alter”命令才能删除外来键约束。
当然,如果你先删除子表,那么就不会出现这个问题。
(除非您有表 A 对表 B 的约束,表 B 对 A 的约束,那么您将需要更改其中一个表,例如 A 以删除约束)
例如,这不起作用,因为订单具有来自 Order_Lines 的约束,
例如这将工作
I suspect that you would have to do an 'alter' command on the offending tables before the drop to remove the forigen key contraints.
Of course if you drop the child tables first, then you wont have this problem.
(unless you have table A contraint to table B and table B constraint to A, then you will need to Alter one of the tables, e.g. A to remove the constraint)
e.g. this WONT work, since Orders has a contraint from Order_Lines
e.g. this will work