ERROR ::外键约束失败

时间:2014-01-22 07:28:06

标签: mysql

错误:: 1452: Cannot add or update a child row: a foreign key constraint fails ( findmybuffet . det_res_item_selected , CONSTRAINT fk_det_res_item_selected_Buf_Off_Id1 FOREIGN KEY ( Buf_Off_Id ) REFERENCES mas_buf_off ( Buf_Off_Id ) ON DELETE CASCADE)


我有三张桌子 ::

当我试图创建表时,

det_res_item_selected抛出错误

我该如何解决这个问题?


CREATE TABLE `det_res_item_selected` (
  `Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Item_Id` int(11) unsigned NOT NULL,
  `Item_Selected` tinyint(4) DEFAULT '0',
  `Buf_Off_Id` int(11) unsigned DEFAULT NULL,
  `Item_Image` varchar(50) NOT NULL,
  PRIMARY KEY (`Line_Selected_Item_Id`),
  KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`),
  KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`),
  CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE,
  CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `mas_buf_off` (
  `Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Buf_Type_Id` int(11) unsigned NOT NULL,
  `From_Time` time NOT NULL,
  `To_Time` time NOT NULL,
  `Res_Id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`Buf_Off_Id`),
  KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`),
  KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`),
  CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE,
  CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `mas_restaurant` (
  `Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Res_Name` varchar(100) NOT NULL,
  `Res_Featured` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`Res_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

1 个答案:

答案 0 :(得分:0)

我只是在脚本下面运行,它在我的系统上工作。

set foreign_key_checks=0;

CREATE TABLE `det_res_item_selected` (

  `Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `Item_Id` int(11) unsigned NOT NULL,

  `Item_Selected` tinyint(4) DEFAULT '0',

  `Buf_Off_Id` int(11) unsigned DEFAULT NULL,

  `Item_Image` varchar(50) NOT NULL,

  PRIMARY KEY (`Line_Selected_Item_Id`),

  KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`),

  KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`),

  CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE,

  CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE

) ENGINE=InnoDB DEFAULT CHARSET=utf8;



CREATE TABLE `mas_buf_off` (

  `Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `Buf_Type_Id` int(11) unsigned NOT NULL,

  `From_Time` time NOT NULL,

  `To_Time` time NOT NULL,

  `Res_Id` int(11) unsigned NOT NULL,

  PRIMARY KEY (`Buf_Off_Id`),

  KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`),

  KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`),

  CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE,

  CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE

) ENGINE=InnoDB DEFAULT CHARSET=utf8;



CREATE TABLE `mas_restaurant` (

  `Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `Res_Name` varchar(100) NOT NULL,

  `Res_Featured` tinyint(4) NOT NULL DEFAULT '0',

  PRIMARY KEY (`Res_Id`)

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

set foreign_key_checks=1;