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>);
CONSTRAINTS | Usage |
NOT NULL | To ensure that column canโt have a NULL value |
UNIQUE | To ensure that all vales in a column are different |
PRIMARY KEY | To ensure unique identification of each row in a table.It is a combinaton of NOT NULL & UNIQUE . |
FOREIGN KEY | To prevent action that would destroy links between tables. |
CHECK | To ensure that the values in a column meet a specific condition. |
DEFAULT | To set a default value for a colum if no value is specified. |
CREATE INDEX | To create and retrieve data from the database quickly. |
Leave a Reply