Good day. There is a table of vehicles_char_of_vehicles (vehicle_id - FK, charact_id - FK, value). How to make it so that it is impossible (by mistake) the same values: 1 - auto, 1 - load capacity, 2000 - sign and second record 1, 1, 3000. Through the unique field, I will simply make it so that one characteristic can be entered once, for example, for the first truck in char_id - 1, value - 2000, and the entry char_id - 1, value - 3000 - for the second truck, there will already be an error, since cgar_id is unique. How to achieve what I need?

  • Make a unique key on two fields, probably. - Visman
  • You write all the more incomprehensible and not clear. only from far away you can guess what it is about. You can include both _id fields in the primary key (nobody obliges it to be made from the same field). Or if PK is and it is actually used (there are links to it), you can make a unique index from these two fields. create unique index NAME on Table(v_id, c_id) - Mike
  • so which path should you choose in this event: table vehicles (id - PK), characteristics (id - PK), vehicles_char_of_vehicles (vehicle_id - FK (vehicles.id), char_id - FK (characteristics (id)), value)? - Muscled Boy
  • @Visman, please explain. After all, the vehicle_id, char_id fields are already foreign keys, so I can't make them PK, or what do you mean? - Muscled Boy
  • one
    PK from both fields, it means that the combination of machine-characteristic will be unique. ru.stackoverflow.com/questions/546703/… - Mike

1 answer 1

Two unique keys on two fields.

  • Ie two PK? but you can't have PK and FK at the same time? - Muscled Boy
  • thanks, already explained - Muscled Boy