我尝试添加外键:
CREATE TABLE `invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
` name` varchar(255) NOT NULL,
`code_text` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `leads` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`lead_id`)
);
CREATE TABLE `leads_invoices` (
leads_invoice_id int(11) AUTO_INCREMENT NOT NULL,
invoice_id int(11) NOT NULL,
lead_id_i int(11) NOT NULL,
PRIMARY KEY(leads_invoice_id),
FOREIGN KEY (invoice_id) REFERENCES invoices(id),
FOREIGN KEY (lead_id_i) REFERENCES leads(lead_id)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8
但我的lead_id_i密钥错误了:
无法创建表
crm_test
。leads_invoices
(错误:150“外来 键约束形成错误“)
我检查了所有类型和表格,看来它们是正确的.. 我怎么解决这个问题?希望你能帮助我。谢谢!
答案 0 :(得分:4)
您必须将ENGINE=InnoDB DEFAULT CHARSET=utf8
添加到潜在客户表
CREATE TABLE `leads` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`lead_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
因为如果要添加外键,两个表必须具有相同的引擎