无法添加外键约束,Mysql错误1215

时间:2018-07-30 13:16:08

标签: mysql foreign-keys

请您告诉我我的外键有什么问题吗?我有MySQl脚本,但是最后,在创建Authorizations表时,我得到了以下信息:

Error 1215: Cannot add foreign key constraints

这是我的表的代码:

CREATE TABLE IF NOT EXISTS ProfilePublicData
(
    idProfile INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    login VARCHAR(64),
    name VARCHAR(45),
    lastName VARCHAR(45),
    location VARCHAR(45),
    age TINYINT,
    photo VARCHAR(45),
    PRIMARY KEY(idProfile)
);

CREATE TABLE IF NOT EXISTS ProfilePrivateData
(
    idProfile INT(11) UNSIGNED NOT NULL,
    CONSTRAINT fkIdProfile 
            FOREIGN KEY(idProfile) 
            REFERENCES `ProfilePublicData`(idProfile)
                ON DELETE CASCADE 
                ON UPDATE CASCADE, 
    email VARCHAR(45),
    password VARCHAR(45),
    PRIMARY KEY(idProfile)
);
CREATE  TABLE IF NOT EXISTS Authorizations
(
    idAuthorization INT(11) UNSIGNED NOT NULL PRIMARY KEY,
    email VARCHAR(45) NOT NULL,
    CONSTRAINT fkPrivateDataProfile
            FOREIGN KEY(email) 
            REFERENCES `ProfilePrivateData`(email)            
                ON DELETE CASCADE 
                ON UPDATE CASCADE, 
     systemStatus ENUM ('ONLINE','OFFLINE','DELETED'),
    `dateTimeStart` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
    `dateTimeRequest` DATETIME
);

1 个答案:

答案 0 :(得分:0)

这是由于电子邮件所致,因为电子邮件不是主键的一部分,请尝试删除电子邮件并再次测试代码。 这部分是我的意思:

CREATE  TABLE IF NOT EXISTS Authorizations
(
    idAuthorization INT(11) UNSIGNED NOT NULL PRIMARY KEY,
    email VARCHAR(45) NOT NULL,
    CONSTRAINT fkPrivateDataProfile
            FOREIGN KEY(email) 
            REFERENCES `ProfilePrivateData`(email)            
                ON DELETE CASCADE 
                ON UPDATE CASCADE, 
     systemStatus ENUM ('ONLINE','OFFLINE','DELETED'),
    `dateTimeStart` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
    `dateTimeRequest` DATETIME
);