简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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