使用 ALTER 删除 MySQL 中存在的列
如果 MySQL 表中存在某列,如何使用 ALTER 删除该列?
我知道我可以使用ALTER TABLE my_table DROP COLUMN my_column
,但如果my_column
不存在,则会抛出错误。 是否有替代语法来有条件地删除列?
我正在使用 MySQL 版本 4.0.18。
How can ALTER be used to drop a column in a MySQL table if that column exists?
I know I can use ALTER TABLE my_table DROP COLUMN my_column
, but that will throw an error if my_column
does not exist. Is there alternative syntax for dropping the column conditionally?
I'm using MySQL version 4.0.18.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于 MySQL,没有: MySQL 功能请求 。
无论如何,允许这样做可以说是一个非常糟糕的主意:
IF EXISTS
表明您正在对结构未知的数据库运行破坏性操作。 在某些情况下,这对于快速而肮脏的本地工作来说是可以接受的,但如果您想针对生产数据(在迁移等中)运行这样的语句,那么您就是在玩火。但如果您坚持的话,首先在客户端检查是否存在或捕获错误并不困难。
从 10.0.2 开始,MariaDB 还支持以下功能:
即
但是依赖于仅由 MySQL 的几个分支之一支持的非标准功能可能是一个坏主意。
For MySQL, there is none: MySQL Feature Request.
Allowing this is arguably a really bad idea, anyway:
IF EXISTS
indicates that you're running destructive operations on a database with (to you) unknown structure. There may be situations where this is acceptable for quick-and-dirty local work, but if you're tempted to run such a statement against production data (in a migration etc.), you're playing with fire.But if you insist, it's not difficult to simply check for existence first in the client, or to catch the error.
MariaDB also supports the following starting with 10.0.2:
i. e.
But it's arguably a bad idea to rely on a non-standard feature supported by only one of several forks of MySQL.
MySQL 中没有对此的语言级别支持。 这是 5.0+ 中涉及 MySQL information_schema 元数据的解决方法,但它不会解决 4.0.18 中的问题。
我在 博客文章。
There is no language level support for this in MySQL. Here is a work-around involving MySQL information_schema meta-data in 5.0+, but it won't address your issue in 4.0.18.
I wrote some more detailed information in a blog post.
我知道这是一个旧线程,但是有一种简单的方法可以在不使用存储过程的情况下处理此要求。 这可能会对某人有所帮助。
希望这对某人有帮助,就像对我一样(经过大量的尝试和错误)。
I know this is an old thread, but there is a simple way to handle this requirement without using stored procedures. This may help someone.
Hope this helps someone, as it did me (after a lot of trial and error).
我刚刚构建了一个可重用的过程,可以帮助使
DROP COLUMN
幂等:用法:
示例:
I just built a reusable procedure that can help making
DROP COLUMN
idempotent:Usage:
Example:
Chase Seibert 的答案有效,但我要补充一点,如果您有多个模式,则需要更改 SELECT:
Chase Seibert's answer works, but I'd add that if you have several schemata you want to alter the SELECT thus:
您可以使用此脚本,使用您的列、架构和表名称
You can use this script, use your column, schema and table name
也许解决这个问题的最简单方法(可行)是:
CREATE new_table AS SELECT id, col1, col2, ...(仅在最终表中实际想要的列)
FROM my_table;
将 my_table 重命名为 old_table,将 new_table 重命名为 my_table;
DROP old_table;
或者保留 old_table 以供需要时回滚。
这可以工作,但外键不会被移动。 您必须稍后将它们重新添加到 my_table 中; 引用 my_table 的其他表中的外键也必须修复(指向新的 my_table)。
祝你好运...
Perhaps the simplest way to solve this (that will work) is:
CREATE new_table AS SELECT id, col1, col2, ... (only the columns you actually want in the final table)
FROM my_table;
RENAME my_table TO old_table, new_table TO my_table;
DROP old_table;
Or keep old_table for a rollback if needed.
This will work but foreign keys will not be moved. You would have to re-add them to my_table later; also foreign keys in other tables that reference my_table will have to be fixed (pointed to the new my_table).
Good Luck...
这只是所有好的答案的摘要:
IF EXISTS
not isn'tiens inALTER TABEL your_table DROP COLUMN your_column
测试:
This is just a summary of all the good answers:
IF EXISTS
not exists inALTER TABEL your_table DROP COLUMN your_column
Test:
非常简单的方法。 您只需更改第 2 行和第 3 行中的“my_table”和“my_column”即可。
无需任何存储过程等。
Very easy way. You just need to change "my_table" and "my_column" from line 2 and 3.
Without any store procedures etc.
我意识到这个线程现在已经很老了,但我也遇到了同样的问题。
这是我使用 MySQL Workbench 的非常基本的解决方案,但它工作得很好...
x
DROPa
;任何具有该表但现在还没有的表
任何没有的表都会在日志中显示错误,
然后您可以找到/替换“drop
a
”,将其更改为“ADD COLUMNb
INT NULL”等再次运行整个过程......有点笨拙,但最后你得到了最终结果,你可以控制/监视整个过程,并记住保存 sql 脚本,以防你再次需要它们。
I realise this thread is quite old now, but I was having the same problem.
This was my very basic solution using the MySQL Workbench, but it worked fine...
x
DROPa
;any tables which had the table now haven't
any tables which didn't will have shown an error in the logs
then you can find/replace 'drop
a
' change it to 'ADD COLUMNb
INT NULL' etc and run the whole thing again....a bit clunky, but at last you get the end result and you can control/monitor the whole process and remember to save you sql scripts in case you need them again.