MySQL错误1215:无法添加外键约束、innodb、新表
我喜欢添加外键,这个表,但不工作!如果我尝试使用新的表,它不起作用,但是其他的,旧的表,它起作用。有什么问题吗MySQL错误1215:无法添加外键约束、innodb、新表,mysql,sql,foreign-keys,innodb,Mysql,Sql,Foreign Keys,Innodb,我喜欢添加外键,这个表,但不工作!如果我尝试使用新的表,它不起作用,但是其他的,旧的表,它起作用。有什么问题吗 CREATE TABLE `tanora` ( `idtanora` int(11) NOT NULL, `tema` varchar(250) NULL, `megjegyzes` varchar(255) DEFAULT NULL, `datum` date NOT NULL, `osztaly` varchar(20) NOT NULL, `megtartott` int(11)
CREATE TABLE `tanora` (
`idtanora` int(11) NOT NULL,
`tema` varchar(250) NULL,
`megjegyzes` varchar(255) DEFAULT NULL,
`datum` date NOT NULL,
`osztaly` varchar(20) NOT NULL,
`megtartott` int(11) NOT NULL,
`targy` varchar(45) NOT NULL,
`kezdete` time NOT NULL,
`vege` time NOT NULL,
PRIMARY KEY (`idtanora`),
UNIQUE KEY `idtanora_UNIQUE` (`idtanora`),
KEY `osztazon_idx` (`osztaly`)
) ENGINE=InnoDB DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci;
CREATE TABLE `osztaly` (
`osztalyazon` varchar(20) NOT NULL,
`osztalyfonokazon` varchar(11) NOT NULL,
`osztalynev` varchar(45) NOT NULL,
`indul` year(4) NOT NULL,
PRIMARY KEY (`osztalyazon`),
UNIQUE KEY `osztalyazon_UNIQUE` (`osztalyazon`),
KEY `ofazon_idx` (`osztalyfonokazon`),
CONSTRAINT `tanarazon01` FOREIGN KEY (`osztalyfonokazon`) REFERENCES `tanar` (`szemelyiszam`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci;
ERROR 1215: Cannot add foreign key constraint
SQL Statement:
ALTER TABLE `enaplo`.`tanora`
ADD CONSTRAINT `osztalyra`
FOREIGN KEY (`osztaly`)
REFERENCES `enaplo`.`osztaly` (`osztalyazon`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'tanora' already exists
SQL Statement:
CREATE TABLE `tanora` (
`idtanora` int(11) NOT NULL,
`tema` varchar(250) NOT NULL,
`megjegyzes` varchar(255) DEFAULT NULL,
`datum` date NOT NULL,
`osztaly` varchar(20) NOT NULL,
`megtartott` int(11) NOT NULL,
`targy` varchar(45) NOT NULL,
`kezdete` time NOT NULL,
`vege` time NOT NULL,
PRIMARY KEY (`idtanora`),
UNIQUE KEY `idtanora_UNIQUE` (`idtanora`)
) ENGINE=InnoDB DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci
foreign key (osztaly)
references enaplo.osztaly (osztalyazon)
on delete no action
on update no action:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables. See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
确保列定义一致: 在引用表中,需要使用与应用于
tanar
表中引用字段的特征完全相同的特征来定义外键列osztalyfonokazon
目前,osztaly
中的osztalyfonokazon
定义如下:
`osztalyfonokazon` VARCHAR(11) NOT NULL
因此,在tanar
表中,szemelyiszam
的定义应该是
`szemelyiszam` VARCHAR(11) PRIMARY KEY
或
这取决于你的需要
无论哪种方式,都要确保szemelyiszam
是主键
或至少是唯一键
,在引用的tanar
表中声明非空
此外,MySQL要求对引用的列进行索引
出于性能原因。但是,该系统不强制执行
要求引用的列是唯一的或不声明
无效的对非唯一键或键的外键引用的处理
对于以下操作,没有很好地定义包含NULL值的
更新或删除级联。建议您使用
仅引用唯一键(包括主键)和非空键
您能提供tanar表模式吗?@user3498248-您能提供反馈吗?现在能用了吗?如果答案是肯定的,不要忘记投票并接受答案。
`szemelyiszam` VARCHAR(11) UNIQUE KEY NOT NULL