1

以下 2 个表的创建失败。谁能帮帮我吗?

错误是 MySQL:无法创建表(错误号:150)

CREATE TABLE PSODEMO.APPLICATION (
    APPLICATION_ID INT NOT NULL AUTO_INCREMENT
    , APPLICATION_TYPE VARCHAR(10)
    , APPLICATION_STATUS VARCHAR(10)
    , CREATE_DATE DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL
    , MODIFIED_DATE DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL
    , DEALER_ID INT
    , COMPANY_NAME VARCHAR(120)
    , WEBSITE VARCHAR(128)
    , EMAIL VARCHAR(64)
    , PHONE VARCHAR(32)
    , STREET VARCHAR(124)
    , CITY VARCHAR(64)
    , ZIPCODE INT NOT NULL
    , COUNTRY VARCHAR(64)
    , DEALER_STATUS VARCHAR(32)
    , REMARKS VARCHAR(128)
    , INVOICE_ID INT
    , PAYMENT_MODE VARCHAR(64)
    , INVOICE_DATE DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL
    , PAYMENT_DATE DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL
    , PAYMENT_STATUS VARCHAR(64)
    , PRIMARY KEY (APPLICATION_ID)
    , KEY ix_APPLICATION_Applicationid(aplication_id)
    , KEY ix_APPLICATION_dealerid(dealer_id)
    );

CREATE TABLE LOGIN (
    LOGIN_ID INT NOT NULL AUTO_INCREMENT
    , USERNAME VARCHAR(10) NOT NULL
    , PASSWORD VARCHAR(10) NOT NULL
    , USER_TYPE VARCHAR(1) NOT NULL
    , APPLICATION_ID_FK INT
    , DEALER_ID_FK INT
    , PRIMARY KEY (LOGIN_ID)
    , FOREIGN KEY (APPLICATION_ID_FK) REFERENCES APPLICATION(APPLICATION_ID)
    , FOREIGN KEY (DEALER_ID_FK) REFERENCES DEALER(DEALER_ID)
    , UNIQUE (USERNAME)
    );
4

2 回答 2

1

来自MySQL 文档

如果您重新创建已删除的表,则它必须具有符合引用它的外键约束的定义。如前所述,它必须具有正确的列名和类型,并且必须在引用的键上具有索引。如果这些不满足,MySQL 返回错误号 1005 并在错误消息中引用错误 150。

于 2012-09-17T09:26:42.443 回答
0

Table2 有这个约束

FOREIGN KEY (DEALER_ID_FK) REFERENCES DEALER(DEALER_ID)

可能DEALER不存在。您需要在表DEALER之前创建LOGIN表,因为LOGIN表引用了DEALER_IDDEALER,我假设这也是表的主键。

还有一件事,在MySQL 保留关键字周围使用反引号

CREATE TABLE LOGIN (
LOGIN_ID INT NOT NULL AUTO_INCREMENT
, `USERNAME` VARCHAR(10) NOT NULL
, `PASSWORD` VARCHAR(10) NOT NULL
, USER_TYPE VARCHAR(1) NOT NULL
, APPLICATION_ID_FK INT
, DEALER_ID_FK INT
, PRIMARY KEY (LOGIN_ID)
, FOREIGN KEY (APPLICATION_ID_FK) REFERENCES APPLICATION(APPLICATION_ID)
, FOREIGN KEY (DEALER_ID_FK) REFERENCES DEALER(DEALER_ID)
, UNIQUE (USERNAME)
);
于 2012-09-17T09:31:59.927 回答