由于外键Mysql错误1452导致事务失败

时间:2012-03-05 18:05:58

标签: mysql sql

我有一个将数据插入三个表的事务。此事务已失败,但我发现,如果我删除了phpmyadmin中的关系,则事务成功完成。

要重新添加我需要删除表中所有数据的关系!

三个表格包含:'User_Account'(具有主键),  'Item'也有一个主键

'User_Account'和'Item'通过名为'User_Item'的连接表与复合键相关联。该项目的图像由名为“Image_Items”的表引用,该表由“Item”表中的外键引用。

我不明白为什么这不起作用?因为只有一个表似乎一次使用密钥?有关为什么会发生这种情况的任何建议?或者我做错了什么!

CREATE TABLE IF NOT EXISTS `user_item` (
  `account_id` int(10) unsigned NOT NULL COMMENT 'Foreign key from account',
  `item_id` int(10) unsigned NOT NULL COMMENT 'Foreign key from item',
  `status` enum('active','deleted') NOT NULL COMMENT 'Is item active or been deleted',
  `date_added` varchar(50) NOT NULL COMMENT 'Date item was added',
  UNIQUE KEY `account_id` (`account_id`,`item_id`),
  KEY `item_id` (`item_id`)
)

CREATE TABLE IF NOT EXISTS `item` (
  `item_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key for item',
  `item_name` varchar(20) NOT NULL COMMENT 'title name of the item',
     `catagory`enum('accessories','jackets','coats','footwear','legwear','jeanswear','dresses','s     hirts','tops','t-shirts','knitwear','skirts','shorts') NOT NULL COMMENT 'item  catagory',
  `brand` varchar(20) NOT NULL COMMENT 'brand of product',
  `store` varchar(20) NOT NULL COMMENT 'store the item was purchased',
  `location` varchar(20) NOT NULL COMMENT 'location the item was purchased',
  `month`  enum('January','February','March','April','May','June','July','August','September','October'      ,'November','December') NOT NULL COMMENT 'month the item was purchased',
  `year` int(2) NOT NULL COMMENT 'year the item was purchased',
  `details` varchar(500) NOT NULL COMMENT 'details about the item description',
  `date` varchar(50) NOT NULL COMMENT 'date item created',
  PRIMARY KEY (`item_id`),
  UNIQUE KEY `item_id` (`item_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='holds data about the item'     AUTO_INCREMENT=20 ;




CREATE TABLE IF NOT EXISTS `user_account` (
  `account_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'unique account ID',
  `user_id` int(10) unsigned NOT NULL COMMENT 'foreign key from user table',
  `username` varchar(50) NOT NULL COMMENT 'users username default emailaddress',
  `account_status` enum('active','online','frozen','offline','hidden') NOT NULL COMMENT    'status of account, online offline frozen hidden active',
  `account_type` enum('admin','user') NOT NULL COMMENT 'type of account user or admin',
  `account_created` varchar(50) NOT NULL COMMENT 'date the account was created',
  `account_modified` varchar(50) NOT NULL COMMENT 'date account was last modified',
  `salt` int(10) unsigned NOT NULL COMMENT 'encryption',
  `password` char(40) NOT NULL COMMENT 'users password',
  PRIMARY KEY (`account_id`),
  UNIQUE KEY `user_id` (`user_id`,`username`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='details of the users account'      AUTO_INCREMENT=56 ;

和查询

$mysql = "INSERT INTO user_item(account_id,item_id,status,date_added)
            VALUES('$accountId','$itemId','$status','$date')";
            $mysqlResult = $this->conn->query($mysql)or   die(mysql_error());

由于

0 个答案:

没有答案