6

我遇到了 MySQL 5.6 InnoDb 在运行INSERT INTO xxx (col) SELECT .... 以其他格式运行插入语句时,会正确执行该约束。启用外键检查,并且sql_mode = STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION

这是一个例子:

CREATE TABLE Test_Parent
(
    id BIGINT(18) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
    dummy VARCHAR(255)
) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_unicode_ci
    COMMENT 'Test parent table';

CREATE TABLE Test_Child
(
    id BIGINT(18) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
    fid BIGINT UNSIGNED NOT NULL,
    FOREIGN KEY Fk_Test_Parent_01(fid) REFERENCES Test_Parent(id)
) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_unicode_ci
    COMMENT 'Test child table';

INSERT INTO Test_Parent(dummy)
VALUES ('test');

## Here's where the FK constraint should be enforced but isn't ##
INSERT INTO Test_Child(fid)
SELECT id
    FROM Test_Parent
WHERE dummy = 'missing value';

1 row affected in 5ms

## Running an insert with a different format, the constraint is enforced ##
INSERT INTO Test_Child(fid)
VALUES (null);
Column 'fid' cannot be null

## Running this format, the foreign key is also enforced ##
INSERT INTO Test_Child(id, fid)
VALUES (123, (SELECT id FROM Test_Parent WHERE dummy = 'missing value'));
Column 'fid' cannot be null

我不明白为什么 MySQL 会为 3 个插入语句中的 2 个强制执行外键。有任何想法吗?

4

1 回答 1

1

来自您的客户的误导性1 row affected in 5ms信息可能是这里造成混淆的根源。在您提到 IntelliJ 正在报告该消息的评论线程中,但我在 MySQL 5.6.35 和 5.7.16-ubuntu 中运行了您定义明确的测试表,并且在这两个版本中,相关语句都报告了 0 个受影响的行:

mysql > INSERT INTO Test_Child(fid)
    -> SELECT id
    ->     FROM Test_Parent
    -> WHERE dummy = 'missing value';
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

因此,查看误导性的受影响行消息,这里真正发生的是SELECT您的INSERT...SELECT语句部分不匹配任何行,因此 MySQL 不会尝试插入任何行。因此没有违反外键约束。

的格式INSERT INTO...SELECT与您后面的示例略有不同:

INSERT INTO Test_Child(id, fid)
VALUES (123, (SELECT id FROM Test_Parent WHERE dummy = 'missing value'));

...因为在这种情况下,数字文字123强制插入一行,并与null从子选择返回的值配对。因此null试图插入并导致违反约束。

如果你强制INSERT...SELECT返回一个空值的行,你可能会因为违反约束而失败:

INSERT INTO Test_Child (fid)
-- Return a literal NULL row...
SELECT NULL as id FROM Test_Parent
-- Column fid cannot be null
于 2016-12-23T01:00:21.357 回答