SQL_Server_Constraints (1)
SQL_Server_Constraints (1)
17 JAN 2025
AGENDA
2. Types of Constrains
2
01
INTRODUCTION TO SQL-SERVER
CONSTRAINTS
Introduction to SQL-Server Constraints
What is Constraints?
- Constraints are rules or restrictions that you define on columns of a table to ensure data integrity and accuracy.
Data Integrity:
- Prevent Invalid Data: Constraints like NOT NULL, CHECK, and UNIQUE prevent invalid data from being inserted into the
table.
Maintain Relationships:
- FOREIGN KEY constraints enforce relationships between tables, ensuring that data in related tables is consistent and
accurate.
Data Accuracy:
- Reduce Errors: By defining rules for data entry, constraints minimize the risk of human error and data entry mistakes.
Data Consistency:
- Enforce predefined rules and restrictions, ensuring that data across the database remains consistent and follows
established standards.
Database Performance:
- Constraints, like UNIQUE and PRIMARY KEY, can improve query performance by creating indexes that speed up data
retrieval.
4
02
TYPES OF CONSTRAINTS
Types of Constraints
Primary Key Constraint:
- Ensures that each row in a table has a unique identifier and that the column(s) involved cannot contain NULL values.
- A combination of a NOT NULL and UNIQUE.
6
Types of Constraints
Unique Constraint:
- Ensures that all values in a column are unique, but unlike a primary key, it allows one NULL value.
7
Types of Constraints
Check Constraint:
- It Allows to define custom rules for data in a column.
Default Constraint:
- Assigns a default value to a column if no value is provided during data insertion.
8
03
WORKING WITH CONSTRAINTS
Working with Constraints
Creating constraints during table creation:
Dropping constraint:
10
Best Practices
Naming conventions for constraints:
Primary Key:
- PK_<TableName>
Foreign Key:
- FK_<TableName>_<ReferencedTable>
Unique Key:
- UK_<TableName>_<Column>
Check Constraint:
- CHK_<TableName>_<Column>
Default Constraint:
- DF_<TableName>_<Column>
11
04
RESOURCES FOR FURTHER
LEARNING
Resources for further Learning
SQL-Server Constraints from Microsoft Documentation:
-
https://learn.microsoft.com/en-us/sql/relational-databases/tables/primary-and-foreign-key-constraints?view=sql-server-v
er16
13
THANK YOU
14