DBMS Module3
DBMS Module3
(DBMS)
Avinash V. Gondal
email: avinash.gondal@gmail.com
Good database design will get you through poor
programming better than good programming will
get you through poor database design….
1
Module 3
Relational Model
2
3. Relational Model
Contents :
Introduction
Mapping the ER and EER Model to the Relational
Model
Data Manipulation
Data Integrity
Advantages of the Relational Model
Relational Algebra
Relational Algebra Queries
Relational Calculus
3
Introduction
4
E. F. Codd Our Hero
5
Introduction
The relational data model was first introduced by E.F.
Codd of IBM research at 1970. The relation model is
based on the concept of mathematical relation. It uses
set theory and first-order predicate logic as theoretical
basis.
The relational model represents both data and the
relationships among those data using relation.
A relation is used to represent information about any
entity (such as book, author etc.) and its relationship
with other entities in the form of attributes (or columns)
and tuples (or rows).
6
Introduction
A relation schema (also termed as relation intension)
depicts the attributes of the table.
A relation instance (also termed as relation extension) is
a two dimensional table with a time-varying set of
tuples.
7
Informal Definitions
• Informally, a relation looks like a table of values.
9
Informal Definitions
• Key of a Relation:
– Each row has a value of a data item (or set of items) that
uniquely identifies that row in the table
• Called the key
– In the STUDENT table, SSN is the key
10
Formal Definitions - Schema
• The Schema (or description) of a Relation:
– Denoted by R(A1, A2, .....An)
– R is the name of the relation
– The attributes of the relation are A1, A2, ..., An
• Example:
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
– CUSTOMER is the relation name
– Defined over the four attributes: Cust-id, Cust-name, Address,
Phone#
• Each attribute has a domain or a set of valid values.
– For example, the domain of Cust-id is 6 digit numbers.
11
Formal Definitions - Tuple
• A tuple is an ordered set of values (enclosed in angled
brackets ‗< … >‘)
• Each value is derived from an appropriate domain.
• A row in the CUSTOMER relation is a 4-tuple and
would consist of four values, for example:
– <632895, "John Smith", "101 Main St. Atlanta, GA
30332", "(404) 894-2000">
– This is called a 4-tuple as it has 4 values
– A tuple (row) in the CUSTOMER relation.
• A relation is a set of such tuples (rows)
12
Formal Definitions - Domain
• A domain has a logical definition:
– Example: ―USA_phone_numbers‖ are the set of 10 digit phone
numbers valid in the U.S.
• A domain also has a data-type or a format defined for it.
– The USA_phone_numbers may have a format: (ddd)ddd-dddd
where each d is a decimal digit.
– Dates have various formats such as year, month, date formatted
as yyyy-mm-dd, or as dd mm,yyyy etc.
• The attribute name designates the role played by a
domain in a relation:
– Used to interpret the meaning of the data elements
corresponding to that attribute
– Example: The domain Date may be used to define two attributes
named ―Invoice-date‖ and ―Payment-date‖ with different
meanings 13
Formal Definitions - State
• The relation state is a subset of the Cartesian product
of the domains of its attributes
– each domain contains the set of all possible values
the attribute can take.
• Example: attribute Cust-name is defined over the
domain of character strings of maximum length 25
– dom(Cust-name) is varchar(25)
• The role these strings play in the CUSTOMER relation
is that of the name of a customer.
14
Formal Definitions - Summary
• Formally,
– Given R(A1, A2, .........., An)
– r(R) dom (A1) X dom (A2) X ....X dom(An)
• R(A1, A2, …, An) is the schema of the relation
• R is the name of the relation
• A1, A2, …, An are the attributes of the relation
• r(R): a specific state (or "value" or ―population‖) of
relation R – this is a set of tuples (rows)
– r(R) = {t1, t2, …, tn} where each ti is an n-tuple
– ti = <v1, v2, …, vn> where each vj element-of dom(Aj)
15
Definition Summary
16
Basic Concepts
17
Concepts of Relational Databases
• A relational database is represented as a collection of
relations.
• A relation is physically expressed as a table in which
each row corresponds to individual record and each
column corresponds to individual attribute, where
attributes can appear in any order within the table.
• In formal relational data model terminology, a row is
called a tuple, a column header is called an attribute and
the table is called a relation.
• The degree (or arity) of a relation is represented by the
number of attributes it contains.
18
Concepts of Relational Databases
• The cardinality of a relation is represented by the
number of tuples it contains.
• The set of permitted values for each attribute is known as
the domain of the attribute.
• A relation R defined over a set of n attributes A1, A2,
…., An with domains D1, D2, ……, Dn is a set of n-
tuples denoted by <d1, d2, ……., dn>, such that d1
D1, d2 D2, ………., dn Dn. Hence, the set {A1:D1,
A2:D2, ………., An:Dn) represents the relational
schema.
19
Concepts of Relational Databases
• The key of a relation is the nonempty subset of its
attributes, which is used to identify each tuple uniquely
from a relation.
• The attributes of a relation that make up the key are
called prime attributes or key attributes.
• The attributes of a relation that do not participate in key
formation are called non-prime attributes or non-key
attributes.
20
Keys in Relational Data Model
• The superset of a key is usually known as superkey.
• A candidate key is a superkey such that no proper
subset is a superkey within the relation. Thus, a minimal
superkey is called candidate key.
• A relational schema can have more than one candidate
key among which one is chosen as primary key during
design time.
• The candidate keys that are not selected as primary key
are known as alternate keys.
• A foreign key is an attribute or a set of attributes within
one relation that matches the candidate key of some
other relation (possibly the same relation). 21
Schema for four relations (Furniture Company)
Primary Key
Foreign Key
(implements 1:N relationship
between customer and order)
24
Characteristics of Relations
• Table name is distinct from all other table names in
the database.
• Each cell of table contains exactly one atomic
(single) value.
• Each column has a distinct name.
• Values of a column are all from the same domain.
• Each record is distinct; there are no duplicate
records.
• Order of columns has no significance.
• Order of records has no significance, theoretically.
25
Relational Database Design by ER-
and EER-to-Relational Mapping
Outline
• ER-to-Relational Mapping Algorithm
– Step 1: Mapping of Regular Entity Types
– Step 2: Mapping of Weak Entity Types
– Step 3: Mapping of Binary 1:1 Relation Types
– Step 4: Mapping of Binary 1:N Relationship Types.
– Step 5: Mapping of Binary M:N Relationship Types.
– Step 6: Mapping of Multivalued attributes.
– Step 7: Mapping of N-ary Relationship Types.
63
Advantages of the Relational Model
• Structural Independence :
– In relational database, changes in the database structure do not
affect the data access. So the relational database has structural
independence.
• Conceptual Simplicity:
– The Relational database model is simpler at the conceptual
level. Since the relational data model frees the designer from
the physical data storage details, the designers can concentrate
on the logical view of the database.
• Set-Processing:
– Relational database model provides facilities for manipulating
a set of records at a time so that programmers are not
operating on the database record by record.
64
Advantages of the Relational Model
• Sound Theoretical Background:
– Relational database model provides a theoretical background
for the database management field.
• It is simpler to navigate.
• Provides greater flexibility.
65
Query Languages
66
Query Languages
• Language in which user requests information from the
database.
• Categories of languages
– Procedural
– Non-procedural, or declarative
• ―Pure‖ languages:
– Relational algebra
– Tuple relational calculus
– Domain relational calculus
• Pure languages form underlying basis of query
languages that people use. 67
Relational Algebra
68
Relational Algebra
• The relational algebra is a procedural query language.
• It consists of a set of operations that take one or two
relations as input and produce a new relation as their
result.
• Relational Algebra
– Unary Relational Operations
– Relational Algebra Operations From Set Theory
– Binary Relational Operations
– Additional Relational Operations
69
Relational Algebra Overview
• Relational algebra is the basic set of operations for
the relational model
• These operations enable a user to specify basic
retrieval requests (or queries)
• The result of an operation is a new relation, which
may have been formed from one or more input
relations
– This property makes the algebra ―closed‖ (all objects
in relational algebra are relations)
70
Relational Algebra Overview
• The algebra operations thus produce new relations
– These can be further manipulated using operations of
the same algebra
• A sequence of relational algebra operations forms a
relational algebra expression
– The result of a relational algebra expression is also a
relation that represents the result of a database query
(or retrieval request)
71
Brief History of Origins of Algebra
• Muhammad ibn Musa al-Khwarizmi (800-847 CE)
wrote a book titled al-jabr about arithmetic of variables
– Book was translated into Latin.
– Its title (al-jabr) gave Algebra its name.
• Al-Khwarizmi called variables ―shay‖
– ―Shay‖ is Arabic for ―thing‖.
– Spanish transliterated ―shay‖ as ―xay‖ (―x‖ was ―sh‖ in
Spain).
– In time this word was abbreviated as x.
• Where does the word Algorithm come from?
– Algorithm originates from ―al-Khwarizmi"
72
Relational Algebra Overview
• Relational Algebra consists of several groups of operations
– Unary Relational Operations
• SELECT (symbol: (sigma))
• PROJECT (symbol: (pi))
• RENAME (symbol: (rho))
– Relational Algebra Operations From Set Theory
• UNION ( ), INTERSECTION ( ), DIFFERENCE (or MINUS, – )
• CARTESIAN PRODUCT ( x )
– Binary Relational Operations
• JOIN (several variations of JOIN exist)
• DIVISION
– Additional Relational Operations
• OUTER JOINS, OUTER UNION
• AGGREGATE FUNCTIONS (These compute summary of
information: for example, SUM, COUNT, AVG, MIN, MAX)
73
Database State for COMPANY
• All examples discussed below refer to the COMPANY database
shown here.
74
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
– Keeps only those tuples that satisfy the qualifying condition
– Tuples satisfying the condition are selected whereas the other
tuples are discarded (filtered out)
• Examples:
– Select the EMPLOYEE tuples whose department number is 4:
DNO = 4 (EMPLOYEE)
– Select the employee tuples whose salary is greater than
$30,000:
SALARY > 30,000 (EMPLOYEE)
75
Unary Relational Operations: SELECT
In general, the select operation is denoted by
<selection condition>(R) where
• the symbol (sigma) is used to denote the select
operator
• the selection condition is a Boolean (conditional)
expression specified on the attributes of relation R
• tuples that make the condition true are selected
– appear in the result of the operation
• tuples that make the condition false are filtered out
– discarded from the result of the operation
76
Unary Relational Operations: SELECT
• SELECT Operation Properties
– The SELECT operation <selection condition>(R) produces a
relation S that has the same schema (same attributes) as R
– SELECT is commutative:
<condition1>( < condition2> (R)) = <condition2> ( < condition1> (R))
– Because of commutativity property, a cascade (sequence) of
SELECT operations may be applied in any order:
<cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R)))
– A cascade of SELECT operations may be replaced by a single
selection with a conjunction of all the conditions:
<cond1>(< cond2> (<cond3>(R)) = <cond1> AND < cond2> AND <
cond3>(R)))
– The number of tuples in the result of a SELECT is less than
(or equal to) the number of tuples in the input relation R
77
The following query results refer to this database state
78
Unary Relational Operations: PROJECT
• PROJECT Operation is denoted by (pi)
• This operation keeps certain columns (attributes) from a
relation and discards the other columns.
– PROJECT creates a vertical partitioning
• The list of specified columns (attributes) is kept in each
tuple
• The other attributes in each tuple are discarded
• Example: To list each employee‘s first and last name
and salary, the following is used:
LNAME, FNAME,SALARY(EMPLOYEE)
79
Unary Relational Operations: PROJECT
• The general form of the project operation is:
<attribute list>(R)
(pi) is the symbol used to represent the project
operation
<attribute list> is the desired list of attributes from
relation R.
• The project operation removes any duplicate tuples
– This is because the result of the project operation
must be a set of tuples
• Mathematical sets do not allow duplicate elements.
80
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
• If the list of attributes includes a key of R, then the
number of tuples in the result of PROJECT is equal to
the number of tuples in R
– PROJECT is not commutative
• <list1> ( <list2> (R) ) = <list1> (R) as long as <list2>
contains the attributes in <list1>
81
Examples of applying SELECT and PROJECT
operations
82
Relational Algebra Expressions
• We may want to apply several relational algebra
operations one after the other
– Either we can write the operations as a single
relational algebra expression by nesting the
operations, or
– We can apply one operation at a time and create
intermediate result relations.
• In the latter case, we must give names to the
relations that hold the intermediate results.
83
Single expression versus sequence of relational
operations (Example)
• To retrieve the first name, last name, and salary of all
employees who work in department number 5, we must
apply a select and a project operation
• We can write a single relational algebra expression as
follows:
- FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
• OR We can explicitly show the sequence of operations,
giving a name to each intermediate relation:
– DEP5_EMPS DNO=5(EMPLOYEE)
– RESULT FNAME, LNAME, SALARY (DEP5_EMPS)
84
Unary Relational Operations: RENAME
85
Unary Relational Operations: RENAME
• 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, B1, …..Bn
– S(R) changes:
• the relation name only to S
– (B1, B2, …, Bn )(R) changes:
• the column (attribute) names only to B1, B1, …..Bn
86
Unary Relational Operations: RENAME
• For convenience, we also use a shorthand for renaming
attributes in an intermediate relation:
– If we write:
• RESULT FNAME, LNAME, SALARY (DEP5_EMPS)
• RESULT will have the same attribute names as
DEP5_EMPS (same attributes as EMPLOYEE)
• If we write:
• RESULT (F, M, L, S, B, A, SX, SAL, SU, DNO)
RESULT (F.M.L.S.B,A,SX,SAL,SU, DNO)(DEP5_EMPS)
• The 10 attributes of DEP5_EMPS are renamed to F,
M, L, S, B, A, SX, SAL, SU, DNO, respectively
87
Example of applying multiple operations and
RENAME
88
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)
89
Relational Algebra Operations from
Set Theory: UNION
• Example:
– To retrieve the social security numbers of all employees
who either work in department 5 (RESULT1 below) or
directly supervise an employee who works in department
5 (RESULT2 below)
– We can use the UNION operation as follows:
DEP5_EMPS DNO=5 (EMPLOYEE)
RESULT1 SSN(DEP5_EMPS)
RESULT2(SSN) SUPERSSN(DEP5_EMPS)
RESULT RESULT1 RESULT2
– The union operation produces the tuples that are in either
RESULT1 or RESULT2 or both
90
Example of the result of a UNION
operation
UNION Example :
91
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 R1R2 (also for R1R2, or R1–
R2, see next slides) has the same attribute names as the first
operand relation R1 (by convention)
92
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‖
93
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‖
94
Example to illustrate the result of UNION,
INTERSECT, and DIFFERENCE
95
Some 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
96
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT
• CARTESIAN (or CROSS Product or CROSS Join )
PRODUCT Operation
– This operation is used to combine tuples from two relations in
a combinatorial fashion.
– Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm)
– Result is a relation Q with degree n + m attributes:
• Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
– The resulting relation state has one tuple for each combination
of tuples—one from R and one from S.
– Hence, if R has nR tuples (denoted as |R| = nR ), and S has nS
tuples, then R x S will have nR * nS tuples.
– The two operands do NOT have to be "type compatible‖ 97
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT
• Generally, CROSS PRODUCT is not a meaningful
operation
– Can become meaningful when followed by other operations
• Example (not meaningful): For example, suppose that we want
to retrieve a list of names of each female employee‘s dependents.
We can do this as follows:
– FEMALE_EMPS SEX=‘F‘(EMPLOYEE)
– EMPNAMES FNAME, LNAME, SSN (FEMALE_EMPS)
– EMP_DEPENDENTS EMPNAMES x DEPENDENT
• EMP_DEPENDENTS will contain every combination of
EMPNAMES and DEPENDENT
– whether or not they are actually related
98
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT
• To keep only combinations where the DEPENDENT is
related to the EMPLOYEE, we add a SELECT
operation as follows
• Example (meaningful):
– FEMALE_EMPS SEX=‘F‘(EMPLOYEE)
– EMPNAMES FNAME, LNAME, SSN (FEMALE_EMPS)
– EMP_DEPENDENTS EMPNAMES x DEPENDENT
– ACTUAL_DEPS SSN=ESSN(EMP_DEPENDENTS)
– RESULT FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)
• RESULT will now contain the name of female employees
and their dependents
99
Example of applying CARTESIAN
PRODUCT
100
Additional Operations
101
Additional Operations
• The fundamental operations of the relational algebra are
sufficient to express any relational-algebra query.
• However, if we restrict ourselves to just the fundamental
operations, certain common queries are lengthy to express.
• Therefore, we define additional operations that do not add
any power to the algebra, but simplify common queries.
• For each new operation, we give an equivalent expression
that uses only the fundamental operations. We define
additional operations that do not add any power relational
algebra, but that simplify common queries.
102
Additional Operations
• Set intersection
• Natural join
• Division
• Assignment
103
Set-Intersection Operation
• Notation: r s
• Defined as:
• r s ={ t | t r and t s }
• Assume:
– r, s have the same arity
– attributes of r and s are compatible
• Note: r s = r - (r - s)
104
Set-Intersection Operation - Example
• Relation r, s:
A B A B
1 2
2 3
1
r s
A B
• rs
2
Set-Intersection Operation - Example
• Suppose that we wish to find all customers who have
both a loan and an account. Using set intersection, we
can write
Πcustomer-name (borrower ) ∩ Πcustomer-name (depositor)
• Note that we can rewrite any relational algebra
expression that uses set intersection by replacing the
intersection operation with a pair of set-difference
operations as:
r ∩ s = r − (r − s)
• Thus, set intersection is not a fundamental operation
and does not add any power to the relational algebra. It
is simply more convenient to write r ∩ s than to write
r − (r − s).
Set-Intersection Operation - Example
• List the names of managers who have at least one
dependent.
MGRS(Ssn) ← πMgr_ssn(DEPARTMENT)
EMPS_WITH_DEPS(Ssn) ← πEssn(DEPENDENT)
MGRS_WITH_DEPS ← (MGRS ∩ EMPS_WITH_DEPS)
RESULT ← πLname, Fname(MGRS_WITH_DEPS *
EMPLOYEE)
Binary Relational Operations: JOIN
• JOIN Operation (denoted by )
– The sequence of CARTESIAN PRODECT 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
– This operation is very important for any relational
database with more than a single relation, because it
allows us combine related tuples from various relations
– The general form of a join operation on two relations
R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S
– where R and S can be any relations that result from
general relational algebra expressions.
Binary Relational Operations: JOIN
• 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.
– DEPT_MGR DEPARTMENT MGRSSN=SSN EMPLOYEE
• MGRSSN=SSN is the join condition
– Combines each department record with the employee
who manages the department
– The join condition can also be specified as
DEPARTMENT.MGRSSN= EMPLOYEE.SSN
Example of applying the JOIN operation
125
Extended Relational-Algebra Operations
Generalized Projection
Outer Join
Aggregate Functions and Grouping
Generalized Projection
• Generalized Projection Operation :
• The generalized projection operation extends the
projection operation by allowing functions of attributes
to be included in the projection list.
• The generalized form can be expressed as:
141
Modification of the Database
• In this section, we address how to add, remove, or
change information in the database.
• The content of the database may be modified using the
following operations:
– Deletion
– Insertion
– Updating
• All these operations are expressed using the assignment
operator.
Deletion
• A delete request is expressed similarly to a query,
except instead of displaying tuples to the user, the
selected tuples are removed from the database.
• Can delete only whole tuples; cannot delete values on
only particular attributes
• A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra query.
Deletion Example
• Delete an employee named John Smith from the
employee table.
Employee ← Employee − σ Fname=‗John‘ AND Lname=‗Smith‘
(Employee)
Insertion
• To insert data into a relation, we either:
– specify a tuple to be inserted
– write a query whose result is a set of tuples to be inserted
• The attribute values for inserted tuples must be members of
the attribute‘s domain. Similarly, tuples inserted must be of
the correct arity.
• In relational algebra, an insertion is expressed by:
r r E
where r is a relation and E is a relational algebra expression.
• The insertion of a single tuple is expressed by letting E be a
constant relation containing one tuple.
Insertion Example
• Insert an employee named Richard K Bob to employee
table:
Employee←Employee ∪ {( ‗Richard‘, ‗K‘, ‗Marini‘, ‗653298653‘,
‗1962-12-30‘, ‗98 Oak Forest, Katy, TX‘,
‗M‘, 37000, ‗653298653‘, 4 )}
Updating
• A mechanism to change a value in a tuple without
changing all values in the tuple
• Use the generalized projection operator to do this task
r F1, F2, …, FI, (r)
• Each Fi is either
– the ith attribute of r, if the ith attribute is not
updated, or,
– if the attribute is to be updated Fi is an expression,
involving only constants and the attributes of r,
which gives the new value for the attribute
Update Example
• Example : Consider the relation
EMPLOYEE (Ssn, Salary, Deduction, Years_service)
• Increase the salary of all employees by 25 percent:
Employee Ssn, Salary*1.25, Deduction, Years_service (Employee)
Views
149
Views
• In some cases, it is not desirable for all users to see the entire
logical model (i.e., all the actual relations stored in the
database.)
• Any relation that is not of the conceptual model but is made
visible to a user as a ―virtual relation‖ is called a view.
• A view is a single table that is derived from other tables.
These other tables can be base tables or previously defined
views.
• A view does not necessarily exist in physical form; it is
considered to be a virtual table, in contrast to base tables,
whose tuples are always physically stored in the database.
• This limits the possible update operations that can be applied
to views, but it does not provide any limitations on querying a
view.
Views
• A view is defined using the create view statement
which has the form
create view v as <query expression >
where <query expression> is any legal relational algebra query
expression. The view name is represented by v.
• Once a view is defined, the view name can be used to
refer to the virtual relation that the view generates.
• View definition is not the same as creating a new
relation by evaluating the query expression
– Rather, a view definition causes the saving of an
expression; the expression is substituted into queries
using the view.
View Example
• Consider the relation
Faculty (id, name, sex, salary, address, major_subject, dname )
• WE may want to let a Head of Departments see only
the department faculty rows for own department.
CREATE VIEW Comp_faculty AS
id, name, major_subject, address (σdname=‗Computer‘ (Faculty))
Updates Through View
• There are some situations in which some rows will be
inserted, updated or deleted from view these operations need
to be translated into equivalent operations against the base
table of the view.
• Example : In the above created view we can write
Comp_faculty←Comp_faculty ∪ {( Comp23,‗Richard‘, ‗DBMS‘,
‗98 Oak Forest, Katy, TX‘, )}
• Modifications are generally not permitted on view relations,
except in limited cases.
Views Defined Using Other Views
• One view may be used in the expression defining
another view
• A view relation v1 is said to depend directly on a view
relation v2 if v2 is used in the expression defining v1
• A view relation v1 is said to depend on view relation v2
if either v1 depends directly to v2 or there is a path of
dependencies from v1 to v2
• A view relation v is said to be recursive if it depends
on itself.
Views Defined Using Other Views
• Example : We can define the view DBMS_Comp_faculty
from above created Comp_faculty view as follows
CREATE VIEW DBMS_Comp_Faculty AS
name (σmajor_subject=‗DBMS‘ (Comp_Faculty))
University Questions
• Explain following relational algebra operations with
examples:-
• Generalized Projection (8T- 3M)
• Set Difference (4T-3M)
• Assignment (5T-3M)
• Aggregate. (1T-3M)
• Set Intersection (4T-3M)
• Cartesian product (1T-3M)
• Rename (3T-3M)
• Outer Join (3T-3M)
• Natural Join (6T-3M)
• Division (2T-3M)
• Project (1T-2M) 156
University Questions
• What is view? How it is defined and stored? What are
the benefits and limitations of view? (8T- 10M)
• Explain five relational algebra operators (2T-10M)
157