In your case I believe your field comentário
must have its own id
and must have the post_id
being used as a foreign key, as in the case of tax bills, since in these cases you are talking about a 1xN relationship (a comment belongs to one, and only one post, already the post has 0 or many comments).
The use of composite primary key can be done when you have two fields that together will always be unique to that table, for example:
At a given airport flights leave every day, the number of flights is always the same for a certain time and a certain destination, for example, the flight to Guarulhos at the time of 20:00 is number 1212. Regardless of the day, Flight 1212 will always be with the destination to Guarulhos and always at the time of 20:00, so you can not use voo
as the primary key, not even data
, because in the same day leave numerous flights. In that case you could use the fields voo
and data
as primary keys, because together they are unique, since they will never have the same flight twice in a single day.
| voo | data | outros campos...
--------------------------------------
| 1212 | 14/05/2014 |
| 1234 | 14/05/2014 |
| 2345 | 14/05/2014 |
| 1212 | 15/05/2014 |
| 1234 | 15/05/2014 |
| 2345 | 15/05/2014 |
You could also assign an id to each record and use a single primary key, however this depends on your modeling.
Transcribing everything that was said in the comments:
I venture to say that for all cases that the situation resembles the fictitious table of flights, you may choose either by using two columns as a composite primary key or by creating an id and having a simple primary key.
Differences:
Choosing to use two columns as a primary key saves you a field in your table, and also you are already adding one Constraint its table ensuring the consistency of the data. The downside in this case is having to use two fields in every operation you do that needs to take a single record, like select, update, delete, etc. Another downside is that for every relational table you do, you will have one more field, So the first advantage I mentioned does not compensate for this disadvantage, since a relational table usually has much more records than the tables it relates.
Choosing to use the id makes it easier to make relationships with other tables, as you will only use one field every time. The disadvantage is that you will have one more field in your table. Another point that is not disadvantage or advantage is that you will have to put the constraints the part, indicating which fields are unique, it is worth remembering that it is possible to say that two fields together are unique, similar to a composite primary key.
In most cases, the advantages and disadvantages of each are almost disregardable, so choose the option you get the most at ease.
I could only imagine a case where one option would be better than the other, it would be if your model has many Nxn relationships, because the amount of fields would increase considerably, even more if instead of two fields you have numerous fields as the primary key, for each table you will possess a much higher amount of fields and data.
What is the difference in the table
comentários
the first and the second example? The way it is posted, they are identical, only something is missing. The only difference is that in the second example you do not show the table ofposts
. The way it is I’d say the answer is to use any one.– Maniero
@bigown Pay attention to comments id’s. In the second example they just don’t repeat themselves in relation to the column
post_id
. In the first example they do not repeat I relation to any other comment, even those of other posts.– user7261
Got it, your question is about using a general ID or an ID that is actually a specific post item.
– Maniero
ID vs composite key: how? when? why?
– Jorge B.