In this tutorial, we will learn what is CONSTRAINT
and how to use SQL.
CONSTRAINT
is assigned to a column or a set of columns of the table that prevents the column from certain types of inconsistent values insertions in those columns.CONSTRAINT
ensures the column to follow the specified rules. i.e. NOT NULL
constraint ensures that column cannot have NULL
values.CONSTRAINT
can be assigned when the table is created OR after the table is created using an ALTER TABLE
statement.CONSTRAINT
is used to enforce data integrity. This ensures the accuracy and reliability of the data in the table.CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
columnN datatype constraint,
);
The most commonly used SQL Constraint are as follow:
NULL
value.NOT NULL
and UNIQUE
constraint. PRIMARY KEY
constraint is used to uniquely identify each row and only one primary key constraint can be created for each table.FOREIGN KEY
in one table points to a primary key in another table.NULL
or no value is specified to a column.