dga*_*ma3 21 mysql sql phpmyadmin
这是我关注的两个表:
CREATE TABLE IF NOT EXISTS `tutorial` (
`beggingTime` time NOT NULL,
`day` varchar(8) NOT NULL,
`tutorId` int(3) NOT NULL,
`maxMembers` int(2) NOT NULL,
`minMembers` int(1) NOT NULL,
PRIMARY KEY (`beggingTime`,`day`,`tutorId`),
KEY `tutorId` (`tutorId`)
)
CREATE TABLE IF NOT EXISTS `group` (
`groupId` tinyint(3) NOT NULL AUTO_INCREMENT,
`status` varchar(20) NOT NULL,
`groupName` varchar(50) NOT NULL,
PRIMARY KEY (`groupId`)
)
Run Code Online (Sandbox Code Playgroud)
我想在'group'中创建一个链接到'tutorial'中复合唯一键的字段.所以我想我的问题是,我如何联系这些表?我是否必须在'tutorial'中为每个主键创建外键字段?
Jus*_*ony 23
根据mySQL文档,您应该能够设置外键映射到复合,这将要求您创建多个列.
添加列并将其放在group
表中
FOREIGN KEY (`beggingTime`,`day`,`tutorId`)
REFERENCES tutorial(`beggingTime`,`day`,`tutorId`)
Run Code Online (Sandbox Code Playgroud)
正如Steven在下面的评论中提到的那样,你应该尝试重新设计它,以便教程表使用实际的主键(即使它只是一个身份代理键).这样可以提高性能,因为SQL是为这种类型的关系而构建的,而不是复合关系.
归档时间: |
|
查看次数: |
36974 次 |
最近记录: |