重命名索引列

发布于 2024-09-03 09:41:11 字数 378 浏览 6 评论 0原文

我有一个包含大约 40 个表的数据库,需要重命名每个索引列。

IE USER 一个表有一堆字段,就像

user_id | user_username | user_password | etc...

我想将 ID 列重命名为 id ie

id | user_username | user_password | etc...

但我在 alter table 命令上不断收到 mysql 错误。

ALTER TABLE 数据库将列 user_id 重命名为 id;

还有许多不同的变体。

最好的方法是什么?

希望大家多多指教

I have a database with around 40 tables and need to rename every index column.

IE USER a table has a bunch of fields like

user_id | user_username | user_password | etc...

I want to rename the ID columns just to id ie

id | user_username | user_password | etc...

But I keep getting mysql errors on the alter table command ie.

ALTER TABLE database RENAME COLUMN user_id to id;

Plus many different variations.

Whats the best way to do this ?

Hope you can advise

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

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

发布评论

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

评论(3

执笔绘流年 2024-09-10 09:41:12

您应该能够使用如下代码为每个表执行此操作(注意:我假设每个 *_id 字段的列描述为 INTEGER NOT NULL 并且这些相同的字段设置了 AUTO_INCRMENT ,这对于 ID 列来说是很正常的。)

# remove auto_increment, allows us to remove primary key
ALTER TABLE user MODIFY user_id INTEGER NOT NULL;

# remove primary key, allows us to change column name
ALTER TABLE user DROP PRIMARY KEY;

# change column name, re-add auto_increment and primary key
ALTER TABLE user CHANGE user_id id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY;

不过,请注意其他人的警告。除非这是您的创建(并且您刚刚决定在项目或其他内容的早期更改命名约定),否则更改此设置将破坏期望与此数据库交互的任何其他代码。

希望这有帮助,

安东尼

编辑:上面假设您没有外键约束,其中其他表显式引用 user.user_id 列。如果不是这种情况,您将必须在更改名称之前删除所有外键约束,然后重新添加它们。如果您不确定数据库中哪里存在外键关系,您可以使用以下代码列出所有外键关系(复制自 http://www.xinotes.org/notes/note/477/ 以提高答案的复制/粘贴能力):

SELECT CONCAT(table_name, '.', column_name, ' -> ', referenced_table_name, '.', referenced_column_name)
FROM information_schema.key_column_usage
WHERE referenced_table_schema = schema()
AND referenced_table_name is not null
ORDER BY table_name, column_name;

You should be able to do it, for each table, with code like the following (Note: I assume that the column description for each *_id field is INTEGER NOT NULL and that those same fields have AUTO_INCREMENT set. This is pretty normal for an ID column.)

# remove auto_increment, allows us to remove primary key
ALTER TABLE user MODIFY user_id INTEGER NOT NULL;

# remove primary key, allows us to change column name
ALTER TABLE user DROP PRIMARY KEY;

# change column name, re-add auto_increment and primary key
ALTER TABLE user CHANGE user_id id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY;

Heed the other guys' warnings, though. Unless this is your creation (and you've just decided to change naming convention early on in the project or something), changing this will break any other code that expects to interface with this database.

Hope this helps,

Anthony

EDIT: the above assumes that you have no foreign key constraints, where other tables explicitly reference the user.user_id column. If this is not the case, you will have to remove the all of the foreign key constraints before changing the name, then re-add them afterwards. If you're unsure where there are foreign key relationships in your database, you can list them all with the following code (copied from http://www.xinotes.org/notes/note/477/ to improve copy/paste-ability of answer):

SELECT CONCAT(table_name, '.', column_name, ' -> ', referenced_table_name, '.', referenced_column_name)
FROM information_schema.key_column_usage
WHERE referenced_table_schema = schema()
AND referenced_table_name is not null
ORDER BY table_name, column_name;
感悟人生的甜 2024-09-10 09:41:11

您必须删除所有外键约束,更改主键,然后再次添加约束。

顺便说一句,更改主键是一件非常严肃的事情,只有在确实需要时才应该这样做。此更改将破坏使用您的数据库的每个应用程序。

You will have to drop all your foreign key constraints, change the primary keys, then add the constraints again.

By the way, changing the primary keys is a very serious thing, and you should only do it if you really, really need to. This change will break every application that uses your database.

贱贱哒 2024-09-10 09:41:11

如果我是你,我就不会那样做。在这样做之前,我会弄清楚如何改变你正在做的任何事情。除非您是数据库结构的工程师或使用数据库的软件的工程师,否则永远不会建议或推荐这一点。这样做就像把糖、水、可乐和水放在一起。油箱里装了沙子,然后说道:“天哪,会出什么问题吗?”

即使上面那个人所说的关于放弃约束并将它们全部放回原处。这对于数据库工程/结构来说一切都很好,但即使完成了这些,使用它们的应用程序也有自己的一系列问题。对数据库列名称进行搜索替换也不起作用。特别是对于您有时看到的命令结构,这样做只会弄乱代码。

I would not do that if I were you. I would figure out how to change whatever you need to in whatever you are doing before doing that. That is never ever suggested or recommended unless you are the engineer of the database structure or the engineer of the software using it. Doing that is like putting sugar,water,coke & sand in a gas tank and saying, "Gee, what could go wrong?"

Even what the guy said above about dropping constraints and placing them all back. That is all fine and good for the database engineering/structure but even with that done, the applications using them have their own set of problems. Doing a search replace for database column names will not work either. Especially with the command structure you see sometime, you will only mess the code up doing that.

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