简体   繁体   中英

MYSQL 1215 Cannot add Foreign Key Constraing

Problem is on the bounding table column GroupDim.FacultyID and FacultyDim.FacultyID , data types are identity BIGINT This data store for OLAP cube wherein Rating_facts stores result of question which i exploring. table without signature Dim is transitional and serve for additional data for analysis.Maybe it is needs to divide this table?

  DROP TABLE IF EXISTS BlockDim;
CREATE TABLE BlockDim (
    BlockKey bigint NOT NULL AUTO_INCREMENT,
    BlockID bigint NOT NULL,
    BlockName varchar(32) NOT NULL,
    PRIMARY KEY (BlockKey)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS FacultyDim;
CREATE TABLE FacultyDim (
    FacultyKey bigint NOT NULL AUTO_INCREMENT,
    FacultyID bigint NOT NULL,
    FacultyName varchar(32) NOT NULL,
    PRIMARY KEY (FacultyKey, FacultyID)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS `Group`; 
CREATE TABLE `Group` (
    GroupID bigint NOT NULL,
    FacultyID bigint NOT NULL,
    GroupName varchar(32) NOT NULL,
    PRIMARY KEY (GroupID),
    FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID) 
)ENGINE=InnoDB;

DROP TABLE IF EXISTS Subblock; 
CREATE TABLE Subblock (
    SubblockKey bigint NOT NULL AUTO_INCREMENT,
    SubblockID bigint NOT NULL,
    BlockID bigint NOT NULL,
    SubblockName varchar(32) NOT NULL,
    PRIMARY KEY (SubblockKey)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS Paragraph; 
CREATE TABLE Paragraph (
    ParagraphKey bigint NOT NULL AUTO_INCREMENT,
    ParagraphID bigint NOT NULL,
    SubblockID bigint NOT NULL,
    ParagraphName varchar(64) NOT NULL,
    PRIMARY KEY (ParagraphKey)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS RatingDim; 
CREATE TABLE RatingDim (
    RatingKey bigint NOT NULL AUTO_INCREMENT,
    RatingID bigint NOT NULL,
    ParagraphID bigint NOT NULL,
    Score double NOT NULL,
    StageOfApprove int NOT NULL,
    Comment varchar(128) NOT NULL,
    `Date` DATE NOT NULL,
    PRIMARY KEY (RatingKey)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS TimeDim; 
CREATE TABLE TimeDim (
    TimeKey bigint NOT NULL AUTO_INCREMENT,
    `Date` DATE NOT NULL,
    DayOfWeek varchar(16) NOT NULL,
    `Month` varchar(16) NOT NULL,
    `Year` varchar(4) NOT NULL,
    PRIMARY KEY (`TimeKey`)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS Rating_Fact;
CREATE TABLE Rating_Fact (
    TimeKey bigint NOT NULL,
    BlockKey bigint NOT NULL,
    FacultyKey bigint NOT NULL,
    RatingKey bigint NOT NULL,
    PopularBlock bigint NOT NULL,
    AvgFacultyScore bigint NOT NULL,
    StudentsMaxRating bigint NOT NULL,
    StudentsNotApprovedRating bigint NOT NULL,
    PRIMARY KEY (TimeKey,BlockKey,FacultyKey,RatingKey),
    FOREIGN KEY (TimeKey) REFERENCES TimeDim(TimeKey),
    FOREIGN KEY (BlockKey) REFERENCES BlockDim(BlockKey),
    FOREIGN KEY (FacultyKey) REFERENCES FacultyDim(FacultyKey),
    FOREIGN KEY (RatingKey) REFERENCES RatingDim(RatingKey)
)ENGINE=InnoDB;

First you have check your table architecture.then try below two solutions

Solution 1:-

 DROP TABLE IF EXISTS FacultyDim;
    CREATE TABLE FacultyDim (
        FacultyKey bigint NOT NULL AUTO_INCREMENT,
        FacultyID bigint NOT NULL,
        FacultyName varchar(32) NOT NULL,
        PRIMARY KEY (FacultyKey, FacultyID)
    )ENGINE=InnoDB;

    DROP TABLE IF EXISTS `Group`; 
    CREATE TABLE `Group` (
        GroupID bigint NOT NULL,
        FacultyID bigint NOT NULL,
        GroupName varchar(32) NOT NULL,
        PRIMARY KEY (GroupID),
        FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID,FacultyKey) 
    )ENGINE=InnoDB;

Solution 2:-

DROP TABLE IF EXISTS FacultyDim;
CREATE TABLE FacultyDim (
    FacultyKey bigint NOT NULL AUTO_INCREMENT,
    FacultyID bigint NOT NULL,
    FacultyName varchar(32) NOT NULL,
    PRIMARY KEY (FacultyID)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS `Group`; 
CREATE TABLE `Group` (
    GroupID bigint NOT NULL,
    FacultyID bigint NOT NULL,
    GroupName varchar(32) NOT NULL,
    PRIMARY KEY (GroupID),
    FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID) 
)ENGINE=InnoDB;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM