我正在尝试创建这些表,但出现此错误


  错误代码:1005。无法创建表
  estadisticastbl008_phase_competition(错误号:150“外键
  约束的格式不正确”)


当我尝试创建tbl008_phase_competition ...

create table if not exists `tbl015_seasons` (
    `id` smallint(4) unsigned not null auto_increment,
    `id_competition` smallint(2) unsigned not null,
    `description` varchar(100) not null,
    primary key(`id`),
    foreign key (`id_competition`) references `tbl001_competition` (`id`)
)ENGINE = InnoDB;

create table if not exists `tbl012_phase` (
    `id` smallint(4) unsigned not null auto_increment,
    `name` varchar(100) not null,
    primary key (`id`)
)ENGINE = InnoDB;

create table if not exists `tbl008_phase_competition` (
    `id_season` smallint(4) unsigned not null,
    `id_phase` smallint(4) unsigned not null,
    `year` int unsigned not null,
    primary key (`id_season`, `id_phase`),
    foreign key (`id_season`) references `tbl015_seasons` (`id`),
    foreign key (`id_phase`) references `tbl012_phase` (`id`)
)ENGINE = InnoDB;


我究竟做错了什么?

最佳答案

请不要在此表中声明主键并检查:

create table if not exists `tbl008_phase_competition` (
    `id_season` smallint(4) unsigned not null,
    `id_phase` smallint(4) unsigned not null,
    `year` int unsigned not null,
    primary key (`id_season`, `id_phase`), // remove this line
    foreign key (`id_season`) references `tbl015_seasons` (`id`),
    foreign key (`id_phase`) references `tbl012_phase` (`id`)
)ENGINE = InnoDB;

关于mysql - MySQL:Errno:150“外键约束格式不正确”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46095690/

10-10 16:41