Introduction to relational model, Terminologies, Types of keys, Relational Algebra, Fundamental operations, Additional operations, Extended operations, Null values, Modification of the database
1. DATABASE SYSTEMS
STAFF NAME : D.SARITHA
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER APPLICATIONS
BON SECOURS COLLEGE FOR WOMEN
THANJAVUR
UNIT : II
CHAPTER : 2
TOPIC : RELATIONAL MODEL
3. ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 18
What is Relational Model?
Relational Model represents how data is stored in Relational
Databases.
A relational database stores data in the form of relations
(tables).
Consider a relation STUDENT with attributes ROLL_NO, NAME,
ADDRESS, PHONE and AGE shown in Table
STUDENT
4. 1 RAM DELHI 9455123451 18
IMPORTANT TERMINOLOGIES
1.Attribute: Attributes are the properties that define a relation.
e.g.; ROLL_NO, NAME
2.Relation Schema: A relation schema represents name of the relation with
its attributes.
e.g.; STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is
relation schema for STUDENT. If a schema has more than 1 relation, it is
called Relational Schema.
3.Tuple: Each row in the relation is known as tuple. The above relation
contains 4 tuples, one of which is shown as:
4.Relation Instance: The set of tuples of a relation at a particular instance
of time is called as relation instance.
5. ROLL_NO
1
2
3
4
5.Degree: The number of attributes in the relation is known as degree of the
relation. The STUDENT relation defined above has degree 5.
6.Cardinality: The number of tuples in a relation is known as cardinality.
The STUDENT relation defined above has cardinality 4.
7.Column: Column represents the set of values for a particular attribute.
The column ROLL_NO is extracted from relation STUDENT.
8.NULL Values: The value which is not known or unavailable is called NULL
value. It is represented by blank space.
e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
7. Super Key
The set of attributes which can uniquely identify a tuple is known
as Super Key.
For Example, STUD_NO, (STUD_NO, STUD_NAME) etc.
Candidate Key
The minimal set of attribute which can uniquely identify a tuple
is known as candidate key.
For Example, STUD_NO in STUDENT relation.
There can be more than one candidate key in a relation.
For Example, STUD_NO as well as STUD_PHONE both are
candidate keys for relation STUDENT.
8. Primary Key
There can be more than one candidate key in a relation out of
which one can be chosen as primary key.
For Example, STUD_NO , STUD_PHONE - candidate keys for
relation STUDENT
STUD_NO - primary key (only one out of many candidate keys).
Alternate Key
The candidate key other than primary key is called as alternate
key.
For Example, STUD_NO , STUD_PHONE - candidate keys for
relation STUDENT
STUD_NO - primary key and STUD_PHONE - alternate key
9. Foreign Key
If an attribute can only take the values which are
present as values of some other attribute, it will
be foreign key to the attribute to which it refers.
For Example,
STUD_NO in STUDENT_COURSE is a foreign key to
STUD_NO in STUDENT relation.
10. What is Relational Algebra?
• Relational algebra is a widely used procedural
query language.
• It collects instances of relations as input and
gives occurrences of relations as output.
• It uses operators to perform queries. An
operator can be either unary or binary.
• It uses various operation to perform this action.
11. The fundamental operations of relational
algebra
Select
Project
Union
Set different
Cartesian product
Rename
12. • The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition.
• Sigma(σ) Symbol denotes it.
• It is used as an expression to choose tuples which meet the
selection condition.
Select operation selects tuples that satisfy a given predicate.
σp
(r)
σ is the predicate
R stands for relation which is the name of the table
p is prepositional logic
SELECT (σ)
15. ROLL_NO NAME ADDRESS PHONE AGE
3 SUJIT ROHTAK 9156253131 20
Eg:
σ (AGE>18)(STUDENT)
RESULT:
16. ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
4 SURESH
Projection Operator (∏)
Projection operator is used to project particular columns from a
relation.
SYNTAX:
∏(Column 1,Column 2….Column n)(Relation Name)
EG:
∏(ROLL_NO,NAME)(STUDENT)
RESULT
17. Cross Product(X)
Cross product is used to join two relations.
For every row of Relation1, each row of
Relation2 is concatenated.
Syntax: Relation1 X Relation2
Eg: STUDENT X STUDENT_SPORTS
19. Union (U)
Union on two relations R1 and R2 can only be computed.
Union operator when applied on two relations R1 and R2 will
give a relation with tuples which are either in R1 or in R2
Syntax: Relation1 U Relation2
Eg: STUDENT U EMPLOYEE
ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 9156768971 18
5 NARESH HISAR 9782918192 22
6 SWETA RANCHI 9852617621 21
RESULT:
20. Minus (-)
Minus on two relations R1 and R2 can only be computed if R1
and R2 are union compatible.
Minus operator when applied on two relations as R1-R2 will
give a relation with tuples which are in R1 but not in R2.
Syntax: Relation1 - Relation2
Eg: STUDENT - EMPLOYEE
ROLL_NO NAME ADDRESS PHONE AGE
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
RESULT
21. Rename(ρ)
Rename operator is used to give another name to a
relation.
Syntax:
ρ(Relation2, Relation1)
Eg:
ρ(STUDENT1, STUDENT)
ρ(STUDENT_NAMES, ∏(ROLL_NO, NAME)(STUDENT))
22. 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 ⋈
Example:
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
EMPLOYEE
EMP_CODE SALARY
101 50000
102 30000
103 25000
SALARY
Operation:
(EMPLOYEE ⋈ SALARY)
25. EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
•A natural join is the set of tuples of all combinations in
R and S that are equal on their common attribute
names.
•It is denoted by ⋈.
Example:
∏ EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
Output:
NATURAL JOIN
26. EMP_
NAME
STREET CITY
Ram Civil line Mumbai
Shyam Park street Kolkata
Ravi M.G. Street Delhi
Hari Nehru nagar Hyderabad
EMP_
NAME
BRANCH SALARY
Ram Infosys 10000
Shyam Wipro 20000
Kuber HCL 30000
Hari TCS 50000
The outer join operation is an extension of the join
operation. It is used to deal with missing information.
Example
OUTER JOIN
FACT_WORKERSEMPLOYEE
27. EMP_NAME STREET CITY BRANCH SALARY
Ram Civil line Mumbai Infosys 10000
Shyam Park street Kolkata Wipro 20000
Hari Nehru
nagar
Hyderabad TCS 50000
(EMPLOYEE ⋈ FACT_WORKERS)
Output
28. EMP_NAME STREET CITY BRANCH SALARY
Ram Civil line Mumbai Infosys 10000
Shyam Park street Kolkata Wipro 20000
Hari Nehru street Hyderabad TCS 50000
Ravi M.G. Street Delhi NULL NULL
•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
EMPLOYEE ⟕ FACT_WORKERS
LEFT OUTER JOIN
29. EMP_NAME BRANCH SALARY STREET CITY
Ram Infosys 10000 Civil line Mumbai
Shyam Wipro 20000 Park street Kolkata
Hari TCS 50000 Nehru street Hyderabad
Kuber HCL 30000 NULL NULL
• 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:
EMPLOYEE ⟖ FACT_WORKERS
Output:
RIGHT OUTER JOIN
30. EMP_NAME STREET CITY BRANCH SALARY
Ram Civil line Mumbai Infosys 10000
Shyam Park street Kolkata Wipro 20000
Hari Nehru street Hyderabad TCS 50000
Ravi M.G. Street Delhi NULL NULL
Kuber NULL NULL HCL 30000
: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
EMPLOYEE ⟗ FACT_WORKERS
Output:
FULL OUTER JOIN
31. CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
CLASS_ID NAME PRODUCT_I
D
CITY
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
It is also known as an inner join. It is based on matched data as per the
equality condition. The equi join uses the comparison operator(=).
Example
CUSTOMER ⋈ PRODUCT
Output
EQUI JOIN
CUSTOMER PRODUCT
32. Relational algebra operations have been
extended in various ways
¤ More generalized
¤ More useful!
¨ Three major extensions:
¤ Generalized projection
¤ Aggregate functions
¤ Additional join operations
33. GENERALIZED PROJECTION
Written as:
∏F1, F2, …, Fn (E)
¤ Fi are arithmetic expressions
¤ E is an expression that produces a relation
¤ Derived attributes - Values are always
computed from other attributes stored in
database
¤ Also useful for updating values in database
35. AGGREGATE FUNCTIONS
sum - sums the values in the collection
avg - computes average of values in the collection count
- counts number of elements in the collection min -
returns minimum value in the collection
Max - returns maximum value in the collection
39. and: (true and unknown) = unknown;
(false and unknown) = false;
(unknown and unknown) = unknown.
or: (true or unknown) = true;
(false or unknown) = unknown;
(unknown or un- known) = unknown.
not: (not unknown) = unknown
NULL VALUES
Null indicates - “value unknown or nonexistent,”
any arithmetic operations (such as +, −, ∗, /) involving
null values must return a null result.
40. MODIFICATION OF THE DATABASE
Often need to modify data in a database
Can use assignment operator
Operations:
r r U E Insert new tuples into a relation
r r – E Delete tuples from a relation
r P(r) Update tuples already in the relation