开发者社区> 问答> 正文

技术运维问题 - MYSQL使用 -RDS for MySQL中创建外键约束提



1. 问题现象


RDS for MySQL是支持外键约束的,但在创建外键约束提示"Cannot add foreign key constraint",会被误认为RDS不支持外键约束:

mysql>alter table tscore add constraint fk_tscore_sno foreign key(sno) references tstudent(sno);
Cannot add foreign key constraint



出现该错误的原因是要关联的表(如tstudent)在该列不是主键。
mysql>alter table tscore add constraint fk_tscore_sno foreign key(sno) references tstudent(sno);
Cannot add foreign key constraint



2. 解决方法


1. 先查看表结构判断是否关联表在对应列不是主键: mysql>show create table tstudent;
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table           | Create Table                                                                                                                                                    |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tstudent        | CREATE TABLE `tstudent` (
  `sno` varchar(4) NOT NULL DEFAULT '',
  `sname` varchar(3) DEFAULT NULL,
  PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
共返回 1 行记录,花费 267.17 ms.
mysql>show create table tscore;
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table           | Create Table                                                                                                                                                                                                                                                          |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tscore          | CREATE TABLE `tscore` (
  `sno` char(4) DEFAULT NULL,
  `cno` char(3) DEFAULT NULL,
  `score` int(10) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
共返回 1 行记录,花费 128.94 ms.





2. 为tstudent添加主键,如图: mysql>alter table tstudent add primary key(sno);
执行成功,花费 124.90 ms.





3. 再次创建外键,即可成功创建,如图: mysql>alter table tscore add constraint fk_tscore_sno foreign key(sno) references tstudent(sno);
执行成功,花费 161.55 ms.






如问题还未解决,请联系 售后技术支持

展开
收起
梨好橙 2018-09-23 10:58:35 1880 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像