Php 错误1005(HY000):Can';t创建表';db.POSTS&x27;(错误编号:150)

Php 错误1005(HY000):Can';t创建表';db.POSTS&x27;(错误编号:150),php,mysql,foreign-keys,innodb,Php,Mysql,Foreign Keys,Innodb,嗨,我在创建帖子数据库时遇到问题。我正在尝试创建一个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

嗨,我在创建帖子数据库时遇到问题。我正在尝试创建一个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.

在第一个表上使用复合主键确实不应该是一个很好的理由。因此,我认为你打算:

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
    对于外键来说是个糟糕的选择,因为可以想象,用户可以更改自己的名字

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

USERS
表中,您已将主键定义为
UserID
UserName
列的复合键

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;
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;;
请看下面的小提琴:

注意:如果可以的话,您应该重新设计它,不要在
用户
表中使用复合主键,因为从我在显示的代码中看到的内容来看,它没有意义。您可以按如下方式更改表:

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;
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;;