0% found this document useful (0 votes)
3 views223 pages

CS3492 DBMS 1

This document provides an overview of relational databases, including their purpose, architecture, and key concepts such as data models, SQL fundamentals, and database management systems. It discusses the advantages of using a DBMS over traditional file processing systems, highlighting issues like data redundancy, access difficulties, and security problems. Additionally, it covers various database users, components of DBMS, and the relational model, emphasizing the importance of data abstraction and integrity in database management.

Uploaded by

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

CS3492 DBMS 1

This document provides an overview of relational databases, including their purpose, architecture, and key concepts such as data models, SQL fundamentals, and database management systems. It discusses the advantages of using a DBMS over traditional file processing systems, highlighting issues like data redundancy, access difficulties, and security problems. Additionally, it covers various database users, components of DBMS, and the relational model, emphasizing the importance of data abstraction and integrity in database management.

Uploaded by

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

UNIT I RELATIONAL DATABASES

Purpose of Database System – Views of data –


Data Models – Database System Architecture –
Introduction to relational databases – Relational Model
– Keys – Relational Algebra – SQL fundamentals–
Advanced SQL features – Embedded SQL– Dynamic SQL
CS3492 -Database Management Systems
Anna university
Regulation 2021

Prepared by:
Dr.K.Manojkumar. ASP/CSE
Govt.College of Engineering
Sengipatti.Thanjavur
Introduction:
•A database-management system (DBMS) is a
collection of interrelated data and a set of programs to
access those data.
•The collection of data, usually referred to as the
database, contains information relevant to an
enterprise.
•The primary goal of a DBMS is to provide a way to
store and retrieve database information that is both
convenient and efficient.
Example of some Database: MySQL, Microsoft Access,
SQL Server,MongoDB,FileMaker,Oracle, RDBMS,
dBASE, Clipper, and FoxPro etc..
Database-System Applications
Some of the applications:
Enterprise Information
Sales: For customer, product, and purchase
information.
Accounting: For payments, receipts, account balances,
assets and other accounting information.
Human resources: For information about employees,
salaries, payroll taxes, and benefits.
Manufacturing: For management of the supply chain
and for tracking production of items in factories,
inventories of items in warehouses and stores
Banking and Finance
Banking: For customer information, accounts, loans,
and banking transactions.
Credit card transactions
Finance:
For storing information about holdings, sales, and
purchases of financial instruments such as stocks and
bonds also for storing real-time market data
Universities:
For student information, course registrations, and
grades
Airlines, Train and Bus ticket reservation system
Telecommunication
Purpose of Database Systems
(Or)
Draw back of File processing System:
•Before database management systems (DBMSs) were
introduced, organizations usually stored information in
File processing System.
•This typical file-processing system is supported by a
conventional operating system. The system stores
permanent records in various files, and it needs
different application programs to extract records from,
and add records to, the appropriate files.
•File-processing system has a number of major
disadvantages:
Data redundancy and inconsistency
•The same information may be duplicated in several
places (files).Redundancy may lead to data
inconsistency; that is, the various copies of the same
data may no longer get matched.
•For example, if a student has a double major (say,
music and mathematics) the address and telephone
number of that student may appear in a file that
consists of student records of students in the Music
department and also student records of students in the
Mathematics department.
Difficulty in accessing data:
•Which makes accessing data very difficult.
Example:
•Suppose our University Vice chancellor wants a data those
who are getting A grade in DBMS overall in tamil nadu, in
this scenario as a developer have to write a new application
program for this purpose,
•several days later same vice chancellor ask the details of
the students those who are getting A grade in Operating
System overall in tamil nadu, as same developer have to
written a another application program for this purpose, It is
so difficult for developer to write a separate programs for
that every request.
Data Isolation:
•Because data are scattered in various files, and files
may be in different formats, writing new application
programs to retrieve the appropriate data is difficult.
Integrity problems:
•The data values stored in the database must satisfy
certain types of consistency constraints.
•In conventional file-processing system constraints
cannot be applied .
•Suppose also that the university requires that the
account balance of a department may never fall below
Rs.1000.
• It can be done in DBMS enforcing these constraints
in the system by adding appropriate code in the
various application programs.
Atomicity problems
• A computer system, like any other device, is subject
to failure. In many applications, it is crucial that, if a
failure occurs, the data be restored to the consistent
state that existed prior to the failure.
• atomic—it must happen in its entirely or not at all.
• It is difficult to ensure atomicity in a conventional
file-processing system.
One of the example of atomic Fund transfer.
Concurrent-access anomalies
•Many systems allow multiple users to update the
data simultaneously.
•In such an environment, interaction of concurrent
updates is possible and may result in inconsistent data.
•Consider department A, with an account balance of
Rs.10,000. If two department clerks debit the account
balance (by say Rs.500 and Rs.100, respectively) of
department A at almost exactly the same time, the
result of the concurrent executions may leave the
balance in an incorrect it may shows the balance of
Rs.9500 or Rs.9900 instead of the correct value of
Rs.9400
Security problems :
•Not every user of the database system should be able
to access all the data.
•For example, in a university, payroll person need to
see only that part of the database that has financial
information.
•They do not need access to information about
academic records.
•Enforcing such security constraints is difficult in File
processing System.
Views of Data
• A major purpose of a database system is to provide
users with an abstract view of the data.
• That is, the system hides certain details of how the
data are stored and maintained.
Data Abstraction:
• Not every database-system users are not computer
trained, developers(Programmer) hide the
complexity from users through several levels of
abstraction
1.Physical Level
2.Logical level
3.View level
Levels of Data abstraction
Example of views of data
Type employee=record
empID:numeric(10)
empName:Char(25)
dept_name:Char(20)
empSalary:numeric(8,2)
end
This code defines a new record type employee with
four fields such as Empid, empname, deptname,
empsalary
Physical level:
•This is the lowest level of Data abstraction.
•At physical level the record employee can be
described as block of Consecutive storage locations.
Many database system hide lowest level of storage
details from the data base programmer.
Logical level:
•This is the next highest level of data abstraction which
describes what data are stored in database and also
describes relationship among the data.
•Type definition of the records is decided at logical
level. The programmer work of the record at this level
and also database admin.
View level:
•This is the highest level of data abstraction that
describe only part of the entire database.
•This view level can provide access to only part of the
database.
•There is a specific view of the record is allowed at the
view level. For that example record customer can view
only the employee name and ID but can not access
employee salary.
Instances and Schemas:
•The overall design of the database is called the
database schema.
CustID CustName CustPhoneno

•The collection of information stored in the database at a


particular moment is called an instance of the database.

•When information is inserted or deleted from the database


then the database get changed

CustID CustName CustPhoneno


101 Balaji 9956784932
102 Siva 9942179881
Type of schema :
•The database has several schema based on the level of
abstraction
Physical schema
describes the database design at the physical level
logical schema
• describes the database design at the logical level.
Subschemas
•A database may also have several schemas at the view
level, sometimes called subschemas, that describe
different views of the database.
Data model
• Underlying the structure of a database is the data model: a
collection of conceptual tools for describing data, data
relationships, data semantics, and consistency constraints.
• A data model provides a way to describe the design of a
database at the physical, logical, and view levels.
• The data models can be classified into four different
categories:
• 1.Relational Model
• 2.Entity Relation ship model
• 3.Object based data model
• 4.Semi structured Data model
Relational Model.
•The relational model uses a collection of tables to represent
both data and the relationships among those data.
• Each table has multiple columns, and each column has a
unique name.
•Tables are also known as relations.
•The relational model is an example of a record-based model

Register_no Name Age

100001 Aadhi 18

100002 Anbuselvam 19

100003 Aghila 18
Entity Relationship Model:
•The entity-relationship (E-R) data model uses a
collection of basic objects, called entities, and
relationships among these objects.
•An entity is a “thing” or “object” in the real world that
is distinguishable from other objects.
•The entity-relationship model is widely used in
database design
• Object-Based Data Model.
• Object-oriented programming (especially in Java, C++, or C#)
has become the dominant software-development
methodology.
• object-oriented data model that can be seen as extending the
E-R model with notions of encapsulation, methods
(functions), and object identity.
• The object-relational data model combines features of the
object-oriented data model and relational data model.
Semi structured Data Model.
Semi-structured data is a type of data that is not purely structured, but
also not completely unstructured.
The semi structured data model there is no separation between data
and schema.
The Extensible Markup Language (XML) is widely used to
represent semistructured data and JSON –Java Script Object Notation
System Catalog(Data Dictionary)
Data dictionary is a repository of information describing the data
in a database, that is meta data or data about data.
Authorization control module uses the system catalog to check
whether the user has necessary authorization to carry out the
requested operation
System catalog has to store:
* The name of the user authorized to use the DB.
* The name of the data item in the DB.
* The Constraints on each data item.
Data Definition Language (DDL)

• Specification notation for defining the


database schema

• DDL compiler generates a set of tables


stored in a data dictionary.
• Ex:Create,Alter,Drop…
Data Manipulation Language (DML)
• Language for accessing and manipulating the data
organized by the appropriate data model
– DML also known as query language
• Two classes of languages
– Procedural – user specifies what data is required
and how to get those data
– Nonprocedural(Declarative) – user specifies what
data is required without specifying how to get
those data.Ex.SQL
• SQL is the most widely used query language
CS3492 -Database Management Systems
Anna university
Regulation 2021

Prepared by:
Dr.K.Manojkumar. ASP/CSE
Govt.College of Engineering
Sengipatti.Thanjavur
Database System Architecture
Database Users and Administrators
People who work with a database can be categorized as
database users or database administrators.
Database Users and User Interfaces
•There are four different types of database-system users,.
Different types of user interfaces have been designed for the
different types of users.
•Naive users are unsophisticated users who interact with the
system by invoking one of the application programs that have
been written previously.
•For example, a clerk in the university who needs to add a new
employee to department of CSE invokes a program called new
employee.
• Naive users may also simply read reports generated
from the database.
• Application programmers are computer
professionals who write application programs.
Application programmers can choose from many
tools to develop user interfaces.
• Sophisticated users interact with the system without
writing programs. Instead, they form their requests
either using a database query language or by using
tools such as data analysis software.
• They submit each such query to query processor,
whose function is to break down DML statement into
instruction that the storage manager understands.
Database Administrator
A person who has central control over the system is
called a database administrator (DBA).
The functions of a DBA include:
•Schema definition. The DBA creates the original
database schema by executing a set of data definition
statements in the DDL.
• Schema and physical-organization modification. The
DBA carries out changes to the schema and physical
organization to reflect the changing needs of the
organization.
Granting of authorization for data access.
By granting different types of authorization, the database
administrator can regulate which parts of the database various
users can access.
Routine maintenance.
Examples of the database administrator’s routine
maintenance activities are:
• Periodically backing up the database, either onto
tapes or onto remote servers.
•Ensuring that enough free disk space is available for
normal operations, and upgrading disk space as
required.
•Monitoring jobs running on the database and ensuring
that performance is not degraded by very expensive
tasks submitted by some users.
• Data-manipulation language (DML) is a language that
enables users to access or manipulate data. Nonprocedural
DMLs, which require a user to specify only what data are
needed, without specifying exactly how to get those data, are
widely used today.
• Data-definition language (DDL) is a language for specifying
the database schema and as well as other properties of the
data.
• Entity-relationship (E-R) datamodel is a widely used
datamodel for database design. It provides a convenient
graphical representation to view data, relationships, and
constraints.
Components of DBMS
• A database system is partitioned into modules that
deal with each of the responsibilities of the overall
system.
• The functional components of a database system can
be broadly divided into the storage manager and the
query processor components.
Storage Manager:
• The storage manager is the component of a
database system that provides the interface between
the low-level data stored in the database and the
application programs and queries submitted to the
system.
• The storage manager components include:
• Authorization and integrity manager, which tests for the
satisfaction of integrity constraints and checks the authority
of users to access data.
• Transaction manager, which ensures that the database
remains in a consistent (correct) state despite system failures,
and that concurrent transaction executions proceed without
conflicting.
File manager, which manages the allocation of space on disk
storage and the data structures used to represent
information stored on disk.
• Buffer manager, which is responsible for fetching data from
disk storage into main memory, and deciding what data to
cache in main memory.
The buffer manager is a critical part of the database system,
since it enables the database to handle data sizes that are
much larger than the size of main memory.
• The storage manager implements several data structures as
part of the physical system implementation:
• Data files, which store the database itself.
• Data dictionary, which stores metadata about the structure
of the database, in particular the schema of the database.
• Indices, which can provide fast access to data items. Like the
index in this textbook
Query Processor:
• The query processor components include:
• DDL interpreter, which interprets DDL statements and
records the definitions in the data dictionary.
• DML compiler, which translates DML statements in a query
language into an evaluation plan consisting of low-level
instructions that the query evaluation engine understands.
• Query evaluation engine, which executes low-level
instructions generated by the DML compiler.
Thank you
INTRODUCTION TO RELATIONAL DATABASES
•A relational database is based on the relational model and uses a
collection of tables to represent both data and the relationships
among those data. It also includes a DML and DDL.
•A relational database consists of a collection of tables, each of
which is assigned a unique name. A row in a table represents a
relationship among a set of values.
Relational Model Concepts
Attribute
•Each column in a Table. Attributes are the properties which
define a relation. e.g., Student_Rollno, NAME,etc

• 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.
• Tuple – It is nothing but a single row of a table, which
contains a single record.
• Relation Schema: A relation schema represents the name of
the relation with its attributes.
• Degree: The total number of attributes which in the relation is
called the degree of the relation.
• Cardinality: Total number of rows present in the Table.

• Column: The column represents the set of values for a


specific attribute.
• Relation key - Every row has one, two or multiple attributes,
which is called relation key.
• Attribute domain – Every attribute has some pre-defined
value and scope which is known as attribute domain

Operations in Relational Model


• Insert, update, delete and select.

With example students should explain the above operations


CS3492 -Database Management Systems
Anna university
Regulation 2021

Prepared by:
Dr.K.Manojkumar. ASP/CSE
Govt.College of Engineering
Sengipatti.Thanjavur
Relational Algebra
Relational Algebra :
The relational algebra is a procedural query language. It consists
of a set of operations that take one or two relations as input and
produce a new relation as their result.
Relational algebra operation are divided into two groups:
Set operation include the following operation:
Union(∪)
Intersection(∩)
Set difference(-)
Cartesian product(× )
Second group relational algebraic operation is developed
specially for relational databases. Some of the operations of this
group are:
Select(σ)
Project(π)
Rename(ρ)
Join ( )

Division
Fundamental operation in relational algebra:
Depositor: Borrower:
Customer_name City Customer_Name City

Hari Pune Antony Mumbai

John Mumbai Bala Pune

Jaffer Sholapur Hari Pune

logu Nashik Jackson Sholapur

siva Pune Jaffer Sholapur

Siva Pune
Tamil mumbai
Williams chennai
UNION(U)
The result of the operation is denoted by depositor ∪
Borrower is the relation that includes all the tuples that
are in either in depositor or borrower or in both.
duplicates are eliminated.
Example: Find the names of all the bank customer who
have an account or loan or both.
The result of the union is :
Depositor ∪ Borrower
Customer_Name City

Hari Pune

John Mumbai

Jaffer Solaphur

Logu Nashik

Siva Pune

Tamil Mumbai

Antony Mumbai

Bala Pune

Jackson Solapur

Williams chennai
Intersection:
The result of the intersection that operation that
includes all tuples that are in both depositor and
borrower.
Depositor ∩ borrower
Example:
Find the name of the customer who have both
account and loan
Depositor ∩ Borrower

Customer_Name City

Hari Pune

Jaffer Solaphur

Siva pune
Setdifference (-)
That gives tuples in one relation but not present in
another relation.
Borrower-Depositor

Customer _Name City


Antony Mumbai
Bala Pune
Jackson Sholapur
willIiams Chennai
• Both union and intersection operation are commutative and
associative operation. This means following figures are true
• A U B =B U A
• A∩B=B∩A
• A U(B U C)=(A U B)U C
• A ∩ (B ∩ C)= (A ∩ B) ∩ C
The difference operation is not commutative . This means A –B is
not the same as B-A
The Cartesian-Product Operation:
• The Cartesian-product operation, denoted by a cross (×),
allows us to combine information from any two relations
• The Cartesian product of two relation A and B is denoted by
A×B
• Also known as Cross product or cross joins.
• The result of Cartesian product of two relations which have X
and Y columns is a relation that has X + Y columns.
• The resulting relation will have one tuple or each
combination of tuples from each participating relations.
• Hence if the relation have n and m tuples respectively, then
the Cartesian product will have n * m tuples.
Publisher_Info Book_Info
Publisher_Code Name Book_ID Title

P0001 Technical
B0001 DBMS

P0002 NV

B0002 C
P0003 pearson
The relation publisher has 2 columns and 3 tuples. The
relation book_info has 2 column and 2 tuples. So the
Cartesian product has 4 columns (2+2) and 6 tuples (3*2)

Publisher_Code Name Book_ID Title

P0001 Technical B0001 DBMS

P0002 NV B0001 DBMS

P0003 pearson B0001 DBMS

P0001 Technical B0002 C

P0002 NV B0002 C

P0003 pearson B0002 C


• The select, project, and rename operations are called unary
operations, because they operate on one relation.
• The other three operations operate on pairs of relations and
are, therefore, called binary operations.
The Select Operation:
• The select operation selects tuples that satisfy a given
predicate. We use the lowercase Greek letter sigma (σ) to
denote selection.
• σ <Select Condition> (R)

• R- relation
• Select those tuples of the instructor relation where the
instructor is in the “Physics” department,
• we write:
σ dept_name =“Physics” (instructor )
• we can combine several predicates into a larger predicate by
using the connectives and (∧), or (∨), and not ( ¬ ).
• Thus, to find the instructors in Physics with a salary greater
than $90,000, we write:
• σ dept_name =“Physics”∧ salary > 90000 (instructor )
find all instructors with salary greater than $90,000 by writing:
• σ salary>90000 (instructor)
The Project Operation (π):
• Suppose we want to list all instructors ID, name, and salary,
but do not care about the dept name.
• The project operation allows us to produce this relation.
• The project operation is a unary operation that returns its
argument relation, with certain attributes left out. Since a
relation is a set, any duplicate rows are eliminated. Projection
is denoted by the uppercase Greek letter pi (π).
• π ID, name, salary (instructor )
• π name (σ dept_name =“Physics” (instructor))
• π name,dept_name(σ salary > 89000(Instructor))
Rename operation:
In relation algebra u can rename either the relation or the
attributes or both.
the rename operator, denoted by the lowercase Greek letter rho
(ρ),
Rename operation can take any one of the following forms
1.ρₓ(new attribute name)(R)
2.ρₓ(R)
3. ρ(new attribute name)(R)
• ρ indicate rename operator
• x indicate new relation name
• R indicate original relation

• Table name (Relation Name)-Student


Regno Name Address
101 Manojkumar Chennai
102 Surya Trichy
Additional operation in Relational Algebra
• Natural join
• Division Operation
• Assignment Operation
 Natural join operation:
 Binary operation
 That allows us to combine certain selection and a cartesian
product into one operation.
 It is denoted by join symbol
 It finally removes the duplicates attributes
 Difference between Cartesian product and natural join is
query involving Cartesian product includes a selection
operation on the result of Cartesian product.
• Employee : salary:

Emp_Code Emp_name Emp_Code Salary

E0001 Aghila E0001 2000


E0002 Bala E0002 5000

E0003 Charulatha E0003 7000

E0004 David E0004 10000

E0005 John
• To display the name of all employees with salary, we need to
combine employee and salary relation using cartesian
product as follows
• ∏emp_name,Salary(employee.emp_Code=
Salary.emp_Code(Employee Χ salary))
The above query is rewritten using natural join as follows:

∏emp_name,Salary(Employee salary)
Emp_name Salary

Aghila 2000
Bala 5000
Charulatha 7000

David 10000
Outer join
The outer join is the extension of join operation to deal with
missing information

Types of Outer join


•Left outer join ⟕
•Right outer join ⟖
•Full outer join ⟗
Relation Name :Employee Relation Name: Employee_Salary

Emp_Name City Emp_Name Department Salary

Hari Pune Hari CSE 15000


Om Mumbai Om IT 10000
Smith Nashik Bill CSE 12000
Jai Solapur Jai IT 9000
Left outer join : ⟕

The Left Outer Join keeps all tuples from the left relation, adding
NULL values for non-matching tuples from the right relation.

Employee ⟕ Employee_Salary
Emp_Name City Department Salary
Hari Pune CSE 15000
Om Mumbai IT 10000
Smith Nashik Null Null
Jai Solapur IT 9000
Right outer join : ⟖

The Right Outer Join keeps all tuples from the right relation,
adding NULL values for non-matching tuples from the left
relation.
Employee ⟖ Employee_Salary
Emp_Name City Department Salary
Hari Pune CSE 15000
Om Mumbai IT 10000
Bill NULL CSE 12000
Jai Solapur IT 9000
Full outer join : ⟗

The Full Outer Join retains all tuples from both relations, adding
NULL values for missing matches on either side..
Employee ⟗ Employee_Salary

Emp_Name City Department Salary


Hari Pune CSE 15000
Om Mumbai IT 10000
Smith Nashik NULL NULL
Jai Solapur IT 9000
Bill NULL CSE 12000
Division Operation:
The division operation is denoted by
Accout: Depoisitor:

Account_no Branch Balance Cust_Name Account_No


Name
A101 AnnaNagar 500 Raja A101

A201 Saidhapet 1000 John A201

A217 KailashNaga 3000 Arjun A217


r
Sona A267
A267 Thuvakudi 5000

A125 AnnaNagar 10000 John A125


Branch:
Branch_Name Branch_City Asset

AnnaNagar Chennai 20000000

Saidhapet Chennai 30000000

KailashNagar Trichy 10000000

Thuvakudi Trichy 500000

Find all customers who have an account at all the


branches located in Chennai
Step 1:
We can obtain all branches in chennai by the
expression

r1= ∏ Branch_Name( σBranch_City=“Chennai”(Branch))


Branch_Name

AnnaNagar
Saidhapet

Step 2: We can find all cust_Name,Branch_Name pairs for


which the customer has an account at a branch by writing
r2= ∏Cust_Name,Branch_Name(Depositor Account)
Cust_Name Branch_Name

Raja AnnaNagar
John Saidhapet
Arjun KailashNagar
Sona Thuvakudi

John AnnNagar

We need to find customers who appear in r2 with every


branch name in r1.
The operation that provides exactly those customers is
the divide operation.
Thus the query is :
• r2= ∏Cust_Name,Branch_Name(Depositor Account)

∏ Branch_Name(σBranch_City=“Chennai”(Branch))
The result of the operation is a relation that contain one
attribute Cust_Name with tuple John

Cust_Name
John
Assignment operation:
•The assignment operation is denoted by ‘←’ .
•It works like assignment in a programming language.
•For relational-algebra queries, assignment must always be
made to a temporary relation variable.
temp1 ← R × S

R-One table name(Student)


S-one table name(Faculty)

Student_faculty-- student X faculty


Aggregate Function ( G )

It takes a collection values and return a single


values as a result
•Avg G avg(Salary) (Employee_Salary)
•Mini G min(Salary) (Employee_Salary)
Max G max(Salary) (Employee_Salary)
•Sum G Sum(Salary) (Employee_Salary)
Count G count(Emp_name) (Employee_Salary)
Thank you
Advanced SQL Features
Embedded SQL(Static SQL)
• The SQL standard defines embedding of SQL in variety of
programming languages such as C,C++,Java and pythons..etc

• Embedded SQL is the one which combines the high level


language with the DB language like SQL

• A Language to which SQL queries are embedded is referred to


as Host Language

• Embedded SQL program must be processed by a special


preprocessor prior to compilation.

• Static SQL is compiled and optimized prior to the execution.


Host variable
•These are the variables of host language used to pass the value
to the query as well as to capture the values returned by the
query.
•Since SQL is dependent on host language we have to use
variables of host language and such variables are known as host
variable.
•Hence we have to declare host variables within BEGIN DECLARE
and END DECLARE section. Again, these declare block should be
enclosed within EXEC SQL and ‘;’.
EXEC SQL BEGIN DECLARE SECTION;
int STD_ID;
char STD_NAME [15];
char ADDRESS[20];
EXEC SQL END DECLARE SECTION;

When host variables are used in a SQL query, it should be


preceded by colon ‘:’ to indicate that it is a host variable.
EXEC SQL SELECT * FROM STUDENT WHERE STUDENT_ID
=:STD_ID;
Dynamic SQL
• The main disadvantage of embedded SQL is that it supports
only static SQLs.
• Dynamic SQL is SQL statements that are constructed at
runtime; for example, the application may allow users to
enter their own queries.
EXECUTE
• This statement is used to compile and execute the SQL
statements prepared in DB.
Syntax :
EXEC SQL EXECUTE sql_query;
Ex:
sql_stmt = ‘DELETE FROM STUDENT WHERE STD_ID = :SID’;
EXEC SQL PREPARE sql_query FROM :sql_stmt;
EXEC SQL EXECUTE sql_query;
EXECUTE IMMEDIATE
•This statement is used to prepare SQL statement as well as
execute the SQL statements in DB. It performs the task of
PREPARE and EXECUTE in a single line.
Syntax:
EXEC SQL EXECUTE IMMEDIATE :sql_stmt;

Ex:
EXEC SQL EXECUTE IMMEDIATE ‘DELETE FROM STUDENT WHERE
STD_ID = 100’;

EXEC SQL EXECUTE IMMEDIATE ‘UPDATE STUDENT SET ADDRESS


= ‘Troy’ WHERE STD_ID =100’;
Difference between Static and Dynamic
SQL
Key Static Dynamic SQL
SQL(Embedded )
Database In Static SQL, database access In Dynamic SQL, how a database will
Access procedure is predetermined in be accessed, can be determine only
the statement. at run time.
Efficiency Static SQL statements are Dynamic SQL statements are less
more faster and efficient efficient
Compilation Static SQL statements are Dynamic SQL statements are
compiled at compile time compiled at run time.
Dynamic Statements like EXECUTE Statements like EXECUTE
Statements IMMEDIATE, EXECUTE, IMMEDIATE, EXECUTE, PREPARE are
PREPARE are not used. used.
Flexibility Static SQL are less Flexibility More Flexibility
CS3492 -Database Management Systems
Anna university
Regulation 2021

Prepared by:
Dr.K.Manojkumar. ASP/CSE
Govt.College of Engineering
Sengipatti.Thanjavur
Keys
• Key is an attribute or set of an attribute which helps you to
identify a row(tuple) in a relation(table).
• They allow you to find the relation between two tables.
• Keys help you uniquely identify a row in a table by a
combination of one or more columns in that table.

employee ID FirstName LastName


11 Andrew Johnson
22 Tom Wood
33 Alex Hale
Various Keys in Database Management System
DBMS has following types of Keys each have their different
functionality:
•Super Key
•Primary Key
•Candidate Key
•Alternate Key
•Foreign Key
What is the Super key?
•A super key is a group of single or multiple keys which identifies
rows in a table.
•A Super key may have additional attributes that are not needed
for unique identification.
Example:
In the above-given example, ( AadharID), (EmpNum ,name), (EmpNum,Phoneno) and
(Empname,phoneno) are superkeys.

AadharID EmpNum Empname Phoneno


9812345098 AB05 Anwar 277315
9876512345 AB06 Bala 277456
199937890 AB07 Dhanaseka 277896
r
What is a Primary Key?
•A column or group of columns in a table which helps us to
uniquely identifies every row in that table is called a primary
key.
•The same value can't appear more than once in the table.
•A table can have only ONE primary key; and in the table, this
primary key can consist of single or multiple columns (fields).

Rules for defining Primary key:


•It must for every row to have a primary key value.
•Primary keys must contain UNIQUE values, and cannot contain
NULL values.
•The value in a primary key column can never be modified or
updated if any foreign key refers to that primary key.
Example:
In the following example StudID is a Primary Key. Student

StudID RollNo FirstName LastName Email


1 11 Tom Price [email protected]
om

2 12 Nick Wright [email protected]


om

3 13 Dana Natan mno@yahoo


.com

14 Hari Prasanth jnmp@gmail.


com
Creation of Primary Key:
MySQL:
Create table student(StudID Number(15),Rollno Varchar2(15),FirstName
Varchar2(15),LastName varchar2(15),Email Varchar2(25), primary key(StudID));

Oracle/MS Access/SQL Server:


Create table student(StudID Number(15) Primary key,Rollno Varchar2(15),FirstName
Varchar2(15),LastName varchar2(15) NOT NULL,Email Varchar2(25));
Two column primary key:
Create table student(StudID Number(15),Rollno
Varchar2(15),FirstName Varchar2(15),LastName varchar2(15),Email
Varchar2(25),
CONSTRAINT PK_Student PRIMARY KEY (StudID,LastName));

What is a Candidate Key?


•A super key with no repeated attribute is called candidate key.
•The Primary key should be selected from the candidate keys.
•The candidate keys are minimal superkeys.
Properties of Candidate key:
•It must contain unique values
•Must not contain null values
•Uniquely identify each record in a table

•In that above table Stud ID, Roll No, and email are candidate keys
which help us to uniquely identify the student record in the table.
What is the Alternate key?
•Candidate key are not chosen as a primary key called an
alternate key.
• However, A table may have single or multiple choices for the
primary key.
•Example: In that above student table.
•StudID, Roll No, Email are qualified to become a primary key.
But since StudID is the primary key, Roll No, Email becomes the
alternate key.
What is the Foreign key?
An attribute or set of attributes within one relation that matches
the candidate key or primary key of some other relation.
A foreign key is a column which is added to create a relationship
with another table.
StudID Roll No First Name LastName Email
1 11 Tom Price [email protected]
2 12 Nick Wright [email protected]
3 13 Dana Natan [email protected]

CourseID Coursename StudID


100 Java 1
101 C 2
102 Python 3
103 Oracle 1
Example for creation of Foreign Key:

CREATE TABLE Course (CourseID Number(10) ,


Coursename varchar2(20), StudID
Number(15), PRIMARY KEY (CourseID),
FOREIGN KEY (StudID) REFERENCES Students(S
tudID)
);
Difference Between Primary key & Foreign key

Primary Key Foreign Key

Helps you to uniquely identify a record in It is a field in the table that is the primary
the table. key of another table.

Primary Key never accept null values. A foreign key may accept multiple null
values.

You can have the single Primary key in a You can have multiple foreign keys in a
table. table.
ENTITY RELATIONSHIP MODEL(ER MODEL)

• ER model stands for an Entity-Relationship model. It is a high-


level data model.

• This model is used to define the data elements and


relationship for a specified system.

• It develops a conceptual design for the database.

• It also develops a very simple and easy to design view of data.

• In ER modeling, the database structure is portrayed as a


diagram called an entity-relationship diagram.
For example, Suppose we design a school database. In this
database, the student will be an entity with attributes like
address, name, id, age, etc. The address can be another entity
with attributes like city, street name, pin code, etc and there will
be a relationship between them.
Component of ER Diagram
Entity:
•An entity may be any object, class, person or place. In the ER
diagram, an entity can be represented as rectangles.
•Consider an organization as an example- manager, product,
employee, department etc. can be taken as an entity

Weak Entity
•An entity that depends on another entity called a weak entity. The
weak entity doesn't contain any key attribute of its own. The weak
entity is represented by a double rectangle.
Attribute
•The attribute is used to describe the property of an entity.
Eclipse is used to represent an attribute.
•For example, id, age, contact number, name, etc. can be
attributes of a student.
a. Key Attribute
•The key attribute is used to represent the main characteristics
of an entity. It represents a primary key.
• The key attribute is represented by an ellipse with the text
underlined.
b. Composite Attribute
•An attribute that composed of many other attributes is known
as a composite attribute.
•The composite attribute is represented by an ellipse, and those
ellipses are connected with an ellipse.
c. Multivalued Attribute
•An attribute can have more than one value. These attributes are
known as a multivalued attribute.
•The double oval is used to represent multivalued attribute.
•For example, a student can have more than one phone
number.

d. Derived Attribute
•An attribute that can be derived from other attribute is known
as a derived attribute. It can be represented by a dashed(dotted)
ellipse.
•For example, A person's age changes over time and can be
derived from another attribute like Date of birth.
Relationship
A relationship is used to describe the relation between entities.
Diamond Symbol is used to represent the relationship.

a. One-to-One Relationship
When only one instance of an entity is associated with the
relationship, then it is known as one to one relationship.
For example
b. One-to-many relationship
•When only one instance of the entity on the left, and more than
one instance of an entity on the right associates with the
relationship then this is known as a one-to-many relationship.
For example,
•Scientist can invent many inventions, but the invention is done
by the only specific scientist.
c. Many-to-one relationship
•When more than one instance of the entity on the left, and only
one instance of an entity on the right associates with the
relationship then it is known as a many-to-one relationship.
•For example, Student enrolls for only one course, but a course
can have many students.

d.Many-to-many relationship
•When more than one instance of the entity on the left, and
more than one instance of an entity on the right associates with
the relationship then it is known as a many-to-many
relationship.
• For example, Employee can assign by many projects and
project can have many employees.
Notation of ER diagram
•Database can be represented using the notations.
• In ER diagram, many notations are used to express the
cardinality. These notations are as follows:
Mapping Constraints
•A mapping constraint is a data constraint that expresses the
number of entities to which another entity can be related via a
relationship set.
•It is most useful in describing the relationship sets that involve
more than two entity sets.
•For binary relationship set R on an entity set A and B, there are
four possible mapping cardinalities. These are as follows:
– One to one (1:1)
– One to many (1:M)
– Many to one (M:1)
– Many to many (M:M)
• One to One: An entity of entity-set A can be associated with
at most one entity of entity-set B and an entity in entity-set B
can be associated with at most one entity of entity-set A.
• One to Many: An entity of entity-set A can be associated with
any number of entities of entity-set B and an entity in entity-
set B can be associated with at most one entity of entity-set
A.
• Many to One: An entity of entity-set A can be associated with
at most one entity of entity-set B and an entity in entity-set B
can be associated with any number of entities of entity-set A.
• Many to Many: An entity of entity-set A can be associated
with any number of entities of entity-set B and an entity in
entity-set B can be associated with any number of entities of
entity-set A.
• We can have these constraints in place while creating tables
in database.
Generalization
•Generalization is like a bottom-up approach in
which two or more entities of lower level combine to
form a higher level entity if they have some
attributes in common.
•Generalization uses the bottom-up approach.
•In generalization, entities are combined to form
a more generalized entity, i.e., subclasses are
combined to make a superclass.
•For example, Faculty and Student entities can
be generalized and create a higher level entity
Person.
Specialization
•Specialization is a top-down approach, and it is opposite to
Generalization.
•In specialization, one higher level entity can be broken down
into two lower level entities.
•For example: In an Employee management system, EMPLOYEE
entity can be specialized as TESTER or DEVELOPER based on
what role they play in the company.
Aggregation
•In aggregation, the relation between two entities is
treated as a single entity.
•In aggregation, relationship with its corresponding
entities is aggregated into a higher level entity.
For example:
• Center entity offers the Course entity act as a single
entity in the relationship which is in a relationship with
another entity visitor. In the real world, if a visitor visits
a coaching center then he will never enquiry about the
Course only or just about the Center instead he will ask
the enquiry about both.
ER diagram of Banking Application
ER diagram of Doctor patient relationship
ER diagram of Library function
Functional Dependency
Definition
Functional dependency play key role in
differentiating good database designs from bad
database design
A functional dependency is defined as a constraint
between two sets of attributes in a relation from a
database.
Given a relation R, a set of attributes X in R is said to
functionally determine another attribute Y, also in R,
(written X → Y) if and only if each X value is
associated with at most one Y value.
In other words….
X is the determinant set and Y is the dependent
attribute. Thus, given a tuple and the values of
the attributes in X, one can determine the
corresponding value of the Y attribute.
X →Y holds whenever two tuples have the same
value for x, then they must have the same
value for Y.
If t1[x]=t2[x] then t1[y]=t2[y] in any relation
instance of r(R)
Example
Employee

Empno EmpName JobType DeptName


6587 Lance Accountan Salary
Smith t
2398 Lance Engineer Product
Smith

Note: empno determines empname


Empno → empnme
empname → empno (wrong) because more than one employee
have the same name
Difference between Keys and FD
Whereas a key is a set of attributes that
uniquely identifies an entire tuple, a
functional dependency allows us to express
constraints that uniquely identify the values of
certain attributes.
Closure
Let a relation R have some functional dependencies F
specified. The closure of F (usually written as F+) is
the set of all functional dependencies that may be
logically derived from F.
Attribute Closure
Attribute closure of an attribute set can be defined as set of attributes which can be
functionally determined from it.

Using FD set of table 1, attribute closure can be determined as:

(STUD_NO)+ = {STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,


STUD_COUNTRY, STUD_AGE}
(STUD_STATE)+ = {STUD_STATE, STUD_COUNTRY}
Prime and Non-Prime Attributes
Attributes which are parts of any candidate key of relation are called as prime
attribute, others are non-prime attributes. For Example, STUD_NO in STUDENT
relation is prime attribute, others are non-prime attribute.
How to Find Candidate Keys and Super Keys Using Attribute
Closure?
• If attribute closure of an attribute set contains all attributes of relation, the
attribute set will be super key of the relation.
• If no subset of this attribute set can functionally determine all attributes of
the relation, the set will be candidate key as well. For Example, using FD
set of table 1
• (STUD_NO, STUD_NAME)+ = {STUD_NO, STUD_NAME,
STUD_PHONE, STUD_STATE, STUD_COUNTRY, STUD_AGE}
• (STUD_NO)+ = {STUD_NO, STUD_NAME, STUD_PHONE,
STUD_STATE, STUD_COUNTRY, STUD_AGE}
• (STUD_NO, STUD_NAME) will be super key but not candidate key
because its subset (STUD_NO)+ is equal to all attributes of the relation.
So, STUD_NO will be a candidate key.
Example
Student
SNo SNa CNo CNam Addr Instr. Offic
me e e
5425 Sury 102 Data … Smit B42
a Struct San h
ure
Roo
Jose, m
CA 112
7845 Vasu 541 SQL ...Sa Anan B24
n d Roo
SNo -> SName CNo -> CName Instr -> Office
SNo -> Addr Dieg m
o, 210
Axioms
We can use the following rules to find logically
implied functional dependencies
Developed by Armstrong in 1974, there are six
rules (axioms) that all possible functional
dependencies may be derived from them.
Axioms Cont.
1. Reflexivity Rule --- If X is a set of attributes and
Y is a subset of X, then X  Y holds.
each subset of Y is functionally dependent on X.
2. Augmentation Rule --- If X  Y holds and W is
a set of attributes, then WX  WY holds.
3. Transitivity Rule --- If X  Y and Y  Z holds,
then X  Z holds.
Derived Theorems from Axioms
4. Union Rule --- If X  Y and X  Z holds, then X
 YZ holds.
5. Decomposition Rule --- If X  YZ holds, then
so do X  Y and X  Z.
6. Pseudotransitivity Rule --- If X  Y and WY 
Z hold then so does WX  Z.
Back to the Example
SNo SNa CNo CNa Addr Instr. Offic
me me e
Based on the rules provided, the following dependencies can
be derived.

(SNo, CNo)  SNo (Rule 1) -- subset


(SNo, CNo)  CNo (Rule 1)
(SNo, CNo)  (SName, CName) (Rule 2) -- augmentation
CNo  office (Rule 3) -- transitivity
SNo  (SName, address) (Union Rule)
etc.
Types of Functional Dependency

 Trivial Functional Dependency

 Non-Trivial Functional Dependency

 Multivalued Functional Dependency

 Transitive Functional Dependency


Trivial Functional Dependency

A dependent is always a subset of the determinant.


i.e. If X → Y and Y is the subset of X, then it is called trivial
functional dependency
Emp_ID Name AGE
1 Vikram 45
2 Bala 40
3 Ashok 34
4 Vikram 29

{Emp_ID,Name} ----> Name

Since the dependent Name is a subset of determinant set


{Emp_ID,Name}
Non-Trivial Functional Dependency
The dependent is strictly not a subset of the determinant.
i.e. If X → Y and Y is not a subset of X, then it is called Non-
trivial functional dependency.

{Emp_ID,Name} ------> Age

In this above Example dependent Age is not a Subset of


Determinant hence its comes under the Non Trivial Functional
Dependency
Multivalued Functional Dependency
Multivalued functional dependency, entities of the dependent set
are not dependent on each other.
i.e. If a → {b, c} and there exists no functional
dependency between b and c, then it is called a multivalued
functional dependency.

Emp_ID ------> {Name,Age}

Name ------> Age (not a functional dependency)


Age ------> Name (not a functional dependency)
Transitive Functional Dependency

In transitive functional dependency, dependent is indirectly


dependent on determinant.
i.e. If a → b & b → c, then according to axiom of transitivity, a → c.
This is a transitive functional dependency.
Emp_ID Name Dept Building_no
42 Satish CSE 5
79 Vivek ECE 7
112 Arjun Mech 6
43 Kumar CSE 5
83 Rajesh ECE 7

Emp_ID → dept and dept → building_no. Hence, according to the axiom of


transitivity, Emp_ID → building_no is a valid functional dependency. This is an
indirect functional dependency, hence called Transitive functional dependency.
Fully Functional Dependency

In full functional dependency an attribute or a set of


attributes uniquely determines another attribute or set of
attributes. If a relation R has attributes X, Y, Z with the
dependencies X->Y and X->Z which states that those
dependencies are fully functional.

Partial Functional Dependency

In partial functional dependency a non key attribute


depends on a part of the composite key, rather than the
whole key. If a relation R has attributes X, Y, Z where X
and Y are the composite key and Z is non key attribute.
Then X->Z is a partial functional dependency in RBDMS.
ANOMALIES
• Insertion
– inserting one fact in the database
requires knowledge of other facts
unrelated to the fact being inserted
• Deletion
– Deleting one fact from the database
causes loss of other unrelated data from
the database
• Update
– Updating the values of one fact requires
multiple changes to the database
ANOMALIES EXAMPLES

TABLE: COURSE
COURSEID SECTIONID C_NAME
CIS564 072 Database Design
CIS564 073 Database Design
CIS570 072 Oracle Forms
CIS564 074 Database Design
ANOMALIES EXAMPLES
Insertion:
Suppose our university has approved a new
course called CIS563: SQL & PL/SQL.

Can this information about the new course be


entered (inserted) into the table COURSE in its
present form?
COURSEID SECTIONID C_NAME
CIS564 072 Database Design
CIS564 073 Database Design
CIS570 072 Oracle Forms
CIS564 074 Database Design
ANOMALIESEXAMPLES
Deletion:
Suppose not enough students enrolled for the
course CIS570 which had only one section 072. So,
the school decided to drop this section and delete
the sectionID 072 for CIS570 from the table COURSE.
But then, what other relevant info also got deleted in
the process?
COURSEID SECTIONID C_NAME
CIS564 072 Database Design
CIS564 073 Database Design
CIS570 072 Oracle Forms
CIS564 074 Database Design
ANOMALIES EXAMPLES
Update:
Suppose the course name (C_Name) for CIS
564 got changed to Database Management. How
many times do you have to make this change in the
COURSE table in its current form?

COURSEID SECTIONID C_NAME


CIS564 072 Database Design
CIS564 073 Database Design
CIS570 072 Oracle Forms
CIS564 074 Database Design
ANOMALIES
• So, a table (relation) is a stable (‘good’) table
only if it is free from any of these anomalies at
any point in time.
• You have to ensure that each and every table
in a database is always free from these
modification anomalies. And, how do you
ensure that?
• ‘Normalization’ theory helps.
Decomposition
• Decomposition is the process of breaking down one
table into multiple Tables.
• A decomposition of relation schema R consists of
replacing the relation schema by two relation
schema that each contain subset of attributes of R
and together include all attributes of R by storing
projections of the instance.
• For example in Employee_Department table

EID Ename Age City Salary Dept_ID Dept_Name


F001 Asai 34 Chennai 95000 Fin01 Finacne
A002 Mani 31 Trichy 78000 AM01 Automobile
E005 Muthuram 36 Coimbatore 75000 EDN01 Education
• We can replace above relation
schema(Employee_Department) into two relation
schema as follows
Employee
Department

EID Ename Age City Salary


F001 Asai 34 Chennai 95000
A002 Mani 31 Trichy 78000
E005 Muthuram 36 Coimbator 75000
e

Dept_ID EID Dept_Name


Fin01 F001 Finacne
AM01 A002 Automobile
EDN01 E005 Education
Properties Associated with Decomposition:
There are two properties associated with
Decomposition
1)Loss-Less join or Non Loss Decomposition
When all information found in original database
is preserved after decomposition , we call it as loss less
or non loss decomposition.
2) Dependency preservation
This is a property in which the constraints on the
original table can be maintained by simply enforcing
some constraints on each of the smaller relations.
NORMAL FORMS
1 NF
2NF
3NF
• BCNF (Boyce-Codd Normal Form)
• 4NF
• 5NF
• DK (Domain-Key) NF
Relationships of Normal Forms
First Normal Form(1NF)
Second Normal Form(2NF)
Third Normal Form(3NF)
Boyce-Codd Normal Form(BCNF)
Fourth Normal Form(4NF)
Fifth Normal Form(5NF) *

Domain/Key Normal Form (DK/NF)

Bordoloi
Database Normalization
Definition
• Normalization is the process of organizing the data in
the database
• Normalization is used to minimize the redundancy from
a relation or set of relations.
• Normalization divides the larger table into smaller table
and links them using relationship.
• A properly normalized database should have the
following characteristics
– Scalar values in each fields
– Absence of redundancy.
– Minimal use of null values.
– Minimal loss of information.
Levels of Normalization
• Levels of normalization based on the amount
of redundancy in the database.
• Various levels of normalization are:
– First Normal Form (1NF)
– Second Normal Form (2NF)

Redundancy

Number of Tables
– Third Normal Form (3NF)

Complexity
– Boyce-Codd Normal Form (BCNF)
– Fourth Normal Form (4NF)
– Fifth Normal Form (5NF)
– Domain Key Normal Form (DKNF)

Most
Mostdatabases
databasesshould
shouldbe
be3NF
3NFor
orBCNF
BCNFin inorder
orderto
toavoid
avoid
the
thedatabase
databaseanomalies.
anomalies.
Benefits of Normalization
• Less storage space
• Quicker updates
• Less data inconsistency
• Clearer data relationships
• Easier to add data
• Flexible Structure
Levels of Normalization
1NF
2NF
3NF
4NF
5NF
DKNF

Each
Eachhigher
higherlevel
levelisisaasubset
subsetof
ofthe
thelower
lowerlevel
level
First Normal Form (1NF)
A table is considered to be in 1NF if all the fields
contain
only scalar(Atomic) values (as opposed to list of
values).
Example
ISBN (NotAuName
Title 1NF)AuPhone PubName PubPhone Price

0-321-32132-1 Balloon Sleepy, 321-321-1111, Small House 714-000-0000 $34.00


Snoopy, 232-234-1234,
Grumpy 665-235-6532

0-55-123456-9 Main Street Jones, 123-333-3333, Small House 714-000-0000 $22.95


Smith 654-223-3455
0-123-45678-0 Ulysses Joyce 666-666-6666 Alpha Press 999-999-9999 $34.00

1-22-233700-0 Visual Roman 444-444-4444 Big House 123-456-7890 $25.00


Basic

Author
Authorand
andAuPhone
AuPhonecolumns
columnsare
arenot
notscalar
scalar
1NF - Decomposition
1. Place all items that appear in the repeating group in a
new table
2. Designate a primary key for each new table produced.
3. All the non key fields are functionally dependent on the
primary key.
Example (1NF)
ISBN AuName AuPhone

0-321-32132-1 Sleepy 321-321-1111

ISBN Title PubName PubPhone Price 0-321-32132-1 Snoopy 232-234-1234

0-321-32132-1 Balloon Small House 714-000-0000 $34.00 0-321-32132-1 Grumpy 665-235-6532

0-55-123456-9 Main Street Small House 714-000-0000 $22.95 0-55-123456-9 Jones 123-333-3333

0-123-45678-0 Ulysses Alpha Press 999-999-9999 $34.00 0-55-123456-9 Smith 654-223-3455

1-22-233700-0 Visual Big House 123-456-7890 $25.00 0-123-45678-0 Joyce 666-666-6666


Basic
1-22-233700-0 Roman 444-444-4444
Second Normal Form (2NF)
For a table to be in 2NF, there are two requirements
– The database is in first normal form
– All non-key attributes in the table must be functionally dependent
on the entire primary key. It has no partial dependency.
– Prime and non prime attributes
– Prime attributes– Which is a part of the Candidate key
– Non prime attributes- which is not a part of the primary
key(Candidate key)
Note: Remember that we are dealing with non-key attributes

Example 1 (Not 2NF)


Scheme  {Title, PubId, AuId, Price, AuAddress}
1. Key  {Title, PubId, AuId}
2. {Title, PubId, AuID}  {Price}
3. {AuID}  {AuAddress}
4. AuAddress does not belong to a entire key
5. AuAddress (Partially)functionally depends on AuId which is
a subset of a key
Second Normal Form (2NF)
Example 2 (Not 2NF)
Scheme  {City, Street, HouseNumber, HouseColor,
CityPopulation}
1. key  {City, Street, HouseNumber}
2. {City, Street, HouseNumber}  {HouseColor}
3. {City}  {CityPopulation}
4. CityPopulation does not belong to entire key.
5. CityPopulation is functionally dependent on the City which is a
proper subset of the key

Example 3 (Not 2NF)


Scheme  {studio, movie, budget, studio_city}
1. Key  {studio, movie}
2. {studio, movie}  {budget}
3. {studio}  {studio_city}
4. studio_city is not a part of a entire key
5. studio_city functionally depends on studio which is a proper
subset of the key
2NF - Decomposition
1. If a data item is fully functionally dependent on only a
part of the primary key, move that data item and that
part of the primary key to a new table.
2. If other data items are functionally dependent on the
same part of the key, place them in the new table also
3. Make the partial primary key copied from the original
table the primary key for the new table. Place all items
that appear in the repeating group in a new table
Example 1 (Convert to 2NF)
Old Scheme  {Title, PubId, AuId, Price, AuAddress}
New Scheme  {Title, PubId, AuId, Price}
New Scheme  {AuId, AuAddress}
2NF - Decomposition
Example 2 (Convert to 2NF)
Old Scheme  {Studio, Movie, Budget, StudioCity}
New Scheme  {Movie, Studio, Budget}
New Scheme  {Studio, City}

Example 3 (Convert to 2NF)


Old Scheme  {City, Street, HouseNumber, HouseColor,
CityPopulation}
New Scheme  {City, Street, HouseNumber, HouseColor}
New Scheme  {City, CityPopulation}
Third Normal Form (3NF)
This form dictates that all non-key attributes of a table must be
functionally dependent on a candidate key i.e. there can be no
interdependencies among non-key attributes.

For a table to be in 3NF, there are two requirements


– The table should be second normal form
– No attribute is transitively dependent on the non key(non key
attributes is functionally depends on one or more non key attributes)

Example (Not in 3NF)


Scheme  {ISBN,Title,Author_name,Author_country,Unit_price}
1. Key  {ISBN}
2. Except Author_country all the remaining fields depend on a
key hence 2NF
3. Transitively {Author_name}  {Author_Country} hence not in
3NF
Third Normal Form (3NF)
Example 2 (Not in 3NF)
Scheme  {BuildingID, Contractor, Fee}
1. Primary Key  {BuildingID}
2. {BuildingID}  {Contractor}
3. {Contractor}  {Fee}
4. {BuildingID}  {Fee}
5. Fee transitively depends on contractor
BuildingID Contractor Fee
Both Contractor and Fee depend on the entire key 100 Raja 1200
hence 2NF 150 Ashok 1100

200 Raja 1200

250 Pooja 1100

300 Raja 1200


3NF - Decomposition
1. Move all items involved in transitive dependencies to a
new entity.
2. Identify a primary key for the new entity.
3. Place the primary key for the new entity as a foreign
key on the original entity.

Example 1 (Convert to 3NF)


Old Scheme 
{ISBN,Title,Author_name,Author_country,Unit
_price}
New Scheme 
{ISBN,Title,unit_price,Author_name}
New Scheme  {Author_name,Author_Country}
3NF - Decomposition

Example 2 (Convert to 3NF)


Old Scheme  {BuildingID, Contractor, Fee}
New Scheme  {BuildingID, Contractor}
New Scheme  {Contractor, Fee}
BuildingID Contractor Contractor Fee

100 Raja Raja 1200

150 Ashok Ashok 1100

200 Raja Pooja 1100

250 Pooja
300 Raja
Boyce-Codd Normal Form (BCNF)

• It is the advance version of 3NF that’s why it is also referred as


3.5 NF.
• BCNF is stricter than 3NF.
• A table complies with BCNF if it is in 3NF and for every
functional dependency X -- Y, X should be the super key of
the table.
Example 1 - Address (Not in BCNF)
Scheme  {City, Street, ZipCode }
1. Key1  {City, Street }
2. Key2  {ZipCode, Street}
3. No non-key attribute hence 3NF
4. {City, Street}  {ZipCode}
5. {ZipCode}  {City}
6. Dependency between attributes belonging to a key
Boyce Codd Normal Form (BCNF)
Example 2 - Movie (Not in BCNF)
Scheme  {MovieTitle, MovieID, PersonName, Role, Payment }
1. Key1  {MovieTitle, PersonName}
2. Key2  {MovieID, PersonName}
3. Both role and payment functionally depend on both candidate keys
thus 3NF
4. {MovieID}  {MovieTitle}
5. Dependency between MovieID & MovieTitle Violates BCNF

Example 3 - Consulting (Not in BCNF)


Scheme  {Client, Problem, Consultant}
1. Key1  {Client, Problem}
2. Key2  {Client, Consultant}
3. No non-key attribute hence 3NF
4. {Client, Problem}  {Consultant}
5. {Client, Consultant}  {Problem}
6. Dependency between attributess belonging to keys violates BCNF
BCNF - Decomposition
1. Place the two candidate primary keys in
separate entities
2. Place each of the remaining data items in one
of the resulting entities according to its
dependency on the primary key.
Example 1 (Convert to BCNF)
Old Scheme  {City, Street, ZipCode }
New Scheme1  {ZipCode, Street}
New Scheme2  {City, Street}
• Loss of relation {ZipCode}  {City}
Alternate New Scheme1  {ZipCode, Street }
Alternate New Scheme2  {ZipCode, City}
Decomposition – Loss of
Information
1. If decomposition does not cause any loss of information
it is called a lossless decomposition.
2. If a decomposition does not cause any dependencies to
be lost it is called a dependency-preserving
decomposition.
3. Any table scheme can be decomposed in a lossless way
into a collection of smaller schemas that are in BCNF
form. However the dependency preservation is not
guaranteed.
4. Any table can be decomposed in a lossless way into 3rd
normal form that also preserves the dependencies.
• 3NF may be better than BCNF in some cases

Use
Useyour
yourown
ownjudgment
judgmentwhen
whendecomposing
decomposingschemas
schemas
BCNF - Decomposition
Example 2 (Convert to BCNF)
Old Scheme  {MovieTitle, MovieID, PersonName, Role, Payment }
New Scheme  {MovieID, PersonName, Role, Payment}
New Scheme  {MovieTitle, PersonName}
• Loss of relation {MovieID}  {MovieTitle}
New Scheme  {MovieID, PersonName, Role, Payment}
New Scheme  {MovieID, MovieTitle}
• We got the {MovieID}  {MovieTitle} relationship back

Example 3 (Convert to BCNF)


Old Scheme  {Client, Problem, Consultant}
New Scheme  {Client, Consultant}
New Scheme  {Client, Problem}
Boyce-Codd Normal Form (BCNF)
Boyce-Codd normal form (BCNF)
A relation is in BCNF, if and only if, every determinant is a
candidate key.

The difference between 3NF and BCNF is that for a functional


dependency A  B, 3NF allows this dependency in a relation
if B is a primary-key attribute and A is not a candidate key,

whereas BCNF insists that for this dependency to remain in a


relation, A must be a candidate key.
ClientInterview
ClientN interviewDat interviewTim
staffNo roomNo
o e e
CR76 13-May-02 10.30 SG5 G101

CR76 13-May-02 12.00 SG5 G101

CR74 13-May-02 12.00 SG37 G102

CR56 1-Jul-02 10.30 SG5 G102

• FD1 clientNo, interviewDate  interviewTime, staffNo, roomNo (Primary Key)

• FD2 staffNo, interviewDate, interviewTime clientNo (Candidate


key)

• FD3 roomNo, interviewDate, interviewTime  clientNo, staffNo (Candidate key)

• FD4 staffNo, interviewDate  roomNo (not a candidate key)

• As a consequece the ClientInterview relation may suffer from update anmalies.

• For example, two tuples have to be updated if the roomNo need be changed for staffNo SG5
on the 13-May-02.
Example of BCNF(2)
To transform the ClientInterview relation to BCNF, we must
remove the violating functional dependency by creating two new
relations called Interview and StaffRoom as shown below,

Interview (clientNo, interviewDate, interviewTime, staffNo)


StaffRoom(staffNo, interviewDate, roomNo)
Interview
ClientNo interviewDate interviewTime staffNo
CR76 13-May-02 10.30 SG5
CR76 13-May-02 12.00 SG5
CR74 13-May-02 12.00 SG37
CR56 1-Jul-02 10.30 SG5

StaffRoom
staffNo interviewDate roomNo
SG5 13-May-02 G101
SG37 13-May-02 G102
SG5 1-Jul-02 G102

CNF Interview and StaffRoom relations


Fourth Normal Form (4NF)
• Fourth normal form eliminates independent many-to-
one relationships between columns.
• To be in Fourth Normal Form,
– a relation must first be in Boyce-Codd Normal Form.
– a given relation may not contain more than one multi-
valued attribute.

Example (Not in 4NF)


Scheme  {MovieName, ScreeningCity, Type)
Primary Key: {MovieName, ScreeningCity, Type)
1. All columns are a part of the only candidate key, hence
BCNF
2. Many Movies can have the same Type
3. Many Cities can have the same movie
4. Violates 4NF
R.Movie  R.ScreeningCity/R.Type

Movie ScreeningCity Type

SMS Thanjavur Comedy

SMS Trichy Comedy

Sami Chennai Action

Sami Coimbatore Action

Aranmanai Trichy Ghost


Fourth Normal Form (4NF)
Example 2 (Not in 4NF) Manager Child Employee

Scheme  {Manager, Child, Employee} Jim Beth Alice

1. Primary Key  {Manager, Child, Employee} Mary Bob Jane

2. Each manager can have more than one childMary NULL Adam

3. Each manager can supervise more than one employee


4. 4NF Violated

Example 3 (Not in 4NF)


Scheme  {Employee, Skill, ForeignLanguage}
1. Primary Key  {Employee, Skill, Language }
2. Each employee can speak multiple languages
3. Each employee can have multiple skills Employee Skill Language

4. Thus violates 4NF 1234 Cooking French

1234 Cooking German


1453 Carpentry Spanish

1453 Cooking Spanish


2345 Cooking Spanish
4NF - Decomposition
1. Move the two multi-valued relations to separate tables
2. Identify a primary key for each of the new entity.

Example 1 (Convert to 3NF)


Old Scheme  {MovieName, ScreeningCity, Type}
New Scheme  {MovieName, ScreeningCity}
New Scheme  {MovieName, Type}
R.Movie R.Type R.Movie  R.ScreeningCity
Movie Type Movie ScreeningCity

SMS Comedy SMS Thanjavur

Sami Action SMS Trichy

Aranmanai Ghost Sami Chennai

Sami Coimbatore

Aranmanai Trichy
4NF - Decomposition
Example 2 (Convert to 4NF) Manager Child Manager Employee

Old Scheme  {Manager, Child, Employee}


Jim Beth Jim Alice

New Scheme  {Manager, Child} Mary Bob Mary Jane


Mary Adam
New Scheme  {Manager, Employee}

Example 3 (Convert to 4NF)


Old Scheme  {Employee, Skill, ForeignLanguage}
New Scheme  {Employee, Skill}
New Scheme  {Employee, ForeignLanguage}
Employee Skill Employee Language

1234 Cooking 1234 French

1453 Carpentry 1234 German

1453 Cooking 1453 Spanish


2345 Cooking 2345 Spanish
Fifth Normal Form (5NF)
• Fifth normal form is satisfied when all tables are broken
into as many tables as possible in order to avoid
redundancy. Once it is in fifth normal form it cannot be
broken into smaller relations without changing the facts or
the meaning.
• It is otherwise called as Project Join Normal
Form(PJNF)
Rules:
 It should be in 4NF
 No Multivalued Dependency
 Does not allow join Dependency
 If a relation is in join dependency if a Schema (R) is
decomposed into (R1,R2,R3…….Rn)
 If a decomposition will be loss less join
Decomposition
Domain Key Normal Form (DKNF)
• The relation is in DKNF when there can be no
insertion or deletion anomalies in the
database.
SQL
• SQL language as a “query language,” it can do more than just
query a database. It can define the structure of the data,
modify data in the database, and specify security constraints.
• IBM developed the original version of SQL, originally called
Sequel in the early 1970s. The Sequel language has evolved
and its name has changed to SQL
• (Structured Query Language).
• Many products now support the SQL language.
• SQL has clearly established itself as the standard relational
database language.
• In 1986, the American National Standards Institute (ANSI) and
the International Organization for Standardization (ISO)
published an SQL standard, called SQL-86. ANSI published
most recently SQL:2008.
• The SQL language has several parts:
Data-definition language (DDL). The SQL DDL provides
commands for defining relation schemas, deleting relations,
and modifying relation schemas.
Data-manipulation language (DML). The SQL DML provides the
ability to query information from the database and to insert
tuples into, delete tuples from, and modify tuples in the
database.
Integrity. The SQL DDL includes commands for specifying
integrity constraints that the data stored in the database
must satisfy. Updates that violate integrity constraints are
disallowed.
View definition. The SQL DDL includes commands for defining
views.
Transaction control. SQL includes commands for specifying the
beginning and ending of transactions.
• Embedded SQL and dynamic SQL. Embedded and dynamic
SQL define how SQL statements can be embedded within
general-purpose programming languages, such as C, C++, and
Java.
• Authorization. The SQL DDL includes commands for
specifying access rights to relations and views.
Data Types
Data types are a classification of a particular type of information.
SQL support different types of data.
• character(n): A fixed-length character string with user-
specified length n. Ex: Name Character(15)
• varchar(n): A variable-length character string with user-
specified maximum length n. The full form, character varying,
is equivalent.
Ex: Name varchar(n)
• int: An integer (a finite subset of the integers that is machine
dependent). The full form, integer, is equivalent.
Ex: Regno integer(15)
• smallint: A small integer (a machine-dependent subset of the
integer type). Ex: age small int(3)
• numeric(p, d):
This data type represents a decimal number ‘p’ digits and sign
with assumed decimal point ‘q’ digits from the sign.Both ‘p’ and
‘q’ are integers. Thus, price numeric(3,1) allows 44.5 to be
stored exactly, but neither 444.5 or 0.32 can be stored exactly in
a field of this type.
•real, double precision: Floating-point and double-precision
floating-point numbers with machine-dependent precision.
•• float(n): A floating-point number, with precision of at least n
digits.
•Ex: Rate float(5,2)
• date: A calendar date containing a (four-digit) year, month,
and day of the month.(DD-MON-YY)
Ex: DOB date
• • time: The time of day, in hours, minutes, and seconds. A
variant, time(p),can be used to specify the number of
fractional digits for seconds (the default being 0).
• Ex: Arrival_Time time
• timestamp: A combination of date and time. A variant,
timestamp(p), can be used to specify the number of fractional
digits for seconds
• Date and time values can be specified like this:
• date ’2001-04-25’
• time ’09:30:00’
• timestamp ’2001-04-25 10:29:01.45’
DDL
SQL Data Definition
•The set of relations in a database must be specified to the
system by means of a data-definition language (DDL).
The SQL DDL allows specification of not only a set of relations,
but also information about each relation, including:
• The schema for each relation.
• The types of values associated with each attribute.
• The integrity constraints.
• The set of indices to be maintained for each relation.
The security and authorization information for each relation.
• The physical storage structure of each relation on disk.
• The different DDL commands are as follows:
• Create table tablename
• Create table tablename as select
• Alter table tablename add
• Alter table tablename modify
• Drop table tablename
Basic Schema Definition
• We define an SQL relation by using the create table
command. The following command creates a relation
department in the database.
Syntax:
Create table relation_name(field1 datatype(size),Field2 datatype
(size)……….fieldn datatype(size));
Example:
Create table department
(dept_name varchar (20),
building varchar (15),
budget numeric (12,2),
primary key (dept_name));

• The primarykey attributes are required to be non-null and


unique; that is, no tuple can have a null value for a primary-key
attribute,
•and no two tuples in the relation can be equal on all the
primary-key attributes.
•The foreign key specification says that the values of attributes
for any tuple in the relation must correspond to values of the
primary key attributes of some tuple in relation s.
Create table course(courseid varchar (7),title varchar (50),
dept_name varchar (20),credits numeric (2,0),
primary key (courseid),
foreign key (dept_name) references department);

Create table instructor(ID varchar (5),


name varchar (20) not null,dept_name varchar (20),
salary numeric (8,2),primary key (ID),
foreign key (dept_name) references department);

Create table section(courseid varchar (8),sec id varchar (8),


semester varchar (6),year number(3),
building varchar (15),room_number number (3),
time slot id varchar (4),primary key (course id, sec id, semester),
foreign key (courseid) references course);
Create table as select:
This type of create command is used to create the structure of a
new table from the structure of existing table

Create table EngDept(dept_name ,building,budget) as select


dept_name,building,budget from department;
Drop:
•To remove a relation from an SQL database, we use the drop
table command. The drop table command deletes all
information about the dropped relation from the database.
•drop table r;
Alter table:
•We use the alter table command to add attributes to an
existing relation
• The form of the alter table command is
• alter table r add A D;
• where r is the name of an existing relation, A is the name of
the attribute to be added, and D is the type of the added
attribute.
• We can drop attributes from a relation by the command
• alter table r drop A
• where r is the name of an existing relation, and A is the name
of an attribute of the relation.
• Many database systems do not support dropping of
attributes.
• alter table r Modify A D;
Ex:
Alter table section modify (room_no varchar2(8));
Truncating a table:
•Removes all the records from the table.
•Structure of the table stays intact(Complete)
Truncate table Tablename;
Indexes:
Indexes are optional structure associated with tables.
We can create indexes explicitly to speed up sql statement
execution on a table.
Types of index:
1.Simple index(It is created on single column of a table)
Ex;
Create index stud_index on students(Regno);
2.Composite index(It is created on multiple column of a table)
EX:
Create index multi_index on students(Rollno,Regno);
• The indexes on above examples do not force uniqueness that
is the column included in the index can have duplicate values.
• To create unique index the keyword unique should be
included in create index.
Ex:
Create unique index stud_index on students(Regno);
• When the user defines a primary key or a unique key
constraints, oracle automatically create unique indexes on
the primary key column or unique key.
Dropping index:
Drop index stud_index;
Basic Structure of SQL:
The basic structure of SQL expression consist of three clauses:
Select
From
Where
•The query takes as its input the relations listed in the from
clause, operates on them as specified in the where and select
clauses, and then produces a relation as the result.
•The role of each clause is as follows:
• The select clause is used to list the attributes desired in the
result of a query.
• The from clause is a list of the relations to be accessed in the
evaluation of the query.
• The where clause is a predicate involving attributes of the
relation in the from clause.
• A typical SQL query has the form
select A1, A2, . . . , An
from r1, r2, . . . , rm
where P;
• Ex:
• select name from instructor;
• select dept_name from instructor;
• we want to force the elimination of duplicates, we insert the
keyword distinct after select. We can rewrite the preceding
query as:
• select distinct dept_name from instructor;
• SQL allows us to use the keyword all to specify explicitly that
duplicates are not removed:
• select all dept name from instructor;
1.Find the names of all instructors in the Computer Science
department who have salary greater than 70,000.

•The select clause may also contain arithmetic expressions


involving the operators +, −, ∗, and / operating on constants or
attributes of tuples.
•For example, the query:
•select ID, name, dept_name, salary * 1.1
from instructor;
returns a relation that is the same as the instructor relation,
except that the attribute salary is multiplied by 1.1.
This shows what would result if we gave a 10% raise to each
instructor; note, however, that it does not result in any change
to the instructor relation.
select name
from instructor
where dept name = ’Comp. Sci.’ and salary > 70000;
Queries on Multiple Relations
suppose we want to answer the query “Retrieve the names
of all instructors, along with their department names and
department building name.”
•Looking at the schema of the relation instructor, we realize that
we can get the department name from the attribute dept name,
but the department building name is present in the attribute
building of the relation department.
•To answer the query, each tuple in the instructor relation must
be matched with the tuple in the department relation whose
dept name value matches the dept name value of the instructor
tuple.
• In SQL, to answer the above query, we list the relations that
need to be accessed in the from clause, and specify the
matching condition in the where clause.
• The above query can be written in SQL as
select name, instructor.dept_name, building
from instructor, department
where instructor.dept_name= department.dept_name;

Name Dept_name Building


Srinivasan Comp.Science Taylor
Katz Comp.Science Taylor
Wu Finance Painter
Singh Finance Painter
Mozart Music Packard
Etc……..
The Natural Join:
The natural join operation operates on two relations
and produces a relation as the result.
natural join considers only those pairs of tuples with
the same value on those attributes that appear in the
schemas of both relations.
Example of the relations instructor and teaches,
instructor natural join teaches considers only those
pairs of tuples where both the tuple from instructor
and the tuple from teaches have the same value on the
common attribute, ID.
• Consider the query “For all instructors in the university who
have taught some course, find their names and the course ID
of all courses they taught”,
select name, course id
from instructor, teaches
where instructor.ID= teaches.ID;
• This query can be written using the natural-join operation in
SQL as:
select name, course id
from instructor natural join teaches;
Both of the above queries generate the same result.
Additional Basic Operations
•There are number of additional basic operations that are
supported in SQL.
The Rename Operation
SQL provides a mechanism for renaming both relations and
attributes.
Consider again the query that we used earlier:
select name, course id from instructor, teaches
where instructor.ID= teaches.ID;
•The result of this query is a relation with the following
attributes:
name, course_id
SQL provides a way of renaming the attributes of a result
relation.
•old-name as new-name
• The as clause can appear in both the select and from clauses.
• For example, if we want the attribute name to be replaced
with the name instructor name, we can rewrite the preceding
query as:
select name as instructor name, course id
from instructor, teaches
where instructor.ID= teaches.ID;
The as clause is particularly useful In renaming relations.

select T.name, S.course_id


from instructor as T, teaches as S
where T.ID= S.ID;
Decomposition
• Decomposition is the process of breaking down in parts or
elements.
• It replaces a relation with a collection of smaller relations.
• It breaks the table into multiple tables in a database.
• It should always be lossless, because it confirms that the
information in the original relation can be accurately
reconstructed based on the decomposed relations.
• If there is no proper decomposition of the relation, then it
may lead to problems like loss of information.
• Properties of Decomposition
• Following are the properties of Decomposition,
1. Lossless Decomposition
2. Dependency Preservation
3. Lack of Data Redundancy
Transaction
• Collections of operations that form a single logical unit of
work are called transactions.
• A database system must ensure proper execution of
transactions despite System failures—either the entire
transaction executes, or none of it does.
• A transaction is a unit of program execution that accesses
and possibly updates various data items.
• Usually, a transaction is initiated by a user program written in
a high-level data-manipulation language (typically SQL), or
programming language (for example, C++, or Java), with
embedded database accesses in JDBC or ODBC.
• The transaction consists of all operations executed between
the begin transaction and end transaction.
Properties of Transaction(ACID)
•Atomicity
•Consistency
•Isolation
•Durability
Atomicity:
•Either all operations of the transaction are reflected properly in the
database, or none are.
•This “all-or-none” property is referred to as atomicity

Consistency(Starting to ending of the transaction to be consistent)


The Db must remain the consistent state after performing any
transaction.
For example: after the ATM withdrawal operation, the balance
must be updated appropriately after performing transaction.
Isolation:
Even though multiple transactions may execute concurrently,
the
system guarantees that, for every pair of transactions Ti and Tj ,
it appears to Ti that either Tj finished execution before Ti started
or Tj started execution after Ti finished. Thus, each transaction is
unaware of other transactions executing concurrently in the
system.
Durability:
After a transaction completes successfully, the changes it has
made to the database persist, even if there are system failures.
Transaction access data using two operation:
Read
write
read(X)
which transfers the data item X from the database to a
variable, also called X, in a buffer in main memory belonging to
the transaction that executed the read operation.
write(X)
which transfers the value in the variable X in the main-
memory buffer of the transaction that executed the write to the
data item X in the database.
•Let Ti be a transaction that transfers Rs.50 from account A to
account B. This transaction can be defined as:
Ti : read(A);
A := A − 50;
write(A);
read(B);
B := B + 50;
write(B).
• Let us now consider each of the ACID properties. (For ease of
presentation, we consider them in an order different from the
order A-C-I-D.)
• Consistency: The consistency requirement here is that the
sum of A and B be unchanged by the execution of the
transaction. Without the consistency requirement, money
could be created or destroyed by the transaction!
• It can be verified easily that, if the database is consistent
before an execution of the transaction, the database remains
consistent after the execution of the transaction.
Atomicity:
• Suppose that, just before the execution of transaction Ti, the
values of accounts A and B are Rs.1000 and Rs.2000,
respectively.
• Now suppose that, during the execution of transaction Ti , a
failure occurs that prevents Ti from completing its execution
successfully.
• Further, suppose that the failure happened after the write(A)
operation but before the write(B) operation.
• In this case, the values of accounts A and B reflected in the
database are $950 and $2000. The system destroyed $50 as a
result of this failure. In particular, we note that the sum A + B
is no longer preserved.
State diagram of the transaction:
Active,
• the initial state; the transaction stays in this state while it is
executing.
Partially committed, after the final statement has been
executed.
Aborted
after the transaction has been rolled back and the database has
been restored to its state prior to the start of the transaction.
Failed
after the discovery that normal execution can no longer proceed
Committed, after successful completion.
Schedules
• Schedule is a order of multiple transaction executing
in concurrent environment.
Types of Schedule
1.Serial schedule
2.Non-Serial schedule
Non-Serial Schedule further classified as
• Serializable [Conflict serializable and view Serializable]
• Non-Serializable
Serial Schedules:
Schedules in which the transactions are executed non-
interleaved,
• i.e., a serial schedule is one in which no transaction starts until
a running transaction has ended are called serial schedules.
•Example: Consider the following schedule involving two
transactions T1 and T2.

T1 T2
R(A)

W(A)

R(B)

W(B)

R(A)

R(B)
Non-Serial Schedule
•This is a type of Scheduling where the operations of multiple
transactions are interleaved.
•This might lead to a rise in the concurrency problem. The
transactions are executed in a non-serial manner, keeping the
end result correct and same as the serial schedule.
• Unlike the serial schedule where one transaction must wait for
another to complete all its operation, in the non-serial schedule,
the other transaction proceeds without waiting for the previous
transaction to complete.
•It can be of two types namely, Serializable and Non-Serializable
Schedule.

You might also like