2 Ways to List CHECK Constraints in MySQL

Ever since the release of MySQL 8.0.16, we’ve had the ability to create CHECK constraints in MySQL. At some point, we may want to return a list of CHECK constraints that have been created in a database or against a given table.

Fortunately, we can use the information_schema.check_constraints view to do just that.

We can alternatively use the information_schema.table_constraints view to get the same info.

Continue reading

How to Change a CHECK Constraint’s Enforcement State in MySQL

When we create or alter a CHECK constraint in MySQL, we have the option of setting it to ENFORCED or NOT ENFORCED. This determines whether or not MySQL will enforce the rules of the constraint whenever data that violates the constraint tries to enter the database.

We can use the ALTER TABLE statement to change the enforcement state of an existing CHECK constraint in MySQL, as shown in the following example.

Continue reading

2 Ways to Verify Whether a CHECK Constraint is Enforced in MySQL

When we create a CHECK constraint in MySQL, we have the option of defining it as ENFORCED or NOT ENFORCED.

MySQL CHECK constraints are enforced by default, so unless we explicitly define the constraint as NOT ENFORCED, it will automatically be enforced.

In any case, we can use either of the following two methods to check whether or not an existing CHECK constraint is enforced.

Continue reading

How to Create a CHECK Constraint Against a JSON Column in MySQL

When we create or modify a table in MySQL, we have the option of applying a CHECK constraint against one or more columns. This allows us to check the data before it enters the database. Data can only enter the database if it doesn’t violate the rules in our CHECK constraint.

If the column is a JSON column, we have the option of creating a CHECK constraint that checks that the JSON document doesn’t violate its JSON schema. To do this, we can include the schema in the CHECK constraint.

Continue reading

Create a CHECK Constraint in MySQL

As of MySQL 8.0.16, we can create CHECK constraints in MySQL.

A CHECK constraint is a type of integrity constraint. It specifies a search condition to check the value being entered into a row. If the value being entered violates the CHECK constraint, then the result of the search condition is FALSE and an error occurs (unless the IGNORE clause is used in the SQL statement, in which case a warning is reported and the offending row is skipped).

The ability to create CHECK constraints was introduced in MySQL 8.0.16. Prior to version 8.0.16, MySQL actually allowed a limited version of CHECK constraint syntax, but it was completely ignored (no CHECK constraint was created or evaluated).

Continue reading

Fix “Cannot drop a default constraint by DROP DEFAULT statement. Use ALTER TABLE to drop a constraint default.” in SQL Server

If you’re getting an error that reads something like “Cannot drop a default constraint by DROP DEFAULT statement. Use ALTER TABLE to drop a constraint default“, it’s because you’re trying to use DROP DEFAULT to drop a DEFAULT constraint.

The DROP DEFAULT statement has been flagged for deletion from SQL Server, and Microsoft recommends that we use the ALTER TABLE statement to drop DEFAULT constraints.

Therefore, to fix this issue, use the ALTER TABLE statement to drop the DEFAULT constraint.

Continue reading