[英]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: 一些注意事项:
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
表中,您已将主键定义为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;
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.