What are different Types of Constraints Available in SQL Server - SQL Server / T-SQL Tutorial Part 50

What are Constraints in SQL Server?


In simple words, Constraints define rules those we implement to avoid insertion of any data that does not follow the rule.
By using the Constraint, we can validate the data and if complies the rule, It will be inserted in table otherwise rolled back.
Constraints are used to maintain Data Integrity. They make sure the data entered follow the rules we have defined.

There are different types of Constraints available in SQL Server.



  1. Not Null Constraint
  2. Check Constraint
  3. Default Constraint
  4. Unique Constraint
  5. Primary Key Constraint
  6. Foreign Key Constraint


3 comments:

  1. I Have one doubt different beetween primary key and unique key

    ReplyDelete
  2. A primary key constraint establishes a referential integrity between two tables. A primary key only can become foreign key to another table.
    Though PK and UK both store unique values, there are few differences listed below:
    a. Primary constraint does not allow NULL values. This constraint is always non nullable.
    b. Unique key can allow only one NULL value by default; but we can create a filtered unique index to include multiple null values.

    ReplyDelete
  3. Primary key constraint
    ----------------------
    *does not allow Null Value
    *there Can only be one primary key constraint per table
    *by dafult if there is no clusterd index in a table primary key will create a clusterd index
    unless explicitly specified

    Unique key
    ----------
    *Allows only one null value
    *There can be multiple unique keys per table
    * By default it creates a non clusterd index unless explicitly specified

    ReplyDelete