In your case, I believe your comentário
field should have its own id
and should have post_id
being used as a foreign key, just like in the case of invoices, since in these cases you are talking about a relationship 1xN (a comment belongs to one, and only one post, since the post has 0 or many comments).
Using the composite primary key can be done when you have two fields that together will always be unique to that table, for example:
In a given airport, flights run every day, the number of flights is always the same for a certain time and destination, for example, the flight bound for Guarulhos at 8:00 p.m. 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
, since in the same day many flights depart. In this case, you could use the voo
and data
fields as primary keys, since they together are unique since you will never have the same flight twice in one 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 for each record and use a single primary key, however that depends on your modeling.
Transcribing everything that was said in the comments:
I venture to say that for all cases the situation resembles the dummy flight table, you can choose either by using two columns as a composite primary key or by creating an id and having a primary key simple.
Differences:
-
Choosing to use two columns as the primary key saves a field in your table, and you're already adding a constraint to your table ensuring data consistency. The disadvantage in this case is having to use two fields in every operation you do that needs to pick up a single record, such as select, update, delete, etc. Another disadvantage is that for each relational table you make, it will have one more field, so the first advantage I mentioned ends up not compensating for this disadvantage, since a relational table usually has many more records than the tables it relates to. / p>
-
By choosing to use the id, it is easier to make relationships with other tables, since you will only use one field at a time. The downside is that you will have one more field in your table. Another point that is neither disadvantage nor advantage is that you will have to put the constraints 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 primary key compound.
In most cases, the advantages and disadvantages of each are almost unmentionable, so choose the option that you feel most comfortable.
I could only imagine a case where one option would be better than the other would be if your model has many NxN relationships, because the number of fields would increase considerably, even more if instead of two fields you have countless fields as the primary key, for each table you will have a much larger number of fields and data.