In this tutorial, we will learn what is
CONSTRAINT and how to use SQL.
CONSTRAINTis 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.
CONSTRAINTensures the column to follow the specified rules. i.e.
NOT NULLconstraint ensures that column cannot have
CONSTRAINTcan be assigned when the table is created OR after the table is created using an
CONSTRAINTis 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:
PRIMARY KEYconstraint is used to uniquely identify each row and only one primary key constraint can be created for each table.
FOREIGN KEYin one table points to a primary key in another table.
NULLor no value is specified to a column.