DATA BASE
PROF
MANAGEMENT SHIVANAND
PATIL
SYSTEM
UNIT - III
1
UNIT – III
Relational Data Model
Relational Model Concepts
1. The relational model represents the database as a collection of relations.
2. A relation is thought of as a table of values, each row in the table represents
a collection of related data values.
3. A row represents a fact that typically corresponds to a real-world entity or
relationship. The table name and column names are used to help to interpret
the meaning of the values in each row.
In the formal relational model terminology,
a row→a tuple, a column header →an attribute, and the table →a relation. The
data type describing the types of values that can appear in each column is
represented by a domain of possible values.
Important Points:
Domain: It contains a set of atomic values that an attribute can take.
Attribute: It contains the name of a column in a particular table. Each
attribute Ai must have a domain, dom(Ai)
Relational instance: In the relational database system, the relational instance
is represented by a finite set of tuples. Relation instances do not have duplicate
tuples.
Relational schema: A relational schema contains the name of the relation and
name of all columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It
can identify the row in the relation uniquely
Characteristics of relations.
1. Ordering of Tuples in a Relation
• A relation is defined as a set of tuples. Mathematically, elements of a set
have no order among them hence, tuples in a relation do not have any
particular order.
• Similarly, when tuples are represented on a storage device, they must be
organized in some fashion, and it may be advantageous, from a
performance standpoint, to organize them in a way that depends upon their
content.
2. Ordering of Values within a Tuple
• The order of attributes and their values is not that important as long as the
correspondence between attributes and values is maintained.
• A tuple can be considered as a set of (<attribute>,<value> ) pairs, where
each pair gives the value of the mapping from an attribute Ai to a value vi
KLE BCA MAHALINGPUR IIIRD SEM DBMS
2
from dom(Ai). The ordering of attributes is not important, because the
attribute name appears with its value.
3. Values and NULLs in the Tuples
• Each value in a tuple is an atomic value; that is, it is not divisible into
components.
• An important concept is NULL values, which are used to represent the
values of attributes that may be unknown or may not apply to a tuple.
• NULL values has several meanings, such as value unknown, value exists
but is not available, or attribute does not apply to this tuple.
4. Interpretation (Meaning) of a Relation
• Each tuple in the relation can then be interpreted as a fact or a particular
instance of the assertion.
• Each relation can be viewed as a predicate and each tuple in that relation
can be viewed as an assertion for which that predicate is satisfied (i.e., has
value true) for the combination of values in it.
• Example : There exists a student having name Benjamin Bayer, having
SSN 305-61-2435, having age 19, etc
Relational model constraints
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of
information.
o 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.
o Thus, integrity constraint is used to guard against accidental damage to the
database.
Domain constraints
o Domain constraints can be defined as the definition of a valid set of values
for an attribute.
o 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.
KLE BCA MAHALINGPUR IIIRD SEM DBMS
3
Example:
. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o 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.
o A table can contain a null value other than the primary key field.
Example:
Key constraints
o Keys are the entity set that is used to identify an entity within its entity set
uniquely.
o 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
KLE BCA MAHALINGPUR IIIRD SEM DBMS
4
Example:
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:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ)
Notation: σ p®
Where:
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND
OR and NOT. These relational can use as relational operators like =, ≠, ≥, <, >,
≤.
KLE BCA MAHALINGPUR IIIRD SEM DBMS
5
LOAN Relation
BRANCH_NAME LOAN_NO AMOUNT
Downtown L-17 1000
Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300
Input:
σ BRANCH_NAME="perryride" (LOAN)
BRANCH_NAME LOAN_NO AMOUNT
Perryride L-15 1500
Perryride L-16 1300
KLE BCA MAHALINGPUR IIIRD SEM DBMS
6
2. Project Operation:
o 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.
o It is denoted by ∏
Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example: CUSTOMER RELATION
NAME STREET CITY
Jones Main Harrison
Smith North Rye
Hays Main Harrison
Curry North Rye
Johnson Alma Brooklyn
Brooks Senator Brooklyn
Input : ∏ NAME, CITY (CUSTOMER)
Output:
ME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
KLE BCA MAHALINGPUR IIIRD SEM DBMS
7
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the
tuples that are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by 𝖴
Notation: R 𝖴 S
A union operation must hold the following condition:
o R and S must have the attribute of the same number.
o Duplicate tuples are eliminated automatically.
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
o It is denoted by intersection ∩.
Notation: R ∩ S
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in R but not in S.
o It is denoted by intersection minus (-).
Notation: R - S
6. Cartesian product
o 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.
o It is denoted by X.
Notation: E X D
7. 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.
Notation: ρ(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 ⋈.
KLE BCA MAHALINGPUR IIIRD SEM DBMS
8
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
Example: EMPLOYEE
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Operation: (EMPLOYEE ⋈ SALARY)
EMP_CODE EMP_NAME SALARY
101 Stephan 50000
102 Jack 30000
103 Harry 25000
Types of Join operations:
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are
equal on their common attribute names.
o It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
KLE BCA MAHALINGPUR IIIRD SEM DBMS
9
∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal
with missing information.
Example: EMPLOYEE
EMP_NAME STREET CITY
Ram Civil line Mumbai
Shyam Park street Kolkata
Ravi M.G. Street Delhi
Hari Nehru nagar Hyderabad
FACT_WORKERS
EMP_NAME BRANCH SALARY
Ram Infosys 10000
Shyam Wipro 20000
Kuber HCL 30000
Hari TCS 50000
Input: (EMPLOYEE ⋈ FACT_WORKERS)
KLE BCA MAHALINGPUR IIIRD SEM DBMS
10
Output:
EMP_NAME STREET CITY BRANCH SALARY
Ram Civil line Mumbai Infosys 10000
Shyam Park street Kolkata Wipro 20000
Hari Nehru nagar Hyderabad TCS 50000
3. 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(=).
Example: CUSTOMER RELATION
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
CUSTOMER ⋈ PRODUCT
KLE BCA MAHALINGPUR IIIRD SEM DBMS
11
Output:
CLASS_ID NAME PRODUCT_ID CITY
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
Aggregate Functions
SQL aggregation function is used to perform the calculations on multiple rows of
a single column of a table. It returns a single value.
Types:
Various Aggregate Functions:
1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()
KLE BCA MAHALINGPUR IIIRD SEM DBMS
12
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table.
It can work on both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows
in a specified table. COUNT(*) considers duplicate and Null.
Syntax:COUNT(*)
Sample table: PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
SELECT COUNT(*) FROM PRODUCT_MAST;
Output: 10
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on
numeric fields only.
Syntax : SUM()
Example: SELECT SUM(COST) FROM PRODUCT_MAST;
Output: 670
3. AVG function
KLE BCA MAHALINGPUR IIIRD SEM DBMS
13
The AVG function is used to calculate the average value of the numeric type.
AVG function returns the average of all non-Null values.
Syntax: AVG()
Example: SELECT AVG(COST) FROM PRODUCT_MAST;
Output: 67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
Syntax: MAX( )
Example: SELECT MAX(RATE) FROM PRODUCT_MAST;
Output:30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column
Syntax : MIN( )
Example : SELECT MIN(RATE) FROM PRODUCT_MAST;
Output: 10
KLE BCA MAHALINGPUR IIIRD SEM DBMS