在 SQL Server 中的其他两列之间添加列

发布于 2024-10-22 17:16:22 字数 60 浏览 3 评论 0原文

您是否可以向表中添加一列,并将其插入 SQL Server 中的两个现有列之间,而无需删除并重新创建该表?

Can you add a column to a table inserting it in between two existing columns in SQL Server without dropping and re-creating the table?

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

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

发布评论

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

评论(8

药祭#氼 2024-10-29 17:16:22

中等长度的答案,是的(ish),但它很丑陋,你可能不想这样做。

请注意:此代码创建一个物理表

CREATE TABLE MyTest (a int, b int, d int, e int)

INSERT INTO MyTest (a,b,d,e) VALUES(1,2,4,5)

SELECT * FROM MyTest

ALTER TABLE MyTest ADD c int
ALTER TABLE MyTest ADD d_new int
ALTER TABLE MyTest ADD e_new int

UPDATE MyTest SET d_new = d, e_new = e

ALTER TABLE MyTest DROP COLUMN d
ALTER TABLE MyTest DROP COLUMN e

EXEC SP_RENAME 'MyTest.d_new', 'd';
EXEC SP_RENAME 'MyTest.e_new', 'e';

SELECT * FROM MyTest 

DROP TABLE MyTest

Mediumly long answer, yes (ish) but it's ugly and you probably wouldn't want to do it.

please note: this code creates a physical table

CREATE TABLE MyTest (a int, b int, d int, e int)

INSERT INTO MyTest (a,b,d,e) VALUES(1,2,4,5)

SELECT * FROM MyTest

ALTER TABLE MyTest ADD c int
ALTER TABLE MyTest ADD d_new int
ALTER TABLE MyTest ADD e_new int

UPDATE MyTest SET d_new = d, e_new = e

ALTER TABLE MyTest DROP COLUMN d
ALTER TABLE MyTest DROP COLUMN e

EXEC SP_RENAME 'MyTest.d_new', 'd';
EXEC SP_RENAME 'MyTest.e_new', 'e';

SELECT * FROM MyTest 

DROP TABLE MyTest
2024-10-29 17:16:22

简单的答案是否定的。列顺序对您来说很重要,有什么原因吗?

The simple answer is no. Is there a reason why column order is important to you?

罪#恶を代价 2024-10-29 17:16:22

是的,您可以在此处添加您关心的查询:

ALTER table table_name ADD column column_name(new) datatype AFTER column_name

Yes you can add here is the query for your concern:

ALTER table table_name ADD column column_name(new) datatype AFTER column_name
败给现实 2024-10-29 17:16:22

看一下这个链接:

http ://www.bobsgear.com/display/ts/Adding+Column+After+Another+Column+-+SQL+Server+2005

如您所见,答案是:

'不移动就不可能将数据保存到临时表中”

这就是 SQL Server Management Studio 实际执行的操作。

Take a look at this link:

http://www.bobsgear.com/display/ts/Adding+Column+After+Another+Column+-+SQL+Server+2005

As you can see, the answer is:

'not possible without moving data to a temp table'

which is what the SQL Server Management Studio actually does.

岁月流歌 2024-10-29 17:16:22

是的。您可以在设计模式下拖放它,或者在编辑表模式下复制并粘贴它


以响应评论,是的,这是通过 UI 使用 SQL Server Management Studio,在幕后,它正在执行删除/重新创建您的操作。我们试图避免,所以不,如果没有服务器为您做这件事,您就无法做到这一点。

yes. You can drag and drop it in design mode, or copy and paste it in edit table mode


in response to comments, yes, this is with SQL Server Management Studio through the UI, and under the hood, it's doing the drop/recreate you're trying to avoid, so no, you can't do it without the server doing it for you.

虫児飞 2024-10-29 17:16:22

这在 SQL Server Management Studio (SSMS) 中是可能的。
在对象资源管理器中过滤表格,然后右键单击表格 >设计

enter此处的图像描述

拖动左侧突出显示的箭头来移动您的列。

This is possible in SQL Server Management Studio (SSMS).
Filter your table in Object Explorer and right click on the table > Design

enter image description here

Drag the arrow highlighted in the left to move your column.

要走就滚别墨迹 2024-10-29 17:16:22

首先回答,没有。

第二个答案,根据此线程 http://www.sqlteam.com/forums /topic.asp?TOPIC_ID=58912,通过 可以找到我在逻辑上对表中的列重新排序?,您可以创建该列,然后通过编辑系统表对表中的列重新排序。但它看起来非常混乱,我不相信事情不会被破坏。

First answer, no.

Second answer, according to this thread http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=58912, found via Can I logically reorder columns in a table?, you could create the column then reorder the columns in the table by editing the system tables. But it looks incredibly messy and I would not trust that things would not break.

最终幸福 2024-10-29 17:16:22

对于 Mysql 是 对于 SQL Server 否
此外,在两者之间添加列也不是一个好习惯,因为如果您依赖项目中的表架构,它可能会妨碍某些查询。

For Mysql yes For SQL server No
also it's not a good practice to add column in between as it can hamper some queries if you are relying on the table schema in your project.

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