MySql 错误 1215:创建时无法添加外键约束 table

MySql error 1215: Cannot add foreign key constraint when create table

我更改了 table Company,修改了列 Comp_discountrate。然后,我创建 table TQuote,由于 Comp_discountrate 作为外键约束而出现错误(1215)。没有 table 定义的最后一行,它工作得很好。我该如何处理?

Alter table Company
modify Comp_discountrate double(2,2) not null default '0.4';

create table TQuote(
    Quo_id varchar(7) not null,
    Mem_id varchar(7) not null,
    Quo_date DATETIME NOT NULL DEFAULT NOW(),
    Comp_discountrate double(2,2) not null default '0.4',
    primary key(Quo_id, Mem_id),
    foreign key(Mem_id) references Tuser(Mem_id) on delete cascade,
    foreign key(Comp_discountrate) references Company(Comp_discountrate) on update cascade
)

您需要公司的唯一索引(Comp_discountrate)

引用的字段必须被索引。虽然我建议这样的索引是唯一的,但 MySQL 并不要求它们是唯一的(除非使用 NDB 存储引擎)。