You are on page 1of 45

Department of Computer Science and Engineering (CSE)

Database
Management
System

Course Outcome Will be covered in


CO Title Level this lecture
Number
CO1 To perceive the significance and Remember
implementation of a commercial  
relational database system (Oracle)
by writing SQL using the system.
CO2 To understand the relational database Understand
theory, and be able to write  
relational algebra expressions for
queries
CO3 To identify the basic issues of Analysis and
transaction processing and application
concurrency control and find out its
solutions.

2
University Institute of Engineering (UIE)
Department of Computer
Department Science
of Computer and Engineering
Science (CSE)(CSE)
and Engineering
Contents of the Syllabus

UNIT-I [10h]
Overview of Databases: Database concepts, DBMS, Data Base System Architecture (Three
Level ANSI-SPARC Architecture), Advantages and Disadvantages of DBMS, Data Independence,
DBA and Responsibilities of DBA, Relational Data Structure, Keys, Relations, Attributes, Schema and
Instances, Referential integrity, Entity integrity.
Data Models: Relational Model, Network Model, Hierarchical Model, ER Model: Design,
issues, Mapping constraints, ER diagram, Comparison of Models.
Relational Algebra & Relational Calculus: Introduction, Syntax, Semantics, Additional
operators, Grouping and Ungrouping, Relational comparisons, Tuple Calculus, Domain Calculus,
Calculus Vs Algebra, Computational capabilities.

UNIT-II [10h]
Functional dependencies and Normalization: Functional dependencies, Decomposition, Full
Functional Dependency (FFD), Transitive Dependency (TD), Join Dependency (JD), Multi-valued
Dependency (MVD), Normal Forms (1NF, 2NF, 3NF, BCNF), De-normalization.
Database Security: Introduction, Threats, Counter Measures.
Control Structures: Introduction to conditional control, Iterative control and sequential control
statements, Cursors, Views.

3
University Institute of Engineering (UIE)
Department of Computer
Department Science
of Computer and Engineering
Science (CSE)(CSE)
and Engineering
Contents of the Syllabus

UNIT-III [10h]
Package, Procedures and Triggers: Parts of procedures, Parameter modes, Advantages of
procedures, Syntax for creating triggers, Types of triggers, package specification and package body,
developing a package, Bodiless package, Advantages of packages.
Transaction Management and Concurrency Control: Introduction to Transaction Processing,
Properties of Transactions, Serializability and Recoverability, Need for Concurrency Control, Locking
Techniques, Time Stamping Methods, Optimistic Techniques and Granularity of Data items.

Database Recovery of database: Introduction, Need for Recovery, Types of errors, Recovery
Techniques.

4
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Chapter 2.3

(Control Structures)
Control Structures: Introduction to conditional control, Iterative control and
sequential control statements, Cursors, Views.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Why PL/SQL
• Procedural Language – SQL
• Acts as host language for stored procedures and triggers.
• Provides the ability to add middle tier business logic to client/server
applications.
• Provides Portability of code from one environment to another
• Improves performance of multi-query transactions.
• Provides error handling

University Institute of Engineering (UIE)


• PL/SQL is Block
Structured
Department of Computer Science and Engineering (CSE)
A block is the basic
unit from which all
PL/SQL programs
are built. A block
can be named
(functions and
procedures) or
anonymous
• Sections of block
Declaration Section
Executable Section

Structure of PL/SQL
Exception Section

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Structure of PL/SQL

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example of Structure of PL/SQL

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL programming language


• The PL/SQL programming language was developed by
Oracle Corporation in the late 1980s as procedural extension
language for SQL and the Oracle relational database.
Following are certain notable facts about PL/SQL −
• PL/SQL is a completely portable, high-performance
transaction-processing language.
• PL/SQL provides a built-in, interpreted and OS independent
programming environment.
• PL/SQL can also directly be called from the command-
line SQL*Plus interface.
• Direct call can also be made from external programming
language calls to database.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Features of PL/SQL

• PL/SQL has the following features −


• PL/SQL is tightly integrated with SQL.
• It offers extensive error checking.
• It offers numerous data types.
• It offers a variety of programming structures.
• It supports structured programming through functions
and procedures.
• It supports object-oriented programming.
• It supports the development of web applications and
server pages

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Advantages of PL/SQL
• SQL is the standard database language and PL/SQL is
strongly integrated with SQL. PL/SQL supports both
static and dynamic SQL. Static SQL supports DML
operations and transaction control from PL/SQL block.
In Dynamic SQL, SQL allows embedding DDL
statements in PL/SQL blocks.
• PL/SQL allows sending an entire block of statements to
the database at one time. This reduces network traffic
and provides high performance for the applications.
• PL/SQL gives high productivity to programmers as it
can query, transform, and update data in a database.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL

• Decision making statements are those statements which are


in charge of executing a statement out of multiple given
statements based on some condition. The condition will
return either true or false. Based on what the condition
returns, the associated statement is executed.

• PL/SQL programming language provides following


types of decision-making statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

•  The flow of control statements can be classified into the


following categories: 
• Conditional Control
• Iterative Control
• Sequential Control

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL:  if...then...else statement

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

•  The flow of control statements can be classified into the


following categories: 
• Conditional Control
• Iterative Control
• Sequential Control

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

• Iterative Control :
•  Iterative control indicates the ability to repeat or skip
sections of a code block. 
 A loop marks a sequence of statements that has to be
repeated. The keyword loop has to be placed before the first
statement in the sequence of statements to be repeated, while
the keyword end loop is placed immediately after the last
statement in the sequence. 

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL Cursor
• When an SQL statement is processed, Oracle creates a
memory area known as context area. A cursor is a pointer to
this context area. It contains all information needed for
processing the statement. In PL/SQL, the context area is
controlled by Cursor. A cursor contains information on a
select statement and the rows of data accessed by it.
• A cursor is used to referred to a program to fetch and process
the rows returned by the SQL statement, one at a time. There
are two types of cursors:
• Implicit Cursors
• Explicit Cursors

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

• PL/SQL Implicit Cursors


• The implicit cursors are automatically generated by Oracle
while an SQL statement is executed, if you don't use an
explicit cursor for the statement.
• These are created by default to process the statements when
DML statements like INSERT, UPDATE, DELETE etc. are
executed.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

PL/SQL Explicit Cursors

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Views
•  A view is the result set of a stored query on the data, which
the database users can query just as they would in a persistent
database collection object.
• This pre-established query command is kept in the database
dictionary.
• Unlike ordinary base tables in a relational database, a view
does not form part of the physical schema: as a result set, it is a
virtual table computed or collated dynamically from data in the
database when access to that view is requested.
• Changes applied to the data in a relevant underlying table are
reflected in the data shown in subsequent invocations of the
view. 

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Read-only , Updatable and Materialized views

• If the database system can determine the reverse mapping from


the view schema to the schema of the underlying base tables, then
the view is updatable. 
• INSERT, UPDATE, and DELETE operations can be performed
on updatable views.
• Read-only views do not support such operations because the
DBMS cannot map the changes to the underlying base tables.
• A view update is done by key preservation.
• Materialized view give a static snapshot of the data and may
include data from remote sources.
• The accuracy of a materialized view depends on the frequency of
trigger mechanisms behind its updates.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Creating and dropping views

• Example: members of Jessica’s Circle


CREATE VIEW JessicaCircle AS SELECT *
FROM User WHERE uid IN (SELECT uid FROM
Member WHERE gid = 'jes');
• Tables used in defining a view are called “base
tables”
• User and Member above
• To drop a view
DROP VIEW JessicaCircle ;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Using views in queries


• Example: find the average popularity of members in
Jessica’s Circle
SELECT AVG(pop) FROM JessicaCircle ;
• To process the query, replace the reference to the view by
its definition
• SELECT AVG(pop) FROM (SELECT * FROM User WHERE
uid IN (SELECT uid FROM Member WHERE gid = 'jes'))
AS JessicaCircle ;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Why use views?


• To hide data from users
• To hide complexity from users
• Logical data independence
If applications deal with views, we can change the
underlying schema without affecting applications
• Recall physical data independence: change the physical
organization of data without affecting applications
• To provide a uniform interface for different implementations
or sources
• Real database applications use tons of views

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Modifying views
• Does it even make sense, since views are virtual?
• It does make sense if we want users to really see views as
tables
• Goal: modify the base tables such that the modification
would appear to have been accomplished on the view

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

A simple case
CREATE VIEW UserPop AS SELECT uid, pop FROM User;
DELETE FROM UserPop WHERE uid = 123;
translates to:
DELETE FROM User WHERE uid = 123;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

An impossible case
CREATE VIEW PopularUser AS SELECT uid, pop FROM
User WHERE pop >= 0.8;
INSERT INTO PopularUser VALUES(987, 0.3);
• No matter what we do on User, the inserted row will not
be in PopularUser

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

A case with too many possibilities


• CREATE VIEW AveragePop(pop) AS SELECT AVG(pop)
FROM User;
• Note that you can rename columns in view definition
UPDATE AveragePop SET pop = 0.5;
• Set everybody’s pop to 0.5?
• Adjust everybody’s pop by the same amount?
• Just lower Jessica’s pop ?

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example
• A view is equivalent to its source query. When queries are
run against views, the query is modified. For example, if
there exists a view named accounts_view with the content as
follows:
• accounts_view:
SELECT name,
money_received, money_sent, (money_received - money_sent)
AS balance, address, ... FROM table_customers c JOIN
accounts_table a ON a.customer_id = c.customer_id

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example(2)
• The application could run a simple query such as:
• Simple query
• SELECT name, balanceFROM accounts_view
• The RDBMS then takes the simple query, replaces the
equivalent view, then sends the following to the query
optimizer

University Institute of Engineering (UIE)

You might also like