DBMS NOTES(Relational Model)
DBMS NOTES(Relational Model)
BCAC 0020
Topic: Relational Model
[email protected], +91-8923015116
Keys
• Keys play an important role in the relational database.
• It is used to uniquely identify any record or row of data from
the table.
• It is also used to establish and identify relationships between
tables.
7. Foreign Key
• It is a key that acts as a primary key in one table and acts
as secondary key in another table.
• The foreign key is useful in linking.
• It combines two or more relations (tables) at a time.
• They act as a cross-reference between the tables.
• Data should be entered in the foreign key column with great
care.
• As wrongly entered data can invalidate the relationship
between the two tables.
8. Surrogate Key
• A surrogate key is a database key that is not derived
from the natural key of the table.
• It is a unique identifier that is generated by the
database system.
• Surrogate keys are often used when the natural key
is not unique or when it is subject to change.
• In other words, a surrogate key is a type of primary
key.
9. Partial Key
• Partial key is a key using which all the records of the
table can not be identified uniquely.
• The set of attributes that are used to uniquely identify
a weak entity set is called the Partial key.
• Only a bunch of the tuples can be identified using the
partial keys.
• The partial Key of the weak entity set is also known as
a discriminator.
• It is just a part of the key as only a subset of the
attributes can be identified using it.
• It is partially unique and can be combined with other
strong entity set to uniquely identify the tuples.
Differences between Primary Key and
Secondary Key
Basis Primary Key Secondary Key
A key that is unique, not A key that uniquely
null, and is selected by identifies rows but is
the database not selected as the
Definition
administrator to uniquely primary key is known as
identify tuples is called a secondary key or
the primary key. alternate key.
NULL
It cannot be NULL. It can be NULL.
values
Important Results
– We can not insert a record into a referencing relation if the
corresponding record does not exist in the referenced
relation.
– We can not delete or update a record of the referenced
relation if the corresponding record exists in the
referencing relation.
• Here, relation ‘Student’ references the relation ‘Department’.
Branch_Code Branch_Name
Roll_no Name Age Branch_Code
CS Computer Science
1 Rahul 22 CS EE Electronics Engineering
Query Language:
● A Language which is used to store and retrieve data
from database is known as query language. For
example – SQL Query
Language
14
Query
1. Procedural Query language: Language
In procedural query language, user instructs
the system to perform a series of operations to
produce the desired results. Here users tells Procedural Non-
Procedural
what data to be retrieved from database and Query
Language Query
how to retrieve it. Language
14
Database State for COMPANY
All examples discussed further will refer to the COMPANY database shown
here.
Unary Relational Operations: SELECT
The SELECT operation (denoted by (sigma)) is used to
select a subset of the tuples from a relation based on a
selection condition.
•The selection condition acts as a filter
•P (r) : Keeps only those tuples from relation r, which
satisfy predicate/condition P.
•The predicate P will involve:-
•Attributes from Schema R of r
•Comparison Operators: <, > , < , >, =,
•Logical Operator: (AND) , (OR) , (NOT)
Unary Relational Operations: SELECT
Examples:
Fname,Lname,Salary(EMPLOYEE)
Unary Relational Operations: PROJECT
PROJECT Operation Properties
• The number of tuples in the result of projection <list>(R) is
always less or equal to the number of tuples in R.
Output
??
Unary Relational Operations: Examples
Output
Lname, Fname, Salary(EMPLOYEE) ??
Unary Relational Algebra Expressions:
TEMP
R
Unary Relational Operations: RENAME
•The RENAME operator is denoted by (rho)
•In some cases, we may want to rename the attributes of a relation
or the relation name or both.
•Useful when a query requires multiple operations
•Necessary in some cases (see JOIN operation later)
•The general RENAME operation can be expressed by any of
the following forms:
• S (B1, B2, …, Bn )(R) changes both:
the relation name to S, and
the column (attribute) names to B1, B2, …..Bn
• S(R) changes:
the relation name only to S
• (B1, B2, …, Bn )(R) changes:
the column (attribute) names only to B1, B2, …..Bn
Unary Relational Operations: RENAME
Example:
DEP5_EMPS ( DNO=5(EMPLOYEE))
DEP5_EMPS
RESULT
Relational Algebra Operations from Set
Theory :
•Type Compatibility of operands is required for the binary
set operation UNION ∪ , (also for INTERSECTION ∩, and
SET DIFFERENCE –, see next slides)
•R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are type
compatible if:
•they have the same number of attributes, and
•the domains of corresponding attributes are type
compatible
(i.e. dom(Ai)=dom(Bi) for i=1, 2, ..., n).
•The resulting relation for R1 ∪ R2 (also for R1 ∩ R2, or R1–
R2, see next slides) has the same attribute names as the
first operand relation R1 (by convention)
Relational Algebra Operations from Set Theory:
UNION
UNION Operation
•Binary operation, denoted by
•The result of R S, is a relation that includes all tuples that
are either in R or in S or in both R and S
•Duplicate tuples are eliminated
•The two operand relations R and S must be “type
compatible” (or UNION compatible):
R and S must have same number of attributes
Each pair of corresponding attributes must be type
compatible (have same or compatible domains)
Relational Algebra Operations from Set
Theory : INTERSECTION
•INTERSECTION is denoted by ∩
•The result of the operation R ∩ S, is a relation that includes
all tuples that are in both R and S
•The attribute names in the result will be the same as the
attribute names in R
•The two operand relations R and S must be “type
compatible”
Relational Algebra Operations from Set
Theory : SET DIFFERENCE
•SET DIFFERENCE (also called MINUS or EXCEPT) is denoted
by –
•The result of R – S, is a relation that includes all tuples that
are in R but not in S
•The attribute names in the result will be the same as the
attribute names in R
•The two operand relations R and S must be “type
compatible”
Relational Algebra Operations from Set
Theory :
Example: The set operations
UNION, INTERSECTION, (b) STUDENT ∪ INSTRUCTOR.
and MINUS. (c) STUDENT ∩ INSTRUCTOR.
(d) STUDENT – INSTRUCTOR.
(a) Two union- (e) INSTRUCTOR – STUDENT
compatible relations.
Properties of UNION, INTERSECT, and
DIFFERENCE
• Notice that both union and intersection are commutative
operations; that is
R ∪ S = S ∪ R, and R ∩ S = S ∩ R
• Both union and intersection can be treated as n-ary
operations applicable to any number of relations as both
are associative operations; that is
R ∪ (S ∪ T) = (R ∪ S) ∪ T
(R ∩ S) ∩ T = R ∩ (S ∩ T)
• The minus operation is not commutative; that is, in
general
R–S≠S–R
The Relational Algebra-II (JOINS)
1. Cartesian Product (Cross Join)
2. Join Operations
i. Inner Join
a. Theta Join
b. EQUI Join
c. Natural Join
ii. Outer Join
a. Left Outer Join
b. Right Outer Join
c. Full Outer Join 69
Introduction – Cartesian Product
• Cartesian Product in DBMS is an operation used to
combine tuples from two relations in a combinatorial
fashion.
• It is also called Cross Product or Cross Join.
• Very rare in practice; mainly used to express joins.
• Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm).
• If relation R1 has n tuples and relation R2 has m tuples ,
total tuples in R1 R2 will be n * m.
• Result is a relation Q with degree n + m attributes:
Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
• The two operands do NOT have to be "type compatible”.
70
Cartesian Product (Contd.)
A B C D E A B C D E
α 10 a α 1 α 10 a
α 1 X A B a α 1 A B a
α 2
α 1 b α 1 α 1 b
α 2 b α 1 α 2 b
α 2 α 10 a
α 2 A B a
α 2 α 1 b
α 2 α 2 b
Join Operation
The sequence of CARTESIAN PRODUCT followed by SELECT
is used quite commonly to identify and select related tuples
from two relations. A special operation, called JOIN
combines this sequence into a single operation
Slide 6-
Join Operation (Contd.)
Example: Suppose that we want to retrieve the name of the
manager of each department.
To get the manager’s name, we need to combine each
DEPARTMENT tuple with the EMPLOYEE tuple whose SSN
value matches the MGRSSN value in the department
tuple.
We do this by using the join operation.
Physically, the join of R1 and R2 with the condition theta is the same than the
selection of the cross product of R1 and R2 with the condition theta.
Theta Join
Example: Display the name of the customer along with the age group in
which his/her age lies.
The standard definition of natural join requires that the two join
attributes, or each pair of corresponding join attributes, have
the same name in both relations.
If this is not the case, a renaming operation is applied first.
We can perform a Natural Join only if there is at least one common
attribute that exists between two relations.
A natural join is a shorthand equi-join where the equality applied to
all shared attributes.
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER
Courses * HoD
Outer Join Overview
The Outer Join operation is an extension of the JOIN operation to
deal with missing values. Let’s consider two table :
employee ft-works
Objective: Generate a single relation with all the information
(employee-name, street, city, branch name, and salary) about full-
time employees.
Solution: Possible approach is Natural join operation .
Output:
But we have lost the street and city information about Smith. Similarly, we
have lost the branch name and salary information about Gates.
14
Outer Join contd..
Full outer
join
All three forms of outer join compute the join, and add extra tuples
to the result of the join.
14
Left outer join ( )
The left outer join ( ) takes all tuples in the left relation that
did not match with any tuple in the right relation, pads the
tuples with null values for all other attributes from the right
relation, and adds them to the result of the natural join.
employee ft-works
Objective :
Results:
All tuples
from left
table
14
Right outer join ( )
The right outer join ( ) takes all tuples in the right relation
that did not match with any tuple in the left relation, pads the
tuples with null values for all other attributes from the left
relation, and adds them to the result of the natural join.
employee ft-works
Objective :
Results:
All tuples
from right
table
14
Full outer join ( )
The full outer join ( ) does both of those operations, padding
tuples from the left relation that did not match any from the
right relation, as well as tuples from the right relation that did
not match any from the left relation, and adding them to the
result of the join.
employee ft-works
Objective :
Results:
All tuples
from both
tables
14
Can we answer?
● Using following table how many number of tuples will be
returned for the following relational algebra expression:
A. 10
B. 15
C. 11
D. 12
Binary relation operation: DIVISION
DIVISION Operation:
● The division operation is applied to two relations .
● R(Z) ÷ S(X), where X subset Z. Let Y = Z - X (and hence Z = X Ư Y);
that is, let Y be the set of attributes of R that are not attributes of
S.
● The result of DIVISION is a relation T(Y) that includes a tuple if
tuples appear in R with , and with.
14
Example of DIVISION operation
14
Can we answer of Division A/B ?
14
Basic Aggregate functions operations
Use of the Aggregate Functional operation :
●Retrieves the maximum salary value from the EMPLOYEE relation
■ Note: count just counts the number of rows, without removing duplicates
14