SlideShare a Scribd company logo
1 of 15
Integrity Constraints
By:
Megha Yadav
Integrity Constraints
• Integrity constraints are used to ensure accuracy and consistency of the data in a
relational database.
• Data integrity is handled in a relational database through the concept of referential integrity.
• Integrity constraints are a set of rules. It is used to maintain the quality of information.
• Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
• Thus, integrity constraint is used to guard against accidental damage to the database.
Types of Integrity Constraints
Domain constraint
• Domain constraints can be defined as the definition of a valid set of values
for an attribute. The data type of domain includes string, character, integer,
time, date, currency, etc.
• Domain constraint are :
Not null
Unique
Check (<predicate>)
NOT NULL Constraint
• By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without
adding a value to this field.
Example
CREATE TABLE STUDENT(
ROLL_NO INT NOT NULL ,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO) )
• In the above example we have set the not null constraint on ROLL_NO column of STUDENT table. Now the roll no
values can not be null .
UNIQUE constraint
• UNIQUE Constraint enforces a column or set of columns to have unique values.
• If a column has a unique constraint, it means that particular column cannot have
duplicate values in a table.
CREATE TABLE STUDENT
(ROLL_NO INT NOT NULL UNIQUE,
STU_NAME VARCHAR (35) NOT NULL ,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) PRIMARY KEY (ROLL_NO)
);
Now the roll number is set to be unique ,which means no two similar values of roll number is
accepted .
CHECK constraint
• The check clause :This constraint is used for specifying range of values for a particular column of a table.
• When this constraint is being set on a column, it ensures that the specified column must have the value
falling in the specified range
CREATE TABLE STUDENT(
ROLL_NO INT CHECK(ROLL_NO >1000) ,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO) );
In the above example we have set the check constraint on ROLL_NO column of STUDENT table. Now, the
ROLL_NO field must have the value greater than 1000.
Entity integrity constraints
• The entity integrity constraint states that primary key value can't be null.
This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows. A table can contain a null value other than the primary key field.
• In the diagram ,say Emp_Id is primary key in the table. Thus from the
definition of Entity Integrity, the value of Emp_Id cannot be null as it
unique identifies an employee record in the table.
• Thus no primary key column of any row in a table can have a null value.
Referential integrity
• Referential integrity refers to the accuracy and consistency of data within a
relationship. In relationships, data is linked between two or more tables.
• This is achieved by having the foreign key (in the associated table) reference a
primary key value (in the primary – or parent – table).
• As a Relational Database Management System (RDBMS), SQL Server uses
the referential integrity constraint to ensure that data in one table points to
data in another table—and doesn't point to data that doesn't exist.
• SQL Server uses constraints, triggers, rules, and defaults to
enforce referential integrity.
Example
• For example, if we delete record number 15 in a primary table, we need to be
sure that there’s no foreign key in any related table with the value of 15. We
should only be able to delete a primary key if there are no associated
records. Otherwise, we would end up with an orphaned record.
• So referential integrity will prevent users from:
• Adding records to a related table if there is no associated record in the
primary table.
• Changing values in a primary table that result in orphaned records in a
related table.
• Deleting records from a primary table if there are matching related records.
Integrity Constraints

More Related Content

What's hot

1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
koolkampus
 
All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
Naresh Kumar
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
emailharmeet
 

What's hot (20)

Files Vs DataBase
Files Vs DataBaseFiles Vs DataBase
Files Vs DataBase
 
Relational model
Relational modelRelational model
Relational model
 
1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
ER-Model-ER Diagram
ER-Model-ER DiagramER-Model-ER Diagram
ER-Model-ER Diagram
 
Relational Data Model Introduction
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
 
Database Normalization
Database NormalizationDatabase Normalization
Database Normalization
 
Database abstraction
Database abstractionDatabase abstraction
Database abstraction
 
DBMS Keys
DBMS KeysDBMS Keys
DBMS Keys
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
 
ER MODEL
ER MODELER MODEL
ER MODEL
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Anomalies in database
Anomalies in databaseAnomalies in database
Anomalies in database
 
All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & Calculus
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Database users
Database usersDatabase users
Database users
 
Data Dictionary
Data DictionaryData Dictionary
Data Dictionary
 

Similar to Integrity Constraints

ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
shalinigambhir3
 
5. relational structure
5. relational structure5. relational structure
5. relational structure
khoahuy82
 
Database Terminology and DBLC.pptx
Database Terminology and DBLC.pptxDatabase Terminology and DBLC.pptx
Database Terminology and DBLC.pptx
Top1064
 

Similar to Integrity Constraints (20)

Database constraints
Database constraintsDatabase constraints
Database constraints
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Joins & constraints
Joins & constraintsJoins & constraints
Joins & constraints
 
ER diagram
ER diagramER diagram
ER diagram
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Pre-sentation.pdf
Pre-sentation.pdfPre-sentation.pdf
Pre-sentation.pdf
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Sql ch 9 - data integrity
Sql ch 9 - data integritySql ch 9 - data integrity
Sql ch 9 - data integrity
 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
 
5. relational structure
5. relational structure5. relational structure
5. relational structure
 
Relational data model
Relational data modelRelational data model
Relational data model
 
DATABASE-1.pptx
DATABASE-1.pptxDATABASE-1.pptx
DATABASE-1.pptx
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
 
Chapter-5 The Relational Data Model
Chapter-5 The Relational Data ModelChapter-5 The Relational Data Model
Chapter-5 The Relational Data Model
 
ER Digramms by Harshal wagh
ER Digramms by Harshal waghER Digramms by Harshal wagh
ER Digramms by Harshal wagh
 
Database Terminology and DBLC.pptx
Database Terminology and DBLC.pptxDatabase Terminology and DBLC.pptx
Database Terminology and DBLC.pptx
 

More from Megha yadav

More from Megha yadav (12)

What is Numerical And Categorical Data .pptx
What is Numerical And Categorical Data .pptxWhat is Numerical And Categorical Data .pptx
What is Numerical And Categorical Data .pptx
 
Overview of DBMS/Introduction to DBMSpptx
Overview of DBMS/Introduction to DBMSpptxOverview of DBMS/Introduction to DBMSpptx
Overview of DBMS/Introduction to DBMSpptx
 
Types of RPA BOT and Tools
Types of RPA BOT and ToolsTypes of RPA BOT and Tools
Types of RPA BOT and Tools
 
Introduction to RPA
Introduction to RPAIntroduction to RPA
Introduction to RPA
 
Digital Marketing Syllabus
Digital Marketing Syllabus Digital Marketing Syllabus
Digital Marketing Syllabus
 
Structure of dbms
Structure of dbmsStructure of dbms
Structure of dbms
 
Security and Authorization
Security and AuthorizationSecurity and Authorization
Security and Authorization
 
Grid computing
Grid computingGrid computing
Grid computing
 
Distributed Computing
Distributed Computing Distributed Computing
Distributed Computing
 
Cluster computing
Cluster computingCluster computing
Cluster computing
 
Introduction to SEO
Introduction to SEOIntroduction to SEO
Introduction to SEO
 
Website Architecture
Website ArchitectureWebsite Architecture
Website Architecture
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 

Integrity Constraints

  • 2. Integrity Constraints • Integrity constraints are used to ensure accuracy and consistency of the data in a relational database. • Data integrity is handled in a relational database through the concept of referential integrity. • Integrity constraints are a set of rules. It is used to maintain the quality of information. • Integrity constraints ensure that the data insertion, updating, and other processes have to be performed in such a way that data integrity is not affected. • Thus, integrity constraint is used to guard against accidental damage to the database.
  • 3. Types of Integrity Constraints
  • 4. Domain constraint • Domain constraints can be defined as the definition of a valid set of values for an attribute. The data type of domain includes string, character, integer, time, date, currency, etc. • Domain constraint are : Not null Unique Check (<predicate>)
  • 5. NOT NULL Constraint • By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field. Example CREATE TABLE STUDENT( ROLL_NO INT NOT NULL , STU_NAME VARCHAR (35) NOT NULL, STU_AGE INT NOT NULL, EXAM_FEE INT DEFAULT 10000, STU_ADDRESS VARCHAR (35) , PRIMARY KEY (ROLL_NO) ) • In the above example we have set the not null constraint on ROLL_NO column of STUDENT table. Now the roll no values can not be null .
  • 6. UNIQUE constraint • UNIQUE Constraint enforces a column or set of columns to have unique values. • If a column has a unique constraint, it means that particular column cannot have duplicate values in a table. CREATE TABLE STUDENT (ROLL_NO INT NOT NULL UNIQUE, STU_NAME VARCHAR (35) NOT NULL , STU_AGE INT NOT NULL, STU_ADDRESS VARCHAR (35) PRIMARY KEY (ROLL_NO) ); Now the roll number is set to be unique ,which means no two similar values of roll number is accepted .
  • 7. CHECK constraint • The check clause :This constraint is used for specifying range of values for a particular column of a table. • When this constraint is being set on a column, it ensures that the specified column must have the value falling in the specified range CREATE TABLE STUDENT( ROLL_NO INT CHECK(ROLL_NO >1000) , STU_NAME VARCHAR (35) NOT NULL, STU_AGE INT NOT NULL, EXAM_FEE INT DEFAULT 10000, STU_ADDRESS VARCHAR (35) , PRIMARY KEY (ROLL_NO) ); In the above example we have set the check constraint on ROLL_NO column of STUDENT table. Now, the ROLL_NO field must have the value greater than 1000.
  • 8. Entity integrity constraints • The entity integrity constraint states that primary key value can't be null. This is because the primary key value is used to identify individual rows in relation and if the primary key has a null value, then we can't identify those rows. A table can contain a null value other than the primary key field. • In the diagram ,say Emp_Id is primary key in the table. Thus from the definition of Entity Integrity, the value of Emp_Id cannot be null as it unique identifies an employee record in the table. • Thus no primary key column of any row in a table can have a null value.
  • 9.
  • 10. Referential integrity • Referential integrity refers to the accuracy and consistency of data within a relationship. In relationships, data is linked between two or more tables. • This is achieved by having the foreign key (in the associated table) reference a primary key value (in the primary – or parent – table).
  • 11. • As a Relational Database Management System (RDBMS), SQL Server uses the referential integrity constraint to ensure that data in one table points to data in another table—and doesn't point to data that doesn't exist. • SQL Server uses constraints, triggers, rules, and defaults to enforce referential integrity.
  • 12. Example • For example, if we delete record number 15 in a primary table, we need to be sure that there’s no foreign key in any related table with the value of 15. We should only be able to delete a primary key if there are no associated records. Otherwise, we would end up with an orphaned record.
  • 13.
  • 14. • So referential integrity will prevent users from: • Adding records to a related table if there is no associated record in the primary table. • Changing values in a primary table that result in orphaned records in a related table. • Deleting records from a primary table if there are matching related records.