且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用SQL语句更改忽略表添加列(如果不存在)

更新时间:2023-10-10 21:22:58

根据文档:

IGNORE是对标准SQL的MySQL扩展.它控制着如何改变 如果新表中的唯一键上有重复项,则TABLE起作用 如果在启用严格模式时发生警告.如果不是IGNORE 指定,如果重复键错误,副本将中止并回滚 发生.如果指定了IGNORE,则只有一行用于 在唯一键上重复.其他冲突的行将被删除. 不正确的值将被截断为最接近可接受的匹配值 值.

IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only one row is used of rows with duplicates on a unique key. The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value.

也就是说,它用于不同的目的,而不是所有的错误.您想要的是类似ALTER TABLE ADD IF NOT EXISTS的东西,并且该语法不存在.

That is, it is used for a different purpose, not all errors. What you want is something like ALTER TABLE ADD IF NOT EXISTS, and that syntax doesn't exist.

在存储过程中,可以使用if语句查看该列是否已经存在(使用INFORMATION_SCHEMA.COLUMNS).

In a stored procedure, you can use an if statement to see if the column already exists (using INFORMATION_SCHEMA.COLUMNS).

此处是显示相同问题的SQL提琴.

Here is a SQL Fiddle that shows the same problem.