DBMS UNIT 3
DBMS UNIT 3
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.
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.
• 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.
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.
Example:
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
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:
LOAN Relation
Input:
σ BRANCH_NAME="perryride" (LOAN)
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 ∏
Where
A1, A2, A3 is used as an attribute name of relation r.
Example: CUSTOMER RELATION
ME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
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 (ρ).
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 ⋈.
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
Example: EMPLOYEE
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
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
FACT_WORKERS
Output:
3. Equi join:
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
CUSTOMER ⋈ PRODUCT
Output:
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:
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(*)
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
Item8 Com1 3 10 30
Item9 Com2 2 25 50
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
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