Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

PRESENTATION

Laiba khan 2762-2022


Anusha Hassan 2735-2022
Muhammad Mehdi 2790-2022
 Introduction to Database Constraints

 Database Constraints: Ensuring Data Integrity and


Consistency
 Constraints define rules for data stored in database
tables
 Main types of constraints: Primary Key, Unique,
Foreign Key, Check, and Not Null
 Primary Key Constraint
Primary Key Constraint: Uniquely identifies each
record in a table
Ensures uniqueness and non-null values
Example Table: "Students"
Columns: "StudentID" (Primary Key),
"Name", "Age", "Grade"
Example Table: "Students"

StudentID Name Age Grade


1 John 18 A

2 Mary 19 B

3 Alex 20 A
Unique Constraint
Unique Constraint: Ensures values in a column are
unique (no duplicates)
Multiple columns can be part of a unique constraint

Example Table: "Employees“


Columns: "EmployeeID" (Primary Key), "Email"
(Unique), "Name", "Department"
Example Table: "Employees"

EmployeeID Email Name Department

1 john@example.com John HR

2 mary@example.com Mary Sales

3 alex@example.com Alex IT
Foreign Key Constraint
 Foreign Key Constraint: Establishes relationships
between tables
 References a primary key in another table
 Ensures referential integrity
 Example Tables: "Orders" and "Customers"
 "Orders" table has a foreign key "CustomerID"
referencing "Customers" table
 Example Tables: "Orders" and "Customers"
Orders Table:

OrderID OrderDate CustomerID


1 2023-01-01 101
2 2023-02-01 102
3 2023-03-01 103

Customers Table:

CustomerID CustomerName City


101 John Smith London
102 Mary Johnson Paris
103 Alex Brown New York
Check Constraint
Check Constraint: Ensures values in a column meet specific
conditions
Conditions can be based on comparison operators or
expressions
Example Table: "Products"
Columns: "ProductID" (Primary Key), "ProductName",
"Quantity" (Check: Quantity >= 0)
Example Table: "Products"

ProductID ProductName Quantity

1 Laptop 10

2 Smartphone 5

3 Tablet 0
Not Null Constraint
Not Null Constraint: Ensures a column cannot contain
null values
Requires every row to have a value in the specified
column
Example Table: "Books"
Columns: "BookID" (Primary Key), "Title", "Author",
"PublicationYear" (Not Null)
Example Table: "Books"

BookID Title Author PublicationYear

1 Introduction John Smith 2017

2 Data Analysis Mary Johnson 2019

3 Programming Alex Brown 2023


Summary
 Database constraints ensure data integrity and
consistency
 Primary Key, Unique, Foreign Key, Check, and Not
Null are commonly used constraints
 Each constraint serves a specific purpose in
maintaining data quality
Conclusion
Proper use of constraints helps create reliable and
accurate databases
Understanding constraints is crucial for effective
database design and data management
Thank you!

You might also like