当在type_name
表中插入记录时,是否可以在type
表中添加或删除language
条目?
我收到#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)
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');
答案 0 :(得分:1)
如果你真的必须使用这样的解决方案,你可以在插入语言表之前使用触发器:
CREATE TRIGGER trigger_name BEFORE INSERT ON language FOR EACH ROW
BEGIN
INSERT IGNORE INTO type
(type_name)
VALUES
(NEW.type_name);
END
答案 1 :(得分:0)
因此,使用piotrgajow的回复,我想出了一种方法,通过在type_index
表中添加type
来规范化我的表格。
我将他的触发器转换为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
索引的方法。并且,在此过程中,从type_name
表中删除language
列。
/** 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 |
+-------------+---------------+---------+