Module 2
Module 2
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
Objectiv
es
2.1 Relational Model Concepts
Domains, Attributes, Tuples, and
Relations
• Informally, a relation looks like a table of values.
• 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
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)
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
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
Administrator
Display Retrieve User Info
Account
User Profile
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
Administrator
Display Retrieve User Info
Account
User Profile
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:
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)
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 R1R2 (also for R1R2, 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.)
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.
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
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
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 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: