SQL Tutorial
The CHECK
constraint in SQL is used to limit the values that can be placed in a column. The constraint ensures that all values in a column satisfy certain conditions. If the condition evaluates to false, then the data modification is not permitted.
The CHECK
constraint is specified with the CHECK
keyword followed by the condition to be checked in parentheses.
Here is the general syntax for CHECK
constraint:
CREATE TABLE table_name ( column1 datatype constraint_name CHECK (condition), column2 datatype, column3 datatype, ... );
Here's an example:
CREATE TABLE Employees ( ID int NOT NULL, Age int CHECK (Age >= 18) );
In this example, the CHECK
constraint ensures that you cannot have an employee younger than 18 years old.
You can also apply a CHECK
constraint to multiple columns:
CREATE TABLE Employees ( ID int NOT NULL, Age int, Salary decimal, CHECK (Age >= 18 AND Salary >= 500) );
In this example, the CHECK
constraint ensures that you cannot have an employee younger than 18 years old, and the salary of any employee must be equal to or greater than 500.
If you need to add a CHECK
constraint to an existing table, you can use the ALTER TABLE
command:
ALTER TABLE Employees ADD CHECK (Age >= 18 AND Salary >= 500);
This will add the same CHECK
constraint as above to the existing Employees
table.
Please note that the SQL CHECK
constraint is supported in most of the relational database management systems like MySQL, SQL Server, SQLite, Oracle, etc. However, the syntax and behavior may slightly differ from one RDBMS to another.
Creating check constraints in SQL: Define a check constraint during table creation to restrict column values.
CREATE TABLE TableName ( ColumnName INT CHECK (ColumnName > 0), OtherColumn VARCHAR(255), CHECK (OtherColumn IS NOT NULL) );
Check constraints for column values: Apply a check constraint to restrict values within a specific range.
CREATE TABLE TableName ( Age INT CHECK (Age >= 18 AND Age <= 100), Name VARCHAR(255) );
SQL CHECK constraint for table-level validation: Use the CHECK constraint at the table level to validate combinations of column values.
CREATE TABLE TableName ( StartDate DATE, EndDate DATE, CHECK (StartDate < EndDate) );
Enforcing business rules with CHECK in SQL: Apply CHECK constraints to enforce business rules on column values.
CREATE TABLE Employees ( Salary DECIMAL(10, 2), CHECK (Salary >= 0) );
Multiple conditions in SQL CHECK constraints: Combine multiple conditions within a single CHECK constraint.
CREATE TABLE TableName ( Quantity INT, UnitPrice DECIMAL(10, 2), CHECK ((Quantity * UnitPrice) < 1000) );
Disabling and enabling CHECK constraints: Temporarily disable or enable a CHECK constraint.
-- Disable ALTER TABLE TableName NOCHECK CONSTRAINT ConstraintName; -- Enable ALTER TABLE TableName CHECK CONSTRAINT ConstraintName;
Naming conventions for CHECK constraints: Provide meaningful names to CHECK constraints for clarity.
CREATE TABLE TableName ( Age INT, CHECK (Age >= 18) CONSTRAINT CHK_Age_Minimum );
CHECK constraints and data integrity in SQL: CHECK constraints contribute to maintaining data integrity by validating values.
CREATE TABLE TableName ( DiscountPercentage INT CHECK (DiscountPercentage >= 0 AND DiscountPercentage <= 100), ProductName VARCHAR(255) );