Professional Documents
Culture Documents
in
DBMS
BSC (MSCS,MPCS)
SEM – IV
BY
CS-SIR
C.AMBADAS
Unit - I
Introduction: Database-System Applications, Purpose of Database Systems, View of Data,
Database Languages, Relational Databases, Database Design, Data Storage and Querying,
Transaction Management, Database Architecture, Database Users and Administrators.
Introduction to the Relational Model: Structure of Relational Databases, Database Schema,
Keys, Schema Diagrams, Relational Query Languages, Relational Operations.
Unit - II
Database Design and the E-R Model: Overview of the Design Process, The Entity- Relationship
Model, Constraints, Removing Redundant Attributes in Entity Sets, Entity- Relationship Diagrams,
Reduction to Relational Schemas, Entity-Relationship Design Issues, Extended E-R Features,
Alternative Notations for Modeling Data, Other Aspects of Database Design.
Relational Database Design: Features of Good Relational Designs, Atomic Domains and First
Normal Form, Decomposition Using Functional Dependencies, Functional- Dependency Theory,
Decomposition Using Multivalued Dependencies, Normal Forms-2 NF, 3 NF, BCNF, The
Database Design Methodology for Relational Databases.
Unit - III
Introduction to SQL: Overview of the SQL Query Language, SQL Data Definition, Basic Structure
of SQL Queries, Additional Basic Operations, Set Operations, Null Values, Aggregate Functions,
Nested Subqueries, Modification of the Database.
Intermediate SQL: Join Expressions, Views, Transactions, Integrity Constraints, SQL Data Types
and Schemas, Authorization.
Advanced SQL: Accessing SQL from a Programming Language, Functions and Procedures,
Triggers, Recursive Queries.
Unit - IV
Transaction Management: Transaction Support–Properties of Transactions, Database Architecture,
Concurrency Control–The Need for Concurrency Control, Serializability and Recoverability,
Locking Methods, Deadlock, Time Stamping Methods, Multi-version Timestamp Ordering,
Optimistic Techniques, Granularity of Data Items, Database Recovery– The Need for Recovery,
Transactions and Recovery, Recovery Facilities, Recovery Techniques, Nested Transaction Model.
Security: Database Security–Threats, Computer- Based Controls–Authorization, Access Controls,
Views, Backup and Recovery, Integrity, Encryption, RAID.
Text book:
1. Silberschatz, H. Korth and S. Sudarshan, Database System Concepts, 6th Ed., Tata
McGraw Hill, 2011
2. Thomas M. Connolly, Carolyn E. Begg, Database Systems–A Practical Approach to
Design, Implementation, and Management (6e)
Railway reservation system: Data base is required to keep record of ticket booking,
Train departure and arrival status. Also if trains get late then people get to know it
through database update.
Library management: There are thousands of books in a library, so it is very difficult
to keep record of all the books in a copy or register. so, DBMS used to maintain all the
information relate to book issue data name of the book, author and availability of the
book.
Banking: We make thousands of transaction through banks daily and we can do this
without going to the bank. so how banking has become so easy that by sitting at name
we can send or get money through banks that is all possible just because of DBMS that
manages all the bank transactions.
University & Colleges: Examinations are done online to day and universities and
colleges maintain all these records through DBMS. Student registration details, courses
and grades are stored in data base.
Credit card transactions: For purchase of credit cards and all the other transactions
are made possible by DBMS. A credit card holder knows the importance of their
information that all are secured through DBMS.
Social media sites: We are all on social media websites to share our views and
connect with our friends. Daily millions of users signed up for the social media accounts
like face book, twitter, pinterest , google plus , etc…
But how all the information‟s of users are stored and how we become able to connect to
other people, yes this is all because of “DBMS”.
Tele communications: Any tele communication company cannot everything about
their business without DBMS. It is must for these companies to store the call details and
monthly passed paid bills.
A file based system is a collection of application programs that perform services for the
users wishing to access information. Each program within a file based system defines
and manages its own data. Because of this, there are limits as to how that data can be
used or transported.
Limitations of File based system:
1. Data Duplication:
Each program stores its own supported files . The same data accessed by different
programs, then each program must store its own copy of the same data.
Data is stored in different files by different application programs . This makes it difficult
and expensive to implement security on data.
With the traditional file processing system each application has its own private files and
users have little opportunities to share data outside this own application.
4. Data dependency : Here, any change to a file structure, forces modifications in all of
the programs that use the data in that file. And these modifications are likely to produce
errors.
As the number of files in the system expands, system administration becomes more
difficult.
Advantages of DBMS:
The DBMS has a no. of advantages as compared to traditional file based processing
approach.
1. Data Independence:
The separation of data description from the application programs that use the data is
called data independence.
This allows an organization's data to change without changing the application programs
that process the data.
In non- database system each application program has its own private files in this case,
the duplicated copies of the same data are created in many places.
In DBMS all data of an organization integrated into a single database file, the data is
recorded in only one place in the database & it is not duplicated.
By controlling the data redundancy the data consistency is obtained. If a data item
appears only once, any update to its value has to be performed only once & the updated
value is immediately available to all users if the DBMS has controlled redundancy the
database system enforces consistency.
For example: If a customer address is stored only once, updating that becomes simple.
B.Sc II Year IV Sem C.AMBADAS Page 5
6. Integrity can be maintained: The data values stored in the database must satisfy
certain types of consistency rules.
For example: The balance of a bank account may never fall below a particular amount
to define integrity constraints to the data in the data base.
7.Security can be enforced: Not every user should be able to access all the data. A data
base can be accessed only while proper authentication usually by verifying login and
password.
8. Improved data access:
The DBMS makes it possible to produce quick answers to any queries. A query is a
request or a question put to the DBMS for data manipulation or retrieval. Without any
programming experience, one can retrieve and display data very easily.
In computers file based system, the user creates the backup of data regularly to protect
the valuable data from damage due to the computer system (or) application program it is
very time consuming method, if the amount of data is large.
1. Internal Level
o The internal level has an internal schema which describes the physical storage
structure of the database.
o The internal schema is also known as a physical schema.
o It uses the physical data model. It is used to define that how the data will be
stored in a block.
o The physical level is used to describe complex low-level data structures in detail.
o This level is at the bottom level architecture of DBMS.
2. Conceptual Level
o The conceptual schema describes the design of a database at the conceptual level.
Conceptual level is also known as logical level.
o The conceptual schema describes the structure of the whole database.
o The conceptual level describes what data are to be stored in the database and also
describes what relationship exists among those data.
o In the conceptual level, internal details such as an implementation of the data
structure are hidden.
o Programmers and database administrators work at this level.
3. External Level
o An external schema is also known as view schema.
o In the external level, a database contains several schemas that sometimes called
as subschema. The subschema is used to describe the different view of the
database.
o In the external level there are the 'n' number of external end users who can view
the data from the database.
o In this level the required data is provided to the end users. Because there is no
need to know about the all data structure from the database. The users need only
that data which is required.
o The data can be viewed uniquely as separately by the users as viewers.
o The external level is at the top level in the architecture of DBMS.
Q) Relational Database?
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 Data-Manipulation Language, Data- Definition Language and Database
access from application programs.
1) Tables: Each table has multiple columns and each column has a unique
name. Each table contains records of a particular type. Each record type defines a
B.Sc II Year IV Sem C.AMBADAS Page 8
Design process:- data base designing is a cruel to high performance database system.
Requirement analysis
B.Sc II Year IV Sem C.AMBADAS Page 9
Authorization and Integrity Manager: Tests for the satisfaction of integrity constraints
and checks the authority of users to access data.
Transaction Manager: Ensures that the database remains in a uniform state despite
system failures where the ongoing transaction executions proceed without conflict.
File Manager: Manages the allocation of space on disk storage and therefore the data
structures used to represent information on the disk.
Buffer Manager: Accountable for fetching data from disk storage into main memory,
and deciding what data to cache in main memory.
Data Files: Stores the database itself.
Q) Transaction management:
A transaction is a collection of operations that performs a single logical function in
a database applications. In other words, it is a logical unit of work that must be either
entirely completed or aborted.
For example: a transaction that changes the contents of the database must alter the
database from one consistent database state of another.
Hierarchical model:- In these model data is organized into a tree like structure. The
structure represents information using parent child relationship each parent can have
B.Sc II Year IV Sem C.AMBADAS Page 11
Network model:- In this network model a parent can have several children and a child
can also have many parent records. Records are physically linked through pointers. This
model was developed in 1960‟s. In this model one to many relationship between the
records. A relationship is called a set. It is composed of parent record and a child record
a set represents a one to many (1:M) relationship between parent and child.
Relational model: The relational data base was introduced in 1970 by E.F codd. In
these model data is maintained in the form of the table consisting of rows & columns
and not through pointers.
Table : EMP
Emp no Emp name Job Salary Dept no
(foreign key)
101 Abc Manager 12000 10
102 Def Clerk 6000 20
103 Ghi Clerk 6500 20
104 Jki Clerk 7000 30
105 Mno Clerk 7500 30
Table: Dept
Deptno D name
(Primay key)
10 Finance
20 Accounts
Group
Enroll Class
STUDENT
credits major
Object based data model: object oriented programming has become the software
development methodology this lead to the development of the object oriented data model
that can be seen as extending the ER model with notations of encapsulation methods
and object identity.
OOD model
IN VOICE Belongs to
INVOICE CUSTOMER
Data
member
HAS
Customer
line
LINE
Q)DBMS Architecture
A Database Architecture is a representation of DBMS design. It helps to design,
develop, implement, and maintain the database management system. A DBMS
architecture allows dividing the database system into individual components that can be
independently modified, changed, replaced, and altered. It also helps to understand the
components of a database.
A Database stores critical information and helps access data quickly and securely.
Therefore, selecting the correct Architecture
Database architecture can be seen as a single tier or multi-tier. But logically, database
architecture is of two types like: 2-tier architecture and 3-tier architecture.
1-Tier Architecture
o In this architecture, the database is directly available to the user. It means the
user can directly sit on the DBMS and uses it.
o Any changes done here will directly be done on the database itself. It doesn't
provide a handy tool for end users.
o The 1-Tier architecture is used for development of the local application, where
programmers can directly communicate with the database for the quick response.
2-Tier Architecture
o The 2-Tier architecture is same as basic client-server. In the two-tier architecture,
applications on the client end can directly communicate with the database at the
server side. For this interaction, API's like: ODBC, JDBC are used.
o The user interfaces and application programs are run on the client-side.
o The server side is responsible to provide the functionalities like: query processing
and transaction management.
o To communicate with the DBMS, client-side application establishes a connection
with the server side.
3-Tier Architecture
o The 3-Tier architecture contains another layer between the client and server. In
this architecture, client can't directly communicate with the server.
o The application on the client-end interacts with an application server which
further communicates with the database system.
o End user has no idea about the existence of the database beyond the application
server. The database also has no idea about any other user beyond the
application.
o The 3-Tier architecture is used in case of large web application.
Sophisticate end users:- sophisticated end users interact with the system without
writing programs. They form requests by writing queries in a database query language.
These are submitted to query processor analysis who submit queries to explore data in
the data base fall in these category.
Specialized end users: - who writes specialized DB applications that do not fit into
fractional DB processing frame work.
Naive end users: - who need not be aware of the presence of the DB systems these are
end users of DB who work through menu driven application.
System analysis: - System analysis determines the requirements of end user and
develop specification for cannel transaction that meets this requirements.
Canned transaction: - Readymade programs through which naïve end users interacts
with the database are called canned transaction.
Online Users: Who may communicate with DB directly through online.
Data base administrator:-
A person in the organization who controls the design and the use of the data base is
called database administrator (DBA). The functions of DBA includes.
Schema definition: - The DBA definition of the logical schema of the data base
according to this schema database will be developed to store required data for an
organization.
Storage structure and access method definition: - The DBA decides how the data is
to be represented in the stored database.
Assisting application programmers: - The DBA provides assistance to application
programmers to develop application programs.
Physical organization modification: - The DBA modifies the physical organization of
the database reflect the changing needs of the organization or to improve performance.
Approving data access:- The DBA determines which user needs access to which part
of the database to which part of the database. According to this, various types of
authorizations are granted to different users.
Monitoring performance:- The DBA monitors performance of the system. The DBA
ensures that better performance is maintained by making changes in physical and
logical schema is required.
Chapter-2
Type Entity
Person EMP, Student
Place Store house
Object Computer, book
Event Sales, admissions
Concept Account, course
Attributes
STUDENT
Entity set
Domains of attributes: A set of values that may be assigned to the attributes of each
individual entity is called value set (or) domain.
Ex: domain: color [Red, Blue, White]
Degree: The degree of relation is the number of attributes it contains.
Tuple/records: In relational model terminology all the rows called tuples or records in
the relation.
B.Sc II Year IV Sem C.AMBADAS Page 17
o A schema diagram can display only some aspects of a schema like the
name of record type, data type, and constraints. Other aspects can't be
specified through the schema diagram.
Q) Schema Diagrams?
A database schema along with primary key and foreign key dependencies can be
depicted pictorially by schema diagrams. Figure shows the schema diagram for our
banking enterprise. Each relation appears as a box, with the attributes listed inside it
and the relation name above it. If there are primary key attributes, a horizontal line
crosses the box, with the primary key attributes listed above the line. Foreign key
dependencies appear as arrows from the foreign key attributes of the referencing
relation to the primary key of the referenced relation. Many database systems provide
design tools with a graphical user interface for creating schema diagrams.
1.UNION: Combines all row from two tables, excluding duplicate rows. The tables must
have the same attribute characteristics( the data type should match).
2. Intersect: Yields only rows that appear in both tables. i.e. common rows.
Query: Select sname from T1 Intersect select sname from T2;
T1 T2 Result
Sname Sname Sname
Ramu Intersect Kiran Gives Kiran
Raju Ramu Ramu
Kiran Srinu
3. Differnce: Yields all rows in one table that are not found in the other table.
Query: Select sname from T1 Minus select sname from T2;
T1 T2 Result
Sname Sname Sname
Ramu Difference Kiran Gives Raju
Raju Ramu
Kiran Srinu
4.Product: Yields all possible pairs of rows from two tables - also known as the
Cartesian product. Therefore, if one table has 3 rows and other table has 2 rows, the
product yields a list composed of 3X 2=6 rows
Query: select sname, cname from T1, T2;
T1 T2 Result
Sname Cname Sname Cname
Ramu Product C Gives Ramu C
Raju C++ Ramu C++
Kiran Raju C
Raju C++
Kiran C
Kiran C++
5. Select: also known as RESTRICT, yields values for all the rows or row found in a
table that satisfy given condition. In other words, Select yields a horizontal subset of a
table.
6. Project: Yields all values for selected attributes. In other words, PROJECT yields a
vertical subset of a table.
Ename Job
Ravi Manager
Rajesh Clerk
Amar Clerk
Kiran Accountant
7. Join: Allows information to be combined from two or more tables. JOIN allows the use
of independent tables linked by common attributes. A natural join links tables by
selecting only the rows with common values in their common attributes.
STD Course
8. Divide: uses one single-column table(A) as the divisor and one 2-column table(B) as
the dividend. The table must be common column.
C1 C2 C1 C2
A 5 Divide A gives 5
A 9 B 9
9. Assignment: Assigns the result set to a variable.
Max_sal:= Select Max(sal) from Emp;
Unit - II
Database Design, ER Model and Relational Database Design
Q) Overview of the Design Process
The task of creating a database application is a complex one, design of the
programs involving design of the database schema, that access and update the
data and design of a security scheme to control access to data. The needs of the
users play a central role in the design process. The design of a complete database
application environment that meets the needs of the enterprise being modeled
requires attention to a broad set of issues. These additional aspects of the expected
use of the database influence a variety of design choices at the physical, logical,
and view levels.
Design Phase: Design phase process is a collection of processes that facilitate the
designing, development, implementation and maintenance of enterprise data
management systems. Properly designed database are easy to maintain, improves
data consistency and are cost effective in terms of disk storage space. The database
designer decides how the data elements correlate and what data must be stored.
The logical model concentrates on the data requirements and the data to be stored
independent of physical considerations. It does not concern itself with how the data
will be stored or where it will be stored physically. The physical data design model
involves translating the logical database design of the database onto physical
media using hardware resources and software systems such as database
management systems.
Design Alternatives: Designing a database schema must ensure that we avoid two
major pitfalls:
Incompleteness: A bad design may make certain aspects of the enterprise difficult
or impossible to model.
BASIC
SYMBOLS IN
Strong WeakERD Relatio Identifying
Entity Entity nship Relationship
Attribut
e
RELATIONSHIPS IN ERD
Entity: An entity is a person, place, object, event or concept in the user environment
about which an organization maintain the data.
TYPE ENTITY
Person EMP, STUDENT
Place STORE HOUSE, LAB
Object COMPUTER, BOOK
Event SALES, ADMISSION
Concept ACCOUNT, COURSE
Types of entity‟s:
Strong entity
Weak entity
Associative entity
Strong entity: Strong entity is the one whose existence does not depend on other
entity‟s it is denoted by single rectangle. A strong entity always has the primary key in
the set of attributes that describes the strong entity.
Weak entity:- A weak entity is the one that depends on the other entity. A weak entity is
denoted by the double rectangle. Weak entity‟s do not have the primary keys instead it
has a partial key that uniquely discriminates the weak entity‟s.
Associate entity: These are formed from many to many relationship between entity
types. It is an entity type that associates the instance of one or more entity types and
contains attributes that are peculiar to related entities in the relationship, in the
addition to the attributes referring to the primary keys to related entities.
Table A Table B
Person 1 1
Drive Vehicle
1 M Table B
Table A
Ex :-
An order related to customer .
A customer can have any orders.
1 M
Customer Order
Place
Ex:- suppose we are interested in courses and student any student may take several
courses a course may be taken by several student this situation is represent with a
many to many relationship.
M M COURSES
STUDENT
REGISTER
Degree of the relationship :- degree
R of the relationship is the no. of entity types that
participated in the relationship . based on the degree of relationship. They can be
categorized a
1. Unary relationship
2 Binary relationship
3 Ternary relationship
1.Unary relationship: unary relationship is a relationship between the instances of
single entity type . It is also called as recursive relationship here degree of the
relationship is one .
Ex:- “is married to” is unary 1:1 relation ship between instance of the person type i.e.,
one person gets married to another person .
Manages is unary 1:M relationship b/w instance of the employee type i.e., an employee
manages one or more other employees
One to one
Library card
Employee Has
One to many
Cardinality constants:- cardinality specifies the min & max no of instances of the entity
that can be associated with each other instances of another entity.
Minimum cardinality:- The min no of instances of one entity that may be associated
with each instances of other entity.
Maximum cardinality:- The max no of instances of one entity that may be associated
with each instances of other entity.
Where cardinality is indicated in ER diagrams, we can understand the nature of
entity‟s and relationship they share.
The different symbols are to represent cardinality are shown below.
Ex:-
Participation constraints:-
There are two types of participation constraints
1. Total 2.Partial
The participation of an entity set E in a relationship set R is said to be total in everything
in E participates in at least one relationship R
If only some entity‟s in „E‟ participate in relationship R is said to be partial
Ex:-
Entity R Entity
The double line indicates the total participation. Single line indicates the partial
participation.
• Once the entities and their corresponding attributes are chosen, the relationship
sets among the various entities are formed.
• These relationship sets may result in a situation where attributes in the various
entity sets are redundant and need to be removed
– Instructor includes the attributes ID, name, dept_name, and salary with ID
as primary key
• Attribute dept_name appears in both entity sets. It is primary key for entity
department, it is redundant in the entity set instructor and needs to be removed.
• But if an instructor has more than one associated department, the relationship
between the entities is recorded in a separate relation inst_dept.
Steps to
Create an ER Diagram
Step 1) Entity Identification
We have three entities
Student
Course
Professor
Once, you have a list of Attributes, you need to map them to the identified entities.
Ensure an attribute is to be paired with exactly one entity. If you think an attribute
should belong to more than one entity, use a modifier to make it unique.
Once the mapping is done, identify the primary Keys. If a unique key is not readily
available, create one.
For Course Entity, attributes could be Duration, Credits, Assignments, etc. For the sake
of ease we have considered just one attribute.
The database can be represented using the notations, and these notations can be
reduced to a collection of tables.
In the database, every entity set or relationship set can be represented in tabular form.
There are some points for converting the ER diagram to the table:
Specialization:-
Specialization is an object set that is a subset of another object set. These is a top down
process of deriving child objects from a parent objects
Specialization is a process of defining one or more subparts of the super type.
Total specialization :- It specifies that each entity instance of the super type must be a
member of some subtype in the specialization in EER diagram by a double line.
Ex:- A patient must be either be on outpatient or in patient
2.Overlap rule:- It specifies that simultaneously be a member of two sub types the
overlap is specified by placing an small „o‟ in the circle.
B.Sc II Year IV Sem C.AMBADAS Page 39
In the above figure X,Y are entity set and R is a Relationship set.
One – to One Relationship
A one – to- one relationship can be alternatively represented as follows,
6. Total Generalization
Total Generalization can be represented in a way similar to generalization but with
double line on the top
Response time: The amount of time a single transaction takes from start to finish
in either the average case or the worst case.
In the most database design, there is sums sort of database already in existence the data
may be stored in different forms like files/folders (or) in excel sheet. The existing data
base information can also provides the nucleus for the constant of new database.
A database is essentially a collection of data tables. The design process in how identify
and describe those each table is a data base once table have been determine and fields
have been assign to each.
A relationship is in first normal form if and only if the domain of each attribute contains
only atomic (individual) value and the value of each attribute contains only a single
LANGUAGES
R.NO LANGUAGES
KNOWN
101 Telugu
101 Hindi
101 English
102 Telugu
102 Hindi
103 Telugu
GROUPS
Group FEE
B.SC 6000
B.COM 5000
�panel 1 exhibits 3NF (button in BCNF) because it satisfies 2NF because it satisfies 2NF
and contains no transitive dependency
�panel 2 exhibits only 2NF because it has partial dependency. Here E determines „B‟
and hence can be a part of primary key
�panel 3 exhibits 3NF and BCNF because it has no transitive dependency and every
determinant is a candidate key.
Ex: The following table a non-key attribute “faculty” determines the part of the primary
key i.e., “subject”. Here the determinant faculty is not a candidate key. Thus it does not
satisfy BCNF.
The given table is in 3NF and can be converted to a table in BCNF using a simple two
step process.
�The table is modified so that determinant in the table i.e., not a candidate key(faculty)
becomes a component of the primary key of the revised table as:
Section Faculty Subject Time
�decomposing the table eliminate the partial dependency. “subject” is dependent on
“faculty” removing the results the following tables which satisfies the BCNF and 3NF
Sectio Faculty Time
n
A Ramesh 7:00
Facult Subject
y
Rames Physics
h Computer
Arun s
Amer Computer
s
EMP
EMP NO PROJECT NO SKILL
1211 1 ANALYSIS
1211 1 DESIGN
1211 1 PROGRAM
1211 7 ANALYSIS
1211 7 DESIGN
1211 7 PROGRAM
�This data base shows project no and skill are independent multivalued facts about
employee no i.e., not contains a multivalued independency.
This forms of the table is obviously full of anomalies. There is a high degree of
redundancy that we lead to update problems since the data base contains MVD‟s, so it
should be decompose.
EMP NO�PROJECT NO
EMP NO�SKILL
[Here project and skill are independent to each other so it should be decomposed
according to the 4NF]
EMP_PROJECT(emp no, project no)
EMP_SKILL (emp no, skill)
EMP_PROJECT
EMP NO SKILL
B.Sc II Year IV Sem C.AMBADAS Page 47
If sales representative sells a certain product and they sell it for particular company ,
then they must sell the product for the company
Now, with the extra dependency we could normalize the table into three separate
tables.
Creating a table with sales rep and product.
Sales rep Product
Ramu Books
Ramu Magazines
Amar Books
Basically a table is in 5NF if it cannot be made into smaller tables with different keys.
Secondary normal form:- A relation is said to be in second normal for if and only.
LANGUAGES
R.NO LANGUAGES
KNOWN
101 Telugu
101 Hindi
101 English
102 Telugu
102 Hindi
103 Telugu
B.Sc II Year IV Sem C.AMBADAS Page 50
Group FEE
B.SC 6000
B.COM 5000
�panel 1 exhibits 3NF (button in BCNF) because it satisfies 2NF because it satisfies 2NF
and contains no transitive dependency
�panel 2 exhibits only 2NF because it has partial dependency. Here E determines „B‟
and hence can be a part of primary key
�panel 3 exhibits 3NF and BCNF because it has no transitive dependency and every
determinant is a candidate key.
Ex: The following table a non-key attribute “faculty” determines the part of the primary
key i.e., “subject”. Here the determinant faculty is not a candidate key. Thus it does not
satisfy BCNF.
The given table is in 3NF and can be converted to a table in BCNF using a simple two
step process.
�The table is modified so that determinant in the table i.e., not a candidate key(faculty)
becomes a component of the primary key of the revised table as:
Section Faculty Subject Time
�decomposing the table eliminate the partial dependency. “subject” is dependent on
“faculty” removing the results the following tables which satisfies the BCNF and 3NF
Sectio Faculty Time
n
A Ramesh 7:00
A Arun 8:00
B Ramesh 9:00
B Arun 7:00
C Amer 6:00
Facult Subject
y
Rames Physics
h Computer
Arun s
Amer Computer
s
EMP
EMPNO PROJECT NO SKILL
1211 1 ANALYSIS
7 DESIGN
PROGRAM
�This data base shows project no and skill are independent multivalued facts about
employee no i.e., not contains a multivalued independency.
This forms of the table is obviously full of anomalies. There is a high degree of
redundancy that we lead to update problems since the data base contains MVD‟s, so it
should be decompose.
EMP NO�PROJECT NO
EMP NO�SKILL
[Here project and skill are independent to each other so it should be decomposed
according to the 4NF]
EMP_PROJECT(emp no, project no)
EMP_SKILL (emp no, skill)
EMP_PROJECT
EMP NO PROJECT NO
1211 1
1211 7
EMP_SKILL
EMP NO SKILL
1211 Analysi
1211 s
1211 Design
Progra
m Fifth normal form:-
A relation is in fifth normal form(5NF) if and only if it satisfies the following conditions.
�A relation is already in 4NF.
�It cannot be further non-lasts decompose.
Sales rep Company Product
Ramu Exclusive Books
Ramu Exclusive Magazine
Amar Wordsworth Books
Raju Wordsworth Books
Raju Wordsworth Magazines
If sales representative sells a certain product and they sell it for particular company ,
then they must sell the product for the company
Now, with the extra dependency we could normalize the table into three separate
tables.
Basically a table is in 5NF if it cannot be made into smaller tables with different keys.
The database design Methodology for Relational Databases
The methodology is depicted as a bit by bit guide to the three main phases of database
design, namely: conceptual, logical, and physical design.
Unit -III
Introduction to SQL
Q) Define SQL? Types of SQL Commands?
Ans) SQL Stands for structured query language. SQL used to write queries that store, alter
and retrieve data and meta data of a relational database.
Features of SQL: -
This is a non- procedural language.
This is the undefined language i.e., common for many databases.
This is the fourth-generation language(4GL) in which programmer concentrates on
what rather than the how.
Insert command :To retrieve data from a table and it allows filtering.
Update command: To update the values of attributes.
Delete command: To delete rows from a table
1.CREATE command
Purpose: - To create oracle database objects such as tables, views, indexes,
clusters and synonyms etc.
Syntax: -
CREATE TABLE table_name (column_name data0type (size),
[CONTRAINT constraint_name] [constraint_type] …);
2. Alter command
Purpose:- To alter the structure of a database object.
Syntax: -
ALTER TABLE table_name
[ADD Column_name datatype(size)]
[Modify column_name datatype(size)]
[Drop column column_name];
B.Sc II Year IV Sem C.AMBADAS Page 57
3. Drop command
Purpose: TO delete a database object
Syntax: -
DROP OBJECT_TYPE OBJECT_NAME;
Example: - The following queries demonstrate deleting a table, an index
and a view respectively.
● DROP TABLE ITEMS;
● DROP INDEX DNAME_IND;
● DROP VIEW EMP_VIEW;
4.Truncate command
Purpose: To delete rows (not the table‟s structure) with auto commit
Syntax: -
TRUNCTATE TABLE tablename;
5.RENAME command
Syntax: -
RENAME OLD_Table_name to new_tablename;
B.Sc II Year IV Sem C.AMBADAS Page 58
1.Insert command
Purpose: - To retrieve data from a table and it allows filtering.
Syntax: -
INSERT INTO table_name(column_list) VALUES (value_list);
Example: - To insert only emp number, ename, job and salary. i.e., values
for only few columns (not all)
INSERT INTO EMP (EMPNO, ENAME, JOB, SAL) values (8888, „KIRAN‟,
CLERK,4000);
Example: - To insert a record through parameter substitution.
INSERT INTO EMP VALUES(&EMPNO, „&ENAME‟, „&JOB‟, &SAL);
Enter value for EMPNO:7666
Enter value for ENAME: SHYAM
Enter value for JOB: MANGER
Enter value for SAL: 9500
2.Update command
Purpose: - To update the values of attributes.
Syntax:
UPDATE Table_name SET column_name=value
[Column_name=value……...] [Where condition];
Example: - To set smith‟s salary to 8000
● Update EMP SET SAL= 8000 WHERE ENAME= „SMITH‟;
Example: - To increase the salary of employees by 10%
● UPDATE EMP SET SAL= SAL+SAL*0.1;
Example: - To update values of multiple attributes at a time.
● UPDATE EMP SET SAL=8000, COMM=100 WHERE JOB= „MANAGER‟;
3.Delete command
Purpose: - To delete rows from a table
B.Sc II Year IV Sem C.AMBADAS Page 59
Athematic operators
Comparison (or) relational operators
Logical operators
Arithmetic operators:-
+ Addition – adds the values on either side of the operator.
-subtraction -subtracts right hand operand from left hand operand.
* Multiplication – multiplies values on either side of operator.
/ Division – Divides lefthand operand by right hand operand.
Ex:-
Select SAL+500 from emp;
Select SAL-500 from emp;
Select SAL*500 from emp;
B.Sc II Year IV Sem C.AMBADAS Page 62
Ex:-
select * from emp where NAME= „SMITH‟ AND emp no= 7368;
select* from emp where NAME= „SMITH‟ AND emp no= 7368;
select * from emp where SAL NOT BETWEEN 1500 AND 3000;
Special Operator:-
ALL – The all operator is used to compare a value to all values in another
value list.
Ex:-
Select * from emp where SQL>= ALL (1500,4000);
IN – checks whether a value matches only value in a list.
Query: - To select records of smith, james and allen from emp table.
SQL supports few set operations which can be performed on the the table data.
1. Union
2.Union All
3.Intersect
4.Minus
1.UNION: Combines all row from two tables, excluding duplicate rows. The tables must
have the same attribute characteristics( the data type should match).
2.UNION ALL: Combines all row from two tables, including duplicate rows.
Query: Select sname from T1 Union ALL select sname from T2;
T1 T2 Result
Sname Sname Sname
Ramu Union ALL Kiran Gives Ramu
Raju Ramu Raju
Kiran Srinu Kiran
B.Sc II Year IV Sem C.AMBADAS Page 64
3. Intersect: Yields only rows that appear in both tables. i.e. common rows.
Query: Select sname from T1 Intersect select sname from T2;
T1 T2 Result
Sname Sname Sname
Ramu Intersect Kiran Gives Kiran
Raju Ramu Ramu
Kiran Srinu
3. Minus: Yields all rows in one table that are not found in the other table.
Query: Select sname from T1 Minus select sname from T2;
T1 T2 Result
Sname Sname Sname
Ramu Difference Kiran Gives Raju
Raju Ramu
Kiran Srinu
Q)Null values
The SQL NULL is the term used to represent a missing value. A NULL
value in a table is a value in a field that appears to be blank.
A field with a null value is field with no value. It is important to
understand that a null value is different than a zero value or a field that
contains spaces.
Basic structure of NULL by creating a table
Syntax:-
CREATE TABLE LIB_BOOKS (ACC_NO NUMBER(10) NOT NULL, TITLE
VARCHAR2(20), AUTHOR VARCHAR2(20), DEPTNM VARCHAR2(20),
PURDATE DATE, PRICE NUMBER(10,2));
Here NOT NULL signifies that columns always accept on explicit value of the
given datatype. There are some columns where we didn‟t use NOT NULL,
which means these columns could be NULL. A field with a null value is 1
that has been left blank during the record creation.
Lib_Books
Acc_no Title Author Deptnm Purdate Price
100 Oracle db 11 g Navathe Cs 02-jun-2000 290
101 DB concepts Miller Cs 13-dec-2008 540
102 Quantum theory Shaw Phy 16-mar-2010 690
103 Discrete maths Schand Mat 20-oct-2012 450
104 C Programming Balaguru Cs 14-aug-2005
swamy
105 Discreating Kneeth Mat 10-jul-2009
106 Engineering maths Michael Mat 15-feb-2011
Write a query to retrieve records from the library books whose price is NULL
Lib_ books
Acc_ no Title Author Deptnm Purdate Price
104 C programing Balaguru Cs 14-aug-2005
swamy
105 Discreating Kneeth Maths 10-jul-2009
106 Engineering Michael Maths 15-feb-2011
maths
Aggregative functions:-
These functions are used to operate on a group of values. They
aggregate the group perform calculation such as finding out sum, max etc…
These functions can be used with the group by clause to categorize results.
1. SUM( ): Finds average of the values of a column
Ex:- SELECT SUM(SAL) FROM EMP;
2. AVG( ): Finds average of the values of a column.
EX:- SELECT AVG(SAL) FROM EMP;
3. MAX( ): Finds maximum of the values of a column.
EX:- SELECT MAX(SAL) FROM EMP WHERE JOB= ‘MANAGER’;
4. MIN( ): Finds minimum of the values of columns.
EX:- SELECT MIN(SAL) FROM EMP;
5. COUNT( ): Finds number of records.
EX:- SELECT COUNT(*) FROM EMP;
6. STDDEV( ): Finds standard deviation of the values of column.
EX:- SELECT STDDEV(SAL) FROM EMP;
7. VARIANCE( ): Finds variance of the values of a column.
Ex:- SELECT VARIANCE(SAL) FROM EMP;
Select ename deptno, sal from emp where (Deptno, sal) IN(select Deptno,
max (sal) from emp group by deptno
Update command
Purpose: - To update the values of attributes.
Syntax:
UPDATE Table_name SET column_name=value
[Column_name=value……...] [Where condition];
Example: - To set smith‟s salary to 8000
Intermediate SQL
Join:- joins are used to fetch information from multiple tables . join is a relational operation
that course two or more tables with a column to be combined into a single set of records.
The type of joins are:
Equi-join, non Equi-join, natural join, outer join, self join, cartesian (product)join.
Consider the following tables for the demonstration of joins :
Std course
Name CID
Rno
33 Ravi 10
44 Kiran 30
r55 Amar 80
2.Non-Equi-join: A join in which the joining condition is based on Non -equality b/w
values in the common columns.The following query lists the names of the student and
subject not known to them in alphabetical order. The output is also shown below.
3.Natural join: same as equi-join except one of the duplicate columns is eliminated in the
result table. The following query list the values of rno, name, course id and course names
4.Outerjoin:- A join in which rows that do not have matching values in common columns
are also included in the result table. It includes left outer join and right outer join.
● Left outer join:- left outer join gives records form second table even if there are no
matching values in the first table. The following query display all the records of course
table even if there is no matching course id in std table.
SELECT RNO, NAME, STD.CID, CNAME FROM STD, COURSE WHERE STD.CID(+)=
COURSE.CID;
10 C 33 Ravi
20 JAVA 44 Kiran
30 ORACLE
● Right outer join:- Right outer join records form table even if there are no
matching values in the second table. The following query display all the records of
student table even if there is no matching course id in course table
SELECT RNO, NAME, STD.CID, CNAME FROM STD, COURSE WHERE STD.CID=
COURSE.CID(+);
6.Cartesian product join:- A join in which all possible combinations of all the rows of
first table with each row of the second table appear. The following query displays all the
combination of name of STD table and cnames of course table has 3 records. If STD table
has 3 records and course table has 3 records the result will have a total of 9(the product of
3*3) records.
SELECT NAME, CNAME FROM STD, COURSE;
NAME Cname
Ravi C
Kiran C
Amar C
Ravi Java
Kiran Java
Amar Java
Ravi Oracle
Kiran Oracle
Amar Oracle
EX:-
Q) What is Transaction?
A transaction is a set of SQL statement which oracle treat as a single unit.ie., all the
statement should execute successfully or none of the statements should execute.
To control transactions oracle doesn’t made permanent any DML statement unless
you commit it. If you don’t commit the transaction and power goes off or system crashes
then the transaction is roll backed.
Transaction control statement manages changes made by DML. Statement TCL
statement available in oracle one.
COMMIT: make changes done in transaction permanent.
ROLLBACK: Roll back the state of database to the last commit point.
SAVE POINT: Use to specify a point in transaction to which later you can
rollback.
Savepoint:
Specify a point a transaction to which later you can rollback.
Example:
Insert into emp (empno,ename,sal) values(101,’aaaa’,9000);
Savepoint A;
Insert into dept (deptno,dname) values(10,’bbbb’);
Savepoint B;
Insert into salgrade (empno,sal) values(103,7000);
If you give
Rollback to B;
Then row inserted into salgrade table will be roll backed. At this point you can commit the
row inserted into dept table and emp table or rollback to savepoint A or completely roll
backed the transaction.
If you give
Rollback;
Then the whole transactions is rollbacked.
Not null: - this constraints ensures all rows in the table contain a define
value for column which specified as not null which means a null value is
not allowed.
Syntax:-
[CONSTRAINT constraint name] NOT NULL
EX:- To create library books table with not null values, the query would be
like.
SQL>create table library_books(access_no(10), not null, title varchar2
(20),author varchar2 (20), pur_date, price number(7,2));
Unique:- The columns does not have the duplicate values but it allows the
null value .
Ex:- To create library- books table with unique key, the query would be like:
SQL>create table library_books (access_no number(10). Unique title
varchar2(20). Author varchar 2(20) pur date date, price number (7,2));
The sequence of statements is executed only if the condition is true. If the condition is
false or null, the if statement does nothing.
IF-THEN-ELSE statement:-
The second form of IF statement adds the keyword ELSE followed by
an alternative sequence of statements as follows:
IF condition THEN
Sequences_of_statements1
ELSE
Sequence_of_statements2
END IF;
The sequence of statements in the ELSE clause is executed only if the
condition is false or null. Thus the ELSE clause ensures that a sequence of
statements is executed.
B.Sc II Year IV Sem C.AMBADAS Page 80
If-then-elsif statement:-
Sometimes you want to select an action from several mutually
exclusive alternatives. The third form of IF statement uses the keyword
ELSIF(not ELSEIF) to introduce additional conditions, as follows:
IF condition1 THEN
Sequence_of_statements1
ELSIF condition2 THEN
Sequence_of_statements2
ELSE
Sequence_of_statement3
END IF;
If the first condition is false or null, the ELSIF clause tests another
condition. An IF statement can have any number of ELSIF clauses; the final
ELSE clause is optional. Conditions are evaluated one by one from top to
bottom. If any condition is true, its associated sequence of statements is
executed and control passes to the next statement. If all conditions are false
or null, the sequence in the ELSE clause is executed.
CASE STATEMENT: The CASE statement works like the IF statement, only using the
keyword WHEN. A CASE statement is evaluated from top to bottom. If it get the
condition TRUE, then the corresponding THEN clause is executed and the execution
goes to the END CASE clause.
Syntax:
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
B.Sc II Year IV Sem C.AMBADAS Page 81
While loop:-
A WHILE LOOP is used when a set of statements has to be executed as long as a
condition is true. The condition is evaluated at the beginning of each iteration. The
iteration continues until the condition become false.
The general syntax to write a WHILE LOOP is:-
WHILE<condition>
LOOP statements;
END LOOP;
Example:
DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
FOR LOOP:-
A FOR LOOP is used to execute a set of statements for a predetermined number of times.
Iteration occurs between the start and end integer values given. The counter is always
incremented by 1. The loop exits when the counter reaches the value of the end integer.
The general syntax to write a FOR LOOP is:-
FOR counter IN initial_value .. final_value LOOP
LOOP statements;
END LOOP;
Example:
BEGIN
FOR k IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(k);
3. Jumping Control Structures: Jumping Control Structures are used to move control
to other portions in a program or to quit loops.
a) Goto: This moves the control to a specified label.
b) Exit: It stops the execution of a corresponding loop.
c) Exit when: It stops the execution of a corresponding loop on meeting the given
condition.
Example: A program to print first n numbers. This uses „simple loop‟ and „exit- when‟
DECLARE
N NUMBER:=&N;
I NUMBER:=0;
BEGIN
LOOP
I:=I+1;
DBMS_OUTPUT.PUT_LINE(I);
EXIT WHEN I=N;
END LOOP;
END;
What is Exception Handling? Give Example?
Exception Handling: Handling run time errors is exception handling. Actually, when
run-time error occurs, a program gets terminated abruptly. But, exception handling
ensures safe termination of a program. A program can handle multiple exceptions.
Exceptions are two types:
1. System-defined exceptions: These are raised automatically by oracle when a
corresponding runtime error occurs
2. User-defined exceptions: These should be defined and raised by users.
Following table lists System- Meaning
Defined Exceptions. System-
Defined EXCEPTIONS
Q) What are Cursors? What are the Types of Cursors? How do you
Create and Access a Cursor? Explain with examples?
Cursor: Cursor is a mechanism that deals with multiple records of a result set fetched
by a SELECT query. It is a temporary work area created in the system memory when a
SQL statement is executed. It iterates multiple records sequentially one by one.
Types of cursors:
1. Explicit cursor: Explicit cursor is declared by the programmer to keep track of a
result set. This must be created in a PL/SQL program explicitly. It has to be opened to
fetch the records. It is applicable only for SELECT command.
2. Implicit cursor: Implicit cursor is defined by PL/SQL and keeps track of information
of rows affected by INSERT, UPDATE, DELETE and SELECT INTO commands. This is
created by oracle automatically when the commands specified are executed. The name of
the implicit cursor is „SQL‟
Syntax of a Procedure:
CREATE [OR REPLACE] PROCEDURE pocdeure_name [(list of arguments)] IS/AS
Declarations
BEGIN
Statements
END:
Executing a Procedure: A procedure can be executed in two different ways.
• At SQL prompt using the syntax: EXEC proc_name;
B.Sc II Year IV Sem C.AMBADAS Page 87
Example: Following is a procedure that finds out whether the supplied parameter is
even or odd.
CREATE OR REPLACE PROCEDURE ISEVEN(X NUMBER) IS
BEGIN
IF MOD(X,2)=0 THEN
DBMS_OUTPUT.PUT_LINE('EVEN NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE('ODD NUMBER');
END IF;
END;
Two different ways of executing the procedure are shown below.
SQL> EXEC ISEVEN(2);
BEGIN
ISEVEN(3);
END;
n! = n*(n-1)!
= n*(n-1)*(n-2)!
...
= n*(n-1)*(n-2)*(n-3)... 1
The following program calculates the factorial of a given number by calling itself
recursively −
DECLARE
num number;
factorial number;
RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1);
END IF;
RETURN f;
END;
BEGIN
num:= 6;
factorial := fact(num);
END;
When the above code is executed at the SQL prompt, it produces the following result −
Factorial 6 is 720
Transaction: A Transaction is any action that reads from and/or writes to a database. It
is a logical unit of work that must be either entirely completed or aborted. Most real-
world database transactions are formed by two or more database requests. A database
request is the equivalent of a single SQL statement in an application program. A
Transaction can be:
Simple SELECT statement to list table contents
A series of related UPDATE statements to change the values of attributes.
A series of INSERT statements to add rows to one or more tables
A combination of SELECT, UPDATE, and INSERT statements
Isolation: It means that the data used during the execution of a transaction
cannot be used by a second transaction until the first one is completed.
Durability: It means that the changes are permanent. Thus, once a transaction is
committed, no subsequent failure of the database can reverse the effect of the
transaction.
1. Active State: A database transaction is in this state while its statements are being
executed. This is the initial state of every transaction.
2. Partially Committed Phase: A database transaction enters this state when its final
statement has been executed. But, it is still possible for the transaction to be aborted in
the case of hardware or output failures.
3. Failed State: A database transaction enters the failed state when its normal
execution can no longer proceed due to hardware or program errors.
4. Buffer Manager: Finally, the buffer manager is responsible for the efficient transfer of
data between disk storage and main memory.
1) Lost Updates: The lost update problem occurs when two concurrent
transactions, T1 and T2, are updating the same data element and one of the
updates is lost (overwritten by the other transaction). This is a kind of
write/write conflict.
Example: Assume that there are two concurrent transactions, T1 and T2
working with X, and Initially, X=50. Then,
As the Non-Interleaving execution performs the transactions as
desired, the value of X will be „600‟.
Example: Assume that there are two concurrent transactions, T1 and T2 working with
X, Y, and Initially, X=10, and Y=20. Then,
As the Non-Interleaving execution performs the transactions as desired, the value
of X will be „300‟.
But, in the case of Interleaving execution, First, T1 reads X (=10) and adds it to
SUM(=10). Then T2 writes X (=100) and writes Y (=200). Now, if T1 reads Y (=200,
updated by T2) and adds it to SUM (=210). Thus, it is evident that the transactions
add before value of X and after value of Y to SUM resulting „210‟. This is nothing
but Inconsistent Retrieval.
Example: The following example shows write/write conflict seen in a problem called „lost
update’. The lost update problem occurs when two concurrent transactions, T1 and T2,
are updating the same data element and one of the updates is lost (overwritten by the
other transaction).
Assume that there are two concurrent transactions, T1 and T2 working with X, and
Initially, X=50. Then,
In the case of Schedule „S1‟, the value of X will be „600‟.
But, in the case of Schedule „S2‟, First, T2 reads X (=50) before its updation by T1;
then T2 updates X to 500 (=50*100) considering the previous value of X. Thus we
lost the update by T1.
The conflict is clearly evident that the above two schedules produce different results.
Recoverability of Schedule
Sometimes a transaction may not execute completely due to a software issue, system
crash or hardware failure. In that case, the failed transaction has to be rollback. But
some other transaction may also have used value produced by the failed transaction. So
we also have to rollback those transactions.
The above table 1 shows a schedule which has two transactions. T1 reads and writes the
value of A and that value is read and written by T2. T2 commits but later on, T1 fails.
Due to the failure, we have to rollback T1. T2 should also be rollback because it reads
the value written by T1, but T2 can't be rollback because it already committed. So this
type of schedule is known as irrecoverable schedule.
Irrecoverable schedule: The schedule will be irrecoverable if Tj reads the updated value
of Ti and Tj committed before Ti commit.
The above table 2 shows a schedule with two transactions. Transaction T1 reads and
writes A, and that value is read and written by transaction T2. But later on, T1 fails. Due
to this, we have to rollback T1. T2 should be rollback because T2 has read the value
written by T1. As it has not committed before T1 commits so we can rollback transaction
T2 as well. So it is recoverable with cascade rollback.
Recoverable with cascading rollback: The schedule will be recoverable with cascading
rollback if Tj reads the updated value of Ti. Commit of Tj is delayed till commit of Ti.
Locking
Locking : A procedure used to control concurrent access to data. When one transaction
is accessing the database, a lock may deny access to other transactions to prevent
incorrect results.
Locking Methods: Locking methods are the most widely used approach to ensure
serializability of concurrent transactions. There are several variations, but all share the
same fundamental characteristic, namely that a transaction must claim a shared (read)
or exclusive (write) lock on a data item before the corresponding database read or write
operation. DBMS uses different lock types:
1. Shared lock
2. Exclusive lock
1. Exclusive Lock: If a transaction has an exclusive lock on a data item, it can both
read and update the item. This mode of locking provides an exclusive use of data item to
one particular transaction. The exclusive mode of locking is also called an UPDATE or a
WRITE lock.
2. Shared Lock: The shared lock is also called as a Read Lock. This mode allows several
transactions to access the same item X only for reading purpose. Thus, any number of
transactions can concurrently place a shared lock and read a data item, but none of
these transactions can update the data item. A data item locked in the shared mode
cannot be locked in the exclusive mode until the shared lock is released. A data item
locked in the exclusive mode cannot be locked in the shared mode until the exclusive
lock on the data item is released.
Lock States: Using the shared/exclusive locking concept, a lock can have three states:
unlocked, shared (read), and exclusive (write).
Lock Operations: Three lock operations exist: READ_LOCK (to check the type of lock),
WRITE_LOCK (to issue the lock), and UNLOCK (to release the lock).
Lock Problems: Locks can lead to two major problems:
The resulting transaction schedule might not be Serialisable.
The schedule might create deadlocks.
Q) Two-Phase Locking
Two-phase locking defines how transactions acquire and release locks. Two-phase
locking ensures serializability, but it does not prevent deadlocks. The two phases are:
1. A growing phase: A phase in which a transaction acquires all required locks but
cannot release any locks. Once all locks have been acquired, the transaction is in its
locked point.
2. A shrinking phase: A phase in which a transaction releases all locks and cannot
obtain any new locks.
Example: The following table shows how a transaction executes in the case of normal
locking and two-phase locking:
Deadlock detection: The DBMS periodically tests the database for deadlocks. If a
deadlock is found, one of the transactions (the „victim‟) is aborted (rolled back and
restarted) and the other transaction continues.
• Deadlock avoidance: The transaction must obtain all of the locks it needs before it can
be executed. This technique avoids the rollback of conflicting transactions by requiring
that locks to be obtained in succession.
The choice of the best deadlock control method to use depends on the database
environment. For example, if the probability of deadlocks is low, „deadlock detection’ is
recommended. However, if the probability of deadlocks is high, „deadlock prevention‟ is
recommended. If response time is not important, ‘deadlock avoidance’ might be
preferable
1. WAIT/DIE Scheme: In the wait/die scheme, the older transaction waits for the
younger to complete and release locks.
If the transaction requesting the lock is the older of the two transactions, it will
wait until the other transaction is completed and the locks are released.
If the transaction requesting the lock is the younger of the two transactions, it will
die (roll back) and is rescheduled using the same time stamp.
Example: Assume that there are three Transactions T10, T20 and T30 with timestamps
10, 20 and 30 respectively i.e. T10 is older than T20 and T30 is younger than T20. Also,
assume that T20 locked a data item „X‟. In the case,
• when T10 requests a lock on „X‟, T10 waits as it is older than T20, and
• when T30 requests a lock on „X‟, T30 is aborted and rolled back as it is younger than
T20
B.Sc II Year IV Sem C.AMBADAS Page 100
Example: Assume that there are three Transactions T10, T20 and T30 with
timestamps 10, 20 and 30 respectively i.e. T10 is older than T20 and T30 is
younger than T20. Also, assume that T20 locked a data item „X‟. In the
case,
• when T10 requests a lock on „X‟, T20 is aborted and rolled back - as T10
being older, preempts T20
• when T30 requests a lock on „X‟, T30 waits as it is younger than T20
As we have seen above, a problem with 2PL is that it can lead to deadlocks. Reed's multi
version timestamp ordering scheme solves this problem by ordering transactions and
aborting transactions that access data out of order. It also increases the concurrency in
the system by never making an operation block (though it does abort transactions.)
The basic idea in this scheme is to assign transactions timestamps when they are
started, which are used to order these transactions. If two transactions access data
items in an order that is inconsistent with their time stamps, then one of them is
aborted.
A possible implementation of this approach is to require each data item to remember the
timestamp of the last transaction that wrote the item. If a transaction with an earlier
timestamp tries to read or write to this item, then it is aborted.
Unfortunately, this scheme is too conservative to Reed's scheme. The problem with it is
that it does not recognize that we can concurrently have transactions working with
different versions of a data item that can be serialized.
Therefore, Reed's algorithm is more complicated. As with the simpler scheme given
above, it assigns transactions timestamps when they are started, which are used to
order these transactions. Moreover, it associates each data item with timestamped
versions and associates each version with readtimestamps. A readtimestamp is
associated with a data item whenever a transaction reads the data item and is the same
as the timestamp of the reading transaction. A timestamped version is created whenever
a transaction writes a new value to the data item and has the timestamp of the writing
transaction. The following steps occur when a transaction accesses the database:
If the operation is a read, then it is allowed, and the version read is the one with the
largest timestamp less than the timestamp of the reading transaction. The timestamp of
the reading transaction is added to the item.
If the operation is a write, then a new version of the data item is created with the
timestamp of the writing transaction as long as no transaction with a more recent
timestamp has read a version of the item with an older timestamp than that of the
writing transaction. If this check fails, the writing transaction is aborted and restarted.
By making sure that T1 does not read the latest version of p, the scheme supports
serializability. The problem with this scheme is the cost of keeping multiple versions and
associated time stamps.
Granularity: The size of data items chosen as the unit of protection by a concurrency
control protocol.
Lock Granularity: An important consideration in implementing concurrency control is
choosing the locking level. Lock granularity indicates the level of lock use. Locking can
take place at the following levels: data-base, table, page, row, or even field (attribute).
Database Level: In a database-level lock, the entire database is locked, thus
preventing the use of any tables in the database by transaction T2 while transaction
T1 is being executed. This level of locking is good for batch processes, but it is
unsuitable for multiuser DBMSs.
Table Level: In a table-level lock, the entire table is locked, preventing access to any
row by transaction T2 while transaction T1 is using the table. However, two
transactions can access the same database as long as they access different tables.
Table-level locks are less restrictive than database-level locks.
Page level lock: The physical storage block (or page) containing a requested record is
locked. This level is the most commonly implemented locking level. A page will be a
fixed size (4K, 8K, etc.) and may contain records of one or more tables. Page-level
locks are currently the most frequently used multiuser DBMS locking method.
Row Level Lock: A row-level lock is much less restrictive than other locks. The
DBMS allows concurrent transactions to access other rows of the same table even
when the rows belong to the same table.
Field Level Lock: The field-level lock allows concurrent transactions to access the
same row as long as they required use of different fields (attributes) within that row.
It is rarely implemented in a DBMS because it requires an extremely high level of
computer overhead.
Database Security is the mechanism that protects the database against intentional or
accidental threats. Breaches of security may affect other parts of the system, which may
in turn affect the database. Database security encompasses hardware, software, people,
and data. To effectively implement security requires appropriate controls. The reason for
this turnaround is the increasing amounts of crucial corporate data being stored on
computer and the acceptance that any loss or unavailability of this data could prove to
be disastrous.
Database security covers the following issues:
• Theft and Fraud: It not only affect the database environment but also the entire
organization.
• Confidentiality / Privacy: Confidentiality refers to the need to maintain secrecy over
data that is critical to the organization. Privacy refers to the need to protect data about
individuals.
• Loss of Integrity: Loss of data integrity results in invalid or corrupted data, which
may seriously affect the operation of an organization.
• Loss of Availability: Loss of availability means that the data, or the system, or both
cannot be accessed, Which can seriously affect an organization‟s financial performance.
In some cases, events that cause a system to be unavailable may also cause data
corruption.
Measures of Control
The measures of control can be broadly divided into the following categories −
Access Control − Access control includes security mechanisms in a database
management system to protect against unauthorized access. A user can gain
access to the database after clearing the login process through only valid user
accounts. Each user account is password protected.
Flow Control − Distributed systems encompass a lot of data flow from one site to
another and also within a site. Flow control prevents data from being transferred
in such a way that it can be accessed by unauthorized agents. A flow policy lists
out the channels through which information can flow. It also defines security
classes for data as well as transactions.
Access Controls: A privilege allows a user to create or access (that is read, write, or
modify) some database object (such as a relation, view, or index) or to run certain DBMS
utilities. Privileges are granted to users to accomplish the tasks required for their jobs.
As excessive granting of unnecessary privileges can compromise security.
Views: A view is the dynamic result of one or more relational operations operating on the
base relations to produce another relation. A view is a virtual relation that does not
actually exist in the database, but is produced upon request by a particular user, at the
time of request. The view mechanism provides a powerful and flexible security
mechanism by hiding parts of the database from certain users. The user is not aware of
the existence of any attributes or rows that are missing from the view.
Backup and Recovery: The process of periodically copying of the database and log file
(and possibly programs) to offline storage media is known as Backup.
A DBMS should provide logging facilities, sometimes referred to as journaling, which
keep track of the current state of transactions and database changes, to provide support
for recovery procedures. The advantage of journaling is that in the event of a failure, the
database can be recovered to its last known consistent state using a backup copy of the
database and the information contained in the log file.
Integrity: Integrity constraints also contribute to maintaining a secure database system
by preventing data from becoming invalid, and hence giving misleading or incorrect
results.
Encryption: The encoding of the data by a special algorithm that renders the data
unreadable by any program without the decryption key is Enryption. To transmit data
securely over
B.Sc II Year IV Sem C.AMBADAS Page 107
RAID Technology: The hardware that the DBMS is running on must be fault-tolerant,
meaning that the DBMS should continue to operate even if one of the hardware
components fails. This suggests having redundant components that can be seamlessly
integrated into the working system whenever there is one or more component failures.
The solution is the use of Redundant Array of Independent Disks (RAID) technology.
RAID works on having a large disk array comprising an arrangement of several
independent disks that are organized to improve reliability and at the same time
increase performance.
RAID 0
In this level, a striped array of disks is implemented. The data is broken down into
blocks and the blocks are distributed among disks. Each disk receives a block of data to
write/read in parallel. It enhances the speed and performance of the storage device.
There is no parity and backup in Level 0.
RAID 1
RAID 1 uses mirroring techniques. When data is sent to a RAID controller, it sends a
copy of data to all the disks in the array. RAID level 1 is also called mirroring and
provides 100% redundancy in case of a failure.
RAID 2
RAID 2 records Error Correction Code using Hamming distance for its data, striped on
different disks. Like level 0, each data bit in a word is recorded on a separate disk and
B.Sc II Year IV Sem C.AMBADAS Page 108
RAID 3
RAID 3 stripes the data onto multiple disks. The parity bit generated for data word is
stored on a different disk. This technique makes it to overcome single disk failures.
RAID 4
In this level, an entire block of data is written onto data disks and then the parity is
generated and stored on a different disk. Note that level 3 uses byte-level striping,
whereas level 4 uses block-level striping. Both level 3 and level 4 require at least three
disks to implement RAID.
RAID 5
RAID 5 writes whole data blocks onto different disks, but the parity bits generated for
data block stripe are distributed among all the data disks rather than storing them on a
different dedicated disk.