Alex Rivera | Logout

How to create a "unique" constraint on a boolean MySQL column?

Asked 2011-01-06T17:43:44.717
29

I would like to add a BOOLEAN column to a MySQL table which will be named is_default.

How can I constrain this column so only one record can have is_default set to true?

Edit
Report

1 Answer

2

Check out triggers. They were introduced in version 5.0.2, I believe. You want a "before insert" trigger. If there is already a row with is_default=true, raise an error. I don't know what problems you might with concurrency and so on, but hopefully this is enough to you started.

answered 2011-01-06T17:52:45.540

Your Answer