0% found this document useful (0 votes)
4 views

SQL_Server_Constraints (1)

The document provides an overview of SQL-Server constraints, which are rules that ensure data integrity and accuracy in databases. It details various types of constraints, including Primary Key, Foreign Key, Unique, Not Null, Check, and Default constraints, along with examples of their implementation. Additionally, it covers best practices for naming constraints and offers resources for further learning.

Uploaded by

workmail1524
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL_Server_Constraints (1)

The document provides an overview of SQL-Server constraints, which are rules that ensure data integrity and accuracy in databases. It details various types of constraints, including Primary Key, Foreign Key, Unique, Not Null, Check, and Default constraints, along with examples of their implementation. Additionally, it covers best practices for naming constraints and offers resources for further learning.

Uploaded by

workmail1524
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SQL-Server Constraints

17 JAN 2025
AGENDA

1. Introduction to SQL-Server Constrains

2. Types of Constrains

3. Working with Constraints

4. Resources for Further Learning

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.

Why we need Constraints:

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.

CREATE TABLE Books (


BookID INT NOT NULL PRIMARY KEY,
Title NVARCHAR(100) NULL,
Author NVARCHAR(100) NULL
);

Foreign Key Constraint:


- A relationship between two tables, ensuring that the value in one table matches a value in another table.

CREATE TABLE Classes (


ClassID INT PRIMARY KEY,
ClassName NVARCHAR(50)
);

CREATE TABLE Students (


StudentID INT NOT NULL PRIMARY KEY,
StudentName NVARCHAR(100),
ClassID INT,
FOREIGN KEY (ClassID) REFERENCES Classes(ClassID)
);

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.

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
EmployeeName NVARCHAR(100),
Email NVARCHAR(100) UNIQUE
);

Not Null Constraint:


- Ensures that a column cannot have NULL values, meaning data must be entered for that column.

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName NVARCHAR(100) NOT NULL,
Price DECIMAL(10, 2) NOT NULL
);

7
Types of Constraints
Check Constraint:
- It Allows to define custom rules for data in a column.

CREATE TABLE BankAccounts (


AccountID INT PRIMARY KEY,
AccountHolder NVARCHAR(100),
Balance DECIMAL(10, 2) CHECK (Balance >= 500)
);

Default Constraint:
- Assigns a default value to a column if no value is provided during data insertion.

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
OrderDate DATE DEFAULT GETDATE(),
OrderStatus NVARCHAR(20) DEFAULT 'Pending’
);

8
03
WORKING WITH CONSTRAINTS
Working with Constraints
Creating constraints during table creation:

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName NVARCHAR(100) NOT NULL,
Price DECIMAL(10, 2) NOT NULL
);

Adding constraints to existing tables:

ALTER TABLE table_name


ADD CONSTRAINT constraint_name PRIMARY KEY (column_name)

Dropping constraint:

ALTER TABLE table_name


DROP CONSTRAINT constraint_name

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

SQL-Server Constraints from W3Schools:


- https://www.w3schools.com/sql/sql_constraints.asp

Self Learning Task:


- Practice all constraints with positive and negative scenarios and share the individual practice by EOD.

13
THANK YOU

14

You might also like