Class 15_Introduction to Relational Algebra and Relational Calculus_19.11.2021.pptx
1.
UML class diagrams
•Represent classes (similar to entity types) as large rounded boxes with three
sections:
– Top section includes entity type (class) name
– Second section includes attributes
– Third section includes class operations (operations are not in basic ER model)
• Relationships (called associations) represented as lines connecting the classes
– Other UML terminology also differs from ER terminology
• Used in database design and object-oriented software design
Slide 3- 1
Relationships of HigherDegree
• Relationship types of degree 2 are called binary
• Relationship types of degree 3 are called
ternary and of degree n are called n-ary
• In general, an n-ary relationship is not
equivalent to n binary relationships
• Constraints are harder to specify for higher-
degree relationships (n > 2) than for binary
relationships
Slide 3- 4
5.
Discussion of n-aryrelationships (n > 2)
• In general, 3 binary relationships can represent different information than a single
ternary relationship (see Figure 3.17a and b on next slide)
• If needed, the binary and n-ary relationships can all be included in the schema
design (see Figure 3.17a and b, where all relationships convey different meanings)
• In some cases, a ternary relationship can be represented as a weak entity if the
data model allows a weak entity type to have multiple identifying relationships
(and hence multiple owner entity types) (see Figure 3.17c)
Slide 3- 5
Discussion of n-aryrelationships (n > 2)
• If a particular binary relationship can be
derived from a higher-degree relationship at
all times, then it is redundant
• For example, the TAUGHT_DURING binary
relationship in Figure 3.18 (see next slide) can
be derived from the ternary relationship
OFFERS (based on the meaning of the
relationships)
Slide 3- 7
Displaying constraints onhigher-degree relationships
• The (min, max) constraints can be displayed on the edges – however, they do not
fully describe the constraints
• Displaying a 1, M, or N indicates additional constraints
– An M or N indicates no constraint
– A 1 indicates that an entity can participate in at most one relationship instance that
has a particular combination of the other participating entities
• In general, both (min, max) and 1, M, or N are needed to describe fully the
constraints
Slide 3- 9
10.
Data Modeling Tools
•A number of popular tools that cover conceptual modeling and mapping into
relational schema design.
– Examples: ERWin, S- Designer (Enterprise Application Suite), ER- Studio, etc.
• POSITIVES:
– Serves as documentation of application requirements, easy user interface - mostly
graphics editor support
• NEGATIVES:
– Most tools lack a proper distinct notation for relationships with relationship attributes
– Mostly represent a relational design in a diagrammatic form rather than a conceptual
ER-based design
(See Chapter 12 for details)
Slide 3- 10
11.
Slide 3- 11
Someof the Currently Available Automated
Database Design Tools
COMPAN
Y
TOOL FUNCTIONALITY
Embarcad
ero
Technolo
gies
ER Studio Database Modeling in ER and
IDEF1X
DB Artisan Database administration, space and
security management
Oracle Developer
2000/Designer 2000
Database modeling, application
development
Popkin
Software
System Architect
2001
Data modeling, object modeling,
process modeling, structured
analysis/design
Platinum
(Compute
r
Associate
s)
Enterprise Modeling
Suite: Erwin, BPWin,
Paradigm Plus
Data, process, and business
component modeling
Persisten
ce Inc.
Pwertier Mapping from O-O to relational
model
Rational
(IBM)
Rational Rose UML Modeling & application
generation in C++/JAVA
Resolutio Xcase Conceptual modeling up to code
12.
Extended Entity-Relationship (EER)Model (in
next chapter)
• The entity relationship model in its original form
did not support the specialization and
generalization abstractions
• Next chapter illustrates how the ER model can
be extended with
– Type-subtype and set-subset relationships
– Specialization/Generalization Hierarchies
– Notation to display them in EER diagrams
Slide 3- 12
14.
Chapter Summary
• ERModel Concepts: Entities, attributes,
relationships
• Constraints in the ER model
• Using ER in step-by-step conceptual schema
design for the COMPANY database
• ER Diagrams - Notation
• Alternative Notations – UML class diagrams,
others
Slide 3- 14
19.
What is RelationalModel?
Relational Model (RM) represents the database as a
collection of relations. A relation is nothing but a table of
values. Every row in the table represents a collection of
related data values. These rows in the table denote a real-
world entity or relationship.
The table name and column names are helpful to
interpret the meaning of values in each row. The data are
represented as a set of relations. In the relational model,
data are stored as tables. However, the physical storage of
the data is independent of the way the data are logically
organized.
20.
Relational Model Conceptsin DBMS
1.Attribute: Each column in a Table. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME,etc.
2.Tables – In the Relational model the, relations are saved in the table format. It is
stored along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
3.Tuple – It is nothing but a single row of a table, which contains a single record.
4.Relation Schema: A relation schema represents the name of the relation with its
attributes.
5.Degree: The total number of attributes which in the relation is called the degree
of the relation.
6.Cardinality: Total number of rows present in the Table.
7.Column: The column represents the set of values for a specific attribute.
8.Relation instance – Relation instance is a finite set of tuples in the RDBMS
system. Relation instances never have duplicate tuples.
9.Relation key – Every row has one, two or multiple attributes, which is called
relation key.
10.Attribute domain – Every attribute has some pre-defined value and scope
which is known as attribute domain
22.
Relational Integrity Constraints
RelationalIntegrity constraints in DBMS are referred to
conditions which must be present for a valid relation.
These Relational constraints in DBMS are derived from the
rules in the mini-world that the database represents.
There are many types of Integrity Constraints in DBMS.
Constraints on the Relational database management
system is mostly divided into three main categories are:
1.Domain Constraints
2.Key Constraints
3.Referential Integrity Constraints
23.
Domain Constraints
Domain constraintscan be violated if an
attribute value is not appearing in the
corresponding domain or it is not of the
appropriate data type.
Domain constraints specify that within each
tuple, and the value of each attribute must be
unique. This is specified as data types which
include standard data types integers, real
numbers, characters, Booleans, variable length
strings, etc.
Example:
Create DOMAIN CustomerName CHECK (value not NULL)
24.
Key Constraints
An attributethat can uniquely identify a tuple in a relation is called
the key of the table. The value of the attribute for different tuples
in the relation has to be unique.
Example:
In the given table, CustomerID is a key attribute of Customer
Table. It is most likely to have a single key for one customer,
CustomerID =1 is only for the CustomerName =” Google”.
CustomerID CustomerName Status
1 Google Active
2 Amazon Active
3 Apple Inactive
25.
Referential Integrity Constraints
ReferentialIntegrity constraints in DBMS are based on the concept of Foreign
Keys. A foreign key is an important attribute of a relation which should be
referred to in other relationships. Referential integrity constraint state happens
where relation refers to a key attribute of a different or same relation.
However, that key element must exist in the table.
Example:
49.
Relational Algebra Operations
•Relational Algebra Operations From Set
Theory
– UNION (U), INTERSECTION , DIFFERENCE (or MINUS, –),
CARTESIAN PRODUCT ( x)
• Unary Relational Operations
Relationship between occurences of same entity
set
– Select (σ)
– Project (Π)
– RENAME (ρ)
Outline: Relational Algebra
UnaryRelational Operations
Relational Algebra Operations From Set
Theory
Binary Relational Operations
Additional Relational Operations
Examples of Queries in Relational Algebra
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)
70.
Relational Algebra Overview(continued)
• The algebra operations thus produce new
relations
• A sequence of relational algebra operations
forms a relational algebra expression, which
represents result of a database query.
Note: Though most commercial relational database
does not provide user interface for relational
algebra queries, functions in internal modules
are based on relational algebra operations.
Relational Algebra Operations
•Relational Algebra Operations From Set
Theory
– UNION (U), INTERSECTION , DIFFERENCE (or MINUS, –),
CARTESIAN PRODUCT ( x)
• Unary Relational Operations
Relationship between occurences of same entity
set
– Select (σ)
– Project (Π)
– RENAME (ρ)
73.
• Binary RelationalOperations:-
Operations on two tables
– JOIN (several variations of JOIN exist)
– DIVISION
• Additional Relational Operations:
– OUTER JOINS, OUTER UNION
– AGGREGATE FUNCTIONS ( SUM, COUNT, AVG,
MIN, MAX)
Relational Algebra Overview
74.
Unary Relational Operations:SELECT
• Denoted by σ(sigma))
• 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
75.
SELECT
• The selectoperation is denoted by
Eg: R : Employee ; selection condition: Salary > 30000
• 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.
SELECT Example
• Selectthe 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)
77.
SELECT properties
• TheSELECT operation produces a relation S that has the
same schema (same attributes) as R
• SELECT σ is commutative:
78.
• A cascade(sequence) of SELECT operations may
be applied in any order:
• A cascade of SELECT operations may be replaced
by a single selection with a conjunction of all the
conditions:
SELECT PROPERTIES(contd.)
79.
• Select theemployees who either work in department
4 and earn salary above 25000 or work in department
4 and earn salary above 30000
• σ (DNO = 4 AND salary>25000) OR (DNO = 4 AND salary>30000)
(EMPLOYEE)
SELECT (contd.)
80.
PROJECT
• PROJECT Operationis 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
81.
PROJECT (cont.)
• Thegeneral form of the project operation is:
• <attribute list> is the desired list of attributes from
relation R.
• The projectoperation 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
PROJECT (cont.)
84.
PROJECT Operation Properties
•PROJECT
– The number of tuples in the result of projection
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
Relational Algebra Expressions
•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.
Single expression versussequence 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
• Method1:
89.
Retrieve the firstname, last name, and salary of all employees who work in department number 5,
we must apply a select and a project operation
• Method2:
90.
RENAME
• The RENAMEoperator is denoted by ρ(rho)
• Useful when a query requires multiple
operations
• Necessary in some cases (we shall see JOIN
operation later)
Topics covered sofar
• Unary Relational Operations
Relationship between occurrences of same entity set
– Select (σ)
– Project (Π)
– RENAME (ρ)
• Relational Algebra Operations From Set Theory
– UNION (U), INTERSECTION , DIFFERENCE (or MINUS, –),
CARTESIAN PRODUCT ( x)
112.
Review- Objective: Retrievethe name of each
female employee’s dependent
Approach: 1. Do Cartesian product (x) between tables
2. Select only rows that satisfy the condition
of Ssn = Essn value
Takeaway
• We usedCartesian product to obtain all possible
combinations of Employee and Dependents
• We then filtered out using SELECT function,
since there was a common field SSN
(Essn of second table is a foreign key, since derived
from ssn of first table)
• Can we combine the two steps into one?
• Yes, by making use of concept of JOINS
Minimum criteria forusing JOINS
• There must be a need to combine two or more
tables
• There must be a common variable between the
two tables, which helps to combine related rows.
• If there are 5 attributes in table 1, 3 attributes in
table 2, how many attributes would be there in
resultant table (Basic version of join)?
Ans: 8
118.
Scenario:
• In theCOMPANY Database , we want to
retrieve the manager of each department.
How to do so?
NATURAL JOIN Operation
Objective:We intend to join DEPARTMENT and DEPT_LOCATIONS
For natural join to be done between two tables,
what’s the first and foremost condition?
Ans: Attribute (column) with the same name
Practice/assignment
1.Find the SSNof employees who work on all
the project controlled by Department No 5
2. List the name of all employees with two or
more dependents.
3. List the name of employees who has atleast
one dependent
4. Retrieve the name of employee who has no
dependent
• For everyproject located in ‘Stafford’, retrieve the
project number, the controlling department number,
and the department manager’s last name, address,
and birth date
Additional Relational Operations
RecursiveClosure
• Recursive closure
– This operation is applied to a recursive
relationship
• Example of a recursive operation
– retrieve all SUPERVISEES of an EMPLOYEE e at all
levels
142.
Recursive Closure
• Borgsupervises - Wong , Wallace
• Wong supervises –Smith ,Narayan and English
• Wallace supervises –Zelaya and Jabbar
Retrieve the superviseesof ‘James Borg’ at all levels
BORG_SSN ← πSsn(
σ
Fname=‘James’ AND Lname=‘Borg’(EMPLOYEE))
SUPERVISION(Ssn1, Ssn2) ← πSsn,Super_ssn(EMPLOYEE)
146.
Retrieve the superviseesof ‘James Borg’ at all levels
RESULT ← RESULT2 RESULT1
∪
RESULT2(Ssn) ← πSsn1(SUPERVISION X Ssn2=Ssn RESULT1)
RESULT1(Ssn) ← πSsn1(SUPERVISION X Ssn2=Ssn BORG_SSN)
147.
Additional Relational Operations:The OUTER
JOIN
• In NATURAL JOIN and EQUIJOIN, tuples without a
matching(or related) tuple are eliminated from the
join result
• Tuples with null in the join attributes are also
eliminated
• This amounts to loss of information
• To keep all tuples from both relation,outer join is
used.
OUTER UNION Example
•STUDENT(Name, SSN, Department, Advisor)
• INSTRUCTOR(Name, SSN, Department, Rank).
Name SSN Depart
ment
Adviso
r
Name SSN Depart
ment
Rank
STUDENT INSTRUCTOR
155.
OUTER UNION Example
•The result relation STUDENT_OR_INSTRUCTOR
will have the following attributes:
• STUDENT_OR_INSTRUCTOR
Name SSN Department Advisor Rank
ER-to-Relational Mapping Algorithm
•How is it useful in creating the actual tables
and build the database ?
• How do we create tables using the relations
and the relationship constraints e etc..
160.
Mapping
• ER-to-Relational MappingAlgorithm
• 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.
162.
Step 1: Mappingof Regular Entity Types
• For each regular (strong) entity type E in the ER
diagram, create a relation R that includes all the
simple attributes of E.
• Choose one of the key attribute as primary key
Employee
Fname Minit Lname SSN Bdate Address Sex Salary
163.
Step 2: Mappingof Weak Entity Types
• For each weak entity type W in the ER schema with owner
entity type E, create a relation R & include all simple attributes
(or simple components of composite attributes) of W as
attributes of R.
• Include as foreign key attributes of R the primary key
attribute(s) of the relation(s) that correspond to the owner
entity type(s).
• The primary key of R is the combination of the primary key(s)
of the owner(s) and the partial key of the weak entity type W.
164.
Step 2: Mappingof Weak Entity Types
The primary key of the DEPENDENT relation is the
combination {ESSN, DEPENDENT_NAME}
SSN: Primary key of Owner entity EMPLOYEE
Dependent_name: Partial key of weak entity
DEPENDENT
165.
Step 3: Mappingof Binary 1:1 Relation Types
• There are three possible approaches:
• 1.Foreign Key approach: Identify the relations
T and S. Include a foreign key in S the primary
key of T.
• Choose an entity type with total participation
in R in the role of S.
– Example: 1:1 relation MANAGES is mapped by
choosing the participating entity type
DEPARTMENT because its participation in the
MANAGES relationship type is total.
2.Merged relation option:Merge the two entity
types and the relationship into a single relation. when
both participations are total.
3.Cross-reference or relationship relation
option: Set up a third relation R for the purpose of
cross-referencing the primary keys of the two relations
S and T representing the entity types
Step 3: Mapping of Binary 1:1 Relation Types
168.
Step 4: Mappingof Binary 1:N Relationship
Types
• For each regular binary 1:N relationship type R,
identify the relation S that represent the participating
entity type at the N-side of the relationship type
• Include as foreign key in S the primary key of the
relation T that represents the other entity type
participating in R
• Include any simple attributes of the 1:N relation type
as attributes of S
169.
Step 4: Mappingof Binary 1:N Relation Types
Fname Minit Lname SSN Bdate Addres
s
Sex Salary Super_
ssn
Dno
Dname Dnumber Mgr_ssn Mgr_start_date
Employee
Department
Pname Pnumber Plocation Dnum
Project
170.
Step 5: Mappingof Binary M:N Relationship Types
• For each regular binary M:N relationship type R, create
a new relation S to represent R.
• Include as foreign key attributes in S the primary keys of
the relations that represent the participating entity
types
• Also include any simple attributes of the M:N
relationship type (or simple components of composite
attributes) as attributes of S.
Step 6: Mappingof Multivalued attributes.
• For each multivalued attribute A, create a new relation
R.
• This relation R will include an attribute corresponding
to A, plus the primary key attribute K-as a foreign key in
R-of the relation
• The primary key of R is the combination of A and K. If
the multivalued attribute is composite, we include its
simple components.
173.
• Example: Therelation DEPT_LOCATIONS is
created.
• The primary key of R is the combination of
{DNUMBER, DLOCATION}.
Dname Dnumber Mgr_ssn Mgr_start_date
Dnumber Dlocation
Department
Dept_locations
Step 6: Mapping of Multivalued attributes.
Step 7: Mappingof N-ary Relationship Types.
• For each n-ary relationship type R, where n>2, create a
new relationship S to represent R.
• Include as foreign key attributes in S the primary keys
of the relations that represent the participating entity
types.
• Also include any simple attributes of the n-ary
relationship type (or simple components of composite
attributes) as attributes of S.
176.
• Example: Therelationship type SUPPY
• This can be mapped to the relation SUPPLY shown in
the relational schema, whose primary key is the
combination of the three foreign keys {SNAME,
PARTNO, PROJNAME}
Step 7: Mapping of N-ary Relationship Types.
Test1
1.Explain the advantagesof DBMS over Traditional File system
2.Discuss about different DBMS users.
3.Explain three schema architecture with proper diagram
4.What is data model? Explain the difference between logical
data and physical data independence.
5.With a proper diagram explain component module of DBMS
6.Discuss different DBMS languages.
SQL INNER JOINKeyword
• The INNER JOIN keyword return rows when
there is at least one match in both tables
SELECT column_name(s)
FROM table_name1 INNER JOIN table_name2
ON table_name1.column_name =
table_name2.column_name
Important Questions
1.Discuss thecharacteristics of a relation.
2.Explain entity integrity constraint.
3.Explain How referential integrity constraint can be implemented?
4.What is foreign key? Can a foreign key exist for a single table?
5.Explain briefly SELECT,PROJECT and RENAME operation.
6.Explain briefly UNION,INTERSECTION and SET DIFERRENCE
with example.
7.Discuss with example ER to relational mapping transformation.
8.Discuss all possible options that can be specified when
referential integrity constraint is violated with proper example.
9. How assertion and trigger is defined in SQL. Give some example.
195.
Important Questions
10.How aview is defined? What are the problems associated with
updating a view?
11.Explain embedded SQL ,Dynamic SQL and SQL/PSM with proper
example
12. Discuss about stored procedure with example
13.How will you connect to a database to fetch records and display.
14.Why Cursor is required?
15. What is SQL CODE and SQL STATE?
16. What is impedance mismatch problem?
17.What are the different DB programming approach?
#25 In the above example, we have 2 relations, Customer and Billing.
Tuple for CustomerID =1 is referenced twice in the relation Billing. So we know CustomerName=Google has billing amount $300
#27 the relational model represents the database as a collection of relations , each relation resembles a table of values each row in the table represents a collection of related data values , all values in a column are of the same data type, terminology, a row is called a tuple, a column header is called an attribute the table is called a relation the data type describing the types of values that can appear in each column is represented by a domain.
#28 The set of ten digit phone numbers valid in the united states. Academic department names,employee ages.