codescracker


sql

SQL Check



« Previous Tutorial Next Tutorial »


SQL CHECK constraint is used to limit the value range that can be placed in a column.

SQL CHECK Constraint on CREATE TABLE

Here are the SQL statements creates a CHECK constraint on the column "P_ID" when the table "Students" is created:

The CHECK constraint specifies that the "P_ID" column must only include integers greater than 0

For MySQL Database

CREATE TABLE Students
(
P_ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CHECK (P_ID>0)
)

For SQL Server / Oracle / MS Access Databases

CREATE TABLE Students
(
P_ID int NOT NULL CHECK (P_ID>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

SQL CHECK Constraint on ALTER TABLE

Here are the SQL statements used to create a CHECK constraint on the column "P_ID" when the table already exists:

For MySQL / SQL Server / Oracle / MS Access Databases

ALTER TABLE Students
ADD CHECK (P_ID>0)

DROP SQL CHECK Constraint

Here are the SQL statements used to drop a CHECK constraint:

For SQL Server / Oracle / MS Access Databases

ALTER TABLE Students
DROP CONSTRAINT chk_Student

For MySQL Database

ALTER TABLE Students
DROP CHECK chk_Student

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test