1

我在两个表之间创建外键约束时遇到问题。

这是第一个表:

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 NULLin的事实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 NULLor的问题DEFAULT NULL

4

2 回答 2

2

您在 lin_98_47 表中定义id_cerdefault null,但另一个表中匹配的 FK 字段定义为not null.

字段定义必须完全匹配才能建立外键关系,其中包括可空性:

`id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,

`id_cer` int(10) unsigned DEFAULT NULL,
于 2012-11-08T17:05:04.200 回答
0

我真的不明白为什么,但是在所有表中重命名id_cerid_codice,同时保持其定义不变,解决了这个问题。

因此,MySQL 似乎对该特定​​字段名称存在某种问题。

于 2012-11-09T08:02:24.373 回答