In MySQL, Can I put a restriction like below at database level?
I need to make sure 1. a specific column can only take either 'true' or 'false' values. 2. and exactly only one row should be having 'true' value?
In MySQL, Can I put a restriction like below at database level?
I need to make sure 1. a specific column can only take either 'true' or 'false' values. 2. and exactly only one row should be having 'true' value?
a specific column can only take either 'true' or 'false' values.
Use Datatype for column either boolean
or tiny-int
2 and exactly only one row should be having 'true' value?
you have to write a query which update all the row columns to false except one you set to true
Set the data-type of the column to "boolean"; then add a trigger-function, that sets the row you want to be always "true" to true on update.
Rather than having the boolean attribute in the table, you could have another table that contains one row and points to the row in the original table that you consider true.
Changing the true value is a matter of updating the foreign key in the TrueRow table.