外键:简单查询不起作用

时间:2014-05-25 14:21:26

标签: mysql sql foreign-keys

我似乎无法使用以下MySQL-Query:

CREATE TABLE task_one_line (
    `timestampStart` BIGINT NOT NULL, 
    `userId` VARCHAR(64) NOT NULL, 
    PRIMARY KEY (`timestampStart`, `userId`)
)ENGINE=InnoDB


CREATE TABLE task_one_included_object (
    `objectId` INTEGER NOT NULL, 
    `timestampStart` BIGINT NOT NULL, 
    `userId` VARCHAR(64) NOT NULL, 
    PRIMARY KEY (`objectId`, `userId`, `timestampStart`), 
    FOREIGN KEY(`userId`, `timestampStart`) REFERENCES task_one_line (`userId`, `timestampStart`)
)ENGINE=InnoDB

它给出了:

ERROR 1005 (HY000): Can't create table 'crowdtracker.task_one_included_object' (errno: 150)

根据MySQL-Manual指出外键约束的错误。但是所有外键都具有与引用属性相同的约束,因此我无法理解错误。我错过了什么?

祝你好运

1 个答案:

答案 0 :(得分:0)

订单会对主键的引用产生影响。您的引用为timestampStart, userId。它应该是另一种顺序:

CREATE TABLE task_one_line (
    `timestampStart` BIGINT NOT NULL, 
    `userId` VARCHAR(64) NOT NULL, 
    PRIMARY KEY (userId, `timestampStart`)
);

CREATE TABLE task_one_included_object (
    `objectId` INTEGER NOT NULL, 
    `timestampStart` BIGINT NOT NULL, 
    `userId` VARCHAR(64) NOT NULL, 
    PRIMARY KEY (`objectId`, `userId`, `timestampStart`), 
    FOREIGN KEY(`userId`, `timestampStart`) REFERENCES task_one_line (`userId`, `timestampStart`)
);