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

发布于 2024-12-28 23:49:05 字数 2500 浏览 0 评论 0原文

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

  1. 邮政局长
  2. 广告
  3. 候选人主体

以下是创建语句

邮政局长

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

广告

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 Ver 14.14 Distrib 5.1.53,适用于Win64

I am creating following three tables in mysql

  1. POSTMASTER
  2. ADVERTISEMENT
  3. CANDIDATEMAIN

Here are the create statements

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

Everything till here is fine but when I go for creation of the CANDIDATEMAIN table I get error

Here is the create statement of 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

The Error I get is :

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

What could be the reason for this error?

MYSql Verion: mysql Ver 14.14 Distrib 5.1.53, for Win64

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2025-01-04 23:49:05

创建 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)

Create advertisment table with default charset as utf8;

latin1 columns are not equivalent to utf8 columns. So foreign key constraint can not form correctly. Hence the 1005 (ER_CANT_CREATE_TABLE) error.

Here is my mysql log.

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)
丑疤怪 2025-01-04 23:49:05

candidatemainadvertismentFOREIGN KEY CONSTRAINT 具有不同的字符集,这会导致错误,让它们具有相同的字符集。

Your FOREIGN KEY CONSTRAINT for table candidatemain and advertisment has different charset, this causes the error, let them have same charset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文