2012-08-12 49 views
0

這是我運行show db innobd status命令時得到的錯誤消息。我是新來的PHP和MySQL和任何幫助將不勝感激。先謝謝你 。表'inventory1`.asset_details`的外鍵約束失敗:錯誤

當我向我的數據庫提交數據時,信息會進入資產表中,但不是assert_details?

LATEST FOREIGN KEY ERROR 
------------------------ 
120811 22:40:43 Transaction: 
TRANSACTION 1A4F, ACTIVE 0 sec inserting 
mysql tables in use 1, locked 1 
4 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 2 
MySQL thread id 73, OS thread handle 0x10ccc4000, query id 339 localhost root update 
INSERT INTO asset_details (asset_type,make, model, serial_number, os,os_version,memory, hdd, processor,notes) 
      Values('Monitor','Apple ','Mac Book Pro 17 inch ','65655453445545','Macintosh','Snow Lion ','5 gb ','1 TB','Intel i 9','This is Paskale New Mac book pro computer.') 
Foreign key constraint fails for table `inventory1`.`asset_details`: 
, 
    CONSTRAINT `asset_tag` FOREIGN KEY (`asset_tag`) REFERENCES `asset` (`asset_tag`) ON DELETE CASCADE ON UPDATE CASCADE 
Trying to add in child table, in index `asset_tag` tuple: 
DATA TUPLE: 2 fields; 
0: len 4; hex 80000000; asc  ;; 
1: len 4; hex 80000001; asc  ;; 

But in parent table `inventory1`.`asset`, in index `PRIMARY`, 
the closest match we can find is record: 
PHYSICAL RECORD: n_fields 8; compact format; info bits 0 
0: len 4; hex 80000007; asc  ;; 
1: len 6; hex 000000001a4f; asc  O;; 
2: len 7; hex c0000001d40110; asc  ;; 
3: SQL NULL; 
4: len 14; hex 4465636f6d6d697373696f6e6564; asc Decommissioned;; 
5: SQL NULL; 
6: len 7; hex 4d6f6e69746f72; asc Monitor;; 
7: SQL NULL; 

這是在我的數據庫中創建表的腳本。

此腳本來創建表

SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0; 
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; 
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL'; 

DROP SCHEMA IF EXISTS `inventory1` ; 
CREATE SCHEMA IF NOT EXISTS `inventory1` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ; 
USE `inventory1` ; 

-- ----------------------------------------------------- 
-- Table `inventory1`.`department` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `inventory1`.`department` ; 

CREATE TABLE IF NOT EXISTS `inventory1`.`department` (
    `department_id` INT NOT NULL AUTO_INCREMENT , 
    `department_name` VARCHAR(45) NOT NULL , 
    `job_id` VARCHAR(45) NOT NULL , 
    PRIMARY KEY (`department_id`)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `inventory1`.`employee` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `inventory1`.`employee` ; 

CREATE TABLE IF NOT EXISTS `inventory1`.`employee` (
    `employee_id` INT NULL AUTO_INCREMENT , 
    `department_id` INT NOT NULL , 
    `fname` VARCHAR(45) NULL , 
    `lname` VARCHAR(45) NULL , 
    `email` VARCHAR(45) NULL , 
    `phone_number` VARCHAR(45) NULL , 
    `hire_date` VARCHAR(45) NULL , 
    `job_id` VARCHAR(45) NULL , 
    `manager_id` VARCHAR(45) NULL , 
    PRIMARY KEY (`employee_id`) , 
    INDEX `department_id` (`department_id` ASC) , 
    CONSTRAINT `department_id` 
    FOREIGN KEY (`department_id`) 
    REFERENCES `inventory1`.`department` (`department_id`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `inventory1`.`invoice` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `inventory1`.`invoice` ; 

CREATE TABLE IF NOT EXISTS `inventory1`.`invoice` (
    `invoice_id` INT NOT NULL AUTO_INCREMENT , 
    `invoice_number` INT NULL , 
    `invoice_date` INT UNSIGNED NULL , 
    `purchase_price` INT UNSIGNED NULL , 
    `quantity` INT UNSIGNED NULL , 
    `order_date` INT UNSIGNED NULL , 
    `vender` INT UNSIGNED NULL , 
    `warranty_end` DATE NULL , 
    `notes` VARCHAR(255) NULL , 
    PRIMARY KEY (`invoice_id`)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `inventory1`.`asset` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `inventory1`.`asset` ; 

CREATE TABLE IF NOT EXISTS `inventory1`.`asset` (
    `asset_tag` INT NOT NULL , 
    `invoice_id` INT NULL , 
    `status` VARCHAR(25) NULL , 
    `cap_ex` VARCHAR(20) NULL , 
    `asset_type` VARCHAR(25) NULL , 
    `invoice_number` INT NULL , 
    PRIMARY KEY (`asset_tag`) , 
    CONSTRAINT `invoice_id` 
    FOREIGN KEY (`invoice_id`) 
    REFERENCES `inventory1`.`invoice` (`invoice_id`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `inventory1`.`location` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `inventory1`.`location` ; 

CREATE TABLE IF NOT EXISTS `inventory1`.`location` (
    `location_id` INT NOT NULL AUTO_INCREMENT , 
    `location_name` VARCHAR(45) NULL , 
    `rack` INT NULL , 
    `row` INT NULL , 
    `unit` INT NULL , 
    PRIMARY KEY (`location_id`)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `inventory1`.`physical_asset` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `inventory1`.`physical_asset` ; 

CREATE TABLE IF NOT EXISTS `inventory1`.`physical_asset` (
    `physical_asset_id` INT NOT NULL AUTO_INCREMENT , 
    `location_id` INT NOT NULL , 
    `employee_id` INT NOT NULL , 
    `physical_asset_name` VARCHAR(45) NOT NULL , 
    PRIMARY KEY (`physical_asset_id`) , 
    INDEX `location_id` (`location_id` ASC) , 
    INDEX `employee_id` (`employee_id` ASC) , 
    CONSTRAINT `location_id` 
    FOREIGN KEY (`location_id`) 
    REFERENCES `inventory1`.`location` (`location_id`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `employee_id` 
    FOREIGN KEY (`employee_id`) 
    REFERENCES `inventory1`.`employee` (`employee_id`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `inventory1`.`asset_details` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `inventory1`.`asset_details` ; 

CREATE TABLE IF NOT EXISTS `inventory1`.`asset_details` (
    `asset_id` INT NULL AUTO_INCREMENT , 
    `asset_tag` INT NOT NULL , 
    `physical_asset_id` INT NULL , 
    `asset_type` VARCHAR(45) NULL , 
    `manufacturer` VARCHAR(45) NULL , 
    `os` VARCHAR(45) NULL , 
    `os_version` VARCHAR(45) NULL , 
    `make` VARCHAR(45) NULL , 
    `model` VARCHAR(45) NULL , 
    `serial_number` VARCHAR(45) NULL , 
    `processor` VARCHAR(45) NULL , 
    `ram` VARCHAR(45) NULL , 
    `memory` VARCHAR(45) NULL , 
    `hdd` VARCHAR(45) NULL , 
    `host_name` VARCHAR(45) NULL , 
    `notes` VARCHAR(250) NULL , 
    PRIMARY KEY (`asset_id`) , 
    INDEX `physical_asset_id` (`physical_asset_id` ASC) , 
    INDEX `asset_tag` (`asset_tag` ASC) , 
    CONSTRAINT `physical_asset_id` 
    FOREIGN KEY (`physical_asset_id`) 
    REFERENCES `inventory1`.`physical_asset` (`physical_asset_id`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `asset_tag` 
    FOREIGN KEY (`asset_tag`) 
    REFERENCES `inventory1`.`asset` (`asset_tag`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 



SET [email protected]_SQL_MODE; 
SET [email protected]_FOREIGN_KEY_CHECKS; 
SET [email protected]_UNIQUE_CHECKS; 


    LATEST FOREIGN KEY ERROR 
    ------------------------ 
    120811 22:40:43 Transaction: 
    TRANSACTION 1A4F, ACTIVE 0 sec inserting 
    mysql tables in use 1, locked 1 
    4 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 2 
    MySQL thread id 73, OS thread handle 0x10ccc4000, query id 339 localhost root update 
    INSERT INTO asset_details (asset_type,make, model, serial_number, os,os_version,memory, hdd, processor,notes) 
       Values('Monitor','Apple ','Mac Book Pro 17 inch ','65655453445545','Macintosh','Snow Lion ','5 gb ','1 TB','Intel i 9','This is Paskale New Mac book pro computer.') 
    Foreign key constraint fails for table `inventory1`.`asset_details`: 
    , 
     CONSTRAINT `asset_tag` FOREIGN KEY (`asset_tag`) REFERENCES `asset` (`asset_tag`) ON DELETE CASCADE ON UPDATE CASCADE 
    Trying to add in child table, in index `asset_tag` tuple: 
    DATA TUPLE: 2 fields; 
    0: len 4; hex 80000000; asc  ;; 
    1: len 4; hex 80000001; asc  ;; 

    But in parent table `inventory1`.`asset`, in index `PRIMARY`, 
    the closest match we can find is record: 
    PHYSICAL RECORD: n_fields 8; compact format; info bits 0 
    0: len 4; hex 80000007; asc  ;; 
    1: len 6; hex 000000001a4f; asc  O;; 
    2: len 7; hex c0000001d40110; asc  ;; 
    3: SQL NULL; 
    4: len 14; hex 4465636f6d6d697373696f6e6564; asc Decommissioned;; 
    5: SQL NULL; 
    6: len 7; hex 4d6f6e69746f72; asc Monitor;; 
    7: SQL NULL; 

回答

0

編輯:對不起,我誤讀

會發生什麼事是你嘗試把一個行有外鍵到其他表,但不表不指定外鍵標識。

在插入asset_details表之前,您必須設置外鍵的值(並將其他表中的鍵存在)。

所以你插入

INSERT INTO asset_details (asset_type,make, model, serial_number, os,os_version,memory, hdd, processor,notes) 
     Values('Monitor','Apple ','Mac Book Pro 17 inch ','65655453445545','Macintosh','Snow Lion ','5 gb ','1 TB','Intel i 9','This is Paskale New Mac book pro computer.') 

還應包括physical_asset_id

+0

好了,但怎麼樣,如果我想這個值是自動遞增的? – 2012-08-12 03:21:16

+0

它是一個僞造鍵,它不能自動增加,因爲它鏈接到另一個表。 asset_details.physical_asset_id的值必須存在於physical_asset.physical_asset_id中。 你需要做的是在表physical_asset中創建元組,然後將該id並將其放入asset_details.physical_asset_id – 2012-08-12 03:23:02

+0

好的謝謝Hugo – 2012-08-12 04:35:07