SQL
SQL Server constraints
Constraints:
Constraint | Description |
---|---|
Primary key | Uniquely 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 key | It 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 |
Unique | Restricts column to contain unique values |
Check | Restrict the values that can be added to a column |
Default | Specifies the default values |
Not Null | Specifies that the column does not accept null values |
Cascading actions:
On Delete Cascade | When rows in the Parent table are deleted then the corresponding rows in the referencing table are deleted |
---|---|
On Update Cascade | When rows in the Parent table are updated then the corresponding rows in the referencing table are updated |
On Delete No Action | When rows in the Parent table are deleted then an error is raised and the delete action is rolled back |
On Update No Action | When rows in the Parent table are updated then an error is raised and the update action is rolled back |
On Delete Set Null | When rows in the Parent table are deleted then the corresponding rows in the referencing table are set to Null |
On Update Set Null | When rows in the Parent table are updated then the corresponding rows in the referencing table are set to Null |
On Delete Set Default | When rows in the Parent table are deleted then the corresponding rows in the referencing table are set to Default |
On Update Set Default | When rows in the Parent table are updated then the corresponding rows in the referencing table are set to Default |