我在两个表之间创建外键约束时遇到问题。
这是第一张表:
CREATE TABLE `agews_rifiuti_cer` (
`id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,
`agews_id` int(10) unsigned DEFAULT '0',
`livello` tinyint(4) DEFAULT '1',
`codice` varchar(10) DEFAULT NULL,
`descrizione` varchar(255) DEFAULT NULL,
`note` text,
`flag_pericoloso` tinyint(1) DEFAULT '0',
`id_cliente` int(10) unsigned DEFAULT '1',
`flag_modificato` char(1) DEFAULT 'N',
PRIMARY KEY (`id_cer`),
KEY `fk_id_cliente_agews_sgs_codici_cer` (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是第二个:
CREATE TABLE `lin_98_47_rifiuti` (
`id_rifiuto` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_azienda` int(10) unsigned NOT NULL DEFAULT '1',
`id_sede` int(10) unsigned NOT NULL DEFAULT '1',
`revisione_documento` int(10) unsigned NOT NULL DEFAULT '0',
`rifiuto` varchar(255) DEFAULT NULL,
`codice_cer` varchar(10) DEFAULT NULL,
`nome_interno` varchar(255) DEFAULT NULL,
`descrizione` text,
`materie_prime` text,
`contenitore` text,
`deposito` text,
`data_ultima_analisi` date DEFAULT NULL,
`stato_fisico` enum('Solido pulverulento','Solido non pulverulento','Fangoso palabile','Liquido') DEFAULT 'Solido non pulverulento',
`quantita` float(9,1) DEFAULT '0.0',
`unita_misura` enum('Kg','l','mc') DEFAULT 'Kg',
`id_pericolo` int(10) unsigned DEFAULT NULL,
`destino` enum('Recupero','Smaltimento') DEFAULT NULL,
`id_recupero` int(10) unsigned DEFAULT NULL,
`id_smaltimento` int(10) unsigned DEFAULT NULL,
`id_cer` int(10) unsigned DEFAULT NULL,
`immagine` varchar(255) DEFAULT NULL,
`image_type` varchar(20) DEFAULT NULL,
`image_content` mediumblob,
`image_size_x` smallint(5) unsigned DEFAULT '0',
`image_size_y` smallint(5) unsigned DEFAULT '0',
`flag_storico` tinyint(1) DEFAULT '0',
`id_responsabile` int(10) unsigned DEFAULT '0',
`nome_responsabile` varchar(255) DEFAULT '0',
`id_ultima_modifica` int(10) unsigned DEFAULT '0',
`create_log` tinyint(1) DEFAULT '1',
PRIMARY KEY (`id_rifiuto`,`id_azienda`,`id_sede`,`revisione_documento`),
KEY `fk_main_lin_98_47_rifiuti` (`id_azienda`,`id_sede`,`revisione_documento`),
KEY `fk_id_responsabile_lin_98_47_rifiuti` (`id_responsabile`,`id_azienda`,`id_sede`,`revisione_documento`,`nome_responsabile`),
KEY `fk_id_pericolo_lin_98_47_rifiuti` (`id_pericolo`),
KEY `fk_id_recupero_lin_98_47_rifiuti` (`id_recupero`),
KEY `fk_id_smaltimento_lin_98_47_rifiuti` (`id_smaltimento`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
问题发生在我尝试这样做:
ALTER TABLE lin_98_47_rifiuti ADD CONSTRAINT fk_id_cer_lin_98_47_rifiuti FOREIGN KEY (id_cer) REFERENCES agews_rifiuti_cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE;
我收到了这个错误:
#1005 - Can't create table 'db_626suite.#sql-71c_13d5' (errno: 150)
命令SHOW INNODB STATUS说:
Error in foreign key constraint of table db_626suite/#sql-71c_13d5:
FOREIGN KEY (id_cer) REFERENCES agews_rifiuti_cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE:
Cannot resolve column name close to:
) ON UPDATE CASCADE ON DELETE CASCADE
但语法和字段定义对我来说似乎是正确的。我做错了什么?
编辑:
Marc B 建议可能是因为id_cer
中的NOT NULL
定义为agews_rifiuti_cer
,但我不认为这是情况,实际上请考虑另一张表:
CREATE TABLE `agews_rifiuti_pericolo` (
`id_pericolo` int(10) unsigned NOT NULL AUTO_INCREMENT,
`agews_id` int(10) unsigned DEFAULT '0',
`codice` varchar(255) DEFAULT NULL,
`pericolo` varchar(255) DEFAULT NULL,
`note` text,
`id_cliente` int(10) unsigned DEFAULT '1',
`flag_modificato` char(1) DEFAULT 'N',
PRIMARY KEY (`id_pericolo`),
KEY `codice_rifiuti_recupero` (`codice`),
KEY `fk_id_cliente_rifiuti_pericolo` (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
此处id_pericolo
被定义为上面NOT NULL
id_cer
,但这完美无缺:
ALTER TABLE lin_98_47_rifiuti ADD CONSTRAINT fk_id_pericolo_lin_98_47_rifiuti FOREIGN KEY (id_pericolo) REFERENCES agews_rifiuti_pericolo(id_pericolo) ON UPDATE CASCADE ON DELETE SET NULL;
即使在lin_98_47_rifiuti
中,字段id_pericolo
也被定义为DEFAULT NULL
编辑2:
我刚试过这个最小的设置:
CREATE TABLE `cer` (
`id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,
`codice` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id_cer`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `rifiuti` (
`id_rifiuto` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_cer` int(10) unsigned NULL DEFAULT NULL,
`rifiuto` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id_rifiuto`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE rifiuti ADD FOREIGN KEY (id_cer) REFERENCES cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE;
这一切都正常,但我不明白我的原始查询可能存在什么问题。
此外,我认为这证明不是将字段定义为NOT NULL
或DEFAULT NULL
。
答案 0 :(得分:1)
您已在lin_98_47表中将id_cer
定义为default null
,但另一个表中的匹配FK字段定义为not null
。
字段定义必须完全匹配才能建立外键关系,并且包括可空性:
`id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_cer` int(10) unsigned DEFAULT NULL,
答案 1 :(得分:0)
我真的不明白为什么,但在所有表格中将id_cer
重命名为id_codice
,同时保持其定义不变,已解决了问题。
因此,MySQL似乎对该特定字段名称存在某种问题。