在插入时将唯一记录隐式插入外键 table

Implicitly insert unique records into foreign key table on insert

当在 language table 中插入记录时,是否可以在 type table 中添加或删除 type_name 条目?

我收到 #1452(外键约束)错误:

Cannot add or update a child row:
 a foreign key constraint fails (`test`.`language`,
    CONSTRAINT `language_ibfk_1`
        FOREIGN KEY (`type_name`)
        REFERENCES `type` (`type_name`)
        ON DELETE CASCADE
        ON UPDATE CASCADE)

Table 架构

CREATE TABLE IF NOT EXISTS `type` (
    `type_name` VARCHAR(128) NOT NULL UNIQUE,
    PRIMARY KEY(`type_name`)
);

CREATE TABLE IF NOT EXISTS `language` (
    `language_id` INT NOT NULL AUTO_INCREMENT,
    `language_name` VARCHAR(256) NOT NULL,
    `type_name` VARCHAR(128) NOT NULL,
    PRIMARY KEY(`language_id`),
    FOREIGN KEY (`type_name`)
        REFERENCES `type`(`type_name`)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

插入语句

INSERT INTO `language`(`language_name`, `type_name`) VALUES
('C', 'programming'),
('Java', 'programming'),
('Python', 'scripting'),
('PHP', 'scripting'),
('HTML', 'markup'),
('XML', 'markup');

由于 CASCADE 而隐式插入值。

INSERT INTO `type`(`type_name`) VALUES
('programming'),
('scripting'),
('markup');

如果你真的必须使用这样的解决方案,你可以在插入语言之前使用触发器 table:

CREATE TRIGGER trigger_name BEFORE INSERT ON language FOR EACH ROW
BEGIN

    INSERT IGNORE INTO type
            (type_name)
        VALUES
            (NEW.type_name);

END

因此,使用 piotrgajow 的响应,我想出了一种方法来规范我的 table,方法是在 type [=34] 中添加 type_index =].

我将他的触发器转换为 MariaDB/MySQL 触发器:

DELIMITER $$
CREATE TRIGGER `insert_language_type`
    BEFORE INSERT ON `language` 
    FOR EACH ROW
BEGIN
    INSERT IGNORE INTO `type`
            (`type_name`)
        VALUES
            (NEW.`type_name`);
END;$$
DELIMITER ;

我更进一步,创建了一种重新索引 type 的方法,方法是创建 type_id 列并将其与 language table 相关联.并且,在此过程中,从 language table.

中删除 type_name
/** Add the new type_id index to both tables. */
ALTER TABLE `type` ADD `type_id` INT FIRST;
ALTER TABLE `language` ADD `type_id` INT;

/** Index the type_id values.  */
SET @i = 0;
UPDATE `type` SET `type_id`=(@i:=@i+1);

/** Apply the new type_id values to the languages.  */
UPDATE `language` L, `type` T
SET L.`type_id` = T.`type_id`
WHERE L.`type_name` = T.`type_name`;

/** Remove all constraints and drop the type_name column. */
ALTER TABLE `type` DROP PRIMARY KEY;
ALTER TABLE `language` DROP FOREIGN KEY `language_ibfk_1`;
ALTER TABLE `language` DROP COLUMN `type_name`;

/** Set primary key for type and add constraint to the language. */
ALTER TABLE `type` MODIFY COLUMN `type_id` INT AUTO_INCREMENT PRIMARY KEY;
ALTER TABLE `language` ADD FOREIGN KEY (`type_id`) REFERENCES `type`(`type_id`);

/** Remove the trigger, because it is meaningless. */
DROP TRIGGER `insert_language_type`;

之前

+-------------------------------------------+   +-------------+
|                  language                 |   |    type     |
+-------------+---------------+-------------+   +-------------+
| language_id | language_name | type_name   |   | type_name   |
+-------------+---------------+-------------+   +-------------+
|           1 | C             | programming |   | markup      |
|           2 | Java          | programming |   | programming |
|           3 | Python        | scripting   |   | scripting   |
|           4 | PHP           | scripting   |   +-------------+
|           5 | HTML          | markup      |
|           6 | XML           | markup      |
+-------------+---------------+-------------+

之后

+---------------------------------------+   +-------------------- --+
|               language                |   |         type          |
+-------------+---------------+---------+   +---------+-------------+
| language_id | language_name | type_id |   | type_id | type_name   |
+-------------+---------------+---------+   +---------+-------------+
|           1 | C             |       2 |   |       1 | markup      |
|           2 | Java          |       2 |   |       2 | programming |
|           3 | Python        |       3 |   |       3 | scripting   |
|           4 | PHP           |       3 |   +---------+-------------+
|           5 | HTML          |       1 |
|           6 | XML           |       1 |
+-------------+---------------+---------+