简体   繁体   English

MySQL:ALTER IGNORE TABLE ADD UNIQUE,什么会被截断?

[英]MySQL: ALTER IGNORE TABLE ADD UNIQUE, what will be truncated?

I have a table with 4 columns: ID, type, owner, description.我有一个包含 4 列的表:ID、类型、所有者、描述。 ID is AUTO_INCREMENT PRIMARY KEY and now I want to: ID 是 AUTO_INCREMENT PRIMARY KEY,现在我想:

ALTER IGNORE TABLE `my_table`
    ADD UNIQUE (`type`, `owner`);

Of course I have few records with type = 'Apple' and owner = 'Apple CO'.当然,我的 type = 'Apple' 和 owner = 'Apple CO' 的记录很少。 So my question is which record will be the special one to stay after that ALTER TABLE, the one with smallest ID or maybe the one with biggest as the latest inserted?所以我的问题是,哪条记录将是在 ALTER TABLE 之后保留的特殊记录,是 ID 最小的记录还是最新插入的最大的记录?

The first record will be kept, the rest deleted §§ :第一个记录将被保留,其余的被删除§§

IGNORE is a MySQL extension to standard SQL. IGNORE是标准 SQL 的 MySQL 扩展。 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.如果新表中的唯一键存在重复项,或者在启用严格模式时出现警告,它会控制 ALTER TABLE 的工作方式。 If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur.如果未指定IGNORE ,则在发生重复键错误时中止并回滚副本。 If IGNORE is specified, only the first row is used of rows with duplicates on a unique key, The other conflicting rows are deleted .如果指定了IGNORE ,则只有第一行用于在唯一键上具有重复项的行,删除其他冲突行。 Incorrect values are truncated to the closest matching acceptable value不正确的值被截断为最接近匹配的可接受值

I am guessing 'first' here means the one with the smallest ID, assuming the ID is the primary key.我猜这里的“第一个”是指 ID 最小的那个,假设 ID 是主键。

Also note:另请注意:

As of MySQL 5.7.4, the IGNORE clause for ALTER TABLE is removed and its use produces an error .从 MySQL 5.7.4 开始, ALTER TABLEIGNORE子句被删除,它的使用会产生错误

It appears that your problem is one of the very reasons that ALTER IGNORE has been deprecated.看来您的问题是 ALTER IGNORE 已被弃用的原因之一。

This is from the MySQL notes on the ALTER IGNORE deprecation:这是来自关于 ALTER IGNORE 弃用的MySQL 注释

"This feature is badly defined (what is the first row?), causes problems for replication, disables online alter for unique index creation and has caused problems with foreign keys (rows removed in parent table)." “此功能定义错误(第一行是什么?),导致复制问题,禁用在线更改以创建唯一索引,并导致外键问题(从父表中删除的行)。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM