如何生成所有约束脚本
我有一个要求,我必须更改数据库的排序规则,因为我需要 删除所有约束并在我的数据库中运行排序规则更改脚本后重新创建它们。我可以知道如何生成数据库所有约束的脚本吗?
I have a requirement where I have to change collation of my DB for that I need to drop all the constraints and recreate them after running the collation change script at my DB. May I know how can I generatescript of all constraints of my DB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
生成所有约束的脚本
获取数据库上的所有约束,然后过滤表
script to generate all constraints
get all constraints on db then filter on your table
这将为您提供数据库中的所有约束,您可以根据需要对其进行过滤:
我认为您可能还需要查看任何索引、统计信息等,这也可能阻止您删除该列。
This will get you all the constraints in the database, you can filter it by what you need:
I think you might also need to look at any indices, statistics, etc. that might also keep you from dropping the column.
这可以通过 SQL Server Management Studio 轻松完成。
右键单击数据库并转到任务、生成脚本...。
这将打开一个脚本生成向导,该向导将为您选择的任何模式构造生成 DROP 和 CREATE 脚本。
选择数据库
确保选择脚本 Drop to True
选择表
选择新查询编辑器窗口并单击完成。
This can be done easily from SQL Server Management Studio.
Right-click on the database and go to Tasks, Generate Scripts....
This will bring up a script generation wizard that will generate DROP and CREATE scripts for whatever schema constructs that you select.
Select your database
Make sure that you select Script Drop to True
Select Tables
Select New Query Editor Window and click Finish.
您可以使用 SMO 进行类似的编程,或者更简单的是仅使用 SSMS 中的脚本向导。转到对象资源管理器中的数据库并右键单击它。选择“任务”->“生成脚本”(不是“脚本数据库为”)。在向导中,您可以确保约束已编写脚本并删除其他项目。检查生成的脚本并确保它满足您的需要。如果没有,请再次通过向导进行必要的调整。
You can program something like this using SMO or even easier is to just use the script wizard in SSMS. Go to your database in the Object Explorer and right-click on it. Select Tasks->Generate Scripts (NOT "Script Database as"). In the wizard you can make sure that constraints are scripted and remove other items. Check the generated script and make sure that it does what you need. If not, go through the wizard again making the necessary adjustments.