项目场景:

通过sql语言在MySQL查询中建立表,其中一个表引用其他表的属性出现问题报错1822


问题描述

被引用的表——用户信息表:

可以看到其中已有主键s_id,为自增长形式

CREATE TABLE 用户信息表
( s_id int unsigned AUTO_INCREMENT not null PRIMARY KEY,s_no varChar(9) unique not null,s_name varchar(10) not null,s_createAt timestamp not null default current_timestamp,s_updateAt timestamp not null default current_timestamp on update current_timestamp);

引用用户信息的表——耗材借出表:

create table 耗材借出表
(loan_id int unsigned AUTO_INCREMENT not null PRIMARY KEY,c_no varchar(50)not null ,loan_num int check(loan_num>0) not null,loan_give_person varchar(9) not null default '用户已注销',loan_take_person varchar(9) not null default '用户已注销',loan_createAt timestamp not null default current_timestamp,loan_updateAt timestamp not null default current_timestamp on update current_timestamp,foreign key(c_no) references 耗材库存信息表(c_no) on delete cascade on update cascade,foreign key(loan_give_person,loan_take_person) references 用户信息表(s_no,s_no) on delete set default on update cascade);

主要语句如下:外键设置了删除和更新级联

foreign key(loan_give_person,loan_take_person) references 用户信息表(s_no,s_no) on delete set default on update cascade


原因分析:

经过修改发现是由于借出人和接收人都需要的是用户信息表中的用户编号s_no,所以两者都引用了用户信息表中的s_no出现报错,将两个外键引用分开即可


解决方案:

将最后一行的外键引用分成两行写:

    foreign key(loan_give_person) references 用户信息表(s_no) on delete set default on update cascade,foreign key(loan_take_person) references 用户信息表(s_no) on delete set default on update cascade

可以成功建立数据库

感谢观看~希望对你有帮助

Mysql 建立外键出错1822 可能原因之一分析 - Failed to add the foreign key constraint. Missing index for constraint相关推荐

  1. [HY000][1822] Failed to add the foreign key constraint. Missing index for constraint ‘fk_com’

    [HY000][1822] Failed to add the foreign key constraint. Missing index for constraint 'fk_comment_hnu ...

  2. 1822 - Failed to add the foreign key constraint. Missing index for constraint ‘fk_tno_course‘ in the

    1822 - Failed to add the foreign key constraint. Missing index for constraint 'fk_tno_course' in the ...

  3. 解决:Navicat for mysql 设置外键出错

    1 看下是不是外键允许为空,不唯一等约束条件不满足 2 或者外键设置删除时为 restrict 1. 两个字段的类型或者大小不严格匹配.例如,如果一个是int(10),那么外键也必须设置成int(10 ...

  4. mysql建立外键快捷方式_mysql建立外键

    InnoDB与Myisam的六大区别: 1.  构成上的区别 MyISAM: 每个MyISAM在磁盘上存储成三个文件.第一个文件的名字以表的名字开始,扩展名指出文件类型..frm文件存储表定义.数据文 ...

  5. mysql建立外键案例_MySQL外键实例

    外键实例 性别表 create table gender( gid int auto_increment primary key not null, gender char(10)); insert ...

  6. MySQL建立外键出现Can't create table‘..’ (errno:150)问题(简单易懂版)

    今天创建表时出现了Can't create table'..' (errno:150),经过排查发现是关联的B表中有一条测试数据,其中的值不在A表所关联字段的范围 会造成error:150的原因大概有 ...

  7. Mysql无法创建外键的原因汇总_查看MySQL创建外键约束失败详细原因的方法

    以下是引用片段: LATEST FOREIGN KEY ERROR -------- 081210 11:57:40 Error in foreign key constraint of table ...

  8. mysql a foreign key constraint fails_外键记录有存在,插入数据却报错a foreign key constraint fails...

    今天在MySQL的一张表里插记录时,出现一个错误提示: Cannot add or update a child row: a foreign key constraint fails (`jeesn ...

  9. mysql中depart_数据库问题:提问:MYSQL 建立外键出现1064错误,建立外键的时候员工表的depart和部门表的depart连接失败...

    --员工表--Table"one_staff"DDLCREATETABLE`one_staff`(`sid`int(11)NOTNULLAUTO_INCREMENT,`sno`va ...

最新文章

  1. MySQL和Oracle中的隐式转换
  2. Webcast.NET Remoting学习笔记(5)使用配置文件
  3. Vue中集成高德地图API实现定位与自定义样式信息窗体
  4. java8 stream 最大值_JDK8-Stream流常用方法
  5. OpenSSL再曝CCS注入漏洞-心伤未愈又成筛子
  6. linux下使用TC模拟弱网络环境
  7. 没有varselect这个函数_C++的虚函数,虚析构函数,纯虚函数
  8. iOS iPhone官方参考资料明细
  9. 【数字图像处理5.3】SLIC算法 超像素分割(无监督聚类方式)python
  10. 手把手教你做视频播放器(二)-获取视频信息
  11. 定投计算器和年化利率计算器
  12. 故障树分析法(FTA)
  13. linux mentohust dhcp,校园网锐捷 openwrt mentohust 成功开启IPv6
  14. otf是什么格式?怎么安装呢?
  15. 睡眠助手APP开发解决方案
  16. 文献阅读(2):王昊奋:大规模知识图谱技术
  17. tcpdump 侦测网络端口数据
  18. Linux 中的TAB键名字补全使用方法
  19. 载体坐标系-惯性坐标系-世界坐标系
  20. 4转自 饮水思源 bbs.sjtu.edu.cn·[FROM: 202.120.25.97]

热门文章

  1. 微信小程序项目实例——摇色子
  2. 关于高频信号PCB挖空
  3. 服务器系统迁移工具,Win2008 R2迁移实战之迁移工具使用
  4. Android非mtk平台T9的实现
  5. 活在IT,半路捡起硌脚的石子
  6. 计算机在材料化学中的应用论文,【多媒体技术论文】网络多媒体技术在材料化学中的应用(共2815字)...
  7. Windows开发之——Win10开机启动及启动设置
  8. ElasticSearch查询地图信息(geo-point类型)
  9. C++-图像目标区裁剪ImageCropping
  10. MySQL中操作关系型数据库 SQL