Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL and Check Constraints

I have inherited an application that uses MySQL and that is used by a PHP front end. The guy that wrote this system has gone to some fairly convoluted lengths to ensure that codes that users enter are valid - and tat means that these codes also exist in another table.

When I first saw this I wondered why he hadn't used CHECK constraints and let the dbms sort this out - I have visions of a load of different programs implementing the same checks instead of just the one place in the dbms. And then I found out that MySQL doesn't support Check constraints (not strictly true - it supports the syntax but just ignores it).

Is there a way that I can implement Check Constraints in MySQL?

Any hints, suggestions etc, would be great.

like image 388
Simon Knights Avatar asked Apr 01 '09 16:04

Simon Knights


People also ask

Does MySQL have check constraint?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column.

Does MySQL 5.7 support check constraint?

CHECK constraints are not supported by MySQL. You can define them, but they do nothing (as of MySQL 5.7). From the manual: The CHECK clause is parsed but ignored by all storage engines.

What does check constraints mean?

A check constraint is a rule that specifies the values that are allowed in one or more columns of every row of a base table. For example, you can define a check constraint to ensure that all values in a column that contains ages are positive numbers.


1 Answers

You can implement something similar to them with triggers, but MySQL itself (before version 8.0.16) doesn't support CHECK constraints. Don't worry though, it'll let you define them and just silently ignore them!

like image 174
Chad Birch Avatar answered Oct 20 '22 15:10

Chad Birch