SlideShare a Scribd company logo
Integrity constraint:
• Integrity Constraints are pre-defined set of rules that are applied on
the table fields(columns).
• It ensures that data entered into the database must be complete,
accurate , valid and consistent.
Types of Integrity Constraints
1. Domain Integrity Constraint: It contains set of rules to restrict the
kind of attributes(or) values a column can hold in the database
table.
Cont:
Ex: Roll No(number) Name(varchar) Age(varchar) class (number)
101 Smith 14 6
102 Amith 16 8
103 Siri 18 A
‘A’ violates the domain integrity constraint because the class attribute
contains only integer values while ‘A’ is a character.
Cont:
2) Entity Integrity Constraint:
• It is used to ensure that the primary key cannot be null
• A Primary Key is used to identify individual records in a table and if
the primary key has a null value, then we can’t identify those records.
• There can be null values any where in the table expect the Primary
Key column
Cont:
ID NAME SALARY
101 A 1000
102 B 2000
NULL C 3000
‘NULL’ violates the rule
3) Referential Integrity Constraint:
• It ensures that there must always exist a valid relationship between
two relational database tables.
• It confirms that a foreign key exists in a table.
• It should always reference a corresponding value in the table(or) be
null.
Cont:
Employee Department table
S.No: Name Sal Dept_Id Dept_Id Dept_name
101 A 1000 2 1 Sales
102 B 2000 1 2 HR
103 C 3000 3 3 Technical
104 D 4000
• Row having Dept_id=4 violates the referential integrity constraints.
Since Dept_id=4 is not defined as a primary key column in the
Department table
Cont:
4) Key Constraint:
• Keys are the set of entities that are used to identify an entity within
its entity set uniquely.
• A Primary Key can only contain unique and not null values in the
relation database table.(Duplicates are not allowed)
Cont:
Roll No: Name Age Class
101 A 2 1
102 B 5 2
103 C 6 1
101 D 7 1
• ‘101’ is repeated twice in primary key column. So, it violates the key
integrity Constraint.
NULL Values:
• Special Value that is supported by SQL is called as null.
• It is used to represent values of attribute that are unknown.
• If the value is not present then it is represented as null but not as zero.
Empno Sal Job
101 1000 Clerk
102 ----- Manager
103 2000 Clerk
104 ----- Manager
105 ----- Clerk
Handling Null Values
1) is null
2) is not null
3) not null constraints
4) NVL()
• IS NULL
Select * from emp
where sal is not null;
Emp_no: Sal
101 1000
103 2000
Cont:
• IS NOT NULL
Select * from emp
where sal is not null;
Emp no: Sal
101 1000
102 2000
Cont:
• Not NULL Constraint
• It prevents the Column to contain null Values.
• Once not null is applied to a particular column, you cannot enter null
values to that column and restricted to maintain only some proper
value other than null.
• A not-null Constraint cannot be applied at table level.
Create table Student( ID int NOT NULL, Name Varchar(20) NOT NULL,
Age int NOT NULL, Address char(25), Salary decimal(18,2), Primary
key(ID))
Cont:
• NVL() NULL function:
• Using NVL function you can substitute a value in the place of NULL values.
• The Substituted Value then temporarily replaces the values in your
calculations (or) expression.
• It does not effect the value stored in the table.
• Syntax:
NVL(exp, replacement_exp)
Exp: Column name of a table(or) an arithmetic expression.
Replacement_exp: value you want to substitute when a NULL value is
encountered.
Cont:
• Datatypeof both the parameters must match otherwise the complier will
raise an error.
Ex:
Select NVL(Sal,5000)fromemp
Where job=Manager;
• All the null values which satisfythe condition will be replaced with 50000.
• Similarly, we can use NVL null function while performing arithmetic
expression.
• Select NVL(sal, 5000) NVL(sal,5000)+1000from emp where job=Manager;
VIEW
• Views are considered as virtual table. It Contains rows &
columns.(Changes will not effect main table.)
• To Create a view, we can select the fields from one or more tables
present in DB.
• A View can contain either all the rows of a table (or) only few rows
based upon certain conditions.
Creating a view
Syntax:
Create view view_name as
Select column1, column2,….. From table_name where condition
Eg: Create view details_view as
Select name, address from student where Roll.No<3;
Select * from details_view
o/p: Name Address
Rama Hyd
Sita Delhi
Creating a view from Multiple tables
Syntax:
Create view marks_view as
Select student.name, student.address, smarks.marks
From student, smarks where student.name=smarks.name;
Select * from marks view;
o/p: Name Address Marks
Rama Hyd 99
Sita Delhi 99
Ammu Hyd 97
Deleting a view
Syntax:
• Drop view view_name
Inserting a row in a view:
• Insert into view_name(column1, column2,……) values(value1,
value2,….);
Deleting a row:
Syntax:
Delete from view_name where condition;
Update View: To add (or) remove fields create (or) Replace.
ALL, ANY,SOME OPERATORS
Sid Sname Class Percentage_in_last_class
1 raj 18 72
2 amith 11 84
3 vivek 9 64
4 Jyothi 10 69
5 vikas 11 92
Cont:
• Select Sname from std5 where percentage_in_last_class > all (select
percentage_in_last_class=10);
• Select Sname from std5 where percentage_in_last_class > any (select
percentage_in_last_class=10);
Sname
amith
vikas
Sname
raj
amith
vikas
Cont:
• Select Sname from std5 where percentage_in_last_class > some
(select percentage_in_last_class=10);
Sname
raj
amith
vikas
Index Definition in SQL
• Index is a Schema object. It is used by the server to speed up the
retrieval of rows.
Syntax:
Create index index_name on table_name column;
/* for single columns*/
Create index index_name on table_name(Column1, Column2,….)
/*Multiple Columns, Composite*/
Unique Indexes
• Unique indexes are used for the maintenance of the integrity of the data
present in the table as well as for fast performance.
• It will not allow multiple values into the table.
Syntax:
Create Unique index index_name on table_name column;
How to choose the column When Creating an Index:
• A Column contain wide range of values.
• A Column doesn’t contain large number of null values.
• One (or) more columns are frequently used together for ‘m’ where clause
(or) ‘n’ join.
Cont:
• Drop index index_name;
• Alter index indexname on tablename rebuild;
JOIN
• In SQL, JOIN clause is used to combine the records from two or more tables
in a database.
Types of SQL Joins
• Inner Join
• Left Join
• Right Join
• Full Join
• Self Join
• Natural Join
• Equi Join
• Theta Join
1. INNER JOIN
• In SQL, INNER JOIN selects records that have matching values in both
tables as long as the condition is satisfied. It returns the combination
of all rows from both the tables where the condition satisfies.
Syntax
• SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Cont:
Cust_id Cust_name Designation
1 abc A
2 pqr B
3 mno c
Ord_id amount Cust_id
601 10000 1
602 2000 4
603 1500 2
Customer
Orders
Cont:
Query
• select orders.ord_id, customers.cust_name
from orders
inner join customers
on orders.cust_id = customers.cust_id;
Output:
ord_id Cust_name
601 abc
603 pqr
2. LEFT JOIN
• The SQL left join returns all the values from left table and the
matching values from the right table. If there is no matching join
value, it will return NULL.
Syntax:
• SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Cont:
Query:
• select orders.ord_id, customers.cust_name
from orders
left join customers
on orders.cust_id = customers.cust_id;
Output:
Ord_id Cust_name
601 abc
602 null
603 pqr
3. RIGHT JOIN
• In SQL, RIGHT JOIN returns all the values from the rows of right table
and the matched values from the left table. If there is no matching in
both tables, it will return NULL.
Syntax:
• SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Cont:
Query:
• select orders.ord_id, customers.cust_name
from orders
right join customers
on orders.cust_id = customers.cust_id;
Output:
Ord_id Cust_name
601 abc
602 pqr
null mno
4. FULL JOIN
• In SQL, FULL JOIN is the result of a combination of both left and right
outer join. Join tables have all the records from both tables. It puts
NULL on the place of matches not found.
Syntax:
• SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Cont:
Query:
• select orders.ord_id, customers.cust_name
from orders
Full join customers
on orders.cust_id = customers.cust_id;
Output: ord_id Cust_name
601 abc
602 null
603 pqr
null mno
5.SELF JOIN
• The SQL SELF JOIN is used to join a table to itself were joining
condition is based on columns of same table.
Syntax:
• SELECT a.column_name, b.column_name,
FROM table1 a, table1 b
WHERE a.column_field = b.column_field;
Cont:
• Write SQL Query list the names of the employees and their manager
names from emp table.
Emp E1 Emp E2
E_id E_name M_id
10 A 40
20 B 30
30 C 20
40 D 10
E_id E_name M_id
10 A 40
20 B 30
30 C 20
40 D 10
Cont:
• Select E1.Ename as E_name, E2.Ename as M_name from Emp E1,
Emp E2, where E1.Eid=E2.Mid;
E_name M_name
A D
B C
C B
D A
Cont:
• select distinct E_name
from employee E
self join employee M
on E.emp_id =M.Manager_id;
• Every employee cannot be a manager but every manager can be an
employee.
6) NATURAL JOIN
• Natural Join joins two tables based on same attribute name and data
types. The resulting table will contain all the attributes of both the
table but keep only one copy of each common column.
• Select *
From student s
Natural Join Marks m;
Cont:
id name
101 abc
102 pqr
103 xyz
id marks
102 100
103 70
104 80
id name marks
102 pqr
100
103 xyz
70
Student Marks
7)EQUI JOIN(= CONDITION)
• The resultant relation of an equijoin operation always has one (or) more
pairs of attributes that have identical values in every tuple.
Syntax:
• Select column_list
From table1, table2
Where table1.column_name = table2.column_name (or)
• Select column_list
From table1
Join table2
[ON (join_condition)]
Cont:
BOOK Publisher
p_id p_name
1 A
2 B
3 C
4 P
B_name p_id price
XYZ 1 200
ABC 1 500
PQR 2 700
STU 2 1000
B_name Price Book.p_id Publisher.p_id p_name
XYZ 200 1 1 A
ABC 500 1 1 A
PQR 700 2 2 B
STU 1000 2 2 B
8)Theta Join (or) Conditional Join
• Theta Join allows you to merge two tables based on the condition
represented by theta. Theta joins work for all comparison operators.
It is denoted by symbol θ. The general case of JOIN operation is called
a Theta join.( Comparision Condition {=,<,>,<=,>=})
Model Price
Nokia 10K
Samsung 20K
Phone 50K
Model Price
Dell 30K
Acer 20K
Lenovo 10K
Mobile Laptop
Cont:
• Purchase both mobile, Laptop but mobile price should be less than
laptop price.
Difference Between Natural Join And Inner Join
SR.NO. NATURAL JOIN INNER JOIN
1.
Natural Join joins two tables based on same
attributenameand datatypes.
Inner Join joins two tableon thebasis of the
column which is explicitly specified in theON
clause.
2.
In Natural Join, The resulting tablewill contain all
theattributes of both thetables but keep only one
copy of each common column
In Inner Join, The resulting tablewill contain all the
attributeof both thetables including duplicate
columns also
3.
In Natural Join, If thereis no condition specifies
then it returns therows based on the common
column
In Inner Join, only thoserecords will return which
exists in both thetables
4.
SYNTAX:
SELECT *
FROM table1 NATURAL JOIN table2;
SYNTAX:
SELECT *
FROM table1 INNER JOIN table2 ON
table1.Column_Name=table2.Column_Name;

More Related Content

Similar to Integrity constraint fundamentals of dbms.pdf

Similar to Integrity constraint fundamentals of dbms.pdf (20)

Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
 
2..basic queries.pptx
2..basic queries.pptx2..basic queries.pptx
2..basic queries.pptx
 
Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
 
Introduction to the Relational Model and SQL
Introduction to the Relational Model and SQLIntroduction to the Relational Model and SQL
Introduction to the Relational Model and SQL
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptxADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
 
Java class 8
Java class 8Java class 8
Java class 8
 
sql language
sql languagesql language
sql language
 
UNIT2.ppt
UNIT2.pptUNIT2.ppt
UNIT2.ppt
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdf
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
Introduction to mysql part 2
Introduction to mysql part 2Introduction to mysql part 2
Introduction to mysql part 2
 
Sql modifying data - MYSQL part I
Sql modifying data - MYSQL part ISql modifying data - MYSQL part I
Sql modifying data - MYSQL part I
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 

Recently uploaded

Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Prakhyath Rai
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationDr. Radhey Shyam
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...Roi Lipman
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfAbrahamGadissa
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdfKamal Acharya
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Aryaabh.arya
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectRased Khan
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdfKamal Acharya
 
Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...
Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...
Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...Aimil Ltd
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfPipe Restoration Solutions
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfKamal Acharya
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringC Sai Kiran
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdfKamal Acharya
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxwendy cai
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdfKamal Acharya
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...Amil baba
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdfKamal Acharya
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdfKamal Acharya
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Krakówbim.edu.pl
 

Recently uploaded (20)

Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...
Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...
Soil Testing Instruments by aimil ltd.- California Bearing Ratio apparatus, c...
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdf
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 

Integrity constraint fundamentals of dbms.pdf

  • 1. Integrity constraint: • Integrity Constraints are pre-defined set of rules that are applied on the table fields(columns). • It ensures that data entered into the database must be complete, accurate , valid and consistent. Types of Integrity Constraints 1. Domain Integrity Constraint: It contains set of rules to restrict the kind of attributes(or) values a column can hold in the database table.
  • 2. Cont: Ex: Roll No(number) Name(varchar) Age(varchar) class (number) 101 Smith 14 6 102 Amith 16 8 103 Siri 18 A ‘A’ violates the domain integrity constraint because the class attribute contains only integer values while ‘A’ is a character.
  • 3. Cont: 2) Entity Integrity Constraint: • It is used to ensure that the primary key cannot be null • A Primary Key is used to identify individual records in a table and if the primary key has a null value, then we can’t identify those records. • There can be null values any where in the table expect the Primary Key column
  • 4. Cont: ID NAME SALARY 101 A 1000 102 B 2000 NULL C 3000 ‘NULL’ violates the rule
  • 5. 3) Referential Integrity Constraint: • It ensures that there must always exist a valid relationship between two relational database tables. • It confirms that a foreign key exists in a table. • It should always reference a corresponding value in the table(or) be null.
  • 6. Cont: Employee Department table S.No: Name Sal Dept_Id Dept_Id Dept_name 101 A 1000 2 1 Sales 102 B 2000 1 2 HR 103 C 3000 3 3 Technical 104 D 4000 • Row having Dept_id=4 violates the referential integrity constraints. Since Dept_id=4 is not defined as a primary key column in the Department table
  • 7. Cont: 4) Key Constraint: • Keys are the set of entities that are used to identify an entity within its entity set uniquely. • A Primary Key can only contain unique and not null values in the relation database table.(Duplicates are not allowed)
  • 8. Cont: Roll No: Name Age Class 101 A 2 1 102 B 5 2 103 C 6 1 101 D 7 1 • ‘101’ is repeated twice in primary key column. So, it violates the key integrity Constraint.
  • 9. NULL Values: • Special Value that is supported by SQL is called as null. • It is used to represent values of attribute that are unknown. • If the value is not present then it is represented as null but not as zero. Empno Sal Job 101 1000 Clerk 102 ----- Manager 103 2000 Clerk 104 ----- Manager 105 ----- Clerk
  • 10.
  • 11.
  • 12. Handling Null Values 1) is null 2) is not null 3) not null constraints 4) NVL() • IS NULL Select * from emp where sal is not null; Emp_no: Sal 101 1000 103 2000
  • 13. Cont: • IS NOT NULL Select * from emp where sal is not null; Emp no: Sal 101 1000 102 2000
  • 14. Cont: • Not NULL Constraint • It prevents the Column to contain null Values. • Once not null is applied to a particular column, you cannot enter null values to that column and restricted to maintain only some proper value other than null. • A not-null Constraint cannot be applied at table level. Create table Student( ID int NOT NULL, Name Varchar(20) NOT NULL, Age int NOT NULL, Address char(25), Salary decimal(18,2), Primary key(ID))
  • 15. Cont: • NVL() NULL function: • Using NVL function you can substitute a value in the place of NULL values. • The Substituted Value then temporarily replaces the values in your calculations (or) expression. • It does not effect the value stored in the table. • Syntax: NVL(exp, replacement_exp) Exp: Column name of a table(or) an arithmetic expression. Replacement_exp: value you want to substitute when a NULL value is encountered.
  • 16. Cont: • Datatypeof both the parameters must match otherwise the complier will raise an error. Ex: Select NVL(Sal,5000)fromemp Where job=Manager; • All the null values which satisfythe condition will be replaced with 50000. • Similarly, we can use NVL null function while performing arithmetic expression. • Select NVL(sal, 5000) NVL(sal,5000)+1000from emp where job=Manager;
  • 17. VIEW • Views are considered as virtual table. It Contains rows & columns.(Changes will not effect main table.) • To Create a view, we can select the fields from one or more tables present in DB. • A View can contain either all the rows of a table (or) only few rows based upon certain conditions.
  • 18. Creating a view Syntax: Create view view_name as Select column1, column2,….. From table_name where condition Eg: Create view details_view as Select name, address from student where Roll.No<3; Select * from details_view o/p: Name Address Rama Hyd Sita Delhi
  • 19. Creating a view from Multiple tables Syntax: Create view marks_view as Select student.name, student.address, smarks.marks From student, smarks where student.name=smarks.name; Select * from marks view; o/p: Name Address Marks Rama Hyd 99 Sita Delhi 99 Ammu Hyd 97
  • 20. Deleting a view Syntax: • Drop view view_name Inserting a row in a view: • Insert into view_name(column1, column2,……) values(value1, value2,….); Deleting a row: Syntax: Delete from view_name where condition; Update View: To add (or) remove fields create (or) Replace.
  • 21. ALL, ANY,SOME OPERATORS Sid Sname Class Percentage_in_last_class 1 raj 18 72 2 amith 11 84 3 vivek 9 64 4 Jyothi 10 69 5 vikas 11 92
  • 22. Cont: • Select Sname from std5 where percentage_in_last_class > all (select percentage_in_last_class=10); • Select Sname from std5 where percentage_in_last_class > any (select percentage_in_last_class=10); Sname amith vikas Sname raj amith vikas
  • 23. Cont: • Select Sname from std5 where percentage_in_last_class > some (select percentage_in_last_class=10); Sname raj amith vikas
  • 24. Index Definition in SQL • Index is a Schema object. It is used by the server to speed up the retrieval of rows. Syntax: Create index index_name on table_name column; /* for single columns*/ Create index index_name on table_name(Column1, Column2,….) /*Multiple Columns, Composite*/
  • 25. Unique Indexes • Unique indexes are used for the maintenance of the integrity of the data present in the table as well as for fast performance. • It will not allow multiple values into the table. Syntax: Create Unique index index_name on table_name column; How to choose the column When Creating an Index: • A Column contain wide range of values. • A Column doesn’t contain large number of null values. • One (or) more columns are frequently used together for ‘m’ where clause (or) ‘n’ join.
  • 26. Cont: • Drop index index_name; • Alter index indexname on tablename rebuild;
  • 27. JOIN • In SQL, JOIN clause is used to combine the records from two or more tables in a database. Types of SQL Joins • Inner Join • Left Join • Right Join • Full Join • Self Join • Natural Join • Equi Join • Theta Join
  • 28. 1. INNER JOIN • In SQL, INNER JOIN selects records that have matching values in both tables as long as the condition is satisfied. It returns the combination of all rows from both the tables where the condition satisfies. Syntax • SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 INNER JOIN table2 ON table1.matching_column = table2.matching_column;
  • 29. Cont: Cust_id Cust_name Designation 1 abc A 2 pqr B 3 mno c Ord_id amount Cust_id 601 10000 1 602 2000 4 603 1500 2 Customer Orders
  • 30. Cont: Query • select orders.ord_id, customers.cust_name from orders inner join customers on orders.cust_id = customers.cust_id; Output: ord_id Cust_name 601 abc 603 pqr
  • 31. 2. LEFT JOIN • The SQL left join returns all the values from left table and the matching values from the right table. If there is no matching join value, it will return NULL. Syntax: • SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 LEFT JOIN table2 ON table1.matching_column = table2.matching_column;
  • 32. Cont: Query: • select orders.ord_id, customers.cust_name from orders left join customers on orders.cust_id = customers.cust_id; Output: Ord_id Cust_name 601 abc 602 null 603 pqr
  • 33. 3. RIGHT JOIN • In SQL, RIGHT JOIN returns all the values from the rows of right table and the matched values from the left table. If there is no matching in both tables, it will return NULL. Syntax: • SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 RIGHT JOIN table2 ON table1.matching_column = table2.matching_column;
  • 34. Cont: Query: • select orders.ord_id, customers.cust_name from orders right join customers on orders.cust_id = customers.cust_id; Output: Ord_id Cust_name 601 abc 602 pqr null mno
  • 35. 4. FULL JOIN • In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join tables have all the records from both tables. It puts NULL on the place of matches not found. Syntax: • SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 FULL JOIN table2 ON table1.matching_column = table2.matching_column;
  • 36. Cont: Query: • select orders.ord_id, customers.cust_name from orders Full join customers on orders.cust_id = customers.cust_id; Output: ord_id Cust_name 601 abc 602 null 603 pqr null mno
  • 37. 5.SELF JOIN • The SQL SELF JOIN is used to join a table to itself were joining condition is based on columns of same table. Syntax: • SELECT a.column_name, b.column_name, FROM table1 a, table1 b WHERE a.column_field = b.column_field;
  • 38. Cont: • Write SQL Query list the names of the employees and their manager names from emp table. Emp E1 Emp E2 E_id E_name M_id 10 A 40 20 B 30 30 C 20 40 D 10 E_id E_name M_id 10 A 40 20 B 30 30 C 20 40 D 10
  • 39. Cont: • Select E1.Ename as E_name, E2.Ename as M_name from Emp E1, Emp E2, where E1.Eid=E2.Mid; E_name M_name A D B C C B D A
  • 40. Cont: • select distinct E_name from employee E self join employee M on E.emp_id =M.Manager_id; • Every employee cannot be a manager but every manager can be an employee.
  • 41. 6) NATURAL JOIN • Natural Join joins two tables based on same attribute name and data types. The resulting table will contain all the attributes of both the table but keep only one copy of each common column. • Select * From student s Natural Join Marks m;
  • 42. Cont: id name 101 abc 102 pqr 103 xyz id marks 102 100 103 70 104 80 id name marks 102 pqr 100 103 xyz 70 Student Marks
  • 43. 7)EQUI JOIN(= CONDITION) • The resultant relation of an equijoin operation always has one (or) more pairs of attributes that have identical values in every tuple. Syntax: • Select column_list From table1, table2 Where table1.column_name = table2.column_name (or) • Select column_list From table1 Join table2 [ON (join_condition)]
  • 44. Cont: BOOK Publisher p_id p_name 1 A 2 B 3 C 4 P B_name p_id price XYZ 1 200 ABC 1 500 PQR 2 700 STU 2 1000 B_name Price Book.p_id Publisher.p_id p_name XYZ 200 1 1 A ABC 500 1 1 A PQR 700 2 2 B STU 1000 2 2 B
  • 45. 8)Theta Join (or) Conditional Join • Theta Join allows you to merge two tables based on the condition represented by theta. Theta joins work for all comparison operators. It is denoted by symbol θ. The general case of JOIN operation is called a Theta join.( Comparision Condition {=,<,>,<=,>=}) Model Price Nokia 10K Samsung 20K Phone 50K Model Price Dell 30K Acer 20K Lenovo 10K Mobile Laptop
  • 46. Cont: • Purchase both mobile, Laptop but mobile price should be less than laptop price.
  • 47. Difference Between Natural Join And Inner Join SR.NO. NATURAL JOIN INNER JOIN 1. Natural Join joins two tables based on same attributenameand datatypes. Inner Join joins two tableon thebasis of the column which is explicitly specified in theON clause. 2. In Natural Join, The resulting tablewill contain all theattributes of both thetables but keep only one copy of each common column In Inner Join, The resulting tablewill contain all the attributeof both thetables including duplicate columns also 3. In Natural Join, If thereis no condition specifies then it returns therows based on the common column In Inner Join, only thoserecords will return which exists in both thetables 4. SYNTAX: SELECT * FROM table1 NATURAL JOIN table2; SYNTAX: SELECT * FROM table1 INNER JOIN table2 ON table1.Column_Name=table2.Column_Name;