简体   繁体   English

没有主键的连接表

[英]Junction table without primary key

Say I have a junction table to resolve a many to many relationship I have between two tables. 说我有一个联结表来解决两个表之间的多对多关系。 My junction table also has its own 'Method' column to describe the relationship. 我的联结表也有自己的“方法”列来描述这种关系。 连接表示例

Normally, I would make a composite primary key of [a], [b] and [method] (the method needs to be part of what makes a row unique), but my problem is that [method] field can be NULL. 通常,我会使用[a],[b]和[method]的复合主键(该方法必须是使行唯一的部分),但是我的问题是[method]字段可以为NULL。 Therefore I cannot add it to the primary key. 因此,我无法将其添加到主键。

So what I've done is create a unique index: 所以我要做的是创建一个唯一索引:

ALTER TABLE A_B ADD UNIQUE INDEX `Unique` (`a`, `b`, `method`);

The table has no primary key. 该表没有主键。 Is this an okay thing to do or do I need to do something differently? 这是可以做的事情还是我需要做一些不同的事情?

Using a primary key is not the only way to ensure records uniqueness. 使用主键不是确保记录唯一性的唯一方法。 There is a unique constraint you can implement to accomplish what is needed. 您可以实施一个独特的约束来完成所需的工作。

http://www.w3schools.com/sql/sql_unique.asp http://www.w3schools.com/sql/sql_unique.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM