mysql添加外键1215错误_MySQL错误1215:不能添加外键约束

bd96500e110b49cbb3cd949968f18be7.png

I am trying to forward engineer my new schema onto my db server, but I can't figure out why I am getting this error. I've tried to search for the answer here, but everything I've found has said to either set the db engine to Innodb or to make sure the keys I'm trying to use as a foreign key are primary keys in their own tables. I have done both of these things, if I'm not mistaken. Any other help you guys could offer?

Executing SQL script in server

ERROR: Error 1215: Cannot add foreign key constraint

-- -----------------------------------------------------

-- Table `Alternative_Pathways`.`Clients_has_Staff`

-- -----------------------------------------------------

CREATE TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients_has_Staff` (

`Clients_Case_Number` INT NOT NULL ,

`Staff_Emp_ID` INT NOT NULL ,

PRIMARY KEY (`Clients_Case_Number`, `Staff_Emp_ID`) ,

INDEX `fk_Clients_has_Staff_Staff1_idx` (`Staff_Emp_ID` ASC) ,

INDEX `fk_Clients_has_Staff_Clients_idx` (`Clients_Case_Number` ASC) ,

CONSTRAINT `fk_Clients_has_Staff_Clients`

FOREIGN KEY (`Clients_Case_Number` )

REFERENCES `Alternative_Pathways`.`Clients` (`Case_Number` )

ON DELETE NO ACTION

ON UPDATE NO ACTION,

CONSTRAINT `fk_Clients_has_Staff_Staff1`

FOREIGN KEY (`Staff_Emp_ID` )

REFERENCES `Alternative_Pathways`.`Staff` (`Emp_ID` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

ENGINE = InnoDB

SQL script execution finished: statements: 7 succeeded, 1 failed

Here is the SQL for the parent tables.

CREATE TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients` (

`Case_Number` INT NOT NULL ,

`First_Name` CHAR(10) NULL ,

`Middle_Name` CHAR(10) NULL ,

`Last_Name` CHAR(10) NULL ,

`Address` CHAR(50) NULL ,

`Phone_Number` INT(10) NULL ,

PRIMARY KEY (`Case_Number`) )

ENGINE = InnoDB

CREATE TABLE IF NOT EXISTS `Alternative_Pathways`.`Staff` (

`Emp_ID` INT NOT NULL ,

`First_Name` CHAR(10) NULL ,

`Middle_Name` CHAR(10) NULL ,

`Last_Name` CHAR(10) NULL ,

PRIMARY KEY (`Emp_ID`) )

ENGINE = InnoDB

解决方案

I'm guessing that Clients.Case_Number and/or Staff.Emp_ID are not exactly the same data type as Clients_has_Staff.Clients_Case_Number and Clients_has_Staff.Staff_Emp_ID.

Perhaps the columns in the parent tables are INT UNSIGNED?

They need to be exactly the same data type in both tables.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值