在两个表之间创建关系并创建外键

问题描述 投票:0回答:2

例如,我们有两个表作为

creators (
creatorid int not null,
title varchar(100) not null, 
primary key(creatorid)
);

authors (
creatorid int not null,
titleid int not null,
primary key (creatorid, titleid)
);

好的,我问如何建立关系?我认为创造者必须与第二张桌子中的创作者有关,但我不知道如何制作它。

mysql database relationships
2个回答
4
投票
ALTER TABLE `authors` ADD INDEX ( `creatorid` );
ALTER TABLE `authors` ADD CONSTRAINT `FK_creators` FOREIGN KEY (`creatorid`) REFERENCES `creators` (`creatorid`);

更多信息:


© www.soinside.com 2019 - 2024. All rights reserved.