【问题标题】:ERROR 1005 (HY000): Can't create table 'db.POSTS' (errno: 150)错误 1005 (HY000): 无法创建表 'db.POSTS' (errno: 150)
【发布时间】:2016-03-19 13:52:35
【问题描述】:

您好,我在创建帖子数据库时遇到问题。我正在尝试制作一个链接到我的用户数据库的 forenge 密钥。有人可以帮忙吗?

这是我的表格的代码:

CREATE TABLE USERS(

UserID int NOT NULL AUTO_INCREMENT,
UserName varchar(255),
UserPassword varchar(255) NOT NULL,
UserEmailAddress varchar(255) NOT NULL,
Admin int DEFAULT 0,
PRIMARY KEY (userID,UserName)
)ENGINE=InnoDB;

CREATE TABLE POSTS(

postID int NOT NULL AUTO_INCREMENT,
postTitle varchar(255) NOT NULL,
postContent varchar(255) NOT NULL,
category varchar(255) NOT NULL,
postDate Date NOT NULL,
postAuthor varchar(255),
tag varchar(255),

PRIMARY KEY(postID),
FOREIGN KEY(postAuthor) REFERENCES USERS(UserName)
)ENGINE=InnoDB;

这是最后一条 InnoDB 错误消息:

Error in foreign key constraint of table db/POSTS:
FOREIGN KEY(postAuthor) REFERENCES USERS(UserName)
)ENGINE=InnoDB:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

【问题讨论】:

    标签: php mysql foreign-keys innodb


    【解决方案1】:

    在第一个表上有一个复合主键确实不应该是一个很好的理由。所以,我想你打算:

    CREATE TABLE USERS (
        UserID int NOT NULL AUTO_INCREMENT,
        UserName varchar(255),
        UserPassword varchar(255) NOT NULL,
        UserEmailAddress varchar(255) NOT NULL,
        Admin int DEFAULT 0,
        PRIMARY KEY (userID),
        UNIQUE (UserName)
    );
    
    CREATE TABLE POSTS (
        postID int NOT NULL AUTO_INCREMENT,
        postTitle varchar(255) NOT NULL,
        postContent varchar(255) NOT NULL,
        category varchar(255) NOT NULL,
        postDate Date NOT NULL,
        postAuthor int,
        tag varchar(255),
    
        PRIMARY KEY(postID),
        FOREIGN KEY(postAuthor) REFERENCES USERS(UserId)
    );
    

    一些注意事项:

    • 自动递增的 ID 在每一行上都是唯一的。它是一个很好的主键。
    • 主键可以由多列组成(称为复合主键)。但是,自动递增的 id 作为列之一没有多大意义。只需使用这样的 id 本身。
    • 如果您使用复合主键,则外键引用需要包含所有列。
    • 我选择UserId 作为外键引用。你也可以使用UserName(因为它是独一无二的)。
    • UserName 对于外键来说是一个糟糕的选择,因为——可以想象——用户可以更改他或她的名字。

    【讨论】:

      【解决方案2】:

      该错误是由不正确的外键定义引起的。在具体情况下,您的外键定义中缺少完整的列。

      USERS 表中,您已将主键定义为UserIDUserName 列的组合键。

      CREATE TABLE USERS (
        UserID int NOT NULL AUTO_INCREMENT,
        UserName varchar(255),
        UserPassword varchar(255) NOT NULL,
        UserEmailAddress varchar(255) NOT NULL,
        Admin int DEFAULT 0,
        PRIMARY KEY (UserID,UserName)
      ) ENGINE=InnoDB;
      

      请注意,尊重标识符(列名)的大小写是一种很好的做法

      POSTS 表中,您声明外键只引用USERS 表中的一列,即UserName 列。这是不正确的,因为您需要引用 USERS 表的整个主键 (UserID, UserName)。因此,要修复错误,您需要向 POSTS 表添加一列,并像这样更改外键定义:

      CREATE TABLE POSTS(
        postID int NOT NULL AUTO_INCREMENT,
        postTitle varchar(255) NOT NULL,
        postContent varchar(255) NOT NULL,
        category varchar(255) NOT NULL,
        postDate Date NOT NULL,
      
        authorId int,
        postAuthor varchar(255),
      
        tag varchar(255),
        PRIMARY KEY(postID),
        FOREIGN KEY(authorId, postAuthor) REFERENCES USERS(UserID, UserName)
      ) ENGINE=InnoDB;
      

      请看下面的小提琴:http://sqlfiddle.com/#!9/92ff1/1

      注意:如果可以的话,您应该重新设计它以不使用USERS 表中的复合主键,因为从我在显示的代码中可以看到它没有意义。您可以像这样更改表格:

      CREATE TABLE USERS (
        UserID int NOT NULL AUTO_INCREMENT,
        UserName varchar(255),
        UserPassword varchar(255) NOT NULL,
        UserEmailAddress varchar(255) NOT NULL,
        Admin int DEFAULT 0,
        PRIMARY KEY (UserID)
      ) ENGINE=InnoDB;
      
      CREATE TABLE POSTS (
          postID int NOT NULL AUTO_INCREMENT,
          postTitle varchar(255) NOT NULL,
          postContent varchar(255) NOT NULL,
          category varchar(255) NOT NULL,
          postDate Date NOT NULL,
      
          postAuthorID int,
      
          tag varchar(255),
          PRIMARY KEY(postID),
          FOREIGN KEY(postAuthorID) REFERENCES USERS(UserID)
      ) ENGINE=InnoDB;;
      

      【讨论】:

        猜你喜欢
        • 2017-04-09
        • 2016-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-07
        • 1970-01-01
        • 2011-02-17
        • 2019-02-26
        相关资源
        最近更新 更多