如何生成所有约束脚本

发布于 2024-11-09 15:24:49 字数 182 浏览 0 评论 0原文

我有一个要求,我必须更改数据库的排序规则,因为我需要 删除所有约束并在我的数据库中运行排序规则更改脚本后重新创建它们。我可以知道如何生成数据库所有约束的脚本吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲琵琶半遮面シ 2024-11-16 15:24:49
SELECT top 1 
    'ALTER TABLE '+ SCHEMA_NAME(schema_id) + '.' + OBJECT_NAME(parent_object_id) + 
    ' ADD CONSTRAINT ' + dc.name + ' DEFAULT(' + definition 
    + ') FOR ' + c.name
FROM sys.default_constraints dc
INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id

生成所有约束的脚本

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 

获取数据库上的所有约束,然后过滤表

SELECT top 1 
    'ALTER TABLE '+ SCHEMA_NAME(schema_id) + '.' + OBJECT_NAME(parent_object_id) + 
    ' ADD CONSTRAINT ' + dc.name + ' DEFAULT(' + definition 
    + ') FOR ' + c.name
FROM sys.default_constraints dc
INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id

script to generate all constraints

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 

get all constraints on db then filter on your table

浅唱ヾ落雨殇 2024-11-16 15:24:49

这将为您提供数据库中的所有约束,您可以根据需要对其进行过滤:

SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'

我认为您可能还需要查看任何索引、统计信息等,这也可能阻止您删除该列。

This will get you all the constraints in the database, you can filter it by what you need:

SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'

I think you might also need to look at any indices, statistics, etc. that might also keep you from dropping the column.

伴我老 2024-11-16 15:24:49

这可以通过 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.

动听の歌 2024-11-16 15:24:49

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文