簡體   English   中英

錯誤1005(HY000):無法創建表“ db.POSTS”(錯誤號:150)

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

嗨,我在創建帖子數據庫時遇到問題。 我正在嘗試制作一個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 (因為它是唯一的)。
  • UserName是外鍵的錯誤選擇,因為-可以想象-用戶可以更改其名稱。

該錯誤是由不正確的外鍵定義引起的。 在具體情況下,您在外鍵定義中缺少完整的列。

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它,以免在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;;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM