0

我不允许创建下表。是因为复合主键吗?我还添加了用户和标题表的信息,以防那里的代码出现问题。

CREATE TABLE store
(
    short_name  VARCHAR(5)                      NOT NULL,
    name        VARCHAR(30)                     NOT NULL,
    address1    VARCHAR(50)                     NOT NULL,
    address2    VARCHAR(20),
    city        VARCHAR(30)                     NOT NULL,
    state       VARCHAR(30)                     NOT NULL,
    zip         VARCHAR(10)                     NOT NULL,
    phone       VARCHAR(15)                     NOT NULL,
    CONSTRAINT store_pk PRIMARY KEY (short_name)
);

CREATE TABLE publishers
(
    name        VARCHAR(50)                         NOT NULL,
    status      TINYINT         DEFAULT 1           NOT NULL,
    CONSTRAINT publishers_pk PRIMARY KEY (name)
);
CREATE TABLE users
(
    id          INT                     NOT NULL    AUTO_INCREMENT,
    name        VARCHAR(70)                     NOT NULL,
    password    VARCHAR(60),        
    address1    VARCHAR(50)                     NOT NULL,
    address2    VARCHAR(20),
    city        VARCHAR(30)                     NOT NULL,
    state       VARCHAR(30)                     NOT NULL,
    zip         VARCHAR(10)                     NOT NULL,
    phone       VARCHAR(15),
    email       VARCHAR(90),
    store       VARCHAR(5),
    admin       TINYINT             DEFAULT 0   NOT NULL,
    mail_list   TINYINT             DEFAULT 0   NOT NULL,
    active      TINYINT             DEFAULT 0   NOT NULL,
    CONSTRAINT users_pk PRIMARY KEY (id),
    CONSTRAINT users_fk FOREIGN KEY (store)
        REFERENCES store (short_name)
        ON DELETE   SET NULL
        ON UPDATE   CASCADE
);

CREATE TABLE titles
(
    id          INT                 NOT NULL    AUTO_INCREMENT,
    publisher   VARCHAR(50),
    title       VARCHAR(50)         NOT NULL,
    status      ENUM('active', 'announced', 'inactive'),
    discount    TINYINT             NOT NULL,
    CONSTRAINT title_pk PRIMARY KEY (id),
    CONSTRAINT title_fk FOREIGN KEY (publisher)
        REFERENCES publishers (name)
        ON DELETE   SET NULL
        ON UPDATE   CASCADE
);

CREATE TABLE subscriptions
(
    user        INT,
    title       INT,
    quantity    TINYINT             DEFAULT 1           NOT NULL,
    CONSTRAINT subscription_pk PRIMARY KEY (user, title),
    CONSTRAINT subscription_user_fk FOREIGN KEY (user)
        REFERENCES users (id)
        ON DELETE   SET NULL
        ON UPDATE   CASCADE,
    CONSTRAINT subscription_title_fk FOREIGN KEY (title)
        REFERENCES titles (id)
        ON DELETE   SET NULL
        ON UPDATE   CASCADE
);
4

3 回答 3

2

使用表定义,只有subscriptions表出现问题,这是因为该ON DELETE SET NULL子句。user表中的和title列都subscriptions定义为NOT NULL!

于 2013-04-09T21:22:00.740 回答
0

主键定义错误。constraint title_pk primary key (id)而那些类似的都是错误的。他们应该像primary key(id)

于 2013-04-09T21:02:34.380 回答
0

它可能在外键约束上失败了。

运行这个:

SHOW ENGINE INNODB STATUS\G

并查看“最新外键错误”部分。那应该告诉你问题所在。

如果用户和/或标题表还不存在,您应该在创建表之前运行它:

set foreign_key_checks = 0;

如果这些表确实存在,则父列(即 user.id)和子列(即 user)之间的数据类型可能不匹配。

于 2013-04-09T21:03:58.997 回答