简体   繁体   English

错误1005(HY000):无法创建表“ db.POSTS”(错误号:150)

[英]ERROR 1005 (HY000): Can't create table 'db.POSTS' (errno: 150)

Hi I'm having issues creating my post database. 嗨,我在创建帖子数据库时遇到问题。 I'm trying to make a forenge key to link to my users database. 我正在尝试制作一个Forenge密钥以链接到我的用户数据库。 Can someone please help? 有人可以帮忙吗?

Here's the code for my tables : 这是我的表的代码:

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;

Here's the last InnoDB error message: 这是最后的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.

There really should not be a good reason to have a compound primary key on the first table. 在第一个表上确实没有充分的理由要有复合主键。 So, I think you intend: 因此,我认为您打算:

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

Some notes: 一些注意事项:

  • An auto-incremented id is unique on every row. 自动递增的ID在每一行都是唯一的。 It makes a good primary key. 它是一个很好的主键。
  • A primary key can consist of multiple columns (called a composite primary key). 一个主键可以包含多个列(称为复合主键)。 However, an auto-incremented id doesn't make much sense as one of the columns. 但是,自动递增的id作为列之一没有多大意义。 Just use such an id itself. 只需使用此类ID即可。
  • If you use a composite primary key, then the foreign key references need to include all columns. 如果使用复合主键,则外键引用需要包括所有列。
  • I chose UserId for the foreign key reference. 我选择UserId作为外键引用。 You could also use UserName (because it is unique). 您也可以使用UserName (因为它是唯一的)。
  • UserName is a bad choice for foreign keys, because -- conceivably -- a user could change his or her name. UserName是外键的错误选择,因为-可以想象-用户可以更改其名称。

The error is caused by incorrect foreign key definition. 该错误是由不正确的外键定义引起的。 In the concrete case you are missing a complete column in your foreign key definition. 在具体情况下,您在外键定义中缺少完整的列。

In the USERS table you have defined primary key as composite key of UserID and UserName columns. 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;

note that it is good practice to respect case of the identifiers (column names) 请注意,尊重标识符(列名)的大小写是一种好习惯

In the POSTS table you declared your foreign key to reference only one column in the USERS table, the UserName column. POSTS表中,您声明了外键仅引用USERS表中的一列UserName列。 This is incorrect as you need to reference entire primary key of the USERS table which is (UserID, UserName) . 这是不正确的,因为您需要引用USERS表的整个主键,即(UserID, UserName) So to fix the error you need to add one additional column to the POSTS table and change your foreign key definition like this: 因此,要纠正该错误,您需要在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;

Please look at following fiddle: http://sqlfiddle.com/#!9/92ff1/1 请查看以下小提琴: http ://sqlfiddle.com/#!9/ 92ff1/1

NOTE: If you can you should re-architect this to not use the composite primary key in the USERS table as it does not make sense from what I can see in the displayed code. 注意:如果可以的话,应该重新USERS它,以免在USERS表中使用组合主键,因为从我在显示的代码中看到的意义来看,这没有意义。 You can change the tables like this: 您可以像这样更改表:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE[HY000]: 一般错误: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is wrongly forms") - SQLSTATE[HY000]: General error: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") MySQL错误代码1005,SQL状态HY000:无法创建表“ cpis.cpis_sudent_profile”(错误号:150) - MySQL Error code 1005, SQL state HY000: Can't create table 'cpis.cpis_sudent_profile' (errno: 150) SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 - SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 SQLSTATE[HY000]:一般错误:1005 无法创建表 Laravel 8 - SQLSTATE[HY000]: General error: 1005 Can't create table Laravel 8 SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4 - SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4 zendfox安装期间发生错误,SQLSTATE [HY000]:常规错误:1005(错误号:150) - Error during zendfox installation, SQLSTATE[HY000]: General error: 1005 (errno: 150) Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确” - Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed" SQLSTATE[HY000]:一般错误:无法创建表 - SQLSTATE[HY000]: General error:Can't create table 一般错误:1005 无法创建表(错误号:150“外键约束格式不正确”) - General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed") 常规错误:1005无法创建表errno:150“外键约束格式错误”) - General error: 1005 Can't create table errno: 150 “Foreign key constraint is incorrectly formed”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM