Skip to content
Advertisement

MySQL Default Record from Table

I use MySQL 5.6 and I have 3 tables. Two of them are a list of data, and the third table is used for a relationship between them. In the third table I would like to identify a default record for each id1, so I added a column named predt to let me know which record is the default one. Something like this:

In this example I will know that the second row is the default one for id1 = 1.

Whats worries me is that it’s possible that more than one record could have the value 1 for column predt where id1 = 1.

I validate that this doesn’t happen, is the first thing I do in the SP that inserts or updates a record in this table. But if someone updates the table directly there is no restriction in the table to stop it from doing so (like a foreign key restriction).

I always treated this case in the same way, and so far it has worked. But I would like to know if there is a better way to work with this kind of cases.

Advertisement

Answer

You should test if for that particular row already exists such predt = 1 In higher mysql Versions you could use a CHECK Constraint with the same purpose

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement