ERROR 1005(HY000):无法创建表格db.POSTS' (错误:150)

时间:2016-03-19 13:52:36

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

2 个答案:

答案 0 :(得分:1)

确实不应该有充分的理由在第一个表上使用复合主键。所以,我想你打算:

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对于外键来说是一个糟糕的选择,因为 - 可以想象 - 用户可以改变他或她的名字。

答案 1 :(得分:1)

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

int count = Table_purchase.getRowCount(); int col = Table_purchase.getColumnCount(); String pod_id[] = new String[count]; String po_id[] = new String[count]; String order_qty[] = new String[count]; String item_id[] = new String[count]; String unit_price[] = new String[count]; String recived_qty[] = new String[count]; String rejected_qty[] = new String[count]; for (int i = 0; i < count; i++) { po_id[i] = Table_purchase.getValueAt(i,0).toString(); pod_id[i] = Table_purchase.getValueAt(i,1).toString(); order_qty[i] = Table_purchase.getValueAt(i,2).toString(); item_id[i] = Table_purchase.getValueAt(i,3).toString(); unit_price[i] = Table_purchase.getValueAt(i,4).toString(); recived_qty[i] = Table_purchase.getValueAt(i, 5).toString(); rejected_qty[i] = Table_purchase.getValueAt(i,6).toString(); try { String sql = "update purchase.purchase_detail set pod_id='" + pod_id[i] + "',order_qty='" + order_qty[i] + "',item_id='" + item_id[i] + "', unit_price='" + unit_price[i] + "', recived_qty='" + recived_qty[i] + "',rejected_qty='" + rejected_qty[i] + "'where po_id= '" + po_id[i] + "'"; pst = conn.prepareStatement(sql); pst.execute(); JOptionPane.showMessageDialog(null, "updated"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } 表中,您已将主键定义为USERSUserID列的组合键。

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

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

注意:如果可以的话,您应该重新设计它,以便不在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; 表中使用复合主键,因为它在我显示的内容中没有意义码。您可以像这样更改表格:

USERS