SQL,如何更改 SQL 表中的列而不破坏其他依赖关系?

发布于 2024-09-06 02:36:11 字数 625 浏览 8 评论 0原文

我确信这可能是很常见的查询,但目前找不到好的答案。

这是我的问题:

我有一个名为 Contacts 的表,其中包含 varchar 列 Title。现在在开发过程中,我想将字段 Title 替换为 TitleID,它是 ContactTitles 表的外键。目前表 Contacts 有 60 多个依赖项(其他表、视图函数)。

我怎样才能以最安全、最简单的方式做到这一点?

我们使用:MSSQL 2005,数据已经迁移,只是想更改架构。

编辑:

感谢大家的快速重播。

正如前面提到的,Contacts 表有超过 60 个依赖项,但是当运行以下查询时,其中只有 5 个使用 Title 列。迁移脚本已运行,因此无需更改数据。

<代码> /*获取所有使用指定列的对象*/

> <代码> 选择名称 FROM 系统注释 sc JOIN sysobjects so ON sc.id = so.id 其中“%Title%”等文本和“%TitleID%”等文本

然后我浏览了这 5 个视图并手动更新了它们。

I'm sure this might be quite common query but couldn't find good answer as for now.

Here is my question:

I've got a table named Contacts with varchar column Title. Now in the middle of development I want to replace field Title with TitleID which is foreign key to ContactTitles table. At the moment table Contacts has over 60 dependencies (other tables, views functions).

How can I do that the safest and easiest way?

We use: MSSQL 2005, data has already been migrated, just want to change schema.

Edit:

Thanks to All for quick replay.

Like it was mentioned Contacts table has over 60 dependents, but when following query was run, only 5 of them use Title column. Migration script was run, so no data changes required.


/*gets all objects which use specified column */


SELECT Name
FROM syscomments sc
JOIN sysobjects so ON sc.id = so.id
WHERE TEXT LIKE '%Title%' AND TEXT LIKE '%TitleID%'

Then I went through those 5 views and updated them manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

久光 2024-09-13 02:36:11

使用重构方法。首先创建一个名为 TitleID 的新字段,然后将所有标题复制到 ContactTitles 表中。然后,一一更新每个依赖项以使用 TitleID 字段。只需确保每个步骤后您仍然拥有一个可以运行的系统即可。

如果数据将发生变化,您必须小心并确保对 Title 列的任何更改也会更改 ContactTitles 表。您只需在进行重构时保持它们同步即可。

编辑:甚至有一本书是关于它的! 重构数据库

Use refactoring methods. Start off by creating a new field called TitleID, then copy all the titles into the ContactTitles table. Then, one by one, update each of the dependencies to use the TitleID field. Just make sure you've still got a working system after each step.

If the data is going to be changing, you'll have to be careful and make sure that any changes to the Title column also change the ContactTitles table. You'll only have to keep them in sync while you're doing the refactoring.

Edit: There's even a book about it! Refactoring Databases.

栖迟 2024-09-13 02:36:11

正如其他人指出的那样,这取决于您的 RDBMS。

有两种方法:

  • 对表进行更改并修复所有依赖项
  • 创建一个可以使用的视图,而不是直接访问表(这可以防止您将来对底层核心表进行更改,但您可能会丢失一些更新功能,具体取决于您的 DBMS)

As others pointed out it depends on your RDBMS.

There are two approaches:

  • make a change to the table and fix all dependencies
  • make a view that you can use instead of direct access to the table (this can guard you against future changes in the underlying core table(s), but you might loose some update functionality, depending on your DBMS)
゛清羽墨安 2024-09-13 02:36:11

对于 Microsoft SQL Server Redgate 有一个(非免费)产品可以帮助进行此重构 http://www.red-gate.com/products/sql_refactor/index.htm

在过去,我通过简单地获取要审查的内容列表

SELECT * FROM sys.objects
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Contacts%' 

(并且可能是考虑依赖关系信息并按对象类型进行过滤)

在 Management Studio 中编写所有感兴趣的脚本,然后简单地查看列表并查看所有内容并将 CREATE 更改为 ALTER。即使对于 60 个可能的依赖项,这也应该是一个非常简单且重复的更改。此外,如果您引用不存在的列,则在运行脚本进行 ALTER 时应该会收到错误消息。

如果您在查询中使用 * 或在应用程序中使用即席 SQL,显然事情可能会更困难一些。

For Microsoft SQL Server Redgate have a (not free) product that can help with this refactoring http://www.red-gate.com/products/sql_refactor/index.htm

In the past I have managed to do this quite easily (if primitively) by simply getting a list of things to review

SELECT * FROM sys.objects
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Contacts%' 

(and possibly taking dependencies information into account and filtering by object type)

Scripting all the ones of interest in Management Studio then simply going down the list and reviewing them all and changing the CREATE to ALTER. It should be quite a simple and repetitive change even for 60 possible dependencies. Additionally if you are referring to a non existent column you should get an error message when you run the script to ALTER.

If you use * in your queries or adhoc SQL in your applications obviously things may be a bit more difficult.

书间行客 2024-09-13 02:36:11

使用 SP_Depend '表名称' 检查表的依赖关系
然后使用 SP_Rename 重命名列名,这非常有用。
每当重命名 PRIMARY KEY 或 UNIQUE 约束时,sp_rename 都会自动重命名关联的索引。如果重命名的索引与 PRIMARY KEY 约束相关联,则 sp_rename 也会自动重命名 PRIMARY KEY 约束。

然后开始一一更新过程和函数,没有其他好的选择可以像这样进行更改,如果您发现也请告诉我。

Use SP_Depend 'Table Name' to check the Dependencies of the table
and then Use the SP_Rename to Rename the Column Name which is very useful.
sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the PRIMARY KEY constraint is also automatically renamed by sp_rename.

and then start Updating the Procedure and Functions one by one there is no other good option for change like this if you found then tell me too.

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