需要 MySQL 4 来忽略 ALTER TABLE 错误
我有一个 MySQL 脚本,它在某些条件下自动执行。 该脚本执行 ALTER TABLE 命令,因为数据库中需要该列,但它可能有也可能没有......
是否可以让 MySQL 4 执行 ALTER TABLE< /code> 语句(如果列不存在)或忽略此单个命令的重复列错误并允许脚本继续执行?
I have a MySQL script which is executed automatically under certain conditions. That script executes an ALTER TABLE
command, because that column is needed in the database, but it may or may not have it...
Is it possible to make MySQL 4 execute the ALTER TABLE
statement if the column doesn't exist or ignore the duplicate column error for this single command and allow the script execution to continue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ALTER [IGNORE] TABLE 只会忽略某些错误,例如添加新 UNIQUE 索引时的重复键错误或 SQL 模式错误。
http://dev.mysql.com/doc/refman/ 4.1/en/alter-table.html
有关您正在使用的“脚本”的更多详细信息将有助于回答该问题。 例如,在 python 中,错误会引发异常,然后可以捕获并处理或忽略该异常。
[编辑] 从下面的评论来看,您似乎正在寻找 mysql -f 命令行选项。
ALTER [IGNORE] TABLE will only ignore certain errors, like duplicate key errors when adding a new UNIQUE index, or SQL mode errors.
http://dev.mysql.com/doc/refman/4.1/en/alter-table.html
More details about the "script" you are using would help to answer the question. In python for example, the error would raise an exception which could then be caught and dealt with or ignored.
[EDIT] From the comment below, seems like you're looking for the mysql -f command line option.
您可以在尝试添加列之前先检查表架构吗? 但是,我强烈怀疑您需要动态添加列的设计。 有些事情不太对劲。 您能详细解释一下这个要求吗? 我确信还有其他更清洁的方法可以解决这个问题。
You can first check the table schema before you attempt an addition of the column? However , I strongly suspect the design where you need to add columns on the fly. Something is not quite right. Can you explain the requirement in a little detail. I'm sure there are other cleaner way around this.