简体   繁体   中英

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. 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:

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. 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. Just use such an id itself.
  • 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. You could also use UserName (because it is unique).
  • UserName is a bad choice for foreign keys, because -- conceivably -- a user could change his or her name.

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.

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. This is incorrect as you need to reference entire primary key of the USERS table which is (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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM