Skip to content
Advertisement

How do I add a custom CHECK constraint on a MySQL table?

I am having trouble with this table

I want to have a foreign key constraint, and that works. Then, I also want to add a constraint to the attribute status so it can only take the values ‘a’, ‘d’ and ‘u’. It is not possible for me to set the field as Enum or set.

Can anyone tell me why this code does not work in MySQL?

Advertisement

Answer

Starting with version 8.0.16, MySQL has added support for CHECK constraints:

Previously, this was only available using BEFORE INSERT and BEFORE UPDATE triggers:

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