解决错误代码:1005。无法创建表''(错误号:150)错误

时间:2012-01-25 08:11:21

标签: mysql mysql-error-1005

我正在mysql中创建以下三个表

  
      
  1. POSTMASTER
  2.   
  3. ADVERTISEMENT
  4.   
  5. CANDIDATEMAIN
  6.   

以下是创建语句

  

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 Verion: mysql Ver 14.14 Distrib 5.1.53,for Win64

2 个答案:

答案 0 :(得分:7)

使用默认字符集创建advertismentutf8;

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)

答案 1 :(得分:1)

您的FOREIGN KEY CONSTRAINTcandidatemainadvertisment有不同的字符集,这会导致错误,让它们拥有相同的字符集。