In postgres we have a constraint defined that essentially allows us to limit the number of entries in a table with a certain value to one. We created this constraint:
I never use MySQL but maybe you can create an index like this:
CREATE UNIQUE INDEX list$default$uk ON list_group ((CASE WHEN list_type='default' THEN NULL ELSE visitor_uid END));
Explanation: A unique index should not care about NULL values. Therefore, make sure the index expression returns NULL for every row where list_type <> 'default'.
I supposed you'd have to write a trigger to check for it.
MYSQL doesn't support such types of constraints.
You should use stored procudures for inserting data instead, so you can do some checking and validation.
Why don't you define your default as such that it must have 1 as primary key? This way a normal unique constraint on the pk would be enough already.
If nothing fits to you, you could also consider changing your data model.
My two cents:
why don't you create a column only to store the unique value (maybe you can call it something like is_list_type_default
) and another to store all the values. If you do this, you can put a unique
constraint on the first column.
Actually it exists. Most contraints depend on the table Engine. I think InnoDB supports this.
To do it, you have to add a UNIQUE index with the unique combination:
ALTER TABLE visitor ADD UNIQUE unique_default_visitor( visitor_uid, list_type );