mysql not in 多列,MySQL中的多列外键?

I have a table that has a primary key consisting of two columns (product_id, attribute_id). I have another table that needs to reference this table. How can I make a foreign key in the other table to link it to a row in the table with two primary keys?

解决方案

Something like this ought to do it:

CREATE TABLE MyReferencingTable AS (

[COLUMN DEFINITIONS]

refcol1 INT NOT NULL,

rofcol2 INT NOT NULL,

CONSTRAINT fk_mrt_ot FOREIGN KEY (refcol1, refcol2)

REFERENCES OtherTable(col1, col2)

) ENGINE=InnoDB;

MySQL requires foreign keys to be indexed, hence the index on the referencing columns

Use of the constraint syntax enables you to name a constraint, making it easier to alter and drop at a later time if needed.

InnoDB enforces foreign keys, MyISAM does not. (The syntax is parsed but ignored)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值