In SQL Constraints are mainly created in two ways. One is Column constrains and another one is table constraints. The column constrains need to create in the each column. Table constraints need to create end of the CREATE query. A constraint is a property that we can assign to column in table. We can assign these Constraint properties during the time of creation of table (By Using CREATE TABLE statement) or during the time of changing the existing table structure (By Using ALTER TABLE statement) .By assigning constraint property on column we can prevent the users to enter inconsistent of data in columns.
Type of Constraints:
• NOT NULL - Indicates that a column cannot store NULL value.
• UNIQUE - Ensures that each rows for a column must have a unique value.
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly.
• FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table.
• CHECK - Ensures that the value in a column meets a specific condition.
• DEFAULT - Specifies a default value when specified none for this column.
Syntax of Constraints:
CREATE TABLE table_name
column_name1 data-type(size) constraint-name,
column_name2 data-type(size) constraint-name,
column_name3 data-type(size) constraint-name,