Unit 2
Unit 2
Syllabus: Relational Model: Structure of relational databases, Domains, Relations, Relational algebra
fundamental operators and syntax, relational algebra queries, Entity-Relationship model :Basic concepts,
Design process, constraints, Keys, Design issues, E-R diagrams, weak entity sets, extended E-R features –
generalization, specialization and aggregation
---------------------------------------------------------------------------------------------------------------------------------------------
Course Objective: To study of different data model and conceptual design using ER diagram
---------------------------------------------------------------------------------------------------------------------------------------------
Course Outcome (CO 2): Analyze the physical and logical database designs, database modelling, relational,
hierarchical, and network models
---------------------------------------------------------------------------------------------------------------------------------------------
Unit II
Relational Model: The relational model 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.
Relations: In relational data model, relations are saved in the format of Tables. This format stores the
relation among entities. A table has rows and columns, where rows represents records and columns
represent the attributes.
Tuple: A single row of a table, which contains a single record for that relation is called a tuple.
Relation instance: A finite set of tuples in the relational database system represents relation instance.
Relation instances do not have duplicate tuples.
Relation schema: A relation schema describes the relation name (table name), attributes, and their
names.
Relation key: Each row has one or more attributes, known as relation key, which can identify the row
in the relation (table) uniquely.
Attribute domain: Every attribute has some pre-defined value scope, known as attribute domain.
Relational Algebra: Relational algebra is a procedural query language, which takes instances of relations as
input and yields instances of relations as output. It uses operators to perform queries. An operator can be
either unary or binary. They accept relations as their input and yield relations as their output. Relational
algebra is performed recursively on a relation and intermediate results are also considered relations.
In the formal relational model terminology, a row is called a tuple, a column header is called an attribute,
and 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 of possible values. We now define these terms—domain, tuple,
attribute, and relation formally.
A domain D is a set of atomic values. By atomic we mean that each value in the domain is indivisible as far
as the formal relational model is concerned. A common method of specifying a domain is to specify a data
type from which the data values forming the domain are drawn. It is also useful to specify a name for the
domain, to help in interpreting its values. Some examples of domains follow:
● Usa_phone_numbers. The set of ten-digit phone numbers valid in the United States.
● Local_phone_numbers. The set of seven-digit phone numbers valid within a particular area code in
the United States. The use of local phone numbers is quickly becoming obsolete, being replaced by
standard ten-digit numbers.
A relation schema R, denoted by R(A 1 , A 2 , ..., A n ), is made up of a relation name R and a list of
attributes, A 1 , A 2 , ..., A n . Each attribute A i is the name of a role played by some domain D in the
relation schema R. D is called the domain of A i and is denoted by dom (A i ). A relation schema is used to
describe a relation R is called the name of this relation. The degree (or arity) of a relation is the number of
attributes n of its relation schema.
Using the datatype of each attribute, the definition is sometimes written as:
STUDENT (Name: string, Ssn: string, Homophone: string, Address: string, Office phone: string, Age: integer,
Gpa: real)
Characteristics of Relations
Ordering of Tuples in a Relation. A relation is defined as a set of tuples. Mathematically, elements of a set
have no order among them hence, tuples in a relation do not have any particular order.
Ordering of Values within a Tuple and an Alternative Definition of a Relation. According to the preceding
definition of a relation, an n-tuple is an ordered list of n values, so the ordering of values in a tuple and
hence of attributes in a relation schema is important.
Values and NULLs in the Tuples. Each value in a tuple is an atomic value that is, it is not divisible into
components within the framework of the basic relational model. Hence, composite and multivalued
attributes are not allowed. An important concept is that of NULL values, which are used to represent the
values of attributes that may be unknown or may not apply to a tuple.
Domain Constraints
Domain constraints specify that within each tuple, the value of each attribute A must be an atomic value
from the domain dom(A).
A super key SK specifies a uniqueness constraint that no two distinct tuples in any state r of R can have the
same value for SK. Every relation has at least one default super key—the set of all its attributes. A super
key can have redundant attributes, however, so a more useful concept is that of a key, which has no
redundancy.
A relation schema may have more than one key. In this case, each of the keys is called a candidate key. For
example, the CAR relation has (Licence_no Eng_sr_no Model Make_year model) two candidate keys:
License_number and Engine_serial_number . It is common to designate one of the candidate keys as the
primary key of the relation. This is the candidate key whose values are used to identify tuples in the
relation.
The definitions and constraints we have discussed so far apply to single relations and their attributes. A
relational database usually contains many relations, with tuples in relations that are related in various
ways. In this section, we define a relational database and a relational database schema.
A relational database schema S is a set of relation schemas S = {R 1, R 2, ..., R m} and a set of integrity
constraints IC. A relational database state 10 DB of S is a set of relation states DB = {r 1, r 2, ..., r m} such
that each r i is a state of R i and such that the r i relation states satisfy the integrity constraints specified in
IC. Below Figure shows a relational database schema that we call COMPANY = {EMPLOYEE, DEPARTMENT,
DEPT_LOCATIONS, PROJECT, WORKS_ON , DEPENDENT }. The underlined attributes represent primary
keys.
EMPLOYEEE
Fname Minit Lname Ssn Bdata Address Sex Salary Super_ssn Dno
DEPARTMENT
DEPT_ LOCATIONS
Dnumber DLocation
PROJECT
The entity integrity constraint states that no primary key value can be NULL. This is because the primary
key value is used to identify individual tuples in a relation. Having NULL values for the primary key implies
that we cannot identify some tuples. For example, if two or more tuples had NULL for their primary keys,
we may not be able to distinguish them if we try to reference them from other relations.
The referential integrity constraint is specified between two relations and is used to maintain the
consistency among tuples in the two relations. Informally, the referential integrity constraint states that a
tuple in one relation that refers to another relation must refer to an existing tuple in that relation.
Figure 2.3: Primary & Referential Integrity
The relational algebra is essential for several reasons. First, it provides a formal foundation for relational
model operations. Second, and perhaps more important, it is used as a basis for implementing and
optimizing queries in the query processing and optimization modules that are integral parts of relational
database management systems (RDBMSs).
BASIS FOR
COMPARISO RELATIONAL ALGEBRA RELATIONAL CALCULUS
N
States Relational Algebra states how to obtain Relational Calculus states what result
the result. we have to obtain.
Order Relational Algebra describes the order in Relational Calculus does not specify
which operations have to be performed. the order of operations.
Notation − σ(r)
Where σ stands for selection predicate and r stands for relation. p is propositional logic formula which may
use connectors like and, or, and not. These terms may use relational operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those books published
after 2010.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
r ∪ s = { t | t ∈ r or t ∈ s}
It performs binary union between two given relations and is defined as −
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
Output − Projects the names of the authors who have either written a book or an article or both.
Notation − r − s
Finds all the tuples that are present in r but not in s.
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tt'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tt.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
● Set intersection
● Assignment
● Natural join
We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE
■ UNION: The result of this operation, denoted by R ∪ S, is a relation that includes all tuples that are either
on two union-compatible relations R and S as follows:
The standard definition of NATURAL JOIN requires that the two join attributes (or each pair of join
attributes) have the same name in both relations. If this is not the case, a renaming operation is applied
first.
PROJ_DEP DEPT_LOCS ← DEPARTMENT * DEPT_LOCATIONS.
PURPOSE
SELECT Selects all tuples that satisfy the selection condition from a σ<selection
relation R. condition>(R)
PROJECT Produces a new relation with only some of the attributes of R, π<attribute
and removes duplicate tuples. list>(R)
THETA JOIN Produces all combinations of tuples from R and R1 2 that R1 <join
satisfy the join condition. condition> R2
Produces all the combinations of tuples from R1 and R2 that
EQUIJOIN satisfy a join condition with only equality comparisons. R1 <join
condition> R2, OR
Same as EQUIJOIN except that the join attributes of R2 are not R1*<join
NATURAL included in the resulting relation; if the join attributes have the condition> R2, OR
JOIN same names, they do not have to be specified at all. R1* (<join
R1 ∪ R2
Produces a relation that includes all the tuples in R1 or R2 or
UNION both R1 and R2; R1 and R2 must be union-compatible.
Produces a relation that includes all the tuples in both R1 and R2;
INTERSECTI R1 and R2 must be union-compatible. R1 ∩ R2
ON
Produces a relation that includes all the tuples in R1 that are not
DIFFERENCE in R2; R1 and R2 must be union-compatible. R1 – R2
Query 1. Retrieve the name and address of all employees who work for the ‘Research’ department.
Query 2. For every project located in ‘Stafford’, list the project number, the controlling department
number, and the department manager’s last name, address, and birth date.
RESULT ← π Pnumber,Dnum,Lname,Address,Bdate(PROJ_DEPT_MGRS)
Query 3. Find the names of employees who work on all the projects controlled by department number 5.
DEPT5_PROJS ← ρ(Pno)(πPnumber(σDnum=5(PROJECT)))
EMP_PROJ ← ρ(Ssn, Pno)(πEssn, Pno(WORKS_ON))
RESULT_EMP_SSNS ← EMP_PROJ ÷ DEPT5_PROJS
RESULT ← πLname, Fname(RESULT_EMP_SSNS * EMPLOYEE)
Query 4. List the names of all employees with two or more dependents.
Strictly speaking, this query cannot be done in the basic (original) relational algebra. We have to use the
AGGREGATE FUNCTION operation with the COUNT aggregate function. We assume that dependents of the
same employee have distinct Dependent_name values.
ƺ
T1(Ssn, No_of_Dependents)← Essn COUNT Dependent_name(DEPENDENT)
T2 ← σ No_of Dependent ≥2(T1)
RESULT ← π Lname, Fname(T2* EMPLOYEE)
ID Name Dept_name Salary
1010 Shrinivasan Comp.sci 65000
1
1212 Wu Finance 90000
1
1515 Mozart Music 40000
1
2222 Einstin Physics 95000
2
Instructor Relation
instructor relation where the instructor is in the “Physics”
department, we write:
dept name =“Physics” (instructor )
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that is, it tells
what to do but never explains how to do it.
Notation − {T | Condition}
For example −
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
Notation −
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ TP ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TP where subject is database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also involves
relational operators.
The expression power of Tuple Relation Calculus and Domain Relational Calculus is equivalent to Relational
Algebra.
E-R Diagram
An ER schema diagram for the COMPANY database
Entities and Attributes
Entities and Their Attributes. The basic object that the ER model represents is an entity, which is a thing in
the real world with an independent existence. An entity may be an object with a physical existence (for
example, a particular person, car, house, or employee) or it may be an object with a conceptual existence
(for instance, a company, a job, or a university course).
Composite versus Simple (Atomic) Attributes. Composite attributes can be divided into smaller subparts,
which represent more basic attributes with independent meanings. For example, the Address attribute of
the EMPLOYEE entity.
Figure 2.7: E-R Diagram
Single-Valued versus Multivalued Attributes. Most attributes have a single value for a particular entity;
such attributes are called single-valued. For example, Age.
A multivalued attribute may have lower and upper bounds to constrain the number of values allowed for
each individual entity. For example, the Colors attribute of a car.
Stored versus Derived Attributes. In some cases, two (or more) attribute values are related—for example,
the Age and Birth_date attributes of a person.
Initial Conceptual Design of the COMPANY Database covers
Relationship & type
Cardinality
Week Entity
Participation Constraints: - The participation constraint specifies whether the existence of an entity
depends on its being related to another entity via the relationship type.
Company schema, with structural constraints specified using (min, max) notation and role names.
Generalization
We can think of a reverse process of abstraction in which we suppress the differences among several entity
types, identify their common features, and generalize them into a single superclass of which the original
entity types are special subclasses. For example, consider the entity types CAR and TRUCK.
Generalization. (a) Two entity types, CAR and TRUCK. (b)Generalizing CAR and TRUCK into the superclass
VEHICLE.
Inheritance
We use all the above features of ER-Model in order to create classes of objects in object-oriented
programming. The details of entities are generally hidden from the user; this process known as abstraction.
Inheritance is an important feature of Generalization and Specialization. It allows lower-level entities to
inherit the attributes of higher-level entities.
Figure 2.11: Inheritance
For example, the attributes of a Person class such as name, age, and gender can be inherited by lower-level
entities such as Student or Teacher.
Aggregation
Aggregation is a process when the relation between two entities is treated as a single entity. Here the
relation between Center and Course is acting as an Entity in relation with Visitor.