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
UML class diagram for COMPANY database schema
Slide 3- 2
Other alternative diagrammatic notations
Slide 3- 3
Relationships of Higher Degree
• 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
Discussion of n-ary relationships (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
Example of a ternary relationship
Slide 3- 6
Discussion of n-ary relationships (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
Another example of a ternary relationship
Slide 3- 8
Displaying constraints on higher-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
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
Slide 3- 11
Some of 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
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
Chapter Summary
• ER Model 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
What is Relational Model?
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.
Relational Model Concepts in 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
Relational Integrity Constraints
Relational Integrity 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
Domain Constraints
Domain constraints can 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)
Key Constraints
An attribute that 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
Referential Integrity Constraints
Referential Integrity 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:
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 (ρ)
Relational Algebra and
Relational Calculus
Module 2
Outline: Relational Algebra
Unary Relational Operations
Relational Algebra Operations From Set
Theory
Binary Relational Operations
Additional Relational Operations
Examples of Queries in Relational Algebra
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)
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 vs Relational Calculus
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 (ρ)
• Binary Relational Operations:-
 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
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
SELECT
• The select operation 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
SELECT Example
• 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)
SELECT properties
• The SELECT operation produces a relation S that has the
same schema (same attributes) as R
• SELECT σ is commutative:
• 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.)
• Select the employees 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.)
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
PROJECT (cont.)
• The general form of the project operation is:
• <attribute list> is the desired list of attributes from
relation R.
PROJECT example
• List each employee‘s first and last name and
salary
• 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
PROJECT (cont.)
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
• PROJECT is not commutative
PROJECT (contd.)
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.
Review
• Types of Relational Algebra Expressions
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
• Method1:
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
• Method2:
RENAME
• The RENAME operator is denoted by ρ(rho)
• Useful when a query requires multiple
operations
• Necessary in some cases (we shall see JOIN
operation later)
RENAME (contd.)
RENAME (contd.)
Relational Algebra operation from Set theory:
UNION
UNION Example
Retrieve Ssn Number of all employees who
either work in department 5 or directly
supervise an employee who works in
department 5
INTERSECTION
SET DIFFERENCE (cont.)
STUDENT UNION INSTRUCTOR
Two tables have same number of attributes and domain are also same
STUDENT INTERSECTION INSTRUCTOR
STUDENT –INSTRUCTOR
INSTRUCTOR –STUDENT
Properties of UNION, INTERSECT, and
DIFFERENCE
Example table
CARTESIAN PRODUCT
CARTESIAN PRODUCT
Retrieve the name of each female
employee’s dependent
CARTESIAN PRODUCT (cont.)
Actual_dependents σ ssn = Essn(Emp_dependents)
Topics covered so far
• 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)
Review- Objective: Retrieve the 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
Actual_dependents σ ssn = Essn(Emp_dependents)
Takeaway
• We used Cartesian 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
Summarization
Earlier Approach: (Cartesian product+Select)
Efficient Approach: (Using concept of Joins)
Actual_dependents  EMPNAMES JOIN DEPENDENT
(ssn = Essn)
Actual_dependents σ ssn = Essn(Emp_dependents)
Minimum criteria for using 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
Scenario:
• In the COMPANY Database , we want to
retrieve the manager of each department.
How to do so?
JOIN
Some properties of JOIN
Some properties of JOIN
EQUIJOIN
NATURAL JOIN Operation
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
Natural Join between DEPARTMENT and
DEPT_LOCATIONS
DIVISION
Retrieve the SSN of employees who work on all project that john smith works
DIVISION
SMITH ← σFname=‘John’ AND Lname=‘Smith’(EMPLOYEE)
SMITH_PNOS ← πPno(WORKS_ON XEssn= Ssn SMITH)
SSN_PNOS ← πEssn, Pno(WORKS_ON)
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
DIVISION T<---- R ÷ S
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
Practice/assignment
1.Find the SSN of 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
Query Tree Notation
• For every project 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:
Aggregate Functions and Grouping
Aggregate Function Operation
Grouping with Aggregation
Examples
Aggregate functions and grouping SELECT
Additional Relational Operations
Recursive Closure
• 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
Recursive Closure
• Borg supervises - Wong , Wallace
• Wong supervises –Smith ,Narayan and English
• Wallace supervises –Zelaya and Jabbar
Retrieve the supervisees of ‘James Borg’ at all levels
Retrieve the supervisees of ‘James Borg’ at all levels
BORG_SSN ← πSsn(
σ
Fname=‘James’ AND Lname=‘Borg’(EMPLOYEE))
SUPERVISION(Ssn1, Ssn2) ← πSsn,Super_ssn(EMPLOYEE)
Retrieve the supervisees of ‘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)
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.
The OUTER JOIN Operation
The OUTER JOIN Operation
Loss of Data
TEMP ← (EMPLOYEE XSsn=Mgr_ssn DEPARTMENT)
The LEFT OUTER JOIN Operation
OUTER UNION Operations
OUTER UNION Operations
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
OUTER UNION Example
• The result relation STUDENT_OR_INSTRUCTOR
will have the following attributes:
• STUDENT_OR_INSTRUCTOR
Name SSN Department Advisor Rank
OUTER UNION Example
Examples of Queries in Relational Algebra :
Procedural Form
Examples of Queries in Relational Algebra –
Single expressions
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..
Mapping
• 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.
Step 1: Mapping of 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
Step 2: Mapping of 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.
Step 2: Mapping of 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
Step 3: Mapping of 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.
Step 3: Mapping of Binary 1:1 Relation Types
Fname Minit Lname SSN Bdate Address Sex Salary
Dname Dnumber Mgr_ssn Mgr_start_date
Employee
Department
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
Step 4: Mapping of 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
Step 4: Mapping of 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
Step 5: Mapping of 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 5: Mapping of Binary M: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
Works_on
ESSN Pno Hours
Step 6: Mapping of 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.
• Example: The relation 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.
Final Mapping
Step 7: Mapping of 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.
• Example: The relationship 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.
The SUPPLY relationship
Mapping the n-ary relationship type SUPPLY
Summary of Mapping constructs and
constraints Table
END OF UNIT 3
Test1
1.Explain the advantages of 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.
More Examples
SQL INNER JOIN Keyword
• 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
SQL INNER JOIN Example
SQL LEFT JOIN (LEFT OUTER JOIN)
SQL LEFT JOIN contd…
SQL RIGHT JOIN (RIGHT OUTER JOIN )
SQL RIGHT JOIN (RIGHT OUTER JOIN ) contd…
SQL FULL JOIN
SQL FULL JOIN
SQL FULL JOIN contd
Important Questions
1.Discuss the characteristics 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.
Important Questions
10.How a view 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?
In-Class Exercise

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
  • 2.
    UML class diagramfor COMPANY database schema Slide 3- 2
  • 3.
    Other alternative diagrammaticnotations Slide 3- 3
  • 4.
    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
  • 6.
    Example of aternary relationship Slide 3- 6
  • 7.
    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
  • 8.
    Another example ofa ternary relationship Slide 3- 8
  • 9.
    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 (ρ)
  • 67.
  • 68.
    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.
  • 71.
    Relational Algebra vsRelational Calculus
  • 72.
    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.
  • 82.
    PROJECT example • Listeach employee‘s first and last name and salary
  • 83.
    • 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
  • 85.
    • PROJECT isnot commutative PROJECT (contd.)
  • 86.
    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.
  • 87.
    Review • Types ofRelational Algebra Expressions
  • 88.
    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)
  • 91.
  • 92.
  • 93.
    Relational Algebra operationfrom Set theory: UNION
  • 94.
    UNION Example Retrieve SsnNumber of all employees who either work in department 5 or directly supervise an employee who works in department 5
  • 96.
  • 97.
  • 98.
    STUDENT UNION INSTRUCTOR Twotables have same number of attributes and domain are also same
  • 99.
  • 100.
  • 101.
  • 102.
    Properties of UNION,INTERSECT, and DIFFERENCE
  • 103.
  • 104.
  • 105.
  • 106.
    Retrieve the nameof each female employee’s dependent
  • 109.
  • 110.
    Actual_dependents σ ssn= Essn(Emp_dependents)
  • 111.
    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
  • 114.
    Actual_dependents σ ssn= Essn(Emp_dependents)
  • 115.
    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
  • 116.
    Summarization Earlier Approach: (Cartesianproduct+Select) Efficient Approach: (Using concept of Joins) Actual_dependents  EMPNAMES JOIN DEPENDENT (ssn = Essn) Actual_dependents σ ssn = Essn(Emp_dependents)
  • 117.
    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?
  • 119.
  • 121.
  • 122.
  • 123.
  • 125.
  • 126.
    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
  • 127.
    Natural Join betweenDEPARTMENT and DEPT_LOCATIONS
  • 129.
    DIVISION Retrieve the SSNof employees who work on all project that john smith works
  • 130.
    DIVISION SMITH ← σFname=‘John’AND Lname=‘Smith’(EMPLOYEE) SMITH_PNOS ← πPno(WORKS_ON XEssn= Ssn SMITH) SSN_PNOS ← πEssn, Pno(WORKS_ON) SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
  • 131.
  • 132.
  • 133.
    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
  • 134.
  • 135.
    • For everyproject located in ‘Stafford’, retrieve the project number, the controlling department number, and the department manager’s last name, address, and birth date
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
    Aggregate functions andgrouping SELECT
  • 141.
    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
  • 143.
    Retrieve the superviseesof ‘James Borg’ at all levels
  • 145.
    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.
  • 148.
    The OUTER JOINOperation
  • 149.
    The OUTER JOINOperation
  • 150.
    Loss of Data TEMP← (EMPLOYEE XSsn=Mgr_ssn DEPARTMENT)
  • 151.
    The LEFT OUTERJOIN Operation
  • 152.
  • 153.
  • 154.
    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
  • 156.
  • 157.
    Examples of Queriesin Relational Algebra : Procedural Form
  • 158.
    Examples of Queriesin Relational Algebra – Single expressions
  • 159.
    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.
  • 166.
    Step 3: Mappingof Binary 1:1 Relation Types Fname Minit Lname SSN Bdate Address Sex Salary Dname Dnumber Mgr_ssn Mgr_start_date Employee Department
  • 167.
    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.
  • 171.
    Step 5: Mappingof Binary M: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 Works_on ESSN Pno Hours
  • 172.
    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.
  • 174.
  • 175.
    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.
  • 177.
  • 178.
    Mapping the n-aryrelationship type SUPPLY
  • 179.
    Summary of Mappingconstructs and constraints Table
  • 180.
  • 181.
    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.
  • 182.
  • 183.
    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
  • 184.
  • 185.
    SQL LEFT JOIN(LEFT OUTER JOIN)
  • 187.
    SQL LEFT JOINcontd…
  • 188.
    SQL RIGHT JOIN(RIGHT OUTER JOIN )
  • 190.
    SQL RIGHT JOIN(RIGHT OUTER JOIN ) contd…
  • 191.
  • 192.
  • 193.
  • 194.
    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?
  • 196.

Editor's Notes

  • #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.