首页 / MYSQL / mysql无法添加外键#1215
mysql无法添加外键#1215
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql无法添加外键#1215,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2858字,纯文字阅读大概需要5分钟。
内容图文
![mysql无法添加外键#1215](/upload/InfoBanner/zyjiaocheng/911/1be2684527ba4dde943c28e4771bc1ae.jpg)
我已经阅读了很多关于这个错误的回答问题,但没有一个答案似乎能帮我解决问题.
我得到的错误是
#1215 - Cannot add foreign key constraint
当我显示引擎innodb状态时,它给了我这个信息:
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.
以下是涉及的表(我正在创建一个创建脚本,并且在执行sql期间出现错误)
CREATE TABLE IF NOT EXISTS customer_type (
customer_type_id int(11) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(128) NOT NULL,
sort int(11) NOT NULL,
is_active tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (customer_type_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS location (
location_id int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
email varchar(255) DEFAULT NULL,
phone varchar(32) DEFAULT NULL,
address varchar(128) DEFAULT NULL,
city varchar(255) DEFAULT NULL,
postal_code varchar(10) DEFAULT NULL,
shipping_cost float unsigned DEFAULT NULL,
is_active tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (location_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS customer_type_location (
customer_type_id int(11) unsigned NOT NULL,
location_id int(11) unsigned NOT NULL,
PRIMARY KEY (customer_type_id,location_id),
FOREIGN KEY (customer_type_id)
REFERENCES customer_type(customer_type_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (location_id)
REFERENCES location(location_id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我不明白为什么它说我需要索引引用列,因为你可以清楚地看到引用的列是主键,应该隐式创建索引.所以这个错误对我来说似乎没有意义.
我已经从this question的答案中尝试了大部分建议,但似乎没有任何帮助.
我以前一直使用PostgreSQL,所以我对大多数MySQL错误并不是很熟悉,但这个项目要求我使用MySQL.任何帮助,将不胜感激.
解决方法:
事实证明问题出在数据库整理上.我的db collat??ion设置为utf8_general_ci,但它无法正常工作.我删除了数据库,并使用utf8_unicode_ci创建了新数据库,然后按预期工作.所以似乎数据库排序规则和表排序规则应该匹配.
从手册页面标题为Using FOREIGN KEY Constraints,摘录:
MySQL requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later, if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.
所以指数很好.
内容总结
以上是互联网集市为您收集整理的mysql无法添加外键#1215全部内容,希望文章能够帮你解决mysql无法添加外键#1215所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。