2012-01-25 116 views
4

我創造下在mysql中解決錯誤代碼:1005無法創建表 ''(錯誤:150)錯誤

  1. POSTMASTER
  2. ADVERTISEMENT
  3. CANDIDATEMAIN
三個表

以下是創建語句

POSTMASTER

CREATE TABLE `postmaster` (
    `POSTCODE` int(2) NOT NULL DEFAULT '0', 
    `POSTNAME` varchar(250) DEFAULT NULL, 
    PRIMARY KEY (`POSTCODE`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

ADVERTISEMENT

CREATE TABLE `advertisment` (
    `ADVTNO` varchar(35) NOT NULL, 
    `ADVTDATE` date NOT NULL, 
    `POSTCODE` int(2) NOT NULL, 
    `ADVTOPENDATE` date NOT NULL COMMENT 'ADVERTISEMENT OPENING DATE', 
    `ADVTCLOSEDATE` date NOT NULL COMMENT 'ADVERTISEMENT CLOSING DATE', 
    `EDITCLOSEDATE` date NOT NULL COMMENT 'CLOSING DATE FOR EDITING APPLICATION', 
    `LASTREPRINTDATE` date NOT NULL COMMENT 'LAST DATE FOR REPRINTING APPLICATION', 
    `FEESCST` int(4) NOT NULL COMMENT 'FEE FOR SC/ST CATEGORY', 
    `FEESTAFF` int(4) DEFAULT NULL COMMENT 'FEE FOR STAFF ', 
    `FEEOBC` int(4) DEFAULT NULL COMMENT 'FEE FOR OBC CATEGORY', 
    `ADVOCATEEXPERIENCE` int(2) DEFAULT NULL, 
    PRIMARY KEY (`ADVTNO`,`ADVTDATE`,`POSTCODE`), 
    KEY `fk_post` (`POSTCODE`), 
    CONSTRAINT `fk_post` FOREIGN KEY (`POSTCODE`) REFERENCES `postmaster` (`POSTCODE`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

一切都到這裏被罰款,但是當我去創造CANDIDATEMAIN表,收到錯誤

這裏是CANDIDATEMAIN

CREATE TABLE `candidatemain` (

    `ADVTNO` varchar(35) NOT NULL DEFAULT '', 
    `ADVTDATE` date NOT NULL DEFAULT '0000-00-00', 
    `POSTCODE` int(2) NOT NULL DEFAULT '0', 
    `REGISTRATIONNO` int(6) NOT NULL DEFAULT '0', 
    `SALUTATION` varchar(10) NOT NULL, 
    `FULLNAME` varchar(90) NOT NULL, 
    `SURNAME` varchar(30) DEFAULT NULL, 
    `NAME` varchar(30) NOT NULL, 
    `LASTNAME` varchar(30) DEFAULT NULL, 
    `LASTUPDATEDIP` varchar(20) NOT NULL DEFAULT '', 
    `LASTUPDATEDDATE` date DEFAULT NULL, 
    `ENTRYDATE` date NOT NULL, 
    PRIMARY KEY (`ADVTNO`,`ADVTDATE`,`POSTCODE`,`REGISTRATIONNO`), 
    KEY `FK_ADVT` (`ADVTNO`, `ADVTDATE`,`POSTCODE`) , 
    CONSTRAINT `FK_ADVT` FOREIGN KEY (`ADVTNO`, `ADVTDATE`,`POSTCODE`) REFERENCES `advertisment` (`ADVTNO`, `ADVTDATE`,`POSTCODE`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

的錯誤,我得到的創建語句是:

Error Code: 1005. Can't create table 'dbName.candidatemain' (errno: 150) 

有什麼能爲這個錯誤的原因是什麼?

優化版本的MySQL:的MySQL版本14.14 DISTRIB 53年5月1日,對於Win64的

+0

問題出在您的約束,按照:http://dev.mysql.com/doc/refman/5.0/en/innodb-error-codes.html說:***「1005(ER_CANT_CREATE_TABLE)無法創建如果錯誤消息指向錯誤150,表創建失敗,因爲外鍵約束未正確形成。「*** – Nanne

+0

是表名'advertisment'故意錯誤嗎? –

+0

@Shiplu它只是一個錯字! –

回答

7

創建advertisment表默認字符集爲utf8;

LATIN1列並不等同於UTF8列。所以外鍵約束無法正確形成。因此1005 (ER_CANT_CREATE_TABLE)錯誤。

這是我的mysql日誌。

mysql> CREATE TABLE `advertisment` (
    -> `ADVTNO` varchar(35) NOT NULL, 
    -> `ADVTDATE` date NOT NULL, 
    -> `POSTCODE` int(2) NOT NULL, 
    -> `ADVTOPENDATE` date NOT NULL COMMENT 'ADVERTISEMENT OPENING DATE', 
    -> `ADVTCLOSEDATE` date NOT NULL COMMENT 'ADVERTISEMENT CLOSING DATE', 
    -> `EDITCLOSEDATE` date NOT NULL COMMENT 'CLOSING DATE FOR EDITING APPLICATION', 
    -> `LASTREPRINTDATE` date NOT NULL COMMENT 'LAST DATE FOR REPRINTING APPLICATION', 
    -> `FEESCST` int(4) NOT NULL COMMENT 'FEE FOR SC/ST CATEGORY', 
    -> `FEESTAFF` int(4) DEFAULT NULL COMMENT 'FEE FOR STAFF ', 
    -> `FEEOBC` int(4) DEFAULT NULL COMMENT 'FEE FOR OBC CATEGORY', 
    -> `ADVOCATEEXPERIENCE` int(2) DEFAULT NULL, 
    -> PRIMARY KEY (`ADVTNO`,`ADVTDATE`,`POSTCODE`), 
    -> KEY `fk_post` (`POSTCODE`), 
    -> CONSTRAINT `fk_post` FOREIGN KEY (`POSTCODE`) REFERENCES `postmaster` (`POSTCODE`) 
    ->) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- <== This little change make it work. 
Query OK, 0 rows affected (0.06 sec) 

mysql> CREATE TABLE `candidatemain` (
    -> 
    -> `ADVTNO` varchar(35) NOT NULL DEFAULT '', 
    -> `ADVTDATE` date NOT NULL DEFAULT '0000-00-00', 
    -> `POSTCODE` int(2) NOT NULL DEFAULT '0', 
    -> `REGISTRATIONNO` int(6) NOT NULL DEFAULT '0', 
    -> `SALUTATION` varchar(10) NOT NULL, 
    -> `FULLNAME` varchar(90) NOT NULL, 
    -> `SURNAME` varchar(30) DEFAULT NULL, 
    -> `NAME` varchar(30) NOT NULL, 
    -> `LASTNAME` varchar(30) DEFAULT NULL, 
    -> `LASTUPDATEDIP` varchar(20) NOT NULL DEFAULT '', 
    -> `LASTUPDATEDDATE` date DEFAULT NULL, 
    -> `ENTRYDATE` date NOT NULL, 
    -> PRIMARY KEY (`ADVTNO`,`ADVTDATE`,`POSTCODE`,`REGISTRATIONNO`), 
    -> KEY `FK_ADVT` (`ADVTNO`, `ADVTDATE`,`POSTCODE`) , 
    -> CONSTRAINT `FK_ADVT` FOREIGN KEY (`ADVTNO`, `ADVTDATE`,`POSTCODE`) REFERENCES `advertisment ` (`ADVTNO`, `ADVTDATE`,`POSTCODE`) 
    ->) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
Query OK, 0 rows affected (0.12 sec) 
+0

非常感謝,但你能告訴我你是如何找出錯誤的嗎?我的意思是說,有任何特定的軟件,可以完全指出錯誤..因爲錯誤1005'給出了一個非常模糊的錯誤圖片...再次感謝! –

+0

不知道。只需掃描您的SQL查詢。 –

1

FOREIGN KEY CONSTRAINT爲表candidatemainadvertisment有不同的字符集,這將導致錯誤,讓他們有相同的字符集。

相關問題