SQL

SQL Server constraints

Constraints:

ConstraintDescription
Primary keyUniquely identifies each row. There can be only one Primary key in a table. It cannot have null values. It creates a clustered index by default. It cannot be a foreign key
Foreign keyIt is a column or combination of columns that establishes a relationship between 2 tables. It ensures referential integrity which is the accuracy and consistency of data within a relationship. By using cascading referential integrity constraints actions can be defined on a foreign key relationship
UniqueRestricts column to contain unique values
CheckRestrict the values that can be added to a column
DefaultSpecifies the default values
Not NullSpecifies that the column does not accept null values

Cascading actions:

On Delete CascadeWhen rows in the Parent table are deleted then the corresponding rows in the referencing table are deleted
On Update CascadeWhen rows in the Parent table are updated then the corresponding rows in the referencing table are updated
On Delete No ActionWhen rows in the Parent table are deleted then an error is raised and the delete action is rolled back
On Update No ActionWhen rows in the Parent table are updated then an error is raised and the update action is rolled back
On Delete Set NullWhen rows in the Parent table are deleted then the corresponding rows in the referencing table are set to Null
On Update Set NullWhen rows in the Parent table are updated then the corresponding rows in the referencing table are set to Null
On Delete Set DefaultWhen rows in the Parent table are deleted then the corresponding rows in the referencing table are set to Default
On Update Set DefaultWhen rows in the Parent table are updated then the corresponding rows in the referencing table are set to Default