无法尝试制作这些表格

时间:2016-08-06 08:19:02

标签: mysql sql

嘿伙计们在尝试创建这5个表时我遇到了一个问题。据我所知,问题必须与冠军打交道。每次我尝试导入我的sql文件时,我都会得到一个errno 150,声明无法创建冠军表。

澄清一个冠军只能有一个派系,但派系可以由许多冠军组成。角色和亲和力的关系相同。感谢您提前提供的任何帮助。

DROP TABLE IF EXISTS `champion`;
DROP TABLE IF EXISTS `role`;
DROP TABLE IF EXISTS `build`;
DROP TABLE IF EXISTS `faction`;
DROP TABLE IF EXISTS `build_type`;
DROP TABLE IF EXISTS `affinity`;


-- roles table 
-- Table consisting of the roles various champions play

CREATE TABLE `role` (
        `role_id` int(11) NOT NULL AUTO_INCREMENT,
        `role_name` varchar(255) NOT NULL,
        PRIMARY KEY (`role_id`)
)ENGINE=InnoDB;

-- build table
CREATE TABLE `build` (
        `build_id` int(11) NOT NULL AUTO_INCREMENT,
        `build_name` varchar(255) NOT NULL,
        PRIMARY KEY (`build_id`),
        UNIQUE KEY (`build_name`)
)ENGINE=InnoDB;

-- faction table 
-- Table consisting of the faction each champion belongs to

CREATE TABLE `faction`(
        `faction_id` int(11) NOT NULL AUTO_INCREMENT,
        `faction_name` varchar(255) NOT NULL, 
        PRIMARY KEY (`faction_id`)
)ENGINE=InnoDB;


-- Champions table 
-- Table consisting of various champions in League of Legends

CREATE TABLE `champion`(
        `champion_id` int(11) NOT NULL AUTO_INCREMENT,
        `champion_name` varchar(255) NOT NULL,
        `f_id` int(11) NOT NULL, 
         `r_id` int(11) NOT NULL,
        `a_id` int(11) NOT NULL, 
        PRIMARY KEY (`champion_id`),
        FOREIGN KEY (`r_id`) REFERENCES `role` (`role_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE, 
        FOREIGN KEY (`f_id`) REFERENCES `faction` (`faction_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE,
        FOREIGN KEY (`a_id`) REFERENCES `affinity` (`affinity_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE,
        UNIQUE KEY (`champion_name`)
)ENGINE=InnoDB;

-- build_type table 
CREATE TABLE `build_type`(
        `cid` int(11) NOT NULL, 
        `bid` int(11) NOT NULL, 
        PRIMARY KEY (`cid`, `bid`),
        FOREIGN KEY (`cid`) REFERENCES `champion` (`champion_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE,
        FOREIGN KEY (`bid`) REFERENCES `build` (`build_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE
)ENGINE=InnoDB;


-- affinity table 
-- Table consisting of the affinity a certain champion synergizes
-- with

CREATE TABLE `affinity`(
        `affinity_id` int(11) NOT NULL AUTO_INCREMENT, 
        `affinity_name` varchar(255) NOT NULL,
        PRIMARY KEY (`affinity_id`)
)ENGINE=InnoDB;

1 个答案:

答案 0 :(得分:2)

那是因为champion表有一列a_id引用了尚未存在的表affinity。要解决此问题,必须先创建affinitychampion

CREATE TABLE `affinity`(
        `affinity_id` int(11) NOT NULL AUTO_INCREMENT, 
        `affinity_name` varchar(255) NOT NULL,
        PRIMARY KEY (`affinity_id`)
)ENGINE=InnoDB;

必须到来之前

CREATE TABLE `champion`(
        `champion_id` int(11) NOT NULL AUTO_INCREMENT,
        `champion_name` varchar(255) NOT NULL,
        `f_id` int(11) NOT NULL, 
         `r_id` int(11) NOT NULL,
        `a_id` int(11) NOT NULL, 
        PRIMARY KEY (`champion_id`),
        FOREIGN KEY (`r_id`) REFERENCES `role` (`role_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE, 
        FOREIGN KEY (`f_id`) REFERENCES `faction` (`faction_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE,
        FOREIGN KEY (`a_id`) REFERENCES `affinity` (`affinity_id`)
                ON DELETE CASCADE 
                ON UPDATE CASCADE,
        UNIQUE KEY (`champion_name`)
)ENGINE=InnoDB;