WHAT ARE THE CONSTRAINTS IN SQL

CONSTRAINTS IN SQL

CONSTRAINTS are used to create rules for data in a table to limit the type of data that can be stored into a table, Constraints can be created when the table is created with the CREATE TABLE statement , or after the table is created with the ALTER TABLE statement in SQL.

Syntax of CONSTRAINTS in SQL-

CREATE TABLE <table_name>(<column_name1><datatype><constraint>,………………………<column_nameN><datatype> <constraint>);

CONSTRAINTSUsage
NOT NULLTo ensure that column can’t have a NULL value
UNIQUETo ensure that all vales in a column are different
PRIMARY KEYTo ensure unique identification of each row in a table.It is a combinaton of NOT NULL & UNIQUE .
FOREIGN KEYTo prevent action that would destroy links between tables.
CHECKTo ensure that the values in a column meet a specific condition.
DEFAULTTo set a default value for a colum if no value is specified.
CREATE INDEXTo create and retrieve data from the database quickly.
TYPE of CONSTRAINTS

Leave a Reply

Your email address will not be published. Required fields are marked *