Is it possible to create a unique constraint over three columns in mySQL, in conjunction with a specific value in one of the columns?
Example: This is a table with links between a user (id 1000) and its properties
user_id, properties_id, value, active, date_add
1000, 1004, "011 23456", 1, 2020-08-11
1000, 1005, "Toyota", 1, 2020-08-11
1000, 1005, "Honda", 0, 2015-01-01
1000, 1005, "BMW", 0, 2002-01-01
The properties define the meaning of the column value:
1004 = Telephone number of the user
1005 = Car brand of the user
The user has already had 3 car brands in his life, so there are three entries with the properties ID 1005, one of which is active because this is his current car. The others are inactive.
I want to create a constraint that allows several combinations of user_id, properties_id and active, but only if active = 0. There must not be several identical properties that are all active. (Even if a user can have several cars, he should always have only one current car in this data model).
Can mySQL map such a thing?
Greeting Markus