HTML


Constraint:

Constraints are used to apply rules or restrictions on database objects like tables.
We can apply constraints while creating the table or after creating the table.

1.Null Constraint:

  •  By default every column in table will accept null values.
  •  We no need to specify null constraint while creating a table.
Syntax: create table <table_name>(column name datatype null);

2.Not Null Constraint:

  Not Null constraint will not allow null values.

  Syntax:  create table <table_name>(column_name datatype not null);

   Ex:
  
  Syntax: After Creating table
     Alter table table_name alter column column_name datatype not null;

    

3.Unique Key Constraint:
  •   It Will not allow duplicate values.
  •   We can have more than one unique key in a table.
  •   It allow only one null value in a table.because it consider the null value also a value.  
  •   But it not accept 2 null values because the other null value is the duplicate value.
  Syntax: 
           Create table <table_name>(column name datatype unique);

           Here i applied not null and unique constraints to student_id because unique constraint will allow one null value.
      
  Syntax: After Creating table

             Alter table table_name add unique (column_name );


4.Check Constraint:
  Check constraint is used to validate the data before inserting into the table.
  Syntax:
      Create table <table_name>( column_name datatype check(condition))

Inserting the value against the specified condition

Output:


 Syntax: After Creating table

   alter table table_name add Check( column_name condition)


5.Primary Key Constraint:
  • Primary key Uniquely identifies each record.
  • We can have one primary key in a table which can be applied on single column or combination of columns.
  • Whenever we apply primary key on a table then not null and unique key will be applied on the columns.
  • It will not allow null and duplicate values.
Syntax: While Creating the table.
    Create table <table_name>( column_name datatype primary key.)
Output:

Inserting the duplicate values into the table.

Output:

6.Foreign Key Constraint:
  • Foreign will establish a relationship between tables.
  • We can have more than one foreign key in a table.
  • It allows null and duplicate values. 
 Syntax: While Creating the table.
  create table table_name(column_name datatype, parent_Column_name datatype foreign key references     Parent_table_name(parent_Column_name))
Syntax: After creating the table

7.Default constraint:
  • Default constraint is used to provide default value to a column.
  •    If the value is specified then the value will be insert into the table.
Syntax: Create table <table_name>( column_name datatype default 'value'.)
Output:

8.Auto Increment:
  • This key will generate a unique value automatically when ever the new record is inserted.
  •  The generated value can not be changed.
Syntax: create table table_name(column _name datatype identity or identity(starting value , increment value))

9.Composite key:
  • Applying primary key for the combinations of columns is called as composite key.
  • It will not allow null values.
  • It will not allow duplicate values.
Syntax:
create table table_name(column 1 _name datatype,column 2 _name datatype, primary key( column 1 _name,column 2)
inserting the value which is already inserted.

Output: