Dbms
Dbms
Database management system: The term 'database management system', often abbreviated
to DBMS, refers to a software system used to create and manage databases. The software of
such systems is complex, consisting of a number of different components.
System catalogue/Data dictionary: The description of the data in the database management
system.
File-based systems were an early attempt to computerize the manual filing system. File-based
system is a collection of application programs that perform services for the end-users, such as
updating, insertion, deletion adding new files to database etc. Each program defines and
manages its data.
Redundancy is more.
Less integrity.
1
3) The database approach
The database approach is an improvement on the shared file solution as the use of a database
management system (DBMS) provides facilities for querying, data security and integrity, and
allows simultaneous access to data by a number of different users.
One of the benefits of the database approach is that the problem of physical data dependence
is resolved; this means that the underlying structure of a data file can be changed without the
application programs needing amendment. This is achieved by a hierarchy of levels of data
specification. Each such specification of data in a database system is called a schema.
2
4) ANSI/SPARC three-level architecture
The external schemas describe the database as it is seen by the user, and the user
applications.
The external schema allows the application programs to see as much of the data as they
require, while excluding other items that are not relevant to that application. In this way, the
external schema provides a view of the data that corresponds to the nature of each task.
.
The conceptual schema
The conceptual schema describes the universe of interest to the users of the database system.
At the level of the conceptual schema we are concerned with the data itself, rather than
storage or the way data is physically accessed on disk. The definition of storage and access
details is the preserve of the internal schema.
The internal schema
which contains definitions of the way in which data is physically stored. The interface between
the internal schema and the conceptual schema identifies how an element in the conceptual
schema is stored, and how it may be accessed.
3
5) Components of a DBMS
The major components of a DBMS are as follows:
DBMS engine :
The engine is the central component of a DBMS. This component provides access to the
database and coordinates all of the functional elements of the DBMS.
The interface subsystem provides facilities for users and applications to access the various
components of the DBMS
Periodically making backup copies of the database, and recovering the database in the event
of some type of failure
Data integrity management subsystem
The data integrity management subsystem provides facilities for managing the integrity of data
in the database and the integrity of metadata in the dictionary
Security management subsystem
The security management subsystem provides facilities to protect and control access to the
database and data dictionary.
Data dictionary subsystem
The data dictionary subsystem is used to store data about many aspects of how the DBMS
works
Performance management subsystem
The performance management subsystem provides facilities to optimise (or at least improve)
DBMS performance
Advantage of DBMS
The DBMS helps create an environment in which end users have better access to more
and better-managed data.
4
Such access makes it possible for end users to respond quickly to changes in their
environment.
A DBMS provides a framework for better enforcement of data privacy and security
policies.
Data inconsistency exists when different versions of the same data appear in different
places.
Better-managed data and improved data access make it possible to generate better-
quality information, on which better decisions are based.
Increased end-user productivity
Disadvantage of DBMS
Increased costs:
The cost of maintaining the hardware, software, and personnel required to operate and
manage a database system can be substantial.
Management complexity:
Database systems interface with many different technologies and have a significant
impact on a company’s resources and culture.
5
Maintaining currency:
DBMS vendors frequently upgrade their products by adding new functionality. Such new
features often come bundled in new upgrade versions of the software
7) Functions of a DBMS
A DBMS makes it possible for users to create, edit and update data in database files. Once
created, the DBMS makes it possible to store and retrieve data from those database files.
More specifically, a DBMS provides the following functions:
Concurrency: concurrent access (meaning 'at the same time') to the same database by
multiple users
Security: security rules to determine access rights of users
Backup and recovery: processes to back-up the data regularly and recover data if a
problem occurs
Integrity: database structure and rules improve the integrity of the data
Data descriptions: a data dictionary provides a description of the data
database designer:
a. logical designer : identify data (entities & attributes), relationships b/w data,
b. physical designer : mapping the logical data models in to a set of tables & integrity
constraints, select separate storage structures & access methods, designing security
measures.
6
application Programmer : responsible for developing application programs. uses a 3rd or 4th
generation programming languages
End Users:
9) Data Models
There are two types of object based data Models – Entity Relationship Model and Object
oriented data model.
ER model is a graphical representation of real world objects with their attributes and
relationship. It makes the system easily understandable.
Entities or real world objects are represented in a rectangular box. Their attributes are
represented in ovals. All the entities are mapped using diamonds
This data model is another method of representing real world objects. It considers each object
in the world as objects and isolates it from each other. It groups its related functionalities
together and allows inheriting its functionality to other related sub-groups.
Physical data model represent the model where it describes how data are stored in computer
memory.
7
It is represented as UML diagram along with table and its columns. Primary key is represented
at the top. The relationship between the tables is represented by interconnected arrows from
table to table.
There are 3 types of record based data models defined so far- Hierarchical, Network and
Relational data models
8
Because of single parent- It establishes the It provides SQL, which
child relationship, difficult to relationship between most makes the access to the
navigate through the child of the objects, hence easy data simpler and quicker.
to access compared to
hierarchical model
Flexibility among the Because of the mapping Primary and foreign key
different object is restricted among the sub level tables, constraint makes the
to the child. flexibility is more flexibility much simpler than
other models.
Based on the physical Based on the physical Based on the logical data
storage details storage details view
Attribute: It contains the name of a column in a particular table. Each attribute Ai must have a domain,
dom(Ai)
Relational instance: In the relational database system, the relational instance is represented by a
finite set of tuples. Relation instances do not have duplicate tuples.
Relational schema: A relational schema contains the name of the relation and name of all columns or
attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify the row in the
relation uniquely.
Properties of Relations
9
NAME ROLL_NO PHONE_NO ADDRESS AGE
o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the attributes.
o The instance of schema STUDENT has 5 tuples.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc. The value
of the attribute must be available in the corresponding domain.
10
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation and if the
primary key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.
11
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key. A primary
key can contain a unique and null value in the relational table.
Notation: σ p(r)
Where:
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result. Rest of the
attributes are eliminated from the table.
o It is denoted by ∏.
12
Where
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples that are
either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples that are
in both R & S.
o It is denoted by intersection ∩.
Notation: R ∩ S
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples that are
in R but not in S.
o It is denoted by intersection minus (-).
Notation: R - S
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the other
table. It is also known as a cross product.
o It is denoted by X.
Notation: E X D
13
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal on their
common attribute names.
o It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing
information.
14
EMPLOYEE
FACT_WORKERS
Input:
1. (EMPLOYEE ⋈ FACT_WORKERS)
Output:
15
EMP_NAME STREET CITY BRANCH SALARY
Input:
EMPLOYEE ⟕ FACT_WORKERS
16
Hari Nehru Hyderabad TCS 50000
street
3. Equi join: It is also known as an inner join. It is the most common join. It is based on matched
data as per the equality condition. The equi join uses the comparison operator(=).
Example:CUSTOMER RELATION
CLASS_ID NAME
17
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
Aggregation is a process in which a single entity alone is not able to make sense in a
relationship so the relationship of two entities acts as one entity.
18
Unit-II
19
Structured Query Language (SQL)
1 Introduction
o SQL stands for Structured Query Language. It is used for storing and managing
data in relational database management system (RDMS).
o It is a standard language for Relational Database System. It enables a user to
create, read, update and delete relational databases and tables.
o All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as
their standard database language.
o SQL allows users to query the database in a number of ways, using English-like
statements.
Rules:
SQL follows the following rules:
o Structure query language is not case sensitive. Generally, keywords of SQL are
written in uppercase.
o Statements of SQL are dependent on text lines. We can use a single SQL statement
on one or multiple text line.
o Using the SQL statements, you can perform most of the actions in a database.
o SQL depends on tuple relational calculus and relational algebra.
SQL process:
o When an SQL command is executing for any RDBMS, then the system figure out the
best way to carry out the request and the SQL engine determines that how to
interpret the task.
o In the process, various components are included. These components can be
optimization Engine, Query engine, Query dispatcher, classic, etc.
o All the non-SQL queries are handled by the classic query engine, but SQL query
engine won't handle logical files.
20
2) Characteristics of SQL
o SQL is easy to learn.
o SQL is used to access data from relational database management systems.
o SQL can execute queries against the database.
o SQL is used to describe the data.
o SQL is used to define the data in the database and manipulate it when needed.
o SQL is used to create and drop the database and table.
o SQL is used to create a view, stored procedure, function in a database.
o SQL allows users to set permissions on tables, procedures, and views.
3) SQL Datatype
o SQL Datatype is used to define the values that a column can contain.
o Every column is required to have a name and data type in the database table.
21
Data type Description
4) SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table, set permission for users.
22
Types of SQL Commands
o CREATE
o ALTER
o DROP
Example:
b. DROP: It is used to delete both the structure and record stored in the table.
c. ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
23
ALTER TABLE MODIFY(COLUMN DEFINITION....);
EXAMPLE
o INSERT
o UPDATE
o DELETE
o SELECT
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of
a table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2,.... col N) VALUES (value1, value2, .... valueN);
Or
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
d. SELECT: This is the same as the projection operation of relational algebra. It is used to
select the attribute based on the condition described by WHERE clause.
Syntax:
DCL commands are used to grant and take back authority from any database user.
o Grant
o Revoke
o COMMIT
o ROLLBACK
Example
Example
c. Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example:
25
DELETE FROM CUSTOMERS WHERE AGE = 25;
Output :COMMIT;
d. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.
Syntax: ROLLBACK;
Output: ROLLBACK;
5) SQL Operators
There are various types of SQL operator: They are
== (equall to)
!= (not equall to)
> (greater than)
>= (greater than or equall to )
< (less than )
<= (less than or euqall to)
26
Operator Description
BETWEEN It is used to search for values that are within a set of values.
6) Views in SQL
o Views in SQL are considered as a virtual table. A view also contains rows and
columns.
o To create the view, we can select the fields from one or more tables present in the
database.
o A view can either have specific rows based on certain condition or all the rows of a
table.
27
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Creating view
A view can be created using the CREATE VIEW statement. We can create a view from a
single table or multiple tables.
Syntax:
Creating View
In this example, we create a View named DetailsView from the table Student_Detail.
Query:
Just like table query, we can query the view to view the data.
28
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
Deleting View
Advantages:
1. Views don't store data in a physical location.
2. The view can be used to hide some of the columns from the table.
3. Views can provide Access Restriction, since data insertion, update and deletion
is not possible with the view.
Disadvantages:
1. When a table is dropped, associated view become irrelevant.
2. Since the view is created when a query requesting data from view is triggered,
its a bit slow.
3. When views are created for large tables, it occupies more memory.
29
7) SQL Index
o Indexes are special lookup tables. It is used to retrieve data from the database very
fast.
o An Index is used to speed up select queries and where clauses. But it shows down
the data input with insert and update statements. Indexes can be created or
dropped without affecting the data.
o An index in a database is just like an index in the back of a book.
Create Index:
Drop Index :
Rules:
o A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
o A subquery is a query within another query. The outer query is known as the main
query, and the inner query is known as a subquery.
o Subqueries are on the right side of the comparison operator.
o A subquery is enclosed in parentheses.
30
o In the Subquery, ORDER BY command cannot be used. But GROUP BY command
can be used to perform the same function as ORDER BY command.
Syntax
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
Output:
4 Alina 29 UK 6500.00
31
9) Aggregate Functions
o SQL aggregation function is used to perform the calculations on multiple rows of a
single column of a table. It returns a single value.
o It is also used to summarize the data.
COUNT( )
o COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.
SUM( )
Sum function is used to calculate the sum of all selected columns. It works on numeric
fields only.
Syntax: SUM()
AVG ( )
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Syntax: AVG()
MAX( ):
MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.
32
Syntax: MAX()
MIN( ):
MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax: MIN()
GROUP BY:
o SQL GROUP BY statement is used to arrange identical data into groups. The GROUP
BY statement is used with the SQL SELECT statement.
o The GROUP BY statement follows the WHERE clause in a SELECT statement and
precedes the ORDER BY clause.
o The GROUP BY statement is used with aggregation function.
Syntax
33
HAVING
Syntax:
Example:
ORDER BY:
Syntax:
Where
34
11) Integrity Constraints
Constraints are the rules that we can apply on the type of data in a table.
The available constraints in SQL are:
i) NOT NULL
If we specify a field in a table to be NOT NULL. Then the field will never accept null value.
ii) UNIQUE
This constraint helps to uniquely identify each row in the table. i.e. for a particular
column, all the rows should have unique values.
Primary Key is a field which uniquely identifies each row in the table.
Foreign Key is a field in a table which uniquely identifies each row of a another table. That
is, this field points to primary key of another table. This usually creates a kind of link
between the tables.
Syntax:
CREATE TABLE studentmarks(ID Number(6) foreign key reference student(ID),marks1
number(3), marks2 number(3));
35
v) CHECK
Using the CHECK constraint we can specify a condition for a field, which should be
satisfied at the time of entering values for this field.
vi) DEFAULT
If you want to add tables in that database, you can use CREATE TABLE statement.
If you want to delete or drop an existing database in a SQL schema, you can use SQL
DROP DATABASE.
ADVANCED SQL
PL/SQL is a block structured language. The programs of PL/SQL are logical blocks that can
contain any number of nested sub-blocks. Pl/SQL stands for "Procedural Language
extension of SQL" that is used in Oracle.
PL/SQL Functionalities
36
It can support Array and handle exceptions (runtime errors).
You can create PL/SQL units like procedures, functions, packages, types and
triggers, etc. which are stored in the database for reuse by applications.
With PL/SQL, you can use SQL statements to manipulate Oracle data and flow of
control statements to process the data.
PL/SQL is not case sensitive so you are free to use lower case letters or upper case
letters except within string and character literals.
A line of PL/SQL text contains groups of characters known as lexical units. It can be
classified as follows:
o Delimeters
o Identifiers
o Literals
o Comments
DECLARE
a integer := 30;
37
b integer := 40;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 100.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
OUTPUT: Value of c: 70 Value of f: 33.3
2) Constants
A constant is a value used in a PL/SQL block that remains unchanged throughout the
program. It is a user-defined literal value. It can be declared and used instead of actual
values.
3 Literals
Literals are the explicit numeric, character, string or boolean values which are not
represented by an identifier.
Literals Examples
38
4) Control Statements
If Statement
PL/SQL supports the programming language features like conditional statements and
iterative statements. Its programming constructs are similar to how you use in
programming languages like Java and C++.
i) IF-THEN statement
IF condition THEN
Statements
END IF;
This syntax is used when you want to execute statements only when condition is TRUE.
IF condition THEN
statements
ELSE
statements
END IF;
This syntax is used when you want to execute one set of statements when condition is
TRUE or a different set of statements when condition is FALSE.
IF condition1 THEN
statements
ELSIF condition2 THEN
statements
END IF;
This syntax is used when you want to execute one set of statements when condition1 is
TRUE or a different set of statements when condition2 is TRUE.
39
iv) IF-THEN-ELSIF-ELSE statement
IF condition1 THEN
statements
ELSIF condition2 THEN
statements
ELSE
Statements
END IF;
It is the most advance syntax and used if you want to execute one set of statements
when condition1 is TRUE, a different set of statement when condition2 is TRUE or a
different set of statements when both the condition1 and condition2 are FALSE.
Case Statement
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.
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
DECLARE
grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Good');
else dbms_output.put_line('Failed');
END CASE;
END;
Output: Excellent
40
Loop
The PL/SQL loops are used to repeat the execution of one or more statements for
specified number of times. These are also known as iterative control statements.
1. Basic Loop
2. While Loop
3. For Loop
Basic Loop
PL/SQL exit loop is used when a set of statements is to be executed at least once before
the termination of the loop. There must be an EXIT condition specified in the loop,
otherwise the loop will get into an infinite number of iterations. After the occurrence of
EXIT condition, the process exits the loop.
1. LOOP
2. Sequence of statements;
3. END LOOP;
DECLARE
i NUMBER := 1;
BEGIN
LOOP
EXIT WHEN i>10;
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
Output: 1 2 3 4 5 6 7 8 9 10
41
While Loop
PL/SQL while loop is used when a set of statements has to be executed as long as a
condition is true, the While loop is used. The condition is decided at the beginning of each
iteration and continues until the condition becomes false.
Syntax:
WHILE <condition>
LOOP statements;
END LOOP;
Write a PL/SQL program to print First 10 Numbers
DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
Output: 1 2 3 4 5 6 7 8 9 10
FOR Loop
The loop is iterated between the start and end integer values. The counter is always
incremented by 1 and once the counter reaches the value of end integer, the loop ends.
Syntax:
42
GOTO Statement
In PL/SQL, GOTO statement makes you able to get an unconditional jump from the GOTO
to a specific executable statement label in the same subprogram of the PL/SQL block.
Here the label declaration which contains the label_name encapsulated within the << >>
symbol and must be followed by at least one statement to execute.
GOTO label_name;
..
..
<<label_name>>
Statement;
5) PL/SQL Procedure
The PL/SQL stored procedure or simply a procedure is a PL/SQL block which performs one
or more specific tasks. It is just like procedures in other programming languages.
o Header: The header contains the name of the procedure and the parameters or
variables passed to the procedure.
o Body: The body contains a declaration section, execution section and exception
section similar to a general PL/SQL block.
1. IN parameters
2. OUT parameters
3. INOUT parameters
Syntax:
43
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
In this example, we are going to insert record in user table. So you need to create user
table first.
Table creation:
Procedure Code:
begin
insert into user values(id,name);
end;
/
Output:
Procedure created.
BEGIN
insertuser(101,'Rahul');
dbms_output.put_line('record inserted successfully');
END;
/ Output:
ID Name
101 Rahul
44
PL/SQL Drop Procedure
6) PL/SQL Function
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other hand
a procedure may or may not return a value.
Syntax:
Example
45
Output:
Addition is: 33
Statement processed.
0.05 seconds
7) Recursive Function
Defination: The function calls itself is called as recursion.
DECLARE
num number;
factorial number;
BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
Output:
Factorial 6 is 720
46
Drop Function:
8) Exception Handling
An error occurs during the program execution is called Exception.
PL/SQL facilitates programmers to catch such conditions using exception block in the
program and an appropriate action is taken against the error condition.
o System-defined Exceptions
o User-defined Exceptions
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
---
END;
9)Trigger
Triggers are stored programs, which are automatically executed or fired when some event
occurs.
47
o A database definition (DDL) statement (CREATE, ALTER, or DROP).
o A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Advantages of Triggers
Syntax:
48
3 Mahesh 24 Ghaziabad 24000
Create trigger:
10) 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.
o Implicit Cursors
o Explicit Cursors
1) 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.
49
The following table specifies the status of the cursor with each of its attribute.
Attribute Description
%FOUND Its return value is TRUE if DML statements like INSERT, DELETE
and UPDATE affect at least one row or more rows or a SELECT
INTO statement returned one or more rows. Otherwise it returns
FALSE.
%ISOPEN It always returns FALSE for implicit cursors, because the SQL
cursor is automatically closed after executing its associated SQL
statements.
Example
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
50
SET salary = salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/
2) Explicit Cursors
The Explicit cursors are defined by the programmers to gain more control over the context
area. It is created on a SELECT statement which returns more than one row.
Steps:
You must follow these steps while working with an explicit cursor.
51
2. Open the cursor to allocate memory.
11 ) Subprograms
A PL/SQL subprogram is a named PL/SQL block that can be invoked repeatedly. If the subprogram
has parameters, their values can differ for each invocation.
A subprogram is either a procedure or a function. Typically, you use a procedure to perform an action
and a function to compute and return a value.
12) Package
A package is a schema object that groups logically related PL/SQL types, variables, constants,
subprograms, cursors, and exceptions. A package is compiled and stored in the database, where many
applications can share its contents.
A package always has a specification, which declares the public items that can be referenced from
outside the package.
52
UNIT-III
Entity Relation Ship Modeling
1) ER model
o ER model stands for an Entity-Relationship model. It is a high-level data model.
This model is used to define the data elements and relationship for a specified
system.
o It develops a conceptual design for the database. It also develops a very simple and
easy to design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an entity-
relationship diagram.
For example: Suppose we design a school database. In this database, the student will be
an entity with attributes like address, name, id, age, etc. The address can be another
entity with attributes like city, street name, pin code, etc and there will be a relationship
between them.
2)Notation of ER diagram
In ER diagram, many notations are used to express the cardinality. These notations are as
follows:
3)Components of a ER Diagram
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity can be
represented as rectangles.
Types of Entities:
a. Weak Entity
An entity that depends on another entity called a weak entity. The weak entity doesn't
contain any key attribute of its own. The weak entity is represented by a double rectangle.
54
b. Strong Entity: An entity that not depends on another entity called a Strong entity.
c. Associative Entity:
If a Relation is also used as entity, that is if it is acting as both as relation and entity it is
known as Associative enity. It is represented by diamond symbol in rectangle box.
2. Attribute
The property (characteristic) of an entity is known as Attribute. Eclipse is used to
represent an attribute.
For example, id, age, contact number, name, etc. can be attributes of a student.
55
Types of Attributes:
The key attribute is used to represent the main characteristics of an entity. It represents a
primary key. The key attribute is represented by an ellipse with the text underlined.
b. Composite Attribute:
c. Multivalued Attribute:
An attribute can have more than one value. These attributes are known as a multivalued
attribute. The double oval is used to represent multivalued attribute.
For example, a student can have more than one phone number.
56
d. Derived Attribute
An attribute that can be derived from other attribute is known as a derived attribute. It
can be represented by a dashed ellipse.
For example, A person's age changes over time and can be derived from another
attribute like Date of birth.
3. Relationship:
Types of relationship:
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it is known as
one to one relationship.
For example, A female can marry to one male, and a male can marry to one female.
57
b. One-to-many relationship
When only one instance of the entity on the left, and more than one instance of an entity
on the right associates with the relationship then this is known as a one-to-many
relationship.
For example, Scientist can invent many inventions, but the invention is done by the only
specific scientist.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance of an entity
on the right associates with the relationship then it is known as a many-to-one
relationship.
For example, Student enrolls for only one course, but a course can have many students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one instance of an
entity on the right associates with the relationship then it is known as a many-to-many
relationship.
For example, Employee can assign by many projects and project can have many
employees.
58
4) Degree of a relationship:
The number of different entity sets participating in a relationship set is called as
degree of a relationship set.
1. Unary Relationship:
When a relation is having only one entity is known as unary relation. The degree of
relationship is ONE.
2. Binary Relationship:
When there are TWO entities set participating in a relation, the relationship is
called as binary relationship. The Degree of relationship is TWO.
3. Ternary Relationship:
When a relation is having more than two entities is known asTernary relation. The
degree of relationship is THREE or FOUR.
59
5) Attributes on Relationships
In ER model, When a relationships can also have attributes associated to them is known
as Attributes on relationships. That is like as entity is having an attribute.
Generally it is not recommended to give attributes to the relationships, because it is very
complex to convert ER diagram into table.
constraint specifies the minimum number of relationship instances that each entity can
participate in, and is sometimes called the minimum cardinality constraint.
the cardinality ratio and participation constraints, taken together, as the structural
constraints of a relationship type.
60
Problems with E-R
Models: (Fan Traps &
Chasm Traps):
In E-R model, due to
misinterpretation
(misunderstanding) of the
meaning of
61
certain relationships, a
problem known as
connection traps results.
There are
mainly two types of
connection traps.
1) Fan traps
2) Chasm traps
1) Fan Trap:
A model represents a
relationship between
62
entity types but the
pathway
between certain entity
occurrences is ambiguous
(confusing).
It occurs when two
relationship sets, having
mapping cardinality one-
to-
63
many converge (come
together) to single entity
set. I.e. two or more one-
to-many relationships fan
out from the same entity
set.
For example consider the
following E-R diagram
7) Problems with ER Models
There are several problems that may arise when designing a conceptual data model.
These are known as connection traps.
1. fan traps
2. chasm traps
Fan traps
64
A fan trap occurs when a model represents a relationship between entity types, but the
pathway between certain entity occurrences is ambiguous. It occurs when 1:m
relationships fan out from a single entity.
Fan Trap
The fan trap is resolved by restructuring the original ER model to represent the correct
association.
Chasm traps
A chasm trap occurs when a model suggests the existence of a relationship between entity
types, but the pathway does not exist between certain entity occurrences.
It occurs where there is a relationship with partial participation, which forms part of the
pathway between entities that are related.
Chasm Trap
You need to therefore be careful when you remove relationships which you consider to be
redundant.
65
Problems with E-R
Models: (Fan Traps &
Chasm Traps):
In E-R model, due to
misinterpretation
(misunderstanding) of the
meaning of
certain relationships, a
problem known as
connection traps results.
There are
66
mainly two types of
connection traps.
1) Fan traps
2) Chasm traps
1) Fan Trap:
A model represents a
relationship between
entity types but the
pathway
67
between certain entity
occurrences is ambiguous
(confusing).
It occurs when two
relationship sets, having
mapping cardinality one-
to-
many converge (come
together) to single entity
set. I.e. two or more one-
68
to-many relationships fan
out from the same entity
set.
For example consider the
following E-R diagram
Problems with E-R
Models: (Fan Traps &
Chasm Traps):
In E-R model, due to
misinterpretation
69
(misunderstanding) of the
meaning of
certain relationships, a
problem known as
connection traps results.
There are
mainly two types of
connection traps.
1) Fan traps
2) Chasm traps
1) Fan Trap:
70
A model represents a
relationship between
entity types but the
pathway
between certain entity
occurrences is ambiguous
(confusing).
It occurs when two
relationship sets, having
mapping cardinality one-
to-
71
many converge (come
together) to single entity
set. I.e. two or more one-
to-many relationships fan
out from the same entity
set.
For example consider the
following E-R diagram
Enhanced Entity Relationship Model (EER Model)
Whenever ER model is adding the feature of Generalization / Specialization that model is
known as EER Model.
1) Generalization
Generalization is a process in which the common attributes of more than one entities
form a new entity. This newly formed entity is called generalized entity or Super Entity.
72
o Generalization is like a bottom-up approach in which two or more entities of lower
level combine to form a higher level entity if they have some attributes in common.
o In generalization, an entity of a higher level can also combine with the entities of
the lower level to form a further higher level entity.
o Generalization is more like subclass and super class system, but the only difference
is the approach. Generalization uses the bottom-up approach.
o In generalization, entities are combined to form a more generalized entity, i.e.,
subclasses are combined to make a super class.
Example:
These two entities have two common attributes: Name and Address, we can make a
generalized entity with these common attributes. Lets have a look at the ER model after
generalization.
73
2)Specialization:
74
All of these three employee types have few attributes common such as name & salary
which we had left associated with the parent entity “Employee” as shown in the above
diagram.
3) Attribute Inheritance:
Deriving (taking) the attributes from General entity to Sub entity is known as attribute
Inheritance.
4) Aggregation
Aggregation is a process in which a single entity alone is not able to make sense in a
relationship so the relationship of two entities acts as one entity.
75
In real world, we know that a manager not only manages the employee working under
them but he has to manage the project as well.
In these cases the relationship of two entities acts as one entity. In our example, the
relationship “Works-On” between “Employee” & “Project” acts as one entity that has a
relationship “Manages” with the entity “Manager”.
5. Functional Dependency
A Constraint between two attributes or two sets of attributes is called as functional
dependency.
A Functional dependency in which one or more than one non key attributes are
functionally depend on part (but not all) of the primary key is called partial functional
dependency.
7.Transitive Dependency:
A Functional dependency between two or more non key attributes is called as transitive
dependency.
8.Mutivalued Dependency:
76
This type of dependency exists when thre are three attributes, For Example X,Y and Z in a
Relation with a well defied set of Y and Z values for each X value , but those Y and Z
values are independent of each other is called as multivalued dependency.
9. Join Dependency
If a table can be recreated by joining multiple tables and each of this table have a subset of the
attributes of the table, then the table is in Join Dependency. It is a generalization of Multivalued
Dependency
Join Dependency can be related to 5NF, wherein a relation is in 5NF, only if it is already in
4NF and it cannot be decomposed further.
6.Anomalies:
Anomalies are problems (Errors) that can occur in poorly planned, un-normalised
databases where all the data is stored in one table.
Insertion Anomaly: The nature of a database may be such that it is not possible
to add a required piece of data unless another piece of unavailable data is also
added.
Example: A library database that cannot store the details of a new member until
that member has taken out a book.
Example: Deleting a book loan from a library member can remove all details of the
particular book from the database such as the author, book title etc.
updation Anomaly: Incorrect data may have to be changed, which could involve
many records having to be changed, leading to the possibility of some changes
being made incorrectly.
Normalization
a. saving space
b. reducing the chances on errors
o During the normalization phase, the database designer will look at which data is
stored where, and in what way. And he will try to reduce the number of times the
same data is stored.
o Normalization is the part of design process. Therefore you should be aware of
good design .
o Principles and procedures, as well as normalization on procedures.
o Normalization is very useful to support to developing an ER diagrams. For
example 2NF represents a macro view of entities within the ER diagram.
Types of Normal Forms: There are the four types of normal forms:
78
Normal Description
Form
2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are
fully functional dependent on the primary key.
EMPLOYEE table:
14 Raju 7272826385, UP
9064738238
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 Raju 7272826385 UP
14 Raju 9064738238 UP
Conditions 2: Evert Non key attribute is functionally dependent on the full set of
Primary key attribute.(i.e Partial functional dependency exit)
Condition 3: No Non key attribute exists in the Relation (i.e all attributes are keys)
80
In the above Example Employee Relation is in 1NF but not in 2NF. So Convert this relation
into 2NF to decompose the into new relations like as follows
Example: WORKER
In the above Relation Worker is in 2 NF but not in in 3NF, because transitive dependency
exited between two non keys i.e SkillType and Rate.
81
o If a Relation is in 3NF and it is not having any anomalies (i.e Insertion, deletion and
Updation) then the relation is in BCNF.
Normally a design methodology is broken down into phases or stages and for each phase
the detailed steps are outlined, and appropriate tools and techniques are specified.
There are THREE main phases of database design methodology for relational
databases.
b. logical Phase: The logical design phase maps the conceptual model on to a logical
model of a specific data model, but independent of the software and physical
consideration
c. physical database design Phase: The physical design phase is tailored to a specific
DBMS and very focused to the hardware requirements
5.Data Redundancy:
Storing the same data in more than one place is called as data redundancy.
Data redundancy is a condition created within a database or data storage
technology in which the same piece of data is held in two separate places.
Whenever data is repeated, if any loss we can recovery from another place.
But is May chance to go inconsistency.
Length and cost will increase.
Complexity to maintain data.
UNIT-IV
Transaction Management
82
Definition: A transaction can be defined as a group of tasks. A single task is the
minimum processing unit which cannot be divided further.
Example of a simple transaction: Suppose a bank employee transfers Rs 500 from A's
account to B's account. This very simple and small transaction involves several low-level
tasks.
A’s Account
Open_Account(A)
Old_Balance = A.balance
New_Balance = Old_Balance - 500
A.balance = New_Balance
Close_Account(A)
B’s Account
Open_Account(B)
Old_Balance = B.balance
New_Balance = Old_Balance + 500
B.balance = New_Balance
Close_Account(B)
A transaction is a very small unit of a program and it may contain several low level tasks.
A transaction in a database system must maintain Atomicity, Consistency, Isolation,
and Durability − commonly known as ACID properties − in order to ensure accuracy,
completeness, and data integrity.
Atomicity − This property states that a transaction must be treated as an atomic
unit, that is, either all of its operations are executed or none. No transaction is left
partially completed.
Consistency − If the database was in a consistent state before the execution of a
transaction, it must remain consistent after the execution of the transaction as
well.
Durability − The database should be durable enough to hold all its latest updates
even if the system fails or restarts. If a transaction commits but the system fails
before the data could be written on to the disk, then that data will be updated once
the system springs back into action.
Isolation − The property of isolation states that all the transactions will be carried
out and executed as if it is the only transaction in the system. No transaction will
affect the existence of any other transaction.
2.Serializability
83
When multiple transactions are being executed by the operating system in a
multiprogramming environment, there are possibilities that instructions of one
transactions are interleaved with some other transaction.
Schedule − A chronological execution sequence of a transaction is called a
schedule. A schedule can have many transactions in it, each comprising of a
number of instructions/tasks.
Serial Schedule − Transactions are ordered one after the other. This type of
schedule is called a serial schedule, as transactions are executed in a serial
manner.
3.Concurrency Control
In a multiprogramming environment where multiple transactions can be executed
simultaneously, it is highly important to control the concurrency of transactions.
Concurrency control protocols can be broadly divided into two categories −
i) Lock-based Protocols
Database systems equipped with lock-based protocols use a mechanism by which any
transaction cannot read or write data until it acquires an appropriate lock on it. Locks are
of two kinds −
Binary Locks − A lock on a data item can be in two states; it is either locked or
unlocked.
Shared/exclusive − If a lock is acquired on a data item to perform a write
operation, it is an exclusive lock. Allowing more than one transaction to write on
the same data item would lead the database into an inconsistent state. Read locks
are shared because no data value is being changed.
There are four types of lock protocols available −
84
c.Two-Phase Locking (2PL) Protocol
This locking protocol divides the execution phase of a transaction into three parts.
In the first part, when the transaction starts executing, it seeks permission for the locks
it requires.
The second part is where the transaction acquires all the locks. As soon as the
transaction releases its first lock, the third phase starts. In this phase, the transaction
cannot demand any new locks; it only releases the acquired locks.
Two-phase locking has two phases, one is growing, where all the locks are being
acquired by the transaction; and the second phase is shrinking, where the locks held by
the transaction are being released.
To claim an exclusive (write) lock, a transaction must first acquire a shared (read) lock
and then upgrade it to an exclusive lock.
85
Strict-2PL does not have cascading abort as 2PL does.
ii).Timestamp-based Protocols
The most commonly used concurrency protocol is the timestamp based protocol. This
protocol uses either system time or logical counter as a timestamp.
Lock-based protocols manage the order between the conflicting pairs among transactions
at the time of execution, whereas timestamp-based protocols start working as soon as a
transaction is created.
Every transaction has a timestamp associated with it, and the ordering is determined by
the age of the transaction.
A transaction created at 0002 clock time would be older than all other transactions that
come after it. For example, any transaction 'y' entering the system at 0004 is two
seconds younger and the priority would be given to the older one.
In addition, every data item is given the latest read and write-timestamp. This lets the
system know when the last ‘read and write’ operation was performed on the data item.
The lost update problem: A second transaction writes a second value of a data-
item (datum) on top of a first value written by a first concurrent transaction, and
the first value is lost to other transactions running concurrently which need, by
86
their precedence, to read the first value. The transactions that have read the
wrong value end with incorrect results.
The dirty read problem: Transactions read a value written by a transaction that
has been later aborted. This value disappears from the database upon abort, and
should not have been read by any transaction (“dirty read”). The reading
transactions end with incorrect results.
The incorrect summary problem: While one transaction takes a summary over the
values of all the instances of a repeated data-item, a second transaction updates
some instances of that data-item.
6. DEAD LOCK
In a multi-process system, deadlock is an unwanted situation that arises in a shared
resource environment, where a process indefinitely waits for a resource that is held by
another process.
For example, assume a set of transactions {T 0, T1, T2, ...,Tn}. T0 needs a resource X to
complete its task. Resource X is held by T 1, and T1 is waiting for a resource Y, which is
held by T2. T2 is waiting for resource Z, which is held by T 0. Thus, all the processes wait
for each other to release resources.
In this situation, none of the processes can finish their task. This situation is known as a
deadlock.
Deadlocks are not healthy for a system. In case a system is stuck in a deadlock, the
transactions involved in the deadlock are either rolled back or restarted.
Deadlock Avoidance
Wait-for Graph
This is a simple method available to track if any deadlock situation may arise. For each
transaction entering into the system, a node is created. When a transaction T i requests
for a lock on an item, say X, which is held by some other transaction T j, a directed edge
is created from Ti to Tj. If Tj releases item X, the edge between them is dropped and
Ti locks the data item.
The system maintains this wait-for graph for every transaction waiting for some data
items held by others. The system keeps checking if there's any cycle in the graph.
87
6. Database Recovery
It is the method of restoring the database to its correct state in the event of a failure at
the time of the transaction or after the end of a process.
Earlier, you have been given the concept of database recovery as a service that should be
provided by all the DBMS for ensuring that the database is dependable and remains in a
consistent state in the presence of failures.
In this context, dependability refers to both the flexibility of the DBMS to various kinds of
failure and its ability to recover from those failures. In this chapter, you will gather a brief
knowledge of how this service can be provided. To gain a better understanding of the
possible problems you may encounter in providing a consistent system, you will first learn
about the need for recovery and its types of failure, which usually occurs in a database
environment.
Due to hardware or software errors, the system crashes, which ultimately resulting in
loss of main memory.
Failures of media, such as head crashes or unreadable media that results in the loss of
portions of secondary storage.
There can be application software errors, such as logical errors that are accessing the
database that can cause one or more transactions to abort or fail.
Natural physical disasters can also occur, such as fires, floods, earthquakes, or power
failures.
Carelessness or unintentional destruction of data or directories by operators or users.
88
Damage or intentional corruption or hampering of data (using malicious software or files)
hardware or software facilities.
8. Recovery Facilities
Every DBMS should offer the following facilities to help out with the recovery mechanism:
Backup mechanism makes backup copies at a specific interval for the database.
Logging facilities keep tracing the current state of transactions and any changes made to
the database.
Checkpoint facility allows updates to the database for getting the latest patches to be
made permanent and keep secure from vulnerability.
Recovery manager allows the database system for restoring the database to a reliable
and steady-state after any failure occurs.
9.Optimistic Technique
In systems with low conflict rates, the task of validating every transaction for
serializability may lower performance. In these cases, the test for serializability is
postponed to just before commit. Since the conflict rate is low, the probability of aborting
transactions which are not serializable is also low. This approach is called optimistic
concurrency control technique.
In this approach, a transaction’s life cycle is divided into the following three phases −
Execution Phase − A transaction fetches data items to memory and performs
operations upon them.
Validation Phase − A transaction performs checks to ensure that committing its
changes to the database passes serializability test.
Commit Phase − A transaction writes back modified data item in memory to the
disk.
Database Security
Database security is the technique that protects and secures the database against
intentional or accident
al threats. Security concerns will be relevant not only to the data resides in an
organization's database: the breaking of security may harm other parts of the system,
which may ultimately affect the database structure. Consequently, database security
includes hardware parts, software parts, human resources, and data. To efficiently do the
uses of security needs appropriate controls, which are distinct in a specific mission and
purpose for the system. The requirement for getting proper security while often having
89
been neglected or overlooked in the past days; is now more and more thoroughly checked
by the different organizations.
We consider database security about the following situations:
1.Threats
Any situation or event, whether intentionally or incidentally, can cause damage, which can
reflect an adverse effect on the database structure and, consequently, the organization. A
threat may occur by a situation or event involving a person or the action or situations that
are probably to bring harm to an organization and its database.
The degree that an organization undergoes as a result of a threat's following which
depends upon some aspects, such as the existence of countermeasures and contingency
plans. Let us take an example where you have a hardware failure that occurs corrupting
secondary storage; all processing activity must cease until the problem is resolved.
2. Computer-Based Controls
The different forms of countermeasure to threats on computer systems range from
physical controls to managerial procedures. In spite of the range of computer-based
controls that are preexisting, it is worth noting that, usually, the security of a DBMS is
merely as good as that of the operating system, due to the close association among them.
Most of the computer-based database security are listed below:
Access authorization.
Access controls.
Views.
Backup and recovery of data.
Data integrity.
Encryption of data.
RAID technology.
90
3. Access Controls
The usual way of supplying access controls to a database system is dependent on the
granting and revoking of privileges within the database. A privilege allows a user to create
or access some database object or to run some specific DBMS utilities. Privileges are
granted users to achieve the tasks required for those jobs.
The database provides various types of access controls:
Every Database Management System should offer backup facilities to help with the
recovery of a database after a failure. It is always suitable to make backup copies of the
database and log files at the regular period and for ensuring that the copies are in a
secure location. In the event of a failure that renders the database unusable, the backup
copy and the details captured in the log file are used to restore the database to the latest
possible consistent state.
5. Integrity
Data integrity is the overall completeness, accuracy and consistency of data. This can be
indicated by the absence of alteration between two instances or between two updates of a
data record, meaning data is intact and unchanged. Data integrity is usually imposed
during the database design phase through the use of standard procedures and rules. Data
integrity can be maintained through the use of various error-checking methods and
validation procedures.
Database encryption is the process of converting data, within a database, in plain text
format into a meaningless cipher text by means of a suitable algorithm.
Database decryption is converting the meaningless cipher text into the original
information using keys generated by the encryption algorithms.
Database encryption can be provided at the file or column level.
91
7. Redundant Array of Independent Disks(RAID)
RAID or Redundant Array of Independent Disks, is a technology to connect multiple
secondary storage devices and use them as a single storage media.
RAID consists of an array of disks in which multiple disks are connected together to
achieve different goals. RAID levels define the use of disk arrays.
RAID 0
In this level, 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.
RAID 1
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
ECC codes of the data words are stored on a different set disks.
92
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.
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.
RAID 6
RAID 6 is an extension of level 5. In this level, two independent parities are generated
and stored in distributed fashion among multiple disks. Two parities provide additional
fault tolerance. This level requires at least four disk drives to implement RAID.
93