mysql错误1215无法添加外键约束

时间:2016-02-29 08:55:01

标签: mysql

DROP TABLE IF EXISTS desk;
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS authority;

CREATE TABLE authority
(
    authorityId     INT             NOT NULL AUTO_INCREMENT PRIMARY KEY,
    deskId          INT             DEFAULT 0,              
    authorityName   VARCHAR(50)     NOT NULL,
    descriptions    VARCHAR(100)    NOT NULL,
    userLevel       TINYINT         NOT NULL,
    actions         VARCHAR(300)    DEFAULT NULL,
    setDate         TIMESTAMP       DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE desk
(
    deskId          INT             NOT NULL AUTO_INCREMENT PRIMARY KEY,
    deskName        CHAR(50)        NOT NULL,
    deskLink        VARCHAR(100)    NOT NULL,
    details         VARCHAR(350)    DEFAULT NULL,
    setDate         TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    closeDate       TIMESTAMP       DEFAULT NULL,
    userId          INT             DEFAULT NULL,                   
    status          TINYINT(1)          DEFAULT 0
);

CREATE TABLE user 
(
    userId      INT                 NOT NULL AUTO_INCREMENT PRIMARY KEY,
    userName    CHAR(150)           NOT NULL,
    password    VARCHAR(500)        NOT NULL,
    department  VARCHAR(150)        DEFAULT NULL,
    deskId      INT                 NOT NULL,
    status      TINYINT(1)          DEFAULT NULL,
    userLevel   TINYINT             NOT NULL,
    FOREIGN KEY (deskId) REFERENCES desk (deskId) ON DELETE CASCADE,
    FOREIGN KEY (userLevel) REFERENCES authority (userLevel)
);

执行查询时出错。 错误代码:1215。无法添加外键约束

我尝试了很多方法,但仍然无法解决问题,我尝试将userLevel类型更改为INT表用户和权限,但没有更好的

我真的不知道如何解决这个问题,请帮忙

2 个答案:

答案 0 :(得分:1)

按照以下步骤

1)您必须将第二个查询更改为

DROP TABLE IF EXISTS desk;

CREATE TABLE desk
(
    deskId          INT             NOT NULL AUTO_INCREMENT PRIMARY KEY,
    deskName        CHAR(50)        NOT NULL,
    deskLink        VARCHAR(100)    NOT NULL,
    details         VARCHAR(350)    DEFAULT NULL,
    setDate         TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    closeDate       TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    userId          INT             DEFAULT NULL,                   
    status          TINYINT(1)          DEFAULT 0
);

2)由于您的列 authority.userLevel 不是主键,并且您希望在其他表中将其用作外键,因此需要在authority.userLevel列上创建索引

使用以下查询

ALTER TABLE `authority` ADD INDEX `UserLevelIndex` (`userLevel`);

3)按原样运行第三个查询以创建用户表

答案 1 :(得分:0)

运行以下查询

DROP TABLE IF EXISTS desk;
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS authority;


CREATE TABLE authority
(
    authorityId     INT             NOT NULL AUTO_INCREMENT PRIMARY KEY,
    deskId          INT             DEFAULT 0,              
    authorityName   VARCHAR(50)     NOT NULL,
    descriptions    VARCHAR(100)    NOT NULL,
    userLevel       TINYINT         NOT NULL,
    actions         VARCHAR(300)    DEFAULT NULL,
    setDate         TIMESTAMP       DEFAULT CURRENT_TIMESTAMP,    
    index `cr_index` (userLevel)
);

CREATE TABLE desk
(
    deskId          INT             NOT NULL AUTO_INCREMENT PRIMARY KEY,
    deskName        CHAR(50)        NOT NULL,
    deskLink        VARCHAR(100)    NOT NULL,
    details         VARCHAR(350)    DEFAULT NULL,
    setDate         TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    closeDate       TIMESTAMP       DEFAULT '0000-00-00 00:00:00',
    userId          INT             DEFAULT NULL,                   
    status          TINYINT(1)          DEFAULT 0
);


CREATE TABLE `user` 
(
    userId      INT                 NOT NULL AUTO_INCREMENT PRIMARY KEY,
    userName    CHAR(150)           NOT NULL,
    password    VARCHAR(500)        NOT NULL,
    department  VARCHAR(150)        DEFAULT NULL,
    deskId      INT                 NOT NULL,
    status      TINYINT(1)          DEFAULT NULL,
    userLevel   TINYINT           NOT NULL,
    FOREIGN KEY (deskId) REFERENCES desk (deskId) ON DELETE CASCADE,
    FOREIGN KEY (userLevel) REFERENCES authority (userLevel)
);

在查询中,您编写时间戳默认值NULL,并且可以创建非索引键userLevel作为外键,因此从查询中删除默认值或更改默认值0000-00-00 00:00:00 (在桌面表中)并在userLevel列中添加索引(在权限表中) 我希望这对你有用