mysql外键一个条目的多个键
我想为一张桌子做这样的事情。 我有两个表,一个带有外键(id),第二个表保存与第一个表相对应的数据。 我的问题是,对于每个条目,我可能也有一个或两个甚至更多的组。 是否可以做这样的事情: table1 - id(外键) table2 - 条目1 - table1.id1,table1.id2 如果可能的话你能解释一下我该怎么做吗?
CREATE TABLE IF NOT EXISTS `network`.`dbo.networkNodes` (
`nodeId` INT(11) NOT NULL AUTO_INCREMENT ,
`nodeName` VARCHAR(45) NULL ,
PRIMARY KEY (`nodeId`) )
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `network`.`dbo.networkIps` (
`networkIpId` INT(11) NOT NULL AUTO_INCREMENT ,
`nodeId` INT(11) NULL ,
`networkIp` INT(20) NULL ,
PRIMARY KEY (`networkIpId`) )
ENGINE = InnoDB
i want to do for a table something like this.
I have two tables one with a foreign key(id) and second table where i hold data that coresponds to the first table.
My problem is that for each entry i might have one or two even more groups that belongs too.
Is it posible to do something like this:
table1 - id(foreign key) table2 - entry1 - table1.id1, table1.id2
And if it is posible can you explain how should i do?
CREATE TABLE IF NOT EXISTS `network`.`dbo.networkNodes` (
`nodeId` INT(11) NOT NULL AUTO_INCREMENT ,
`nodeName` VARCHAR(45) NULL ,
PRIMARY KEY (`nodeId`) )
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `network`.`dbo.networkIps` (
`networkIpId` INT(11) NOT NULL AUTO_INCREMENT ,
`nodeId` INT(11) NULL ,
`networkIp` INT(20) NULL ,
PRIMARY KEY (`networkIpId`) )
ENGINE = InnoDB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您要求的是多对多关系,或者多个定义的一对多关系,所以这取决于用途。
多对多关系实际上需要一个中间表,其中包含指向两个表中每一个的“id”字段:
如果您希望添加多个一对多关系,则应包含特定关系“原因”以外键的名称。
I think you're asking for either a many-to-many relationship, or multiple, defined one-to-many relationships, so it depends on the use.
A many-to-many relationship would actually require an intermediate table, containing an 'id' field pointing to each of the two tables:
If you would rather add multiple one-to-many relationships, the specific relationship 'reason' should be contained in the name of the foreign key.