0% found this document useful (0 votes)
24 views107 pages

Module 2

The document outlines the objectives and content of a Database Management System (DBMS) course, focusing on relational models, SQL commands, and relational algebra. It covers key concepts such as relations, tuples, keys, and constraints, as well as operations for updating data in a relational database. The aim is to equip students with a foundational understanding of database concepts and the ability to design and manipulate relational databases effectively.

Uploaded by

shreyasrs150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views107 pages

Module 2

The document outlines the objectives and content of a Database Management System (DBMS) course, focusing on relational models, SQL commands, and relational algebra. It covers key concepts such as relations, tuples, keys, and constraints, as well as operations for updating data in a relational database. The aim is to equip students with a foundational understanding of database concepts and the ability to design and manipulate relational databases effectively.

Uploaded by

shreyasrs150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 107

Revolutionising B.

Tech
DATABASE MANAGEMENT SYSTEM
(23TCSE304)

Module-2
Welcome to DBMS

X
The primary objective of this course is to enable students to Have a broad understanding of database
concepts and database management system software and be able to write SQL commands to create
tables and views insert/update/delete data, and query data in a relational DBMS.
Table of Contents
2.1 Relational Model
2.1.1.Relational Model Concepts
2.1.2 Relational Model Constraints
2.1.3 Relational database schemas
2.2. Update operations
2.2.1 Transactions
2.2.2 dealing with constraint violations
2.3 Relational Algebra
2.3.1 Unary
2.3.2 Binary relational operations
2.3.3 Examples
2.4 Additional relational operations
2.4.1 aggregate
2.4.2 grouping
2.4.3 Examples of Queries in relational algebra
2.5. Mapping Conceptual Design into a Logical Design
2.5.1 Relational Database Design using ER-to-Relational mapping.
Aim

To equip students in the fundamentals and


better understanding of relational model and
relation algebra.
a. Explain the basics of relational model concepts and constraints

b. Relational algebra and operations

c. Aggregate and group relational algebra operations

d. Mapping Conceptual Design into a Logical Design

Objectiv
es
2.1 Relational Model Concepts
Domains, Attributes, Tuples, and
Relations
• Informally, a relation looks like a table of values.

• A relation typically contains a set of rows.

• The data elements in each row represent certain facts that correspond to a real-world
entity or relationship
• In the formal model, rows are called tuples

• Each column has a column header that gives an indication of the meaning of the data
items in that column
• In the formal model, the column header is called an attribute name (or just attribute)
Example
of a
X

Relation
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, USN is the key

• Sometimes row-ids or sequential numbers are assigned as keys


to identify the rows in a table
• Called artificial key or surrogate key
2.1.1 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.
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 relation is a set of such tuples (rows)
Formal Definitions - Domain
• A domain 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:


• Example: The domain Date may be used to define two attributes named “Invoice-date”
and “Payment-date” with different meanings
Formal Definitions - State
• The relation state is a subset of the Cartesian product of the
domains of its attributes
• Example: attribute Cust-name is defined over the domain of
character strings of maximum length 25
• dom(Cust-name) is varchar(25)
• It refers to the actual content of the relation at any given time,
represented as a set of tuples (rows)
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)
Formal Definitions - Example
• Let R(A1, A2) be a relation schema:
• Let dom(A1) = {0,1}
• Let dom(A2) = {a,b,c}
• Then: dom(A1) X dom(A2) is all possible combinations:
{<0,a> , <0,b> , <0,c>, <1,a>, <1,b>, <1,c> }

• The relation state r(R)  dom(A1) X dom(A2)


• For example: r(R) could be {<0,a> , <0,b> , <1,c> }
• this is one possible state (or “population” or “extension”) r of the relation R, defined
over A1 and A2.
• It has three 2-tuples: <0,a> , <0,b> , <1,c>

Slide 5- 15
Example –
X
A relation
STUDENT
2.1.2 Characteristics Of Relations
• Ordering of tuples in a relation r(R):
• A relation is defined as a set on “n” tuples;
tuples in a relation do not have any specific
order.
• Ordering of attributes in a relation schema R
(and of values within each tuple):
• We will consider the attributes in R(A1,
A2, ..., An) and the values in t=<v1, v2, ...,
vn> to be ordered .
• A tuple in a relation is an ordered list of values;
order of values according to attribute created
should match.
• Example: t= { <name, “John” >, <usn,
“123456789”>, <age, 23>,<phonenumber,
9743220034> }
• This representation may be called as “self-
describing”.
• Values in a tuple:
• All values are considered atomic
• Each value in a tuple must be from the
domain of the attribute for that column
• If tuple t = <v1, v2, …, vn> is a tuple (row) in
the relation state r of R(A1, A2, …, An)
• Then each vi must be a value from dom(Ai)

• A special null value is used to represent


values that are unknown or not
available(not revealed) or inapplicable
(undefined) in certain tuples.
keys
• Keys play an important role in the relational database.
• It is used to uniquely identify any record or row of data
from the table. It is also used to establish and identify
relationships between tables.
• Primary key is a column of a table or a set of columns
that helps to identify every record present in that table
uniquely.
• The PRIMARY KEY (PK) constraint put on a column or set of
columns will not allow them to have any null values or
any duplicates
• An entity can contain multiple keys, The key which is most
suitable from those lists becomes a primary key.
Candidate key
• A candidate key is an attribute or set of attributes that
can uniquely identify a tuple.
• Except for the primary key, the remaining attributes are
considered a candidate key. The candidate keys are as
strong as the primary key.
Super Key
Super key is an attribute set that can uniquely identify a tuple. A
super key is a superset of a candidate key.
Foreign key
• Foreign keys are the column of the table used to point to the primary
key of another table.
• Every employee works in a specific department in a company, and
employee and department are two different entities. So we can't store the
department's information in the employee table. That's why we link these
two tables through the primary key of one table.
• We add the primary key of the DEPARTMENT table, Department_Id, as a
new attribute in the EMPLOYEE table.
• In the EMPLOYEE table, Department_Id is the foreign key, and both the
tables are related.
Alternate key
the total number of the alternate keys is the total number of candidate
keys minus the primary key. The alternate key may or may not exist. If
there is only one candidate key in a relation, it does not have an
alternate key.
Composite key
Whenever a primary key consists of more than one attribute, it is
known as a composite key. This key is also known as Concatenated
Key.
Artificial key
The key created using arbitrarily assigned data are known as
artificial keys. These keys are created when a primary key is
large and complex and has no relationship with many other
relations. The data values of the artificial keys are usually
numbered in a serial order.
2.2 Relational Model Constraints
Constraints determine which values are permissible and which are not in the
database.
They are of three main types:
1. Inherent or Implicit Constraints: These are based on the data model itself.
(E.g., relational model does not allow a list as a value for any attribute)
2. Schema-based or Explicit Constraints: They are expressed in the schema
by using the facilities provided by the model. (E.g., max. cardinality ratio
constraint in the ER model)
3. Application based or semantic constraints: These are beyond the
expressive power of the model and must be specified and enforced by the
application programs.
Relational Integrity Constraints
• Constraints are conditions that must hold on all valid relation states.
• There are three main types of (explicit schema-based) constraints that can
be expressed in the relational model:
• Key constraints
• Entity integrity constraints
• Referential integrity constraints
• Another schema-based constraint is the domain constraint
• Every value in a tuple must be from the domain of its attribute (or it could be
null, if allowed for that attribute)
Key Constraints
• Superkey of R:
• Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the same value for SK
• That is, for any distinct tuples t1 and t2 in r(R), t1[SK]  t2[SK]
• This condition must hold in any valid state r(R)
• Key of R:
• A "minimal" superkey
• That is, a key is a superkey K such that removal of any attribute from K
results in a set of attributes that is not a superkey (does not possess the
superkey uniqueness property)
• A Key is a Superkey but not vice versa
Key Constraints (continued)
• Example: Consider the CAR relation schema:
• CAR(State, Reg#, SerialNo, Make, Model, Year)
• CAR has two keys:
• Key1 = {State, Reg#}
• Key2 = {SerialNo}
• Both are also superkeys of CAR
• {SerialNo, Make} is a superkey but not a key.
• In general:
• Any key is a superkey (but not vice versa)
• Any set of attributes that includes a key is a superkey
• A minimal superkey is also a key
Key Constraints (continued)
• If a relation has several candidate keys, one is chosen arbitrarily to be the
primary key.
• The primary key attributes are underlined.
• Example: Consider the CAR relation schema:
• CAR(State, Reg#, SerialNo, Make, Model, Year)
• We chose SerialNo as the primary key
• The primary key value is used to uniquely identify each tuple in a relation
• Provides the tuple identity

Slide 5- 32
CAR table with
two candidate
X
keys – License
Number chosen
as Primary Key
2.3 Relational Database Schema
• Relational Database Schema:
• A set S of relation schemas that belong to the same database.
• S is the name of the whole database schema
• S = {R1, R2, ..., Rn} and a set IC of integrity constraints.
• R1, R2, …, Rn are the names of the individual relation schemas
within the database S
• Following slide shows a COMPANY database schema with 6
relation schemas
COMPANY Database Schema
Access User Info

Administrator
Display Retrieve User Info
Account
User Profile
Info

User Account Info

Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
Relational Database State
• A relational database state DB of S is a set of
relation states DB = {r1, r2, ..., rm} such that each ri is a
state of Ri and such that the ri relation states satisfy
the integrity constraints specified in IC.
• A relational database state is sometimes called a
relational database snapshot or instance.
• A database state that does not meet the constraints is
an invalid state
Populated database state: Update operations,
transactions and dealing with constraint violations:
• Each relation will have many tuples in its current relation state
• The relational database state is a union of all the individual relation states
• Whenever the database is changed, a new state arises
• Basic operations for changing the database:
• INSERT a new tuple in a relation
• DELETE an existing tuple from a relation
• MODIFY (UPDATE) an attribute of an existing tuple
• Next slide (Fig. 5.6) shows an example state for the COMPANY database
schema shown in Fig. 5.5.
Populated database state for COMPANY

Access User Info

Administrator
Display Retrieve User Info
Account
User Profile
Info

User Account Info

Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
Entity Integrity
• Entity Integrity:
• The primary key attributes PK of each relation schema R in S cannot
have null values in any tuple of r(R).
• This is because primary key values are used to identify the individual tuples.
• t[PK]  null for any tuple t in r(R)
• If PK has several attributes, null is not allowed in any of these attributes
• Note: Other attributes of R may be constrained to disallow null values,
even though they are not members of the primary key.
Referential Integrity
• Tuples in the referencing relation R1 have attributes FK
(called foreign key attributes) that reference the primary key
attributes PK of the referenced relation R2.
• A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].
• A referential integrity constraint can be displayed in a relational
database schema as a directed arc from R1.FK to R2.
Referential Integrity Constraints for COMPANY database

Access User Info

Administrator
Display Retrieve User Info
Account
User Profile
Info

User Account Info

Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
Other Types of Constraints
• Semantic Integrity Constraints:
• based on application semantics and cannot be expressed by the model
• Example: “the max. no. of hours per employee for all projects he or she
works on is 56 hrs per week”
• A constraint specification language may be used to express these
• SQL-99 allows CREATE TRIGGER and CREATE ASSERTION to express
some of these semantic constraints
• Keys, Permissibility of Null values, Candidate Keys (Unique in SQL),
Foreign Keys, Referential Integrity etc. are expressed by the CREATE
TABLE statement in SQL.

Slide 5- 44
2.4 Update operations, transactions,
and dealing with constraint violations.
• There are three basic operations that can change the states of
relations in the database: Insert, Delete, and Update (or
Modify).
• They insert new data, delete old data, or modify existing data
records, respectively.
• Insert is used to insert one or more new tuples in a relation,
Delete is used to delete tuples, and
• Update (or Modify) is used to change the values of some
attributes in existing tuples
Possible violations for each operation:Dealing
with constraint violations
• DELETE may violate only referential integrity:
• If the primary key value of the tuple being deleted is referenced from other
tuples in the database
• Can be remedied by several actions: RESTRICT, CASCADE, SET NULL (
• RESTRICT option: reject the deletion
• CASCADE option: propagate the new primary key value into the foreign keys of the
referencing tuples
• SET NULL option: set the foreign keys of the referencing tuples to NULL
• One of the above options must be specified during database design for each
foreign key constraint
• INSERT can violate any of the four types of constraints.
• Domain constraints can be violated if an attribute value is given
that does not appear in the corresponding domain or is not of the
appropriate data type.
• Key constraints can be violated if a key value in the new tuple t
already exists in another tuple in the relation r(R).
• Entity integrity can be violated if any part of the primary key of the
new tuple t is NULL.
• Referential integrity can be violated if the value of any foreign key
in t refers to a tuple that does not exist in the referenced relation.
Possible violations for each
operation:Dealing with constraint violations
• UPDATE may violate domain constraint and NOT NULL constraint on an
attribute being modified
• Any of the other constraints may also be violated, depending on the
attribute being updated:
• Updating the primary key (PK):
• Updating a foreign key (FK):
• May violate referential integrity
• Updating an ordinary attribute (neither PK nor FK):
• Can only violate domain constraints
The Transaction Concept
• A database application program running against a
relational database typically executes one or more
transactions.
• A transaction is an executing program that includes some
database operations, such as reading from the database,
or applying insertions, deletions, or updates to the
database.
• At the end of the transaction, it must leave the database
in a valid or consistent state that satisfies all the
constraints specified on the database schema.
Summary
• Presented Relational Model Concepts
• Definitions
• Characteristics of relations
• Discussed Relational Model Constraints and Relational Database Schemas
• Domain constraints
• Key constraints
• Entity integrity
• Referential integrity
• Described the Relational Update Operations and Dealing with Constraint
Violations

Slide 5- 52
2.3 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
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)
Database State for COMPANY
• All examples discussed below refer to the COMPANY database shown here.
The
following
query results
refer to this
X
database
state
2.3.1 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)
•  <selection condition>(R)
• 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)


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
Unary Relational Operations: SELECT (continued)
• 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
2.3.2 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)
Unary Relational Operations: PROJECT (cont.)

• 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
Unary Relational Operations: PROJECT (contd.)
• 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) ) =  <list2> ( <list1> (R) ) as long as <list2> contains the attributes
in <list1>

Slide 8- 62
Examples of
applying
X
SELECT and
PROJECT
operations
Relational Algebra Expressions
• 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.
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)
Unary Relational Operations: RENAME
• The RENAME operator is denoted by  (rho)
• In some cases, we may want to rename the attributes of a
relation or the relation name or both
• Useful when a query requires multiple operations
• Necessary in some cases (see JOIN operation later)
Unary Relational Operations: RENAME (continued)
• 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
Unary Relational Operations: RENAME (continued)
• 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,NO)
(DEP5_EMPS)

• The 10 attributes of DEP5_EMPS are renamed to F, M, L, S, B, A, SX, SAL, SU,


DNO, respectively
Note: the  symbol is an assignment operator
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)
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
Result of the
UNION X

operation
RESULT ←
RESULT1 ∪
RESULT2.
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 R1R2 (also for R1R2, or R1–R2, see next
slides) has the same attribute names as the first operand relation R1 (by
convention)
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”
Relational Algebra Operations from Set Theory: SET
DIFFERENCE (cont.)

• 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”
Example to
illustrate the
result of
UNION, X

INTERSECT,
and
DIFFERENCE
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
Write relational algebra for the following statements.
• Select the EMPLOYEE tuples whose department number is 4.
• Select the employee tuples whose salary is greater than $30,000,
• Select the employee tuples who work in department 5 and whose salary is greater than
$30,000.
• Select the tuples for all employees who either work in department 4 and make over $25,000
per year, or work in department 5 and make over $30,000.
Relational Algebra Operations from Set Theory:
CARTESIAN PRODUCT
• CARTESIAN (or CROSS) 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”
Relational Algebra Operations from Set Theory:
CARTESIAN PRODUCT (cont.)
• Generally, CROSS PRODUCT is not a meaningful operation
• Can become meaningful when followed by other operations
• Example (not meaningful):
• 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
Relational Algebra Operations from Set Theory:
CARTESIAN PRODUCT (cont.)
• 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
2.4 Binary Relational Operations:
JOIN (EQUIJOIN )
• 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 (cont.)
• 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
Some properties of JOIN
Consider the following JOIN operation:
• R(A1, A2, . . ., An) S(B1, B2, . . ., Bm)
R.Ai=S.Bj
• 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—r
from R and s from S, but only if they satisfy the join condition r[Ai]=s[Bj]
• Hence, if R has nR tuples, and S has nS tuples, then the join result will
generally have less than nR * nS tuples.
• Only related tuples (based on the join condition) will appear in the result

88
Binary Relational Operations:
NATURAL JOIN Operation
• NATURAL JOIN Operation
• Another variation of JOIN called NATURAL JOIN — denoted by * — was
created to get rid of the second attribute in an EQUIJOIN condition.
• The standard definition of natural join requires that the two join attributes, or
each pair of corresponding join attributes, have the same name in both
relations
Binary Relational Operations
NATURAL JOIN (continued)
• Example: To apply a natural join on the DNUMBER attributes of
DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write:
• DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS
• Only attribute with the same name is DNUMBER
• An implicit join condition is created based on this attribute:
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER

• Another example: Q  R(A,B,C,D) * S(C,D,E)


• The implicit join condition includes each pair of attributes with the
same name, “AND”ed together:
• R.C=S.C AND R.D=S.D
• Result keeps only one attribute of each such pair:
• Q(A,B,C,D,E)
Binary Relational Operations: DIVISION
• DIVISION Operation
• The division operation is applied to two relations R and S
• For a tuple t to appear in the result T of the DIVISION, the values in t must
appear in R in combination with every tuple in S.
• R(Z)  S(X), where X is a subset Z. Let Y = Z - X (and hence Z = X 
Y); that is, let Y be the set of attributes of R that are not attributes of S.

• The result of DIVISION is a relation T(Y) that includes a tuple t if tuples t R


appear in R with tR [Y] = t, and with tR [X] = ts for every tuple ts in S.
Example of DIVISION
2.4 Additional Relational Operations:
Aggregate Functions and Grouping
• Common functions applied to collections of numeric values include
• SUM, AVERAGE, MAXIMUM, and MINIMUM.
• The COUNT function is used for counting tuples or values.
• Group function is used for grouping tuples or values

95
Aggregate Function Operation
• Use of the Aggregate Functional operation ℱ
• ℱMAX Salary (EMPLOYEE) retrieves the maximum salary value from the
EMPLOYEE relation
• ℱMIN Salary (EMPLOYEE) retrieves the minimum Salary value from the
EMPLOYEE relation
• ℱSUM Salary (EMPLOYEE) retrieves the sum of the Salary from the EMPLOYEE
relation
• ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE) computes the count (number) of
employees and their average salary
• Note: count just counts the number of rows, without removing duplicates

96
Using Grouping with Aggregation
• Grouping can be combined with Aggregate Functions
• Example: For each department, retrieve the DNO, COUNT SSN, and
AVERAGE SALARY
• A variation of aggregate operation ℱ allows this:
• Grouping attribute placed to left of symbol
• Aggregate functions to right of symbol
• DNO ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)
• Above operation groups employees by DNO (department number) and
computes the count of employees and average salary per department

97
Examples of applying aggregate functions and
grouping

98
Illustrating aggregate functions and grouping

99
2.5 Relational Database Design by ER- and EERR-
to-Relational 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.

• Mapping EER Model Constructs to Relations


• Step 8: Options for Mapping Specialization or Generalization.
• Step 9: Mapping of Union Types (Categories).
2.5.1 ER-to-Relational Mapping Algorithm
• Step 1: Mapping of Regular Entity Types.
• For each regular (strong) entity type E in the ER schema, create a relation R
that includes all the simple attributes of E.
• Choose one of the key attributes of E as the primary key for R.
• If the chosen key of E is composite, the set of simple attributes that form it will
together form the primary key of R.
• Example: We create the relations EMPLOYEE, DEPARTMENT, and
PROJECT in the relational schema corresponding to the regular entities in
the ER diagram.
• SSN, DNUMBER, and PNUMBER are the primary keys for the relations
EMPLOYEE, DEPARTMENT, and PROJECT as shown.
The ER
conceptual
schema
diagram for
the
COMPANY
X

database.
Result of
mapping the
COMPANY ER
schema into a
X
relational
database
schema.
Assessments
Here are some assessments you can use to evaluate your understanding of the fundamentals of DBMS.

• Quizzes

• Relational algebra queries

• Relational mapping
• Consider a relation STUDENT shown below.
STUDENT (NAME, AGE, DEPARTMENT, CITY, MARKS)
Write Relational Algebra queries for the following statements.
Retrieve students details those belongs to CSE department.
Retrieve details of students whose department is CSE with age is more than
21.
Retrieve name and department of the students whose marks is more than 90
and staying in Bangalore city.
Retrieve name of the students studying in ECE department with age is more
than 20..
Retrieve all the students details those who belongs to CSE with age is less
than 20 or marks is more than 80.
Consider the following relations for a database that keeps track of
student enrollment in courses and the books adopted for each course:
STUDENT(SSN, Name, Major, Bdate)
In-Class COURSE(Course#, Cname, Dept)

Exercise
X ENROLL(SSN, Course#, Quarter, Grade)
BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
TEXT(Book_ISBN, Book_Title, Publisher, Author)
Draw a relational schema diagram specifying the foreign keys for
this schema.
Summary

Outcomes:

a. Basics of relational model concepts and constraints

b. Relational algebra and operations

c. Aggregate and group relational algebra operations

d. Mapping Conceptual Design into a Logical Design


Terminal Questions
Terminal Questions

1. what are the Characteristics Of Relations?


2. explain three types of constraints.
3. write short notes on key constraints.
4. write schema diagram for company database.
5. list out the relational algebra operations.
6. explain select, project and rename operations with examples.
7. explain unary relational operators.
8. explain relational algebra operations from set theory.
9. write short notes on binary relational operators.
10.Give example for mapping the COMPANY ER schema into a relational
database schema.
Thank you

You might also like