Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Department of Computer Science and Engineering (CSE)

Database
Management
System

Course Outcome Will be covered in


CO Title Level this lecture
Numbe
r
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 Science and Engineering (CSE)

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 Science and Engineering (CSE)

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 and Communication Engineering (CCE)

(Package, Procedures and Triggers)

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.

University Institute of Engineering (UIE) 5


Department of Computer and Communication Engineering (CCE)

Learning Objective
• Procedures with example
• Parameters and type of parameters
– Actual and Formal Parameters

• Functions and its uses.


• Triggers
– Statement trigger
– Row trigger
– Enabling, disabling and dropping trigger

• Packages, its specifications and body

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

Learning Outcome
• Understanding the concept of procedures with example.
• Discuss the various parameters and its types.
• Understanding the use of functions in Data Base Management
System.
• Discuss the concepts of Trigger and its various types.
• Discuss the concepts of packages and its advantages

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

Advantages of Packages
• Modularity : Packages let you encapsulate logically related types, items, and
subprograms in a named PL/SQL module. Each package is easy to understand,
and the interfaces between packages are simple, clear, and well defined. This
aids application development.
• Easier Application Design : When designing an application, all you need
initially is the interface information in the package specs. You can code and
compile a spec without its body. Then, stored subprograms that reference the
package can be compiled as well. You need not define the package bodies fully
until you are ready to complete the application.
• Information Hiding : With packages, you can specify which types, items, and
subprograms are public (visible and accessible) or private (hidden and
inaccessible). For example, if a package contains four subprograms, three might
be public and one private. The package hides the implementation of the private
subprogram so that only the package (not your application) is affected if the
implementation changes. This simplifies maintenance and enhancement. Also,
by hiding implementation details from users, you protect the integrity of the
package.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

Advantages of Packages
• Added Functionality : Packaged public variables and cursors
persist for the duration of a session. So, they can be shared by all
subprograms that execute in the environment. Also, they allow
you to maintain data across transactions without having to store
it in the database.
• Better Performance : When you call a packaged subprogram
for the first time, the whole package is loaded into memory. So,
later calls to related subprograms in the package require no disk
I/O. Also, packages stop cascading dependencies and thereby
avoid unnecessary recompiling. For example, if you change the
implementation of a packaged function, Oracle need not
recompile the calling subprograms because they do not depend
on the package body.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Cursors
• A cursor is a temporary work area created in the system
memory when a SQL statement is executed.
• A cursor contains information on a select statement and the
rows of data accessed by it.
• This temporary work area is used to store the data retrieved
from the database, and manipulate this data.
• A cursor can hold more than one row, but can process only
one row at a time.
• The set of rows the cursor holds is called the active set.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Cursor Types
• There are two types of cursors in PL/SQL.
– Implicit cursors.
– Explicit cursors.
• Both implicit and explicit cursors have the same
functionality, but they differ in the way they are accessed.
Cursor Attributes
Name Description
%FOUND Returns TRUE if record was fetched
successfully, FALSE otherwise.
%NOTFOUND Returns TRUE if record was not fetched
successfully, FALSE otherwise.
%ROWCOUNT Returns number of records fetched from
cursor at that point in time.
%ISOPEN Returns TRUE if cursor is open, FALSE
otherwise.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Implicit cursors

• Implicit cursors are automatically created by Oracle


whenever an SQL statement is executed, when there is no
explicit cursor for the statement.
• Whenever a DML statement (INSERT, UPDATE and
DELETE) is issued, an implicit cursor is associated with this
statement.
• For INSERT operations, the cursor holds the data that needs
to be inserted.
• For UPDATE and DELETE operations, the cursor identifies
the rows that would be affected.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example

• The following program will update the table and increase the
salary of each customer by 500 and use the SQL
%ROWCOUNT attribute to determine the number of rows
affected.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example
• DECLARE
• total_rows number(2);
• BEGIN UPDATE customers SET salary = salary + 500;
• IF sql%notfound THEN
• dbms_output.put_line('no customers selected');
• ELSIF sql%found THEN
• total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected');
• END IF;
• END; /

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Explicit Cursors

• An explicit cursor should be defined in the declaration


section of the PL/SQL Block.
• It is created on a SELECT Statement which returns more
than one row.
• Syntax:
• CURSOR cursor_name IS select_statement;
• It involves following steps:
• Declaring the cursor for initializing the memory
• Opening the cursor for allocating the memory
• Fetching the cursor for retrieving the data
• Closing the cursor to release the allocated memory
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Explicit Cursor
• Declaring the cursor defines the cursor with a name and the
associated SELECT statement.
• Syntax: CURSOR c_customers IS SELECT id, name, address
FROM customers;
• Opening the cursor allocates the memory for the cursor and
makes it ready for fetching the rows returned by the SQL
statement into it.
• Syntax: OPEN c_customers;
• Fetching the cursor involves accessing one row at a time.
• Syntax: FETCH c_customers INTO c_id, c_name, c_addr;
• Closing the cursor means releasing the allocated memory.
• CLOSE c_customers;
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Example
• DECLARE
• c_id customers.id%type;
• c_name customer S.No.ame%type;
• c_addr customers.address%type;
• CURSOR c_customers is
• SELECT id, name, address FROM customers;
• BEGIN OPEN c_customers;
• LOOP FETCH c_customers into c_id, c_name, c_addr; EXIT
WHEN c_customers%notfound; dbms_output.put_line(c_id ||
' ' || c_name || ' ' || c_addr); END LOOP; CLOSE c_customers;
• END; /

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)

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, balance
• FROM 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)


Department of Computer Science and Engineering (CSE)

Example(3)
• Preprocessed query:
• SELECT name, balance
• FROM (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 and Communication Engineering (CCE)

FAQ
• What is procedures ? Explain with the help of examples.
• What is formal and actual parameter?
• Explain Row trigger with the help of example.
• How we can enable, disable and drop trigger?
• What do you understand by packages and write its advantages.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

References
• https://docs.oracle.com/database/121/LNPLS/packages.htm
• https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/p
ackages.htm
• https://www.guru99.com/packages-pl-sql.html
• https://docs.oracle.com/cd/B19306_01/server.102/b14200/st
atements_6009.htm
• http://searchoracle.techtarget.com/definition/stored-procedur
e
• https://www.w3resource.com/sql/sql-procedure.php
• https://docs.oracle.com/cd/B19306_01/server.102/b14220/tri
ggers.htm
• https://docs.oracle.com/cd/A57673_01/DOC/server/doc/SCN7
3/ch15.htm
• https://docs.microsoft.com/en-us/sql/t-sql/statements/create-
trigger-transact-sql
University Institute of Engineering (UIE)

You might also like