1

This seems to be a very common problem but I can't seem to find an answer despite of all the posts and pseudotutorials I have read in the last 2 days :/

The project I am currently working on forces me to learn how to use MySQL and I seem to have run into a problem concerning ForeignKeys or Indexes.

I have these creates:
http://paste.ubuntu.com/1503473/

Edit: Inserting crutial part here cause the link above might die sometime:

   CREATE TABLE user_book (
        userid  Varbinary(16),
        bookid Varbinary(16),

        PRIMARY KEY(userid, bookid),

        INDEX(userid),
        FOREIGN KEY(userid)  REFERENCES user,

        INDEX (bookid),
        FOREIGN KEY(bookid) REFERENCES book

    )ENGINE=INNODB;

    Made the same mistake at user_chapter.

But sql throws an error at Table "user_book":
#1005 - Can't create table 'mangressdb.user_book' (errno: 150)

It would be really great if you could help me out here as I am close to crying. It is probably just a very stupid mistake on my part but I can't find it.

4

1 回答 1

2

看起来您在 DDL 中遇到了名称冲突。尝试明确指定要用于外键的父表列:

CREATE TABLE user_book (
    userid  Varbinary(16),
    bookid Varbinary(16),

    PRIMARY KEY(userid, bookid),

    INDEX(userid),
    FOREIGN KEY(userid)  REFERENCES user (userid),

    INDEX (bookid),
    FOREIGN KEY(bookid) REFERENCES book (bookid)

)ENGINE=INNODB;

您还需要显式指定user_chapter表的父列名称。

于 2013-01-06T16:04:13.637 回答