I have two tables:
First
------
id
name
Second
------
id
name
And another table connecting the first two:
Third
------
first_id
second_id
The third table is there only to resolve the M:N issue. Should it have its own ID?
I have two tables:
First
------
id
name
Second
------
id
name
And another table connecting the first two:
Third
------
first_id
second_id
The third table is there only to resolve the M:N issue. Should it have its own ID?
If the table only contains the two foreign keys, there is no reason to have an additional key. You won't be using it in any query.
When you join the tables using the connection table, you are making a join against one foreign key at a time, not against both foreign keys at once, so there is no use for another key in the connection table. Example:
select t1.name, t2.name
from First t1
inner join Third t3 on t3.first_id = t1.id -- one foreign key
inner join Second t2 on t2.id = t3.second_id -- the other foreign key
Just make a primary key combining the two foreign keys.
PRIMARY KEY (first_id, second_id)
No, you normally don't need an id field on the table connecting the two. You can make the primary key for the connecting table (first_id, second_id).
If it's just a simple mapping table, then I would say no. What purpose would that extra ID serve? Just make the primary key a composite: (first_id, second_id).
Having said that, I have seen cases where it's arguable to have a separate ID because it's easier to work with some ORM tools. But generally I would say you should avoid the extra ID column if you can.
I started using Composite PKs out of the two columns and it worked for me for a while.
As business grew and business rules changed, some of these table started getting additional attributes or participating in relationships and then I either needed to add a single column PK or just carry a double key down (which grows old real fast).
I decided then to standardize my own design. Now, as a part of my design, I use an additional single column PK for all join tables.
It depends on role of the table "'third" (connection or join table). Lets assume for example, that this table express a relation between two others tables. Since it is all, there is no need for additional Id.
But what if that relation, between table "One" and "Two" became temporary? I mean, that relation is for some period of time. For example ownership, marriage, etc. Then you can't add another record/entity to the connection table ("Third") because such a relation already exist.
So it is safer to establish for each table it's own Id. Exept we are sure, that in the future, temporary (since that multitimes) relation will not happen.
========================================= sorry for my english