You are on page 1of 55

Integrity Constraints

 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.
Domain constraints
 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. The value of the attribute must be available in the
corresponding domain.
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.
Referential Integrity Constraints
 A referential integrity constraint is specified between two tables.
 In the Referential integrity constraints, if a foreign key in Table 1 refers to the
Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be
null or be available in Table 2.
Key constraints
 Keys are the entity set that is used to identify an entity within its entity set
uniquely.
 An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.
Relational Algebra and Relational
Calculus
Relational Algebra
 Relational algebra is a procedural query language. It gives a step by step
process to obtain the result of the query. It uses operators to perform queries.
1. Select Operation:
 Customer(Cust_name, Cust_street, Cust_city)
 Branch(Branch_name, Branch_city, Assets)
 Account (Branch_name, Account_number, Balance)
 Loan(Branch_name, Loan_number, Amount)
 Depositor(Cust_name, Account_number)
 Borrower(Cust_name, Loan_number)
Project Operation:

 This operation shows the list of those attributes that we wish to appear in the
result. Rest of the attributes are eliminated from the table.
 It is denoted by ∏.
1. Notation: ∏ A1, A2, An (r)
 Where
 A1, A2, A3 is used as an attribute name of relation r.
Union Operation:
Set Intersection:
• Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in both R & S.
• It is denoted by intersection ∩.
1. Notation: R ∩ S
 Example: Using the above DEPOSITOR table and BORROW table
Set Difference:
• Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in R but not in S.
• It is denoted by intersection minus (-).
1. Notation: R - S
 Example: Using the above DEPOSITOR table and BORROW table
 Input:
1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Cartesian product
 The Cartesian product is used to combine each row in one table with each
row in the other table. It is also known as a cross product.
 It is denoted by X.
 Notation: E X D
Input:
EMPLOYEE X DEPARTMENT
Rename Operation:

 The rename operation is used to rename the output relation. It is denoted by


rho (ρ).

 Example: We can use the rename operator to rename STUDENT relation to


STUDENT1.

 ρ(STUDENT1, STUDENT)
Join Operations:

 A Join operation combines


related tuples from different
relations, if and only if a given
join condition is satisfied. It is
denoted by ⋈.
 Operation: (EMPLOYEE ⋈ SAL
ARY)
Types of Join operations:
Natural Join:
Outer Join:
 The outer join
operation is an
extension of the
join operation. It
is used to deal
with missing
information.
Left outer join:

• Left outer join contains the set of tuples of all combinations in R and S that are equal
on their common attribute names.
• In the left outer join, tuples in R have no matching tuples in S.
• It is denoted by ⟕.
 Example: Using the above EMPLOYEE table and
FACT_WORKERS table
 Input:
1. EMPLOYEE ⟕ FACT_WORKERS
Right outer join:

• Right outer join contains the set of tuples of all combinations in R and S that are equal
on their common attribute names.
• In right outer join, tuples in S have no matching tuples in R.
• It is denoted by ⟖.
 Example: Using the above EMPLOYEE table and FACT_WORKERS Relation
 Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Full outer join:

• Full outer join is like a left or right join except that it contains all rows from both
tables.
• In full outer join, tuples in R that have no matching tuples in S and tuples in S that have
no matching tuples in R in their common attribute name.
• It is denoted by ⟗.
 Example: Using the above EMPLOYEE table and FACT_WORKERS table
 Input:
 EMPLOYEE ⟗ FACT_WORKERS
Equi join:
 It is also known as an
inner join. It is the
most common join. It
is based on matched
data as per the
equality condition.
The equi join uses
the comparison
operator(=).
Relational Calculus

 There is an alternate way of formulating queries known as Relational


Calculus. Relational calculus is a non-procedural query language. In the non-
procedural query language, the user is concerned with the details of how to
obtain the end results.
 The relational calculus tells what to do but never explains how to do. Most
commercial relational languages are based on aspects of relational calculus
including SQL-QBE and QUEL.
 It is based on Predicate calculus, a name derived from branch of symbolic
language. A predicate is a truth-valued function with arguments. On
substituting values for the arguments, the function result in an expression
called a proposition. It can be either true or false.
 Many of the calculus expressions involves the use of Quantifiers.
There are two types of quantifiers:
• Universal Quantifiers: The universal quantifier denoted by ∀ is read
as for all which means that in a given set of tuples exactly all tuples
satisfy a given condition.
• Existential Quantifiers: The existential quantifier denoted by ∃ is read
as for each which means that in a given set of tuples there is at least one
occurrences whose value satisfy a given condition.
Tuple Relational Calculus (TRC)
 Tuple Relational Calculus (TRC) is a non-procedural query language used in relational
database management systems (RDBMS) to retrieve data from tables. TRC is based on
the concept of tuples, which are ordered sets of attribute values that represent a single
row or record in a database table.
 TRC is a declarative language, meaning that it specifies what data is required from the
database, rather than how to retrieve it. TRC queries are expressed as logical formulas
that describe the desired tuples.
 The basic syntax of TRC is as follows:
 {T | P (T)} or {T | Condition (T)} where t is a tuple variable and P(t) is a logical
formula that describes the conditions that the tuples in the result must satisfy. The curly
braces {} are used to indicate that the expression is a set of tuples.
Domain Relational Calculus (DRC)
 Domain Relational Calculus is a non-procedural query language equivalent in
power to Tuple Relational Calculus. Domain Relational Calculus provides only
the description of the query but it does not provide the methods to solve it.
In Domain Relational Calculus, a query is expressed as,

 { < x1, x2, x3, ..., xn > | P (x1, x2, x3, ..., xn ) }
 where, < x1, x2, x3, …, xn > represents resulting domains variables and P (x1,
x2, x3, …, xn ) represents the condition or formula equivalent to the
Predicate calculus.
 Note:
The domain variables those will be in resulting relation must appear before | within ≺
and ≻ and all the domain variables must appear in which order they are in original
relation or table.
Example
 a) Get the complete details of all flights to New Delhi.
 b) Get the details about all flights from Chennai to New Delhi.
 C) Find only the flight numbers for passenger with pid 123 for flights to
Chennai before 06/11/2020.
 D) Find the passenger names for those who do not have any bookings in any
flights.
 E) Get the details of flights that are scheduled on both dates 01/12/2020 and
02/12/2020 at 16:00 hours.
σ destination = “New Delhi” (flight)
σ src = “Chennai” ^ dest = “New Delhi” (flight)
 Π fid (σ pid = 123 (booking) ⨝ σ dest = “Chennai” ^ fdate < 06/11/2020 (flight))
 Π pname ((Π pid (passenger) - Π pid (booking)) ⨝ passenger)
 (σ fdate = 01/12/2020 ^ time = 16:00 (flight)) ∩ (σ fdate = 02/12/2020 ^ time = 16:00 (flight))

You might also like