DBMS With PL - SQL PROGRAMMING
DBMS With PL - SQL PROGRAMMING
AUTHORS
ACKNOWLEDGEMENT
Many persons have contributed to make this book a reality. We would like to
express our gratitude to Thiru. V. Chandrasekaran, Founder, Sri Vidya Mandir
Group of Educational Institutions, Uthangarai for his continuous guidance, support
and encouragement.
We would like to express our gratitude to Dr. T. Balasubramanian, Principal
and Dr. N. Gunasekaran, Vice Principal, Sri Vidya Mandir Arts & Science College
(Autonomous), Katteri, Uthangarai for his unstinted support and encouragement.
We wish to express thanks to all the staff members of Department of
Computer Science & Applications for their contribution and ideas in creating this
book.
Finally, we wish to express our sincere thanks to all our family members for
their moral support.
AUTHORS
CONTENTS
CHAPTER – 1 (INTRODUCTION)
1.1 INTRODUCTION 1
1.1.1 DBMS 1
1.1.2 DATABASE 1
1.1.3 GOALS OF DBMS 1
1.1.4 WHAT IS THE NEED OF DBMS 1
1.2 DATABASE SYSTEM APPLICATIONS 2
1.3 PURPOSE OF DATABASE SYSTEMS 3
1.4 DIFFERENCE BETWEEN DATABASE SYSTEMS AND FILE SYSTEMS 6
1.5 VIEW OF DATA 7
1.5.1 DATA ABSTRACTION 7
1.5.2 INSTANCES AND SCHEMAS 8
1.6 DATA MODELS 9
1.6.1 RECORD BASED LOGICAL MODEL 10
1.6.1.1 RELATIONAL MODEL 10
1.6.1.2 HIERARCHICAL MODEL 10
1.6.1.3 NETWORK MODEL 11
1.6.2 OBJECT BASED LOGICAL MODEL 11
1.6.2.1 ENTITY-RELATIONSHIP MODEL 11
1.6.2.2 OBJECT-BASED DATA MODEL 11
1.7 DATABASE LANGUAGES 12
1.7.1 DATA-DEFINITION LANGUAGE 12
1.7.2 DATA-MANIPULATION LANGUAGE 14
1.8 DATABASE USERS AND ADMINISTRATORS 15
1.8.1 DATABASE USERS AND USER INTERFACES 15
1.8.1.1 NAIVE USERS 16
1.8.1.2 APPLICATION PROGRAMMERS 16
1.8.1.3 SOPHISTICATED USERS 16
1.8.1.4 SPECIALIZED USERS 16
1.8.2 DATABASE ADMINISTRATOR 17
1.8.2.1 FUNCTIONS OF A DBA 17
1.8.2.2 TYPES OF DBA 17
1.9 TRANSACTION MANAGEMENT 19
1.10 DATABASE ARCHITECTURE 21
1.10.1 DATABASE SYSTEM STRUCTURE 21
1.10.2 APPLICATION ARCHITECTURE 23
CHAPTER – 2 (DATA MODELS)
2.1 DATA MODELS 24
2.2 ENTITY-RELATIONSHIP MODEL 24
2.3 CONSTRAINTS 27
2.3.1 TYPES OF CONSTRAINTS 27
2.4 KEYS 31
2.5 ENTITY RELATIONSHIP DIAGRAMS 33
2.6 WEAK ENTITY SET 35
2.7 EXTENDED ER FEATURES 36
2.7.1 SPECIALIZATION 36
2.7.2 GENERALIZATION 39
2.7.3 ATTRIBUTE INHERITANCE 40
2.8 DESIGN OF ER DATABASE SCHEMA 41
2.8.1 DECISIONS 41
2.8.2 DESIGN PHASES 42
CHAPTER – 3 (RELATIONAL MODEL)
3.1 STRUCTURE OF RELATIONAL DATABASE 44
3.2 DATABASE SCHEMA 45
3.3 KEYS 45
3.4 QUERY LANGUAGES 47
3.5 RELATIONAL-ALGEBRA 47
3.5.1 FUNDAMENTAL OPERATIONS 47
3.6 VIEWS 55
3.6.1 VIEW DEFINITION 56
3.6.2 VIEWS DEFINED BY USING OTHER VIEWS 57
CHAPTER – 4 (SQL)
4.1 SQL (STRUCTURED QUERY LANGUAGE) 58
4.1.1 BACKGROUND 58
4.2 BASIC STRUCTURE OF SQL QUERIES 59
4.2.1 RENAME OPERATION 61
4.2.2 TUPLE VARIABLES 61
4.2.3 STRING OPERATIONS 61
4.3 SET OPERATIONS 63
4.3.1 UNION OPERATION 64
4.3.2 INTERSECT OPERATION 64
4.3.3 EXCEPT OPERATION 65
4.4 AGGREGATE FUNCTIONS 66
4.5 NULL VALUES 68
4.6 NESTED SUBQUERIES 68
4.6.1 SET MEMBERSHIP 69
4.6.2 SET COMPARISON 69
4.6.3 TEST FOR EMPTY RELATIONS 70
4.7 VIEWS 70
4.7.1 VIEW DEFINITION 70
4.7.2 VIEWS DEFINED BY USING OTHER VIEWS 71
4.8 MODIFICATION OF THE DATABASE 72
4.8.1 DELETION 72
4.8.2 INSERTION 72
4.8.3 UPDATES 73
4.8.4 TRANSACTIONS 73
4.9 DATA DEFINITION LANGUAGE 74
4.9.1 BASIC DOMAIN TYPES 74
4.9.2 BASIC SCHEMA DEFINITION IN SQL 75
4.10 EMBEDDED SQL 77
4.11 DYNAMIC SQL 79
4.11.1 ODBC 80
4.11.2 JDBC 80
CHAPTER – 5 (NORMALIZATION)
5.1 DATA NORMALIZATION 84
5.1.1 DEFINITION 84
5.1.2 PITFALLS IN RELATIONAL DATABASE DESIGN 84
5.2 DECOMPOSITION 85
5.2.1 REASON FOR LOSSY DECOMPOSITION 87
5.2.2 DESIRABLE PROPERTIES OF DECOMPOSITION 87
5.3 FUNCTIONAL DEPENDENCIES 88
5.3.1 TRIVIAL FUNCTIONAL DEPENDENCY 89
5.3.2 NON TRIVIAL FUNCTIONAL DEPENDENCY 89
5.4 NORMALIZATION 90
5.4.1 FIRST NORMAL FORM (1NF) 90
5.4.2 SECOND NORMAL FORM (2NF) 91
5.4.3 THIRD NORMAL FORM (3NF) 92
5.4.4 BOYCE-CODD NORMAL FORM (BCNF) 93
5.4.5 FOURTH NORMAL FORM (4NF) 94
5.4.6 FIFTH NORMAL FORM (5NF) 96
5.5 DENORMALIZATION 97
5.6 OVERALL DATABASE DESIGN PROCESS 97
CHAPTER – 6 (DATA SECURITY)
6.1 DATA SECURITY REQUIREMENTS 101
6.1.1 CONFIDENTIALLY 101
6.1.2 INTEGRITY 101
6.1.3 AVAILABILITY 103
6.2 ASSERTIONS 104
6.3 SECURITY 104
6.4 PROTECTING THE DATA WITHIN THE DATABASE 105
6.4.1 PRIVILEGES 106
6.4.2 ROLES 107
6.4.3 REVOKING AUTHORIZATION IN SQL 107
6.5 ENCRYPTION 108
6.6 AUTHENTICATION 108
CHAPTER – 7 (PL/SQL: A PROGRAMMING LANGUAGE)
7.1 HISTORY OF PL/SQL 110
7.2 FUNDAMENTALS OF PL/SQL 111
7.2.1 RESERVED WORDS 111
7.2.2 USER-DEFINED IDENTIFIERS 111
7.2.3 LITERALS 112
7.2.4 COMMENTS 112
7.3 PL/SQL BLOCK STRUCTURE 113
7.4 DATA TYPES 115
7.4.1 SCALAR 115
7.4.2 LOB 117
7.5 VARIABLE DECLARATION 117
7.6 ASSIGNMENT OPERATION 118
7.7 BIND VARIABLES 118
7.8 SUBSTITUTION VARAIBLES IN PL/SQL 119
7.9 PRINTING IN PL/SQL 120
7.10 ARITHMETIC OPERATIONS 120
7.11 CONTROL STRUCTURES & EMBEDDED SQL 121
7.11.1 SEQUENTIAL STRUCTURE 121
7.11.2 SELECTION STRUCTURE 122
7.11.3 LOOPING STRUCTURE 129
7.12 NESTED BLOCKS 133
7.13 SQL IN PL/SQL 133
7.14 DATA MANIPULATION IN PL/SQL 135
7.16 TRANSACTION CONTROL STATEMENT 136
CHAPTER – 8 (PL/SQL CURSORS & EXCEPTIONS)
8.1 CURSORS 137
8.2 IMPLICIT CURSOR 137
8.2.1 IMPLICIT CURSOR ATTRIBUTES 138
8.3 EXPLICIT CURSOR 138
8.3.1 EXPLICIT CURSOR ATTRIBUTES 140
8.4 CURSOR FOR LOOPS 142
8.5 SELECT FOR….UPDATE CURSOR 142
8.6 WHERE CURRENT OF CLAUSE 143
8.7 CURSOR WITH PARAMETERS 144
8.8 CURSOR VARIABLES 145
8.9 EXCEPTIONS 145
8.9.1 TYPES OF EXCEPTION 146
8.10 RAISE_APPLICATION_ERROR PROCEDURE 150
CHAPTER – 9 (COMPOSITE DATA TYPES)
9.1 PL/SQL RECORDS 151
9.1.1 CREATING PL/SQL RECORD 151
9.1.2 REFERENCING FIELDS IN A RECORD 152
9.1.3 WORKING WITH RECORDS 152
9.1.4 NESTED RECORDS 153
9.2 PL/SQL TABLES 154
9.2.1 DECLARING A PL/SQL TABLE 154
9.2.2 PL/SQL TABLE STRUCTURE 154
9.2.3 REFERENCING TABLE ELEMENTS/ROWS 155
9.2.4 ASSIGNING VALUES TO ROWS IN A PL/SQL TABLE 155
9.2.5 BUILT-IN TABLE METHODS 156
9.2.6 TABLE OF RECORDS 157
9.3 PL/SQL VARRAYS 157
CHAPTER – 10 (NAMED BLOCKS)
10.1 PROCEDURES 160
10.1.1 CALLING A PROCEDURE 160
10.1.2 PROCEDURE HEADER 161
10.1.3 PROCEDURE BODY 161
10.4.4 PARAMETERS 161
10.2 FUNCTIONS 162
10.2.1 FUNCTION HEADER 163
10.2.2 FUNCTION BODY 163
10.2.3 CALLING A FUNCTION 164
10.2.4 FUNCTION CALL 165
10.3 PACKAGES 165
10.3.1 STRUCTURE OF A PACKAGE 166
10.3.2 PACKAGE SPECIFICATION 166
10.3.3 PACKAGE BODY 167
10.4 TRIGGERS 169
10.4.1 USES OF TRIGGER 170
10.4.2 RESTRICTIONS FOR CREATING A TRIGGER 170
10.4.3 BEFORE TRIGGERS 170
10.4.4 AFTER TRIGGERS 171
10.4.5 INSTEAD OF TRIGGER 172
REFERENCES 173
CHAPTER - 1
1.1 INTRODUCTION:
1.1.1 DBMS:
A Database Management System (DBMS) is a collection of interrelated data
and a set of programs to access those data.
1.1.2 DATABASE:
The collection of data usually referred to as the database.
1.1.3 GOAL OF DBMS:
The primary goal of a DBMS is to provide a way to store and retrieve
database information that is both convenient and efficient.
Database systems are designed to manage large bodies of information.
Management of data involves both defining structures for storage of
information and providing mechanisms for the manipulation of information.
The database system must ensure the safety of the information stored,
despite crashes or attempts at unauthorized access.
If data are to be shared among several users, the system must avoid
possible anomalous results.
1.1.4 What is the need of DBMS?
Database systems are basically developed for large amount of data. When
dealing with huge amount of data, there are two things that require optimization:
Storage of data and
Retrieval of data
Storage:
According to the principles of database systems, the data is stored in such a
way that it acquires lot less space as the redundant data (duplicate data) has been
removed before storage. In a banking system, suppose a customer is having two
accounts, one is saving account and another is salary account.
Let‘s say bank stores saving account data at one place and salary account
data at another place, in that case if the customer information such as customer
name, address etc. are stored at both places then this is just a wastage of storage
(redundancy/ duplication of data), to organize the data in a better way the
Page | 1
information should be stored at one place and both the accounts should be linked to
that information somehow. The same thing we achieve in DBMS.
Fast Retrieval of data:
Along with storing the data in an optimized and systematic manner, it is
also important that we retrieve the data quickly when needed. Database systems
ensure that the data is retrieved as quickly as possible.
1.2 DATABASE SYSTEM APPLICATIONS:
Databases are widely used, the application areas are,
Banking
Airlines
Universities
Credit card transactions
Telecommunication
Finance
Sales
Online retailers
Manufacturing
Human resources
Banking:
Here, Database used for customer information accounts, loans and banking
transactions.
Airlines:
Database used for reservations and schedule information.
Universities:
Database used for student information, course registration and grades.
Credit card transactions:
In this case, database used for purchases on credit cards and generation of
monthly statements.
Telecommunication:
Database used for keeping records of calls generating monthly bills,
maintaining balances on prepaid calling cards.
Page | 2
Finance:
Database used for storing information about holdings, sales and purchases
of financial instruments.
In this case, database is also used for storing real time market data to enable
online trading by customers.
Sales:
Database used for customer, product and purchase information.
Online retailers:
Database used for generation of recommendation lists and maintenance of
online product evaluation.
Manufacturing:
Database used for management of supply chain and tracking production of
items, inventories of items and stores and orders for items.
Human resources:
In this case, database used for maintaining information about employees,
salaries, payroll taxes benefits and generation for pay checks.
1.3 PURPOSE OF DATABASE SYSTEMS / FILE PROCESSING SYSTEM:
Database systems arose in response in early computerized management of
commercial data.
In early days , one way to keep the information on computer is to store it is
operating system files, it allow users to manipulate the information so the system
has number of application programs to manipulate the files.
New application programs are added to the system as the need arises thus,
after some times the system acquires more files and more application programs.
The file – processing system is supported by a conventional operating
system.
The system stores permanent records in various files, and it needs different
application programs to extract records from, and add records to the appropriate
files.
Page | 3
Keeping organizational information in a file-processing system has a
number of disadvantages,
Data Redundancy and inconsistency
Difficulty in accessing data
Data isolation
Integrity problems
Atomicity problems
Concurrent access anomalies
Security problems
Data Redundancy and inconsistency:
Different programmers create the files and application programs over a
long period and various files have different structures and programs may written in
several programming languages, so there is a chance for same information may be
duplicated in several places(files).
This redundancy leads to higher storage and access cost. In addition it may
lead to data inconsistency that is, the various copies of the same data may no longer
agree.
Difficulty in accessing data:
For example, the bank officer needs to find out the names of all customers
who live within a particular postal code area. The officer asks the data-processing
department to generate such a list but there is no application program to meet this
requirement.
In this situation, the bank officer has two choices, i.e.
i) Either obtain the list of all customers and extract the needed information
manually. (or)
ii) Ask a system programmer to write the necessary application program.
These both alternatives are unsatisfactory so the conclusion is that
conventional file-processing environment do not allow needed data to be retrieved
in a convenient and efficient manner. In this case, more responsive data-retrieval
systems are required.
Page | 4
Data isolation:
Writing new application programs to retrieve the appropriate data is
difficult, because data are scattered in various files.
Integrity problems:
The data values stored in the database must satisfy certain types of
consistency constraints.
For example, the balance of certain types of bank accounts may never fall
below a prescribed amount.
Developers enforce these constraints in the system by adding appropriate
code in the various application programs. When new constraints are added, it is
difficult to change the programs to enforce them.
Atomicity problems:
A computer system is subject to failure. In many applications, it is crucial
that, if a failure occurs, the data be restored to the consistent state that existed prior
to the failure.
For example, A program to transfer $50 from account A to account B. If a
system failure occurs during the execution of the program, it is possible that the $50
was removed from account A but was not credited to account B so resulting in a
consistent database state.
So the funds transfer must be atomic that is it must happen in its entirely or
not at all. It is difficult to ensure atomically in a conventional file-processing system.
Concurrent access anomalies:
Many systems allow multiple users to update the data simultaneously for
improving overall performance of the system and faster response.
Consider bank account A, containing $500. If two customers withdraw
funds $50 and $100 respectively from account A at the same time, the result of the
concurrent executions mat leave the account in an incorrect state.
If two programs run concurrently, they may both read the value $500 and
write back $450 and $400 respectively. Depending on which one writes the values
last, the account may contain either $450 or $400 instead of the correct value of $350.
Page | 5
To guard against this possibility, the system must maintain some form of
supervision, but supervision is difficult to provide because data may be accessed by
many different application programs.
Security problems:
Not every user of the database system should be able to access all the data.
For example, In banking system, payroll personal need to see only that part
of the database that has information about the various bank employees.
They do not need access to information about customer accounts, but
application programs are added to the file-processing system in an ad hoc manner,
enforcing such security constraints s difficult.
These difficulties are prompted the development of database systems.
The database system should be developed to solve the problems which
appeared in file-processing system.
1.4 DIFFERENCE BETWEEN DBMS AND FILE SYSTEM:
DBMS File system
A database is intended for easily A File System is a collection of raw data
organizing, storing and retrieving files stored in the hard-drive
large amounts of data.
A database, the inbuilt DBMS will In File System, most tasks such as
provide automated methods to storage, retrieval and search are done
complete these tasks manually and it is quite tedious
Redundancy is control on DBMS File system it can‘t control redundancy
These problems could be avoided Using a File System will lead to problems
by using a database. like data integrity, data inconsistency and
data security
Database minimum maintenance File system requires excessive program
required maintenance
Page | 6
1.5 VIEW OF DATA:
A database system is a collection of interrelated data and a set of programs
that allow users to access and modify these data.
A major purpose of a database system is to provide users with an abstract
view of the data. That is the system hides certain details of how the data are stored
and maintained.
1.5.1 DATA ABSTRACTION:
Database systems are made-up of complex data structures. To ease the user
interaction with database, the developers hide internal irrelevant details from users.
This process of hiding irrelevant details from user is called data abstraction.
The levels of abstraction are,
Physical level
Logical level
View level
Physical level:
The lowest level of abstraction describes how the data are stored and it
describes complex low level data structures in detail.
Logical level:
The next higher level of abstraction describes what data are stored in the
database and what relationship exists among those data.
The logical level describes the entire database in small number of relative
simple structures.
The user of the logical level does not need to be aware of this complexity.
Database administrator must decide what information to keep in the
database use the logical level of abstraction.
View level:
The highest level of abstraction describes only part of the entire database.
Many users of the database system do not need all the information instead
they need to access only a part of the database so view level of abstraction exists to
satisfy this need.
Page | 7
View level simplifies their interaction with the system. The system may
provide many views for the same database.
Relationship among the three levels of abstraction,
Page | 8
The overall design of the database is called the database schema. It changed
infrequently.
A database schema corresponds to the variable declarations in a program.
Each variable has a particular value at a given instant, the values of the
variables in a program at a point in time correspond to an instance of a database
schema.
Database systems have several schemas, partitioned according to the levels
of abstraction.
Physical schema describes the database at the physical level.
Logical schema describes the database design at the logical level.
A database may also have schemas at the view level, sometimes called
subschemas that describe different views of the database.
1.6 DATA MODELS:
The structure of a database is the data model.
Data Model is a collection of conceptual tools for describing data, data
relationships, data semantics and consistency constraints.
A data model provides a way to describe the design of a database at the
physical, logical and view level.
The data models can be classified in two categories.
1. Record based logical model
Relational model
Hierarchical model
Network model
2. Object based logical model
Entity Relational model [ER Model]
Object oriented model
The network data model and the hierarchical data model preceded the
relational data model. These models were tied closely to the implement and
complicated the task of modeling data.
Page | 9
1.6.1 Record Based Logical Model:
1.6.1.1 Relational Model:
The relational model uses a collection of tables to represent both data and
the
relationship among those data. Each table has multiple columns and each column
has a unique name.
The relational model is an example of a record-based model. The
database is structured in fixed format records of several types so the name is
Record-based models.
Each table contains records of a particular type. Each record type defines a
fixed number of fields or attributes.
The relational model is the most widely used data model and a vast
majority of current database systems are based on the relational model.
Page | 10
1.6.1.3 Network Model:
Network model consist of collection of records connected through links.
Each record is a collection of fields, each of field contains only one data
value.
Page | 11
1.7 DATABASE LANGUAGES:
Database languages are used to create, store, read, update and maintain the
database.
There are two widely used database languages,
Data Definition Language (DDL)
Data Manipulation Language (DML)
Database system provides data-definition language to specify the database
schema and a data-manipulation language to express database queries and updates.
1.7.1 Data-Definition Language:
We can specify a database schema by a set of definitions expressed by a
special language called a Data Definition Language (DDL).
The DDL is also used to specify additional properties of data. We specify
the storage structure and access methods used by the database system by a set of
statements in a special type of DDL called data storage and definition languages.
The data values are stored in the database must satisfy certain consistency
constraints.
For Example, The balance on an account should not fall below $100.
The DDL provides facilities to specify such constraints the database
systems check these constraints every time the database is updated. The
database system concentrates on integrity constraints that can be tested with
minimal overhead.
It is used to specify the structure of each table,
Domain constraints
Referential integrity
Assertions
Authorization
The types of authorization are,
Read authorization which allows reading but not modification of data.
Insert authorization which allows insertion of new data, but not
modification of existing data.
Update authorization which allows modification but not deletion of data.
Page | 12
Delete authorization which allows deletion of data.
DDL statements are compiled and resulting in a set of table stored in
special files called data dictionary.
The output of the DDL is placed in the data dictionary which contains
metadata. Metadata is a data about data.
DDL allows to Create, Alter, Drop, Truncate and Rename tables.
To create the database instance – CREATE
The Create table command defines each column of the table uniquely. Each
column has minimum of three attributes.
Name
Data type
Size (Column width)
Syntax:
CREATE TABLE TABLENAME (ATTRIBUTE1 DATATYPE (SIZE),
ATTRIBUTE2 DATATYPE (SIZE),
.
.
.
ATTRIBUTEn DATATYPE (SIZE));
Example:
CREATE TABLE Student (Reg_no varchar2(10), Name char(30), DOB date,
Address varchar2(50));
To drop database instances – DROP
It will destroy the table and all data which will be recorded in it.
Syntax
DROP TABLE <table_name>
Example:
DROP TABLE Student;
Page | 13
1.7.2 Data-Manipulation Language:
A Data Manipulation Language (DML) is a language that enables users to
access or manipulate data as organized by the appropriate data model.
The types of access are,
Retrieval of information stored in the database.
Insertion of new information into the database.
Deletion of information from the database.
Modification of information stored in the database.
These are basically two types,
Procedural DML
Declarative DML (or) non-procedural DML
A query is a statement requesting the retrieval of information. The portion
of a DML that involves information retrieval is called a query language. The most
widely used query language is SQL.
Example of an SQL Query:
Select account_number, balance
From account
Where balance=500
This query used retrieve the information which is stored in customer table.
Retrieving Data in the Table (Select Command):
Once data has been inserted into a table, the next most
logical operation would be to view what has been inserted. The SELECT SQL verb
is used to achieve this.
To select all Rows and all Columns
Syntax:
SELECT * FROM Table_name;
Example:
i) Select * from Student; It will show all the table records.
ii) SELECT First_name, DOB FROM STUDENT WHERE Reg_no = 'S101';
This Command will show one row.
Page | 14
INSERT Operation:
The INSERT query used to insert a new record into the table.
Example:
i) INSERT INTO student (reg_no, first_name, last_name, dob, address,
pincode) VALUES('A101', 'Pandiaraj', 'Murugesan', '13-NOV-87',
'Uthangarai', 635207);
ii) INSERT INTO student VALUES ('A101', 'Pandiaraj', 'Murugesan', '13-NOV-
87', 'Uthangarai', 635207);
Character expression placed within the insert into statement must be
enclosed in single quotes (').
DELETE Operation:
The DELETE command can remove all the rows from the table or a set of
rows from the table.
Example:
i) DELETE FROM student; It will delete all the rows from student table.
ii) DELETE FROM student WHERE reg_no='A101';
If condition will be satisfied then it will delete a row from the table Register
number A101 will be deleted from the table.
1.8 DATABASE USERS AND ADMINISTRATORS:
A primary goal of a database system is to retrieve information from and
store new information in the database.
1.8.1 Database Users and User Interfaces:
There are four different types of database system users, differentiated by
the way they expect to interact with the system.
Different types of user interfaces have been designed for the different types
of users.
Naive users
Application programmers
Sophisticated users
Specialized users
Page | 15
1.8.1.1 Naive users:
Naive users are unsophisticated users who interact with the system by
invoking one of the application programs that have been written previously.
For example, a bank teller who needs to transfer $50 from account A to
account B invokes a program called transfer. The typical user interface for naive
users is a forms interface, where the user fill in appropriate fields of the form.
Naive users may also simply read reports generated from the database.
1.8.1.2 Application Programmers:
Application programmers are computers professionals who write
application programs. Application programmers can choose many tools to develop
user interfaces.
Rapid application development (RAD) tools are tools that enable an
application programmer to constraint forms and report with minimal programming
effort.
1.8.1.3 Sophisticated users:
Sophisticated users interact with the system without writing programs.
Instead they form their requests in a database query language.
They submit each such query to a query processor, whose function is to
break down DML statements into instructions that the storage manager
understands.
Analysts who submit queries to explore data in the database fall in this
category.
1.8.1.4 Specialized users:
Specialized users are sophisticated users who write specialized database
application.
These applications are computer aided design systems, knowledgebase and
expert systems. Systems that store data with complex data types and environment
modeling systems.
Page | 16
1.8.2 Database administrator:
The main reasons for using DBMS are to have central control of both the
data and the programs that access those data.
A person who has such central control over the system is called a database
administrator (DBA).
1.8.2.1 Functions of a DBA:
Schema definition: The DBA creates the original database scheme by executing
a set of data definition statements is DDL.
Storage structure and access method definition.
Schema and physical organization modification: The DBA carries out changes
to the schema and physical organization to reflect changing needs of the
organization, or to alter the physical organization to improve performance.
Granting of authorization for data access: By granting different types of
authorization, the database administrator can regulate which parts of the
database various users can access. The authorization information is kept in a
special system structure that the database system consults whenever someone
attempts to access the data in the system.
Routine maintenance: DBA‘s routine maintenance activities are
Backing up the database,
Ensuring that enough disk space is available, and
Monitoring jobs running on database and ensuring the performance.
1.8.2.2 Types of DBA:
There are different kinds of DBA depending on the responsibility that he
owns.
Administrative DBA
Development DBA
Database Architect
Data Warehouse DBA
Application DBA
OLAP DBA
Page | 17
Administrative DBA:
This DBA is mainly concerned with installing, and maintaining DBMS
servers.
His primary tasks are installing, backups, recovery, security, replications,
memory management, configurations and tuning. He is mainly responsible for all
administrative tasks of a database.
Development DBA:
He is responsible for creating queries and procedure for the requirement.
Basically his task is similar to any database developer.
Database Architect:
Database architect is responsible for creating and maintaining the users,
roles, access rights, tables, views, constraints and indexes. He is mainly responsible
for designing the structure of the database depending on the requirement. These
structures will be used by developers and development DBA to code.
Data Warehouse DBA:
DBA should be able to maintain the data and procedures from various
sources in the data warehouse.
These sources can be files, COBOL, or any other programs. Here data and
programs will be from different sources. A good DBA should be able to keep the
performance and function levels from these sources at same pace to make the data
warehouse to work.
Application DBA:
He acts like a bridge between the application program and the database. He
makes sure all the application program is optimized to interact with the database.
He ensures all the activities from installing, upgrading, and patching,
maintaining, backup, recovery to executing the records works without any issues.
OLAP DBA:
He is responsible for installing and maintaining the database in OLAP
systems. He maintains only OLAP databases.
Page | 18
1.9 TRANSACTION MANAGEMENT:
A transaction is a collection of operations that performs a single logical
function in a database application.
Properties of transaction (ACID Properties)
Atomicity
Consistency
Isolation and
Durability
ACID Properties:
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.
There must be no state in a database where a transaction is left partially
completed. States should be defined either before the execution of the transaction or
after the execution/abortion/failure of the transaction.
Consistency:
The database must remain in a consistent state after any transaction. No
transaction should have any adverse effect on the data residing in the database.
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 updates a chunk of data in a database and commits, then the
database will hold the modified data. 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.
Page | 19
Isolation:
In a database system where more than one transaction are being executed
simultaneously and in parallel, 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.
Several operations on the database form a single logical unit of work.
An example is a funds transfer in which one account is debited and another
account is credited it is essential that either both the credit and debit occur or that
neither occur that is the funds transfer must happen in its entirely or not at all.
This all-or-none requirement is calls atomicity.
In addition it is essential that the execution of the funds transfer preserve
the consistency of the database that is the value at the Sum A+B must be preserved.
This correctness requirement called consistency.
After the successful execution of funds transfer the new values of accounts
A and B must persist despite the possibility of system failure this persistence
requirement is called durability.
A transaction is a collection of operations that performs a single logical
function in a database application.
Each transaction is a unit of both atomicity and consistency, thus we
require that transactions do not violate any database consistency constraints.
Ensuring the atomicity and durability properties is the responsibility of the
database system itself of the transaction management component. The database
system must perform failure recovery that is detect system failures and restore the
database to the existed prior to the occurrence of the failure.
Finally when several transactions update the database concurrently, the
consistency of data may no longer be preserved.
It is the responsibility of the concurrency control manager to control the
interaction among the concurrent transaction to ensure the consistency of the
database.
Page | 20
1.10 DATABASE ARCHITECTURE:
1.10.1 Database System Structure:
The architecture of a database system is influenced by the computer system
on which the database system runs.
Database system can be centralized or client-server where one server
machine executes work on behalf of multiple client machines.
Database systems can also be designed to exploit parallel computer
architecture. The issues include how to store data, how to ensure atomicity of
transactions that execute at multiple sites, how to perform concurrency control and
how to provide high availability in the presence of failure.
The functional components of database systems are divided into 3 parts.
They are,
Storage manager
Query processor
Database Users & Administrators
Storage manager:
A storage manager is a program module that provides the interface
between the low level data stored in the database and the application programs and
queries.
The storage manager is responsible for the interaction with the file
manager. The storage manager translates the various DML statements into low
level file system commands.
The storage manager components include.
1. Authorization and integrity manager, which tests for the satisfaction of
integrity constraints and checks the authority of users to access data.
2. Transaction manager, which ensures that the database remains in a consistent
state despite system failures.
3. File manager, which manager the allocation of space on disk storage and the
data structures used to represent information stored on disk.
4. Buffer manager, which is responsible for fetching data from disk storage into
main memory.
Page | 21
The storage manager implements several data structures.
1. Data File, which store the database itself.
2. Data dictionary, which stores metadata about the structure of the
database.
3. Indices, which can provide fast access to data items a database index
provides pointers to those data items that hold a particular value.
Query Processor:
The query processor components include,
DDL interpreter, which interprets DDL statements and records the
definitions in the data dictionary.
Page | 22
DML compiler, which translates DML statements in a query language into
low-level instructions. The DML compiler also performs query
optimization that is; it picks the lowest cost evaluation plan from among
the alternatives.
Query evaluation engine, which executes low-level instructions generated
by the DML compiler.
1.10.2 Application Architecture:
Two tier architecture
Three tier architecture
Database applications are usually partitioned into two or three parts.
Page | 23
CHAPTER - 2
2.1 DATA MODELS:
The structure of a database is the data model.
Data Model is a collection of conceptual tools for describing data, data
relationships, data semantics and consistency constraints.
A data model provides a way to describe the design of a database at the
physical, logical and view level.
There are two types of data models.
ER Model
Relational Model
2.2 ENTITY-RELATIONSHIP MODEL:
Entity relationship model [E-R model] is a way of graphically representing
the logical relationship of entities (or) object to create database.
The E-R model is very useful in mapping the meaning and interactions of
real world enterprises onto a conceptual schema.
The E-R data model employs three basic notations,
1) Entity sets
2) Relationship sets
3) Attributes.
The E-R data model consists of collection of basic objects called entities and
relationship among these objects.
Entity:
An entity can be a real world object that can be easily identified.
Page | 24
Example:
School databaseStudents, Teachers, Classes and Courses
Entity Sets:
An entity set is a collection of similar types of entities. An entity is a ―thing‖
or ―object‖ in the real world that is distinguishable from all other objects.
An entity set may contain entities with attributes sharing similar values.
Example:
Student set may contain all the students of a school.
Teacher set may contain all the teachers of a school.
A bank database consists of two entity sets, customer and loan.
Relationship:
The association among entities is called relationship.
Example:
Employee work at a department.
Student enrolls in a course.
Here, work at and enrolls are called relationship.
Relationship sets:
A set of relationships of similar type is called relationship set.
If E1, E2,....En are entity sets, then a relationship set R is a subset of
{ (e1,e2,.....,en) | e1 ϵ E1, e2 ϵ E2,....en ϵ En }
Where (e1, e2....en)is a relationship.
Attributes:
Entities are represented by means of their properties, called attributes. All
attributes have values.
Page | 25
Example:
A student entity may have,
A set of permitted values for each attribute is called the domain or value set
of that attributes.
Types of attributes:
Simple attribute
Composite attribute
Derived attribute
Single value attributes
Multi value attributes
Simple attribute:
Simple Attribute cannot be divided in to subparts.
Composite Attribute:
Composite Attribute can be divided in to subparts.
For example, an attribute name could be structured as a composite
attributes consisting of first_name, middle_name, last_name.
Page | 26
If the customer entity set also has an attribute date of birth we can calculate
age from date of birth thus, age is a derived attribute.
An attribute takes a null value when an entity does not have a value for it.
Single-valued and Multi-valued Attributes:
The attribute have a single value for a particular entity.
For example, the loan_number attribute for a specific loan entity refers to
only one loan number, such attribute are said to be Single valued.
An attribute has a set of values for a specific entity.
An employee entity set with the attribute phone_number. ie, an employee
may have zero, one or several phone numbers and different employees may have
different numbers of phones this types of attribute is said to be Multivalued.
2.3 CONSTRAINTS:
Constraints enforce limits to the data or type of data that can be
inserted/updated/deleted from a table.
The whole purpose of constraints is to maintain the data integrity during an
update/delete/insert into a table.
2.3.1 TYPES OF CONSTRAINTS:
NOT NULL
UNIQUE
DEFAULT
CHECK
Key Constraints – PRIMARY KEY, FOREIGN KEY
Domain constraints
Mapping constraints
NOT NULL:
NOT NULL constraint makes sure that a column does not hold NULL
value.
When we don‘t provide value for a particular column while inserting a
record into a table, it takes NULL value by default.
By specifying NULL constraint, we can be sure that a particular column(s)
cannot have NULL values.
Page | 27
Example:
CREATE TABLE STUDENT(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (235),
PRIMARY KEY (ROLL_NO)
);
UNIQUE:
UNIQUE Constraint enforces a column or set of columns to have unique
values.
If a column has a unique constraint, it means that particular column cannot
have duplicate values in a table.
Example:
CREATE TABLE STUDENT(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
DEFAULT:
The DEFAULT constraint provides a default value to a column when there
is no value provided while inserting a record into a table.
Example:
CREATE TABLE STUDENT(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO)
);
Page | 28
CHECK:
This constraint is used for specifying range of values for a particular
column of a table. When this constraint is being set on a column, it ensures that the
specified column must have the value falling in the specified range.
Example:
CREATE TABLE STUDENT(
ROLL_NO INT NOT NULL CHECK(ROLL_NO >1000) ,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO)
);
In the above example we have set the check constraint on ROLL_NO
column of STUDENT table. Now, the ROLL_NO field must have the value greater
than 1000.
Key Constraints:
PRIMARY KEY:
Primary key uniquely identifies each record in a table. It must have unique
values and cannot contain nulls.
In the below example the ROLL_NO field is marked as primary key, that
means the ROLL_NO field cannot have duplicate and null values.
Example:
CREATE TABLE STUDENT(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
Page | 29
FOREIGN KEY:
Foreign keys are the columns of a table that points to the primary key of
another table. They act as a cross-reference between tables.
Domain Constraints:
Each table has certain set of columns and each column allows a same type
of data, based on its data type. The column does not accept values of any other data
type.
Domain constraints are user defined data type and we can define them like
this:
Domain Constraint = data type + Constraints (NOT NULL / UNIQUE /
PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)
Mapping Constraints:
Mapping constraints can be explained in terms of mapping cardinality.
Mapping cardinalities or cardinality ratios express the number of entities to
which another entity can be associated via a relationship set. Mapping cardinalities
are most useful in describing binary relationship sets.
For a binary relationship set R between entity sets A and B, the binary
cardinality must be one of the following.
One to one relation
One to many relation
Many to one relation
Many to many relation
One-to-one:
An entity in A is associated with at most one entity in B and an entity in B is
associated with at most one entity in A.
Page | 30
One-to-many:
An entity in A is associated with any number of entities in B. An entity in B
can be associated with at most one entity in A.
Many-to-one:
An entity in A is associated with at most one entity in B. An entity in B can
be associated with any number of entities in A.
Many-to-many:
An entity in A associated with at most one entity in B. An entity in B can be
associated with any number of entities in A.
Page | 31
Super key:
A super key is a set of one or more attributes that allow us to identity
uniquely a tuple in the relation.
For example, the customer_id attribute of the relation customer is sufficient
to distinguish one customer tuple from another. Thus, customer_id is a super key.
Candidate key:
The concept of super key is not sufficient because a super key may contain
extraneous attributes. If K is a super key, then so is any superset of K, which no
proper subset is a super key, such minimal super keys are called Candidate keys.
Primary key:
Primary key to denote a candidate key that is chosen by the database
designer to identifying entities within an entity set.
Foreign key:
A relation schema, say r1 may include among its attributes the primary key
of another relation schema say r2. This attribute is called a foreign key from r1
referencing r2.
The relation r1 is also called the referencing relation of the foreign key
dependency and r2 is called the referenced relation of the foreign key.
A database schema, along with primary key and foreign key dependencies
can be depicted pictorially by schema diagrams.
Each relation appears as a box, with the attributes listed inside it and the
relation name above it.
Page | 32
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.
2.5 ENTITY RELATIONSHIP DIAGRAMS:
E – R diagram can express the overall logical structure of a database
graphically.
E-R diagram consist of the following major components,
Entity:
Rectangle box are used to represent entity and name of the entity is written
inside the rectangle.
Weak entity:
Weak entity is represented by double rectangles.
CLASS
Attributes:
An ellipse shape is used to represent the attribute and name of the attribute
is written inside the oval shape.
REG. NO
Multivalue Attributes:
Multivalue attributes are represented by double ellipses.
PHONE
Derived Attributes:
Derived attributes are represented by dashed ellipse.
AGE
Page | 33
Primary key:
Primary key attribute is represented by the attribute name with underline.
STUDENT ID
Relationship:
A diamond shape is used to represent the relationship between the entities.
Relationship name will be written inside the shape.
The Lines are used to link attributes to entity sets and entity sets to
relationship sets.
Weak relationship:
Weak relationship is represented by double diamond shape.
Relationship types:
One to one relation:
Page | 34
Example:
Example:
payment-number – discriminator of the payment entity set
Primary key for payment – (loan-number, payment-number).
Page | 35
Note: the primary key of the strong entity set is not explicitly stored with
the weak entity set, since it is implicit in the identifying relationship.
If loan-number were explicitly stored, payment could be made a strong
entity, but then the relationship between payment and loan would be
duplicated by an implicit relationship defined by the attribute loan-number
common to payment and loan.
Example:
Page | 36
Customer
Employee
Each of these person types is described by a set of attributes that includes
all the attributes of entity set person plus possibly additional attributes.
For example, customer entities may be described further by the
attribute customer-id, whereas employee entities may be described further by the
attributes employee-id and salary.
The process of designating sub groupings within an entity set is
called specialization. The specialization of person allows us to distinguish among
persons according to whether they are employees or customers.
As another example, suppose the bank wishes to divide accounts into two
categories, checking account and savings account.
Savings accounts need a minimum balance, but the bank may set interest
rates differently for different customers, offering better rates to favored customers.
Checking accounts have a fixed interest rate, but offer an overdraft facility;
the overdraft amount on a checking account must be recorded.
The bank could then create two specializations of account, namely savings-
account and checking-account.
The account entities are described by the attributes account-
number and balance.
The entity set savings-account would have all the attributes of account and
an additional attribute interest-rate.
The entity set checking account would have all the attributes of account,
and an additional attribute overdraft amount. We can apply specialization
repeatedly to refine a design scheme.
For instance, bank employees may be further classified as one of the
following:
Officer
Teller
Secretary
Page | 37
Each of these employee types is described by a set of attributes that
includes all the attributes of entity set employee plus additional attributes.
For example, officer entities may be described further by the
attribute office-number, teller entities by the attributes station-number and hours-
per-week, and secretary entities by the attribute hours-per- week.
Further, secretary entities may participate in a relationship secretary-for,
which identifies which employees are assisted by a secretary.
An entity set may be specialized by more than one distinguishing feature.
In our example, the distinguishing feature among employee entities is the job the
employee performs.
Another, coexistent, specialization could be based on whether the person is
a temporary (limited-term) employee or a permanent employee, resulting in the
entity sets temporary-employee and permanent-employee.
When more than one specialization is formed on an entity set, a particular
entity may belong to multiple specializations.
For example, a given employee may be a temporary employee who is a
secretary.
In terms of an E-R diagram, specialization is depicted by
a triangle component labeled ISA. The label ISA stands for ―is a‖ and represents, for
example, that a customer ―is a‖ person. The ISA relationship may also be referred to
as a super class-subclass relationship. Higher- and lower-level entity sets are
depicted as regular entity sets — that are, as rectangles containing the name of the
entity set.
Page | 38
2.7.2 Generalization:
The refinement from an initial entity set into successive levels of entity sub
groupings represents a top-down design process in which distinctions are made
explicit.
The design process may also proceed in a bottom-up manner, in which
multiple entity sets are synthesized into a higher-level entity set on the basis of
common features.
The database designer may have first identified a customer entity set with
the attributes name, street, city, and customer-id, and an employee entity set with
the attributes name, street, city, employee-id, and salary.
There are similarities between the customer entity set and
the employee entity set in the sense that they have several attributes in common.
This commonality can be expressed by generalization, which is a
containment relationship that exists between a higher-level entity set and one or
more lower-level entity sets.
For example, person is the higher-level entity set
and customer and employee are lower-level entity sets.
Higher-level and lower-level entity sets also may be designated by the
terms superclass and subclass, respectively. The person entity set is the superclass
of the customer and employee subclasses.
For all practical purposes, generalization is a simple inversion of
specialization. We will apply both processes, in combination, in the course of
designing the E-R schema for an enterprise.
In terms of the E-R diagram itself, we do not distinguish between
specialization and generalization.
New levels of entity representation will be distinguished (specialization) or
synthesized (generalization) as the design schema comes to express fully the
database application and the user requirements of the database.
Specialization stems from a single entity set; it emphasizes differences
among entities within the set by creating distinct lower-level entity sets.
Page | 39
These lower-level entity sets may have attributes, or may participate in
relationships, that do not apply to all the entities in the higher-level entity set.
If customer and employee neither have attributes that person entities do not
have nor participate in different relationships than those in which person entities
participate, there would be no need to specialize the person entity set.
Generalization proceeds from the recognition that a number of entity sets
share some common features (namely, they are described by the same attributes
and participate in the same relationship sets).
Page | 40
Thus, customer is described by its name, street, and city attributes, and
additionally a customer-id attribute; employee is described by its name, street,
and city attributes, and additionally employee-id and salary attributes.
A lower-level entity set (or subclass) also inherits participation in the
relationship sets in which its higher-level entity (or superclass) participates.
The officer, teller, and secretary entity sets can participate in the works-
for relationship set, since the superclass employee participates in the works-
for relationship.
Attribute inheritance applies through all tiers of lower-level entity sets. The
above entity sets can participate in any relationships in which the person entity set
participates.
Whether a given portion of an E-R model was arrived at by specialization
or generalization, the outcome is basically the same:
A higher-level entity set with attributes and relationships that apply to all
of its lower-level entity sets
Lower-level entity sets with distinctive features that apply only within a
particular lower-level entity set
2.8 DESIGN OF ER DATABASE SCHEMA:
2.8.1 Decisions:
Some of the schema decisions that must be made by the database designer
include:
o Whether to use an attribute or an entity set to represent an object
o Whether a real-world concept is most accurately expressed as an entity set or
relationship set
o Whether to use a ternary relationship or a pair of binary relationships
o Whether to use a strong or a weak entity set
o Whether using aggregation is appropriate
o Whether using generalization is appropriate
Page | 41
2.8.2 Design phases:
Different authors will have different names for the different phases,
however all of them are essentially describing the Software Development Life
Cycle:
Requirements specification
Analysis
Design
Implementation
Test
Documentation
Modification and Enhancement
Requirements specification:
There can be a series of specifications that can use defined:
User requirements
Functional requirements
Software requirements
System/Subsystem requirements
Analysis:
The result of analysis should be the input and output of the systems, as well
as the E-R Diagram with the entity and relationship sets.
Design:
The results of design should be the definition of all the database
components and the logical description of the processing that the system will
provide.
Implementation:
Here is where the tables are actually created, and the databases are
populated with data. The software is developed. This will be the shortest phase if
all of the other phases are correctly executed.
Test:
Testing is necessary to provide the development team built the correct
system and built the system correctly.
Page | 42
Documentation:
Actually, the documentation should been included in other phases and
finalized during this phase.
Modification and Enhancement:
This is the most expensive time to correct bugs! This is also the phase where
the system evolves to meet new and unforeseen requirements.
Page | 43
CHAPETER - 3
RELATIONAL MODEL
3.1 STRUCTURE OF RELATIONAL DATABASE:
A relational database consists of a collection of tables, each of which is
assigned a unique name. A row in a table represents a relationship among a set of
values. A table is an entity set and a row is an entity basic structure.
Consider the account table.
Page | 44
3.2 DATABASE SCHEMA:
Database Schema is the logical design of the database. Database instance is
a snapshot of the data in the database at a given instant in time.
The concept of a relation corresponds to the programming-language notion
of a variable.
The concept of relation schema corresponds to the programming-language
notion of type definition.
It is convenient to give a name to a relation schema, the convention of using
lower case names for relations and names beginning with an upper case letter for
relation schemas.
We use Account_Schema to denote the relation schema for relation account,
thus
Account_Schema = (accout_number,branch_name,balance).
We denote the fact that account is a relation on Account_Schema by
account (Account_Schema).
In general, a relation schema consists of a list of attributes and their
corresponding domains. The concept of a relation instance corresponds to the
programming-language notion of a value of variable.
The value of a given variable may change with time. Similarly the contents
of a relation instance may change with time as the relation is updated.
An example of a relation instance, consider the branch relation, the schema
for that relation is
Branch_Schema = (branch_name, branch_city, assets).
The attributes branch_name appears in both Branch_Schema and
Account_Schema, using common attributes in relation schemas is one way of
relating tuples of distinct relations.
3.3 KEYS:
The values of the attribute values of a tuple must be unique. In other words,
no two tuples in a relation are allowed to have exactly the same value for all
attributes.
Page | 45
Super key:
A super key is a set of one or more attributes that allow us to identity
uniquely a tuple in the relation.
For example, the customer_id attribute of the relation customer is sufficient
to distinguish one customer tuple from another. Thus, customer_id is a super key.
Candidate key:
The concept of super key is not sufficient because a super key may contain
extraneous attributes.
If K is a super key, then so is any superset of K, which no proper subset is a
super key, such minimal super keys are called Candidate keys.
Primary key:
Primary key to denote a candidate key that is chosen by the database
designer to identifying entities within an entity set.
Foreign key:
A relation schema, say r1 may include among its attributes the primary key
of another relation schema say r2. This attribute is called a foreign key from r1
referencing r2.
The relation r1 is also called the referencing relation of the foreign key
dependency and r2 is called the referenced relation of the foreign key.
A database schema, along with primary key and foreign key dependencies
can be depicted pictorially by schema diagrams.
Page | 46
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.
3.4 QUERY LANGUAGES:
A query language is a language in which a user requests information from
the database. These languages are usually on a level higher than that of a started
programming language.
Query language can be categorized as either,
Procedural
Non-Procedural
In a procedural language, the user instructs the system to perform a
sequence of operations on the database to compute the desired result.
In a non-procedural language, the user describes the desired information
without giving a specific procedure for obtaining that information.
The relational algebra is procedural, whereas the tuple relation calculus and
domain relational calculus are non-procedural.
3.5 RELATIONAL-ALGEBRA:
The relational algebra is a procedural query language. It consists of a set of
operations that take one or two relations as input and produce a new relation as
their result.
3.5.1 FUNDAMENTAL OPERATIONS:
The fundamental operations in the relational algebra are,
Select
Project
Union
Set-Difference
Cartesian-Product
Rename
Join operations
Page | 47
The select, project and rename operations are called unary operations,
because they operate on one relation.
The other three operations operate on pair of relation are called binary
operations.
The Select operation:
The Select operation selects tuples that satisfy a given predicate. We use the
lowercase Greek letter sigma (σ) to denote selection.
Thus, to select those tuple of the loan relation where the branch is
―perryridge‖, we write,
We can find all tuples, in which the amount is more than 1200,
Page | 48
The Project Operation:
The project operation allows us to produce this relation. The project
operation is a unary operation that returns its argument relation, with certain
attributes left out, because a relation is a set, any duplicate rows are eliminated.
Projection is denoted by the uppercase pi( Π ). We list those attributes that
we wish to appear in the result as a subscript to Π.
Next, write a query to find the names of all customers with an account in
the bank,
Then, we need the union of these two sets, that is, we need all customer
name that appear in either or both of the two relations.
Page | 49
We find these data by the binary operation Union denoted by ―U‖.
Page | 50
The Cartesian-Product Operation:
The Cartesian-Product operation, denoted by a cross(X), allows us to
combine information from any two relations.
We write the Cartesian product of relations r1 and r2 as r1 X r2.
The same attribute name may appear in both r1 and r2, we need to devise a
naming schema to distinguish between these attributes.
For example,
The relation schema for r = borrower X loan is (borrower.customer_name,
borrower.loan_number, loan.loan_number, loan.branch_name, loan.amount)
If we want to find the names of all customer who have a loan at the
―Perryridge‖ branch. If we write,
Page | 51
The results of relational algebra expressions do not have a name that we
can use to refers to them. It is useful to be able to give them names.
The rename operator, denoted by the lowercase Greek letter rho (ρ).
Given a relational algebra E, the expression, ρx (E) returns the result of
expression E under the name x.
A relation r by itself is a relational-algebra expression, thus we can also
apply the rename operation to a relation r to get the same relation under a new
name.
A second form of the rename operation is as follows, Assume that a
relational algebra expression E has arity E. Then, the expression
Returns the result of expression E under the name x, and with the attributes
renamed to A1, A2,….., An.
The temporary relation that consists of the balances that are not the largest
can be written as,
This expression gives those balances in the account relation for which a
larger balance appears somewhere in the account relation (renamed as d). The
result contains all balances except the largest one.
Formal definition of the relational algebra:
A basic expression in the relational algebra consists of either one of the
following.
A relation in the database.
A constant relation
A constant relation is written by listing its tuples within { }.
For example, {(A-101, Downtown, 500)(A-215, Mianus, 700)}.
Relational-algebra expressions:
E1 U E 2
E1 – E2
E1 x E2
Page | 52
σp (E1), where p is a predicate on attribute in E1.
Πs (E1), where s is a list consisting of some of the attributes
in E1.
ρ x (E1), where x is the new name for the result of E1.
Join operation:
The Join operation, which combines two relations to form a new relation, is
one of the essential operations in the relational algebra.
There are various types of Join operation,
Theta Join
Equijoin (a particular type of Theta join)
Natural Join
Outer Join
Semi Join
Natural-Join Operation:
The natural join is a binary operation that allows us to combine certain
selections and a Cartesian product into one operation.
Page | 53
Outer Join:
The outer join operation is an extension of the join operation to deal with
missing information.
In the result, we have lost the street and city information about smith,
because the tuple describing smith is absent from ft_works relation.
Similarly, we have lost the branch name and salary information about
Gates.
We can use the outer_join operation to avoid this loss of information.
There are actually three forms of the operation
Left outer join denoted
Right outer join denoted
Full outer join denoted
Page | 54
Left Outer join:
The left outer join takes all tuples in the left relation that did not
match with any tuple in the right relation, pads the tuples with null values for all
other attributes from the right relation and adds them to the result of natural join.
3.6 VIEWS:
It is not desirable for all users to see the entire logical model. Security
considerations may require that certain data be hidden from users.
Page | 55
Any relation that is not part of the logical model, but is made visible to s
user as a virtual relation is called a view.
3.6.1 VIEW DEFINITION:
We define a view in SQL by using the create view command. To define a
view, we must give the view a name. The form of the create view command is,
Create view ν as <query expression>
where <query expression> is any legal query expression. The view name is
represented by ν.
Example:
Create view all_customer as
(select branch_name, customer_name,
from depositor, account
where
depositor.account_number=account.account_number)
union
(select branch_name, customer_name,
from borrower, loan
where borrower.loan_number=loan.loan_number).
Once we have defined a view, we can use the view name to refer to the
virtual relation that the view generates.
Using the view all_customer, we can find all customers of the perryridge
branch by writing,
select customer_name
from all_customer where branch_name=‘perryridge‘
View names may appear in any place where a relation name may appear.
Certain database systems allow view relations to be stored, but they make
sure that, if the actual relations used in the view definition change, such views are
called materialized view. The process of keeping the view up to date is called view
maintenance.
Page | 56
3.6.2 VIEWS DEFINED BY USING OTHER VIEWS:
One view may be used in the expression defining another view.
Example:
We can define the view perryridge_customer as follows,
create view perryridge_customer as
select customer_name
from all_customer where branch_name=‘perryridge‘
where all_customer is itself a view relation.
View expansion is one way to define the meaning of views defined in terms
of other views.
View expansion of an expression repeats the replacement step as follows:
repeat
find any view relation νi in e1
replace the view relation νi by the expression defining νi
until no more view relations are present in e1.
Page | 57
CHAPTER - 4
4.1 SQL (Structured Query Language):
4.1.1 BACKGROUND:
IBM developed the original version of SQL, called sequel, as part of the
system R project in the early 1970s. The sequel language has evolved and its name
has changed to SQL (Structured Query Language).
SQL has established itself as the standard relational database language.
In 1986, the American National Standards Institute (ANSI) and
International Organization for Standardization (ISO) published an SQL standard
called SQL-86.
ANSI published an extended standard for SQL, SQL-89 in 1989, the next
version of standard was SQL-92. The most recent version is SQ2003.
The SQL language has several parts:
Data-Definition language(DDL):
The SQL DDL provides commands for defining relational schemas,
deleting relations and modifying relation schemas.
Interactive data-manipulation language(DML):
The SQL DML includes a query language based on both the relational
algebra and tuple relational calculus. It also includes commands to insert tuples
into, delete tuples from and modify tuples in the database.
Integrity:
The SQL DDL includes commands for specifying integrity constraints
that the data stored in the database must satisfy.
View definition:
The SQL DDL includes commands for defining views.
Transaction control:
SQL includes commands for specifying the beginning and ending of
transaction.
Embedded SQL and dynamic SQL:
Embedded and dynamic SQL define how SQL statements can be
embedded within general purpose programming languages, such as C, C++,
JAVA, COBOL and PASCAL.
Page | 58
Authorization:
The SQL DDL includes commands for specifying access rights to relations and
views.
4.2 BASIC STRUCTURE OF SQL QUERIES:
A relational database consists of a collection of relation each of which is
assigned a unique name, each relation has a structure.
The basic structure of an SQL expression consists of three clauses.
Select
From
Where
Select:
The select clause corresponds to the projection operation of the relational
algebra. It is used to list the attributes desired in the result of a query.
From:
The from clause corresponds to the Cartesian-product operation of the
relational algebra. It lists the relations to be scanned in the evaluation of the
expression.
Where:
The where clause corresponds to the selection predicate of the relational
algebra. It consists of a predicate involving attributes of the relations that appear in
the from clause.
Example:
A typical SQL query has the form:
Select A1, A2, …. ,An from r1, r2, …. ,rm Where p
Here, A1, A2, …. ,An are Attributes, r1, r2, …. ,rm are Relations and P is
Predicate.
The query is equivalent to the relational algebra is
ПA1, A2, …. ,An(σp(r1x r2x …. xrm)).
SELECT CLAUSE:
A simple query to find the names of all branches in the loan relation,
i) Select branchname from loan
Page | 59
SQL allows duplicates in relations, in those cases, we need to eliminate the
duplicates, we can use the keyboard distinct after select.
Example:
ii) Select distinct branchname from loan
Page | 60
Example:
Select customer_name, borrower.loan_number, amount from borrower,
loan
where borrower.loan_number = loan.loan_number.
4.2.1 RENAME OPERATION:
SQL provides a mechanism for renaming both relations and attributes. It
uses the as clause.
old-name as new-name
The as clause can appear in both the select and from clauses.
For example, if we want the attribute name loan_number to be replaced
with the name loan_id,
Select customer_name, borrower.loan_number as loan_id, amount
from borrower, loan where borrower.loan_number =
loan.loan_number
4.2.2 TUPLE VARIABLES:
The as clause is particularly useful in defining the notion of tuple variables.
Tuple variables are defined in the from clause by way of the as clause.
To write a query for all customers who have a loan from the bank, find their
names, loan numbers, and loan amount as
Select customer_name, T.loan_number, S.amount
from borrower as T, loan as S
where T.loan_number = S.loan_number
Tuple variables are most useful for comparing two tuples in the same
relation.
Example:
Select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city= “Brooklyn”
4.2.3 STRING OPERATIONS:
SQL specifies strings by enclosing them in single quotes.
For example, „Perryridge‟.
Page | 61
The most commonly used operation on strings in pattern matching using
the operator “like”.
We describe patterns by using two special characters:
Percent (%): The % character matches any substring.
Underscore ( _ ): The _ character matches any character.
Patterns are case sensitive, that is uppercase characters do not match
lowercase characters.
‗Perry%‘ matches any string beginning with ―Perry‖.
‗%idge%‘ matches any string containing ―idge‖ as a substring.
For ex: ‗Perryridge‘, ‗Rock Ridge‘, ‗Mianus Bridge‘,
‗- - - ‗matches any string of exactly three characters.
‗- - -%‘ matches any string of at least three characters.
SQL expresses patterns by using the like comparison operator.
Find the names of all customers whose street address includes the substring
„Main‟.
This query can be written as,
Select customer-name from customer where customer-street like
„%Main%‟
We define the escape character for a like comparison using the escape
keyword, which use backslash (\) as the escape character.
like ‗ab\%cd%‘ escape ‗\‘ matches all strings beginning with ―ab%cd‖
like ‗ab\\cd%‘ escape ‗\‘ matches all strings beginning with ―ab\cd‖
SQL allows to search for mismatches instead of matches by using the not
like comparison operator.
Ordering the display of Tuples:
SQL offers the user some control over the order in which tuples in a relation
are displayed.
The order by clause cause the tuples in the result of a query to appear in
sorted order.
To list in alphabetic order all customers who have a loan at the perryridge
branch,
Page | 62
select distinct customer_name from borrower, loan
where borrower.loan-number = loan.loan_number and
branch_name=‟Perryridge‟
order by customer-name.
By default, the order by clause list items in ascending order. To specify the
sort order, we may specify desc for descending order or asc for ascending order.
Ex:
Select * from loan
Order by amount desc, loan_number asc.
Duplicates:
SQL formally defines not only what tuples are in the result of a query, but
also how many copies of each of those tuples appear in the result.
We can define the duplicate semantics of an SQL query using multiset
versions of the relational operator.
Given multiset relations r1and r2,
1. If there are c1 copies of tuple t1 in r1 and t1 satisfies selection σө, then there
are c1 copies of t1 in σө (r1).
2. For each copy of tuple t1 in r1, there is a copy of tuple πA (t1) in πA (r1).
3. I f there are c1 copies of tuple t1 in r1 and c2 copies of tuple t2 in r2 there are
c1 * c2 copies of tuple t1.t2 in r1Xr2,
4.3 SET OPERATIONS:
Set operations are used to join the result of two (or) more SELECT
statements. The set operations are,
1. Union Operation (∪)
2. Intersect Operation (∩)
3. Except Operation (-).
Example:
Page | 63
4.3.1 Union Operation:
Union operation is used to combine the result of two (or) more SELECT
statement. It removes duplicate rows from its result set.
Example:
Select * from first
Union
Select * from second
Result:
If you want to retain all duplicates, we must write union all in place of
union,
Example:
Select * from first
Union all
Select * from second
Result:
Page | 64
Result:
Page | 65
4.4 AGGREGATE FUNCTIONS:
Aggregate functions are functions that take a collection of values as in input
and return a single value.
SQL offers five built-in aggregate functions,
Average: AVG
Minimum: MIN
Maximum: MAX
Total: SUM
Count: COUNT
Example:
Page | 66
4.4.3 Maximum: MAX
The max function returns the minimum value from the selected column of
the table.
Syntax:
Select max (Column name) from table name;
Example:
Select max (SALARY) from Employee;
Page | 67
4.5 NULL VALUES:
SQL allows the use of the null values to indicate absence of information
about the values of an attribute.
We can use the special keyword null in a predicate to test for a null value.
Thus to find all loan numbers that appear in the loan relation with the null
values for amount, we write,
Select loan_number from loan where amount is null
The predicate is not null tests for the absence of a null value.
The result of an arithmetic expression (involving +, -, * or /) is null if any of
the input values is null. SQL treats as unknown the result of any comparison
involving a null value (other than is null and is not null).
Since the predicate in a where clause can involve Boolean operations such
as and, or and not on the results of comparisons, the definitions of the Boolean
operations are extended to deal with the value unknown.
And: The result of true and unknown is unknown, false and unknown is
false, while unknown and unknown is unknown
Or: The result of true or unknown is true, false or unknown is unknown,
while unknown or unknown is unknown
Not: The result of not unknown is unknown
SQL defines the result of an SQL statement of the form
Select …. From R1,…Rn where P
to contain (projection of) tuples in R1 x …. X Rn for which predicate P evaluates to
true.
If the predicate evaluates to either false or unknown for tuple in R 1 x ….. x
Rn (the projection of) the tuple is not added to the result.
4.6 NESTED SUBQUERIES:
SQL provides a mechanism for nesting subqueries; a subquery is a select –
from – where that is nested within another query.
A common use of subqueries is to perform tests for set membership, make
set comparison, and determine set cardinality.
Page | 68
4.6.1 SET MEMBERSHIP:
SQL allows testing tuples for membership in a relation.
The in connective tests for set membership, where the set is collection of
values produced by a select clause. The not in connective test for the absence of set
membership.
Example:
We need to find those customers who are the borrower from the bank and
who appear in the list of account holders obtained in the subquery.
Nesting the subquery in an outer select
Select distinct customer_name from borrower where customer_name
in
( Select customer_name from depositor).
We can write a query to find all customers who have both an account and a
loan at the Perryridge branch,
Select distinct customer_name from borrower, loan
where borrower.loan_number = loan.loan_number and
branch_name=‘Perryridge‘ and (branch_name, customer_name) in
(Select branch_name, customer_name
from depositor, account
where depositor.account_number = account.account_number)
We use the not in construct to find all customers who do have a loan at the
bank an account at the bank
Select distinct customer_name from borrower
where customer_name not in
(select customer_name from depositor)
4.6.2 SET COMPARISON:
The ability of a nested subquery to compare sets. Find the names of all
branches that have assets greater than those of at least one branch located in
Brooklyn,
Select distinct T.branch_name
from branch as T, branch as S
Page | 69
where T.assets > S.assets and S.branch_city = ‘Brooklyn‘
The phrase ―greater than at least one‖ is represented in SQL by > some.
Select branch_name
from branch
where assets > some (select assets from branch where branch_city =
‗Brooklyn‘)
4.6.3 TEST FOR EMPTY RELATIONS:
SQL includes a feature for testing whether a subquery has any tuples in its
result.
The exists construct returns the value true if the argument subquery is non-
empty.
Using the exists construct, we can write the query
―Find all customers who have both an account and loan at the bank‖.
Select customer-name from borrower where exists
(Select * from depositor where depositor.customer_name =
borrower.customer_name)
4.7 VIEWS:
It is not desirable for all users to see the entire logical model. Security
considerations may require that certain data be hidden from users.
Any relation that is not part of the logical model, but is made visible to s
user as a virtual relation is called a view.
4.7.1 VIEW DEFINITION:
We define a view in SQL by using the create view command. To define a
view, we must give the view a name. The form of the create view command is,
Create view ν as <query expression>
where <query expression> is any legal query expression. The view name is
represented by ν.
Example:
Create view all_customer as
(select branch_name, customer_name,
Page | 70
from depositor, account
where
depositor.account_number=account.account_number)
union
(select branch_name, customer_name,
from borrower, loan
where borrower.loan_number=loan.loan_number).
Once we have defined a view, we can use the view name to refer to the
virtual relation that the view generates.
Using the view all_customer, we can find all customers of the perryridge
branch by writing,
select customer_name
from all_customer
where branch_name=‘perryridge‘.
View names may appear in any place where a relation name may appear.
Certain database systems allow view relations to be stored, but they make
sure that, if the actual relations used in the view definition change, such views are
called materialized view.
The process of keeping the view up to date is called view maintenance.
4.7.2 VIEWS DEFINED BY USING OTHER VIEWS:
One view may be used in the expression defining another view.
Example:
We can define the view perryridge_customer as follows,
create view perryridge_customer as
select customer_name
from all_customer
where branch_name=‘perryridge‘
where all_customer is itself a view relation.
View expansion is one way to define the meaning of views defined in terms
of other views.
View expansion of an expression repeats the replacement step as follows:
Page | 71
repeat
find any view relation νi in e1
replace the view relation νi by the expression defining νi
until no more view relations are present in e1.
4.8 MODIFICATION OF THE DATABASE:
4.8.1 DELETION:
We can delete only whole tuples, we cannot delete values on only
particular attributes.
SQL expresses a deletion by,
delete from r
where p
where p represents a predicate where represents a relation.
A delete command operates only one relation. If we want to delete tuples
from several relations, we must use one delete command for each relation.
delete from loan
deletes all tuples from the loan relation.
Delete all account tuples in the Perryridge branch.
delete from account
where branch_name = ‗Perryridge‘
Delete all loans with loan amounts between $1300 and $1500.
delete from loan
where amount between 1300 and 1500
Delete all account tuples at every branch located in ‗Brooklyn‘.
delete from account
where branch_name in (select branch_name
from branch
where branch_city = ‗Brooklyn‘).
4.8.2 INSERTION:
To insert data into a relation, we either specify a tuple to be inserted or
write a query whose result is a set of tuples to be inserted.
The simplest insert statement is a request to insert one tuple,
Page | 72
insert into account
values (‗A-973‘, ‗Perryridge‘, 1200).
SQL allows the attributes to be specified as part of the insert statement,
Insert into account (account_number, branch_name, balance)
values (‗A-973‘, ‗Perryridge‘, 1200).
4.8.3 UPDATES:
The update statement can be used to change a value in a tuple without
changing all values in the tuple.
Example1:
update account
set balance = balance * 1.05
Example2:
update account
set balance = balance * 1.05
where balance >=100
4.8.4 TRANSACTIONS:
A transaction consists of sequence of query and update statements.
One of the following SQL statements must end the transaction.
Commit work :
It commits the current transaction that is, it makes the updates
performed by the transaction become permanent in the database.
After the transaction is committed, a new transaction is automatically
started.
Rollback work :
Rollback work causes the current transaction to be rolled back, that is it un
does all the updates performed by the SQL statements in the transaction.
Thus, the database state is restored.
Transaction rollback is useful if some error condition is detected during
execution of a database.
Commit is to saving changes to a document that is being edited.
Page | 73
4.9 DATA DEFINITION LANGUAGE:
The set of relations in a database must be specified to the system by means
of a data definition language (DDL).
The SQL DDL allows specification of not only a set of relation but also
information about each relation, including
The schema for each relation
The domain of values associated with each attributes
The integrity constraints
The set of indices to be maintained for each relation
The security and authorization information for each relation
The physical storage structure of each relation.
4.9.1 Basic Domain Types:
The SQL standard supports a variety of built-in domain types, including:
Char(n): A fixed length character string with user-specified length n. The full
form character can also be used.
Varchar(n): A variable length character string with user-specified maximum
length n. The full form is character varying.
int: An integer (a finite subset of the integers that is machine dependent).
smallint: A small integer (a machine dependent subset of the integer domain
type).
numeric(p, d): A fixed point number with user specified precision. The
number consists of p digits (plus a sign), and d of the p digits are to the right
of the decimal point. For example, numeric(3, 1) allows 44.1 to be stored.
real, double precision: Floating point and double precision floating point
numbers with machine dependent precision.
float(n): A floating point number, with precision of at least n digits.
date: A calendar date containing a year, month, and day of the month.
time: The time of day, in hours, minutes, and seconds.
timestamp: A combination of date and time.
Page | 74
4.9.2 Basic Schema Definition in SQL:
To create the database instance – CREATE
The Create table command defines each column of the table uniquely. Each
column has minimum of three attributes.
Name
Data type
Size (Column width)
Syntax:
CREATE TABLE TABLENAME (ATTRIBUTE DATATYPE (SIZE));
Example:
CREATE TABLE Student (Reg_no varchar2(10), Name char(30), DOB date,
Address varchar2(50));
To alter the structure of database – ALTER:
Syntax:
ALTER TABLE <table_name>
ADD (<NewColumnName> <Data_Type>(<size>),......n)
Example:
ALTER TABLE Student ADD (Age number(2), Marks number(3));
The Student table is already exists and then we added two more
columns Age and Marks respectively.
To drop database instances – DROP
It will destroy the table and all data which will be recorded in it.
Syntax
DROP TABLE <table_name>
Example:
DROP TABLE Student;
To delete tables in a database instance – TRUNCATE
Syntax:
TRUNCATE TABLE <Table_name>
Example:
TRUNCATE TABLE Student;
Page | 75
To rename database instances – RENAME
Syntax
RENAME <OldTableName> TO <NewTableName>
Example
RENAME <OldTableName> TO <NewTableName>
DML Queries:
Retrieving Data in the Table (Select Command):
Once data has been inserted into a table, the next most
logical operation would be to view what has been inserted. The SELECT SQL verb
is used to achieve this.
To select all Rows and all Columns
Syntax:
SELECT * FROM Table_name;
Example:
iii) Select * from Student;
It will show all the table records.
iv) SELECT First_name, DOB FROM STUDENT WHERE Reg_no = 'S101';
This Command will show one row.
INSERT Operation:
More than one are there to Insert data into a table
Example:
iii) INSERT INTO student (reg_no, first_name, last_name, dob, address,
pincode) VALUES('A101', 'Pandiaraj', 'Murugesan', '13-NOV-87',
'Uthangarai', 635207);
iv) INSERT INTO student VALUES ('A101', 'Pandiaraj', 'Murugesan', '13-NOV-
87', 'Uthangarai', 635207);
Character expression placed within the insert into statement must be
enclosed in single quotes (').
DELETE Operation:
The DELETE command can remove all the rows from the table or a set of
rows from the table.
Page | 76
Example:
iii) DELETE FROM student; It will delete all the rows from student table.
iv) DELETE FROM student WHERE reg_no='A101';
If condition will be satisfied then it will delete a row from the table Register
number A101 will be deleted from the table.
UPDATE Operation:
The UPDATE command is used to change or modify data values in a table
and UPDATE command can update all the rows from the table or a set of rows
from the table.
Example:
UPDATE Student SET course='MCA';
Course is a column name, suppose we want to update something like that
in the student table course should be MCA for all students then we can use this
type of query.
It will update all the rows in the table all rows will have MCA course.
Now, if we want update particular row then see below.
UPDATE Student SET course='MCA' where reg_no='A101';
It will update only one row that will have Register no. A101.
4.10 EMBEDDED SQL:
SQL provides a powerful declarative query language. Writing queries in
SQL is usually much easier than coding for same queries in general purpose
programming language.
However, access to a database from a general-purpose programming
language is required because,
SQL is not as powerful as a general-purpose programming language.
There are queries that cannot be expressed in SQL, but can be programmed
in C, Fortran, Pascal, Cobol, etc.
Nondeclarative actions -- such as printing a report, interacting with a user,
or sending the result to a GUI -- cannot be done from within SQL.
The SQL standard defines embedding of SQL as embedded SQL and the
language in which SQL queries are embedded is referred as host language.
Page | 77
The result of the query is made available to the program one tuple (record)
at a time.
To identify embedded SQL requests to the preprocessor, we use EXEC SQL
statement.
Syntax:
EXEC SQL <embedded SQL statement> END-EXEC
A semi-colon is used instead of END-EXEC when SQL is embedded in C or
Pascal.
The Embedded SQL statements declare cursor, open, and fetch used to
write relational query.
Example:
EXEC SQL
declare c cursor for
select cname, ccity
from deposit, customer
where deposit.cname = customer.cname and deposit.balance >
:amount
END-EXEC
where, amount is a host-language variable.
The open statement for the query is,
EXEC SQL open c END-EXEC
This statement causes the DB system to execute the query and to save the
results within a temporary relation.
A series of fetch statement are executed to make tuples of the results
available to the program.
EXEC SQL fetch c into :cn, :cc END-EXEC
The program can then manipulate the variable cn and cc using the features
of the host programming language.
A single fetch request returns only one tuple. We need to use a while loop
(or equivalent) to process each tuple of the result until no further tuples (when a
variable in the SQLCA is set).
Page | 78
We need to use close statement to tell the database system to delete the
temporary relation that held the result of the query.
EXEC SQL close c END-EXEC
Embedded SQL can execute any valid update, insert, or delete statements.
4.11 DYNAMIC SQL:
Dynamic SQL is SQL statements that are constructed at runtime; for
example, the application may allow users to enter their own queries.
Dynamic SQL is a programming technique that enables to build SQL
statements dynamically at runtime.
We can create more general purpose, flexible applications by using
dynamic SQL because the full text of a SQL statement may be unknown at
compilation.
Dynamic SQL component allows programs to construct and submit SQL
queries are run time.
In embedded SQL, statements must be completely present at compile time;
they are compiled by embedded SQL preprocessor.
In dynamic SQL, programs can create SQL queries as strings at run time
and can execute immediately or have them prepared for subsequent use.
SQL defines standards for embedding dynamic SQL calls in a host
language, such as C.
Example:
char *sqlprog = ―update account set balance = balance * 1.05
where account-number = ?‖;
EXEC SQL prepare dynprog from :sqlprog;
char account[10] = ―A-101‖;
EXEC SQL execute dynprog using :account;
The dynamic SQL program contains a ?, which is a placeholder for a value
that is provided when the SQL program is executed.
The above syntax requires extensions to the language or a preprocessor for
the extended language.
Page | 79
An alternative is to use an application program interface (API) to send SQL
queries or updates to a database system and not to make changes in the
programming language.
We have two standards for connecting to an SQL database and performing
queries and updates. They are,
1) ODBC (Open Database Connectivity)
2) JDBC (Java Database Connectivity)
4.11.1 ODBC:
The Open Database Connectivity (ODBC) is an application program
interface developed for the C language and extended for the languages C++, C#
and Visual Basic.
The ODBC standard defines a way for an application program to
communicate within a database server.
ODBC defines an application program interface (API) that applications can
use to open a connection with a database, send queries and updates, and get back
results.
Applications such as graphical user interfaces, statistics package and
spreadsheets can make use of the same ODBC API to connect to any database
server that supports ODBC.
Each database system supporting ODBC provides a library that must be
linked with the client program.
When the client program makes an ODBC API call, the code in the library
communicates with the server to carry out the requested action, and fetch results.
4.11.2 JDBC:
The Java Database Connectivity (JDBC) is an application program interface
for the Java language.
The JDBC standard defines an API that java programs can use to connect to
database servers.
Opening a Connection and Executing Queries:
Here, java program uses the JDBC interface. The program first open a
connection to a database and can then execute SQL statements, but before opening a
Page | 80
connection, it loads the appropriate drivers for the database by using the class
name.
The first parameter to the getConnection call specifies the machine name
where the server runs, the port number it uses for communication.
The parameter also specifies which schema on the server is to be used, since
a database server may support multiple schemas.
The first parameter also specifies the protocol to be used to communicate
with the database.
A JDBC driver may support multiple protocols and we must specify one
supported by both the database and the driver.
The other two arguments to getConnection are user identifier and
password.
The program then creates a statement handle on the connection and uses it
to execute an SQL statement and get back results.
The program can execute a query by using stmt.executeQuery. It can
retrieve the set of rows in the result into a ResultSet and fetch them one tuple at a
time using the next( ) function on the result set. The connection is closed at the end
of the procedure.
Prepared Statements:
We can create a prepared statements in which some values are replace by
―?‖, thereby specifying that actual values will be provided later.
For example,
PreparedStatement pStmt = conn.prepareStatement
(―insert into account values (?,?,?)‖);
pStmt.setString(1, ―A-9732‖);
pStmt.setString(2, ―Perryridge‖);
pStmt.setInt(3, 1200);
pStmt.executeUpdate( );
pStmt.setSring(1, ―A-9733‖);
pStmt.executeUpdate( );
The setString function allows us to specify the values for the parameters.
Page | 81
Prepared statements are the preferred method of executing SQL queries,
when the query uses values entered by a user.
Suppose that the values for the variables account-number, branch-name
and balance have been entered by a user and a corresponding row is to be inserted
into the account relation.
Suppose that, instead of using a prepared statement, a query is
concatenating the strings as follows,
―insert into account values(‗ ― + account-number + ― ‗, ‗ ― + branch-name +
― ‗,
― + balance + ―)‖
and the query is executed directly.
JDBC also provides a CallableStatement interface that allows invocation of
SQL stored procedures and functions.
Example:
CallableStatement cStmt1 = conn.prepareCall(―{? = call
some_function(?)}‖);
CallableStatement cStmt2 = conn.prepareCall(―{call some_procedure(?,?)}‖);
The data types of function return values and out parameters of procedures
must be registered using the method registerOutParameter() and can be retrieved
using get methods similar to those for results set.
Metadata features:
JDBC also provides mechanisms to examine database schemas and to find
the types of attributes of a result set.
The interface ResultSet has a method getMetaData( ) to get a
ResultSetMetaData object providing metadata about the result set.
The interface ResultSetMetaData has methods to find metadata information
such as the number of columns in the result, the name of a specified column, or the
type of a specified column.
The DatabaseMetaData interface provides a way to find metadata about the
database.
Page | 82
The interface Connection has a method getMetaData that returns a
DatabaseMetaData object.
The DatabaseMetaData interface has a large number of methods to get
metadata about the database.
Other features:
JDBC provides a number of other features such as updatable result sets. It
can create an updatable result set from a query that performs a selection and / or a
projection on database relation.
Difference between Static or Embedded and Dynamic or Interactive SQL:
In Static SQL, how database will be In Dynamic SQL, how database will be
accessed is predetermined in the accessed is determined at run time.
embedded SQL statement.
It is more swift and efficient. It is less swift and efficient.
SQL statements are compiled at compile SQL statements are compiled at run
time. time.
Parsing, Validation, Optimization and Parsing, Validation, Optimization and
Generation of application plan are done Generation of application plan are done
at compile time. at run time.
It is generally used for situations where It is generally used for situations where
data is distributed uniformly. data is distributed non uniformly.
EXECUTE IMMEDIATE, EXECUTE and EXECUTE IMMEDIATE, EXECUTE and
PREPARE statements are not used. PREPARE statements are used.
It is less flexible. It is more flexible.
Page | 83
CHAPTER – 5
5.1 DATA NORMALIZATION:
5.1.1 DEFINITION:
Normalization is the process of decomposing the complex relations and
transforms them into smaller, simpler and well – structured relations.
Normalization serves as a tool for validating and improving the logical
design, so that the logical design satisfies certain constraints and avoids
unnecessary duplication of data.
5.1.2 PITFALLS IN RELATIONAL DATABASE DESIGN:
Storing data in relational tables can result in many problems if the database
is not designed properly.
We use normalization and other techniques to make the database designs
more efficient and proper.
The most problematic issues in the design of relational database are,
Repetition of information (redundancy)
Inability to represent certain information.
Loss of information.
Consider the table Books:
If we wish to add a book to the table, that is the title of the book is
Economics published by Artech House and written by Alexis so we will add
(Economics, Artech House, Cochin). Here publisher and author informations are
repeated.
This repetition of information is undesirable and it is waste of space. It also
complicates the modification.
Another problem with this kind of a design is that we cannot add a
publisher or author if they do not have a book.
Page | 84
Suppose a publisher has a just started the operation but no book has been
released. In this case we cannot add the publisher details to the table as the table as
all fields should have values in it. This problem can be solved by using null values.
The problem with null values is that they are difficult to handle and can
create problems for the inexperienced users.
If we are not willing to use null values, then the details of a new publisher
or author can be added only after they have a published book.
Similarly, when all the books of a publisher or author are deleted from the
table, the information about the publisher or author will also be lost.
There are totally unacceptable solutions and are cases of poor database
design.
Some other pitfalls in relational database design are,
Too much of data
Compound fields
Missing keys
Bad keys
Missing relations
Unnecessary relationship
Incorrect relations
Duplicate field names
Missing or incorrect constraints
Referential integrity
Database security
Missing or incorrect business rules.
5.2 DECOMPOSITION:
The problems created by the bad design of relations suggest that we should
decompose a relation schema that has several attributes into several schemas with
fewer attributes.
The careless decomposition or decomposing without any valid reason can
result in another bad design.
Consider the relation Book:
Page | 85
Now we decompose the Book relation into two relations PUBLISHER and
AUTHOR.
When we join PUBLISHER and AUTHOR, we obtain not only the tuples in
the result, we actually have less information.
Although we have more tuples in the result, we actually have less
information. We are no longer able to represent the database information about
books, authors and publisher correctly. Because of this loss of information we call
the decomposition a lossy decomposition.
A decomposition that is not a lossy-join decomposition is called lossless-
join decomposition.
The lossy-join decomposition is case of bad database design.
Page | 86
5.2.1 REASON FOR LOSSY DECOMPOSITION:
There is one attribute in common between the AUTHOR and PUBLISHER
relations – ―Author‖.
The only way to represent a relationship between title and publisher is
through author. This representation is not adequate because an author can have
many books, yet these books need not be published by the same publisher.
Author alternative design in which we decompose the Book relation into
AUTHOR and PUBLISHER relations
Page | 87
Lack of Redundancy:
Repetitions of information should be avoided.
5.3 FUNCTIONAL DEPENDENCIES:
Functional dependency is defined as a constraint between two sets of
attributes in a relation from a database.
Functional dependency is a property of the meaning or semantics of the
attributes in the relation.
The semantics indicate how attributes relate to one another and specify the
functional dependencies between attributes.
Given a relation R a set of attributes X in R is said to functionally determine
another attributes Y also in R.
X→ Y
Determinant refers to the attribute or group of attributes on the left-hand
side of the arrow of a functional dependency.
When a functional dependency exists, the attribute or group of attributes on
the left hand side of the arrow is called the determinant.
Example:
Page | 88
5.3.1 TRIVIAL FUNCTIONAL DEPENDENCY:
The dependency of an attributes on a set of attributes is known as trivial
functional dependency, if the set of attributes include that attributes.
A dependency is trivial, if and only if, the right-hand side is a subset of the
left-hand side.
A→ B is trivial functional dependencies, if B is Subset of A.
A→ A & B→ B
Example:
Page | 89
5.4 NORMALIZATION:
The process of normalization was first developed by E. F. Codd.
The main goal of normalization is to restructure the logical data model of a
database to,
1. Eliminate redundancy
2. Organize data efficiency
Normalization rule can be divided into following normal forms.
1. First Normal Form (1NF)
2. Second Normal Form (2NF)
3. Third Normal Form (3NF)
4. Boyce Codd Normal Form (BCNF)
5. Fourth Normal Form (4NF)
6. Fifth Normal Form (5NF)
5.4.1 FIRST NORMAL FORM (1NF):
Rules:
The values in each column of a table are ―Atomic value‖ (No multi value
attributes are allowed).
There are ―no repeating groups‖ two column do not store similar
information in the same table.
Example:
This table is not in first normal because the (Colour) column can certain
multiple values.
To bring this table to 1NF, we split the table into two tables.
Page | 90
Now the first normal form is satisfied, as the columns on each table are
hold just one value.
The advantage of designing the table like this that avoid the need for a
relationship but the disadvantages are many,
Page | 91
The table in this example is in 1NF since all attributes are single value, but
it is not yet 2NF.
To solve the problem, we must create a new table Professor (IDProf,
Prof_Name) and Student (IDSt, Stu_Name).
5.4.3 THIRD NORMAL FORM (3NF):
Rules:
A relation is in third normal form if it is in 2NF.
No non key attribute is transitive dependent on the primary key.
Transitive dependent:
Transitive dependency is a condition where A, B and C are attributes such
that if
A → B and B → C then C is transitively dependent on A via B.
A → B and B → C then A→ C
Example:
Page | 92
could be dependent on Unit_Code, therefore it is transitively dependent on
Unit_Code.
The table is in 3NF A and B are keys and C and D are depend on both A
and B. The table is not in BCNF because dependency exists between C and B.
Dependencies are
AB → CD
C→B
Page | 93
Example:
An example table of University Database
BCNF is a stronger form of the 3NF such that every relation in BCNF is also
in the 3NF.
5.4.5 FOURTH NORMAL FORM (4NF):
Rules:
It is in 3NF (or) BCNF.
Multi-Valued dependencies are removed that is isolation of independent
multiple relationships.
No table should contain two (or) more one-to-many (or) many-to-many
relationships that are not directly related to the key. These kinds of relationships are
called Multi-Valued dependencies.
Page | 94
Multi-Valued Dependency (MVD):
If we have two or more multi-valued independent attributes in the same
relation, we get into a situation where we have to repeat every value of one of the
attributes with every value of the other attribute.
This constraint is specified by a multi-valued dependency.
Thus, a multi-valued dependency represents a dependency between
attributes A, B and C in a relation for each value of A there is a set of values for B
and set of values for C.
We represent a MVD between attributes A, B and C.
A →→ B
A →→ C
A MVD can be further defined as trivial or nontrivial.
A MVD A →→ B in a relation R is defined as being trivial if B is a subset of
A or A∪B = R.
A MVD is defined as being nontrivial if B is not a subset of A and A∪B ≠ R.
Fourth normal form (4NF) is a relation that is in BCNF and contains no
nontrivial MVDs.
4NF is stronger than BCNF as it prevents relations from containing
nontrivial and data redundancy.
The normalization of BCNF to 4NF involves the removal of the MVD from
the relation by placing the attribute(s) in a new relation along with a copy of the
determinant.
Example:
A table EMPLOYEE that has the attributes Name, Project and Hobby
Page | 95
A row in the EMPLOYEE table represents the fact that an employee works
for a project and has a hobby.
In case, employee can work in more than one project and can have more
than one hobby.
The employee‘s projects and hobbies and independent of one another. To
keep the relation consistent state, we must have a separate tuple to represent every
combination of an employee‘s project and an employee‘s hobbies. This constraint is
specified as MVD on the EMPLOYEE relation.
Whenever two independent one-to-many relationship (A : B and A : C) are
mixed in the same relation, a MVD arises.
MVD can be avoided using forth normal form.
The above relation has two MVDs – (Name, Project) and (Name, Hobby).
We divide the EMPLOYEE table into two tables to satisfy 4NF.
Page | 96
Hence, we have decomposed the table in such a way that it satisfies all the
rules in 4NF and when join them by using key.
We can divide table into three (Subject, Lecturers), (Lecturers, Class) and
(Subject, class).
We joined key columns of each table to get the correct data. Hence there is
no loss of data satisfying 5NF form.
5.5 DENORMALIZATION:
Denormalization is the opposite of normalization. It is the process of
increasing redundancy in the database either for convenience or to improve
performance.
Proper Denormalization takes place after a model has been fully
normalized.
Denormalization is usually done for convenience and to improve
performance.
Denormalization of frequently performed operations or calculations can
be used to improve performance.
5.6 OVERALL DATABASE DESIGN PROCESS:
In Database Design Process, there are six stages in the design of a database:
1. Requirement analysis
2. Conceptual database design
3. Choice of the DBMS
4. Data model mapping
5. Physical design
6. Implementation
Page | 97
1. Requirement Collection and Analysis:
Purpose: to document the data requirements of the users
Functional requirements are the operations that will be applied to the
database, including queries and update.
The specification will then be used as the basis for the design of the
database.
Typical activities:
Identification of application areas and user groups
Analysis of existing documentation of application areas, e.g. policy
documents, forms, reports, organization charts
Analysis of current operating environments and the planned use of the
information, e.g. information flow, types of transactions, frequency of transaction
types
Use a technique to transform that into a specification of the database
requirements which is:
Formal – homogeneous – consistent – complete
2. Conceptual Design:
Two parallel activities:
1. Schema design – examines the data requirements of the database resulting from
the analysis (phase 1) and produces a conceptual schema in a DBMS-independent
high level data model
2. Transaction design – examines the database applications whose requirements
were analysed in phase 1 and product.
Conceptual Schema Design:
Purpose: to produce a conceptual schema of the database
Expressed using concepts of the high level data model
Not including implementation details (has to be understood by non-
technical users)
But detailed in terms of the ―objects‖ of the domain the database will
represent.
Page | 98
Transaction Design:
Purpose: to produce a design of the transactions, that will run on the
database
1. Retrieval: retrieve data for display or as part of a report
2. Update: enter new data or amend existing data
3. Mixed: more complex applications may do both retrieval and update.
3. Choosing a DBMS:
Purpose: establish which is the best framework for implementing the
produced schema:
– Type of DBMS (relational, network, deductive, Object Oriented and etc
– User and programmer interfaces
– Types of query languages.
4. Logical Design Purpose:
To transform the generic, DBMS independent conceptual schema in the
data model of the chosen DBMS (data model mapping) two stages:
1. System independent mapping: no consideration of any specific
characteristics that may apply to the specific DBMS package
2. Tailoring to DBMS: different DBMSs may implement the same data
model in slightly different ways result is a set of DDL statements in the language of
the chosen DBMS – some CASE tools generate DDL statements from a conceptual
design.
5. Physical Design Purpose:
To choose the specific storage structures and access paths for the database
files
– Attention to performances
Some relevant criteria:
Response time: may want to minimize database access time for data items
referenced by frequently used transactions
Space utilization: less frequently used data and queries may be archived
Transaction throughput: average number of transactions that can be
processed per minute.
Page | 99
6. Implementation:
Purpose: to create the database
Compile and execute DDL statements
Populate the database
Manually/automatically (may need to convert data from a previous
format)
Implement application programs (transactions)
Programs are written with embedded DML statements
Page | 100
CHAPTER - 6
DATA SECURITY
6.1 DATA SECURITY REQUIREMENTS:
The basic security standards which technology can ensure are
Confidentiality
Integrity and
Availability
6.1.1 CONFIDENTIALITY:
A secure system ensures that the confidentiality of data. This means that it
allows individuals to see only the data that they are supposed to see.
Confidentiality has several aspects,
Privacy of Communications
Secure storage of Sensitive Data
Authenticated Users
Granular Access Control
Privacy of Communications:-
The DBMS should be capable of controlling the spread of confidential
personal information. It should also keep the corporate data such as trade secrets,
proprietary information, and competitive analysis.
Secure storage of Sensitive Data:-
Once confidential data has been entered, its integrity and privacy must be
protected.
Authenticated Users:-
Authentication method seeks to guarantee the identity of system users.
Granular Access Control:-
Access control is the ability to hide portions of the database.
6.1.2 INTEGRITY:
It is mechanism used to prevent invalid data entry into the table.
Types:
Domain integrity constraints
Entity integrity constraints
Referential integrity constraints
Page | 101
Domain Integrity Constraints:
A Domain is a set of values that may be assigned to an attribute. All values
that appear in a column of a relation (table) must be taken from the same domain.
Types:
Not null constraint
Check constraint
Entity Integrity Constraints:
The entity integrity constraint states that no primary key value can be null.
Because the primary key value is used to identify individual tuples in a relation;
having null values for the primary key implies that we cannot identify some tuples.
Types:
Unique constraint
Primary key constraint
Referential Integrity:
It ensures that a value appears in one relation for a given set of attributes
also appears for a certain set of attributes in another relation. This condition is
called referential integrity.
Foreign key can be specified as part of the SQL creates table statement by
using the foreign key clause.
For example, in the banking database.
SQL > create table account (acc_no char(10),
branch_name char(15),
balance number(10,2),
primary key(acc_no),
foreign key(branch_name) references branch,
check (balance>=0));
The definition of the account table has a declaration ―foreign key
(branch_name) references branch‖. This foreign key declaration specifies that for
each account tuple the branch name specified in the tuple must exist in the branch
relation.
Page | 102
Without this constraint, it is possible for an account to specify a non-
existent branch name.
For example, if ―Uthangarai‖ is a branch name appearing in one of the
tuples in the account relation, then there exists a tuple in the branch relation for
branch ―Uthangarai‖.
Referential Integrity in SQL:
Primary, candidate and foreign keys can be specified as part of the SQL
creates table statement.
By default, a foreign key references the primary key attributes of the
referenced table. foreign key(acc_no) references account.
For example, consider the following create table statements.
SQL>create table customer (customer-name char(10), customer-street char
(20), customer-city char(30), primary key(customer-name));
SQL>create table depositor (customer-name char(10), account-no char(10),
primary key(customer-name, account-no), foreign key(customer-name) references
customer);
6.1.3 AVAILABILITY:
A secure system makes data available to authorized users without delay.
It has number of aspects,
Resistance
Scalability
Flexibility
Ease of use
Resistance:
The system can be protected against users consuming too much memory or
too many processes. Thus preventing others from doing their work.
Scalability:
System performance must remain adequate regardless of the number of
users or processes demanding service.
Page | 103
Flexibility:
Administrators must have adequate means of managing the user
population.
Ease of Use:
The security implementation itself must not diminish the ability of valid
users to get their work.
6.2 ASSERTIONS:
An assertion is a predicate expressing a condition that we wish the database
always to satisfy. An assertion in SQL takes the from
SQL > create assertion < assertion-name > check < predicate>;
When an assertion is made, the system tests it for validity and tests it again
on every update that may violate the assertion.
Asserting for all X, P (X) is achieved in a round-robin fashion using not
exists X such that not P(x).
For example, The sum of all loan amounts for each branch must be less than
the sum of all account balances at the branch.
SQL > create assertion sum_constraint check (not exists
(select * from branch where loan.branch_name = branch.
Branch_name)
> = (select sum(amount) from account
where loan.branch_name = branch.branch_name));
6.3 SECURITY:
Security of data is important concept in DBMS because it is essential to
safeguard the data against any unwanted users.
It is a protection from malicious attempts to steal or modify data.
There are five different levels of security
1. Database system level
Authentication and authorization mechanism to allow specific users access
only to required data.
2. Operating
Protection from invalid logins
File-level access protection
Protection from improper use of ―superuser‖ authority.
Page | 104
Protection from improper use of privileged machine instructions.
3. Network level
Each site must ensure that it communicates with trusted sites.
Links must be protected from theft or modification of messages.
Mechanisms used
Identification protocol (password based)
Cryptography
4. Physical level
Protection of equipment from floods, power failure, etc.
Protection of disks from theft, erasure, physical damage, etc.
Protection of network and terminal cables from wire tapes, non-invasive
electronic eavesdropping, physical damage, etc.
Solution:
Replication hardware-mirrored disks, dual busses etc.
Multiple access paths between every pair of devices
Physical security by locks, police etc.
Software techniques to detect physical security breaches
5. Human level
Protection from stolen passwords, sabotage, etc
Solution:
Frequent change of passwords
Use of ―non-guessable‖ passwords
Log all invalid access attempts
Data audits
6.4 PROTECTING THE DATA WITHIN THE DATABASE:
Privileges specify the type of DML operations like SELECT, UPDATE,
INSERT, DELETE etc. with in the user can perform upon data.
The access control is done by using
Privileges and
Roles
Page | 105
6.4.1 Privileges:
A privilege is permission to access a named object in a prescribed manner.
Types of privileges:
1. Database Privileges
2. System Privileges
3. Object Privileges
GRANT command:
This permission is given using the GRANT command.
Syntax:
GRANT {ALL | privilege-list}
ON {table-name | view-name}
To {PUBLIC | user-list}
[WITH GRANT OPTION]
The grant statement is used to center authorization
Granting a privilege on a view does not imply granting any privileges on
the underlying relations. The grantor of the privilege must already hold the
privilege on the specified item. (or be the DBA).
Privileges in SQL:
Select: allows read access to relation, or the ability to query using the view.
Example:
Grant users U1, U2 and U3 select authorization on the branch relation.
SQL>grant select on branch to U1, U2, U3 ;
Insert: the ability to insert tuples.
Update: the ability to update using the SQL update statement.
Delete: the ability to delete the tuple.
References: ability to declare foreign keys when creating relations.
Usage: in SQL-92; authorizes a user to use a specified domain.
All privileges: used as a short form for all the allowable privileges.
Example:
SQL> grant select on branch to U1 with grant option;
gives U1 the select privileges on branch and allows U1 to grant this
privilege to others.
Page | 106
6.4.2 Roles:
Roles permit common privileges for a class of users can be specified just
once by creating a corresponding ―role‖.
Privileges can be granted to or revoked from roles, just like user.
Roles can be assigned to users, and even to other roles.
SQL>create role manager grant all privileges on account to
manager;
6.4.3 Revoking Authorization in SQL:
The revoke statement is used to revoke authorization.
The revoke command is used to take away a privilege that was granted.
Syntax:
REVOKE {ALL | privilege-list}
ON {relation-name | view name}
FROM {PUBLIC | user-list}
Example:
Revoke select on branch from U1, U2, U3 cascade;
Revocation of the privilege from a user may cause other users also to lose
that privilege, referred to as cascading of the revoke.
We can prevent cascading by specifying restrict.
SQL>revoke select on branch from U1, U2, U3 restrict;
With restrict, the revoke command fails if cascading revokes are required.
< privilege-list > may be all to revoke all privileges the revokee
may hold.
It < revokee_list > includes public all users lose the privilege except
those granted it explicitly.
Limitations of SQL Authorization
SQL does not support authorization at a tuple levels.
With the growth in Web accesses to the databases, database accesses come
primarily from application servers.
All end-users of an application may be mapped to a single database user.
Page | 107
6.5 ENCRYPTION:
Data may be encrypted when database authorization provisions do not
offer sufficient protection.
Properties of Good Encryption Technique:
o Relatively simple for authorized users to encrypt and decrypt data.
o Encryption scheme depends not on the secrecy of the algorithm but on the
secrecy of a parameter of the algorithm called the encryption key.
o Extremely difficult for an intruder to determine the encryption key.
Data Encryption Standard (DES) substitutes characters and rearranges
their order on the basis of an encryption key which is provided to
authorized users via a secure mechanism. Scheme is no more secure
than the key transmission mechanism since the key has to be shared.
Advanced Encryption Standard (AES) is a new standard replacing DES,
and is based on the Rijndael algorithm, but it is also dependent on
shared secret key.
Public key encryption is based on each user having 2 keys:
Public key - publicly published key used to encrypt data, but cannot be
used to decrypt data.
Private key - key known only to individual user, and is used to decrypt
data.
Encryption scheme is such that it is impossible or extremely hard to decrypt
data given only the public key.
6.6 AUTHENTICATION:
o Password based authentication is widely used, but is susceptible to sniffing
on a network.
o Challenge-response systems avoid transmission of passwords.
o DB sends a challenge string to user. User encrypts string and returns result.
o DB verifies identity by decrypting result. Can use public-key encryption
scheme by DB sending a message encrypted using user‘s public key, and
user decrypting and sending the message back.
o Digital Signatures are used to verify authenticity of data.
Page | 108
For example, use private key to encrypt data, and anyone can verify authenticity by
using public key to decrypt data.
Digital Certificates:
They are used to verify authenticity of public keys.
Problem:
When you communicate with a website, how do you know if you are
talking with a genuine website or an imposter?
Solution:
Use the public key of the website.
Problem:
How to verify if the public key itself is genuined.
Solution:
Every client (eg. browser) has public keys of a few root-level certification
authorities.
A site can get its name URL and public key signed by a certification
authority, signed document is called certificate.
Client can use public key of certification authority to verify certificate.
Page | 109
CHAPTER - 7
PL/SQL: A PROGRAMMING LANGUAGE
7.1 HISTORY OF PL/SQL:
SQL is a great query language, but it has limitation so, Oracle Corporation
has added a procedural language extension to SQL known as Programming
Language Extensions to Structured Query Language (PL/SQL).
PL/SQL also allows embedding of SQL statements and data manipulation
in its blocks. SQL statements are used to retrieve data and PL/SQL control
statements are used to process data in a PL/SQL program.
The data can be inserted, deleted or updated through a PL/SQL block.
PL/SQL version 1.0 was introduced with oracle 6.0 in 1991. Version 1.0 had
very limited capabilities. It was merely used for batch processing.
With versions 2.0, 2.1 and 2.2, the following new features were introduced.
The Transaction control statements SAVEPOINT, ROLLBACK and
COMMIT.
The DML statement INSERT, DELETE and UPDATE.
The extended data types Boolean, BINARY_INTEGER, PL/SQL records
and PL/SQL tables.
Build-in functions - character, numeric, conversion and data functions.
Build-in packages.
The Control structures sequence, selection and looping.
Database access through work areas called cursors.
Error Handling.
Modular programming with procedures and function.
Stored procedures, functions and Packages.
Programmer – defined subtypes.
DDL support through the DBMS_SQL package.
The PL/SQL Wrapper.
The DBMS-JOB job scheduler.
File I/O with the UTF-File package.
PL/SQL version 8.0 also known as PL/SQL8 came with oracle8.
Page | 110
7.2 FUNDAMENTALS OF PL/SQL:
A PL/SQL program consist of statements. It may use upper or lower case
letters in program. i.e. PL/SQL is not case sensitive except for character string
values enclosed in single quotes.
PL/SQL statements consist of
Reserved Words
Identifiers
Literals
Comments
7.2.1 RESERVED WORDS:
The reserved words or key words are words provided by the language that
have a specific use in the language.
Reserved Words in PL/SQL:
DECLARE EXCEPTION
BEGIN PROCEDURE
END FUNCTION
IF PACKAGE
WHILE TRIGGER
7.2.2 USER-DEFINED IDENTIFIERS:
User-defined identifiers are used to name variables, constants, procedures,
functions, cursors, tables, records and exceptions.
Rules for naming Identifiers:
The name can be from 1 to 30 characters in length.
The name must start with a letter.
Letters (A-Z, a-z) numbers, dollar sign ($), number sign (#) and underscore
(_) are allowed.
Spaces are not allowed.
Other Special characters are not allowed.
Keywords cannot be used as Identifiers.
Names must be unique within a block.
A name should not be the same as the name of a column used in the block.
Page | 111
VALID Identifiers INVALID Identifiers
Rate_of_pay 2number (starts with a Number)
Num Emp_name (Special character
hyper)
All END (Reserved Word)
SS# Dept no (Spaces)
7.2.3 LITERALS:
Literals are values that are not represented by user – defined identifiers.
Literals are of three types:
Numeric
Character
Boolean
For ex:
Numeric: 100, 3.14, -55 or NULL
Character: ‗A‘ ‗this is a str‘ ‗0001‘ ‘25- May-00‘ or NULL
Boolean: TRUE, FALSE OR NULL
In this list of values ‘25-May-00‘ looks like a date value, can be converted to
date format by using the TO_DATE function.
7.2.4 COMMENTS:
Comments are used to document programs, they are written as part of a
program, but they are not executed, comments are ignored by the PL/SQL engine.
There are two ways to write comments in PL/SQL:
1. To write a single_line comments, two dashes (--) are entered at the
beginning of a new line.
For ex:
--This is a single-line comment.
2. To write a multiline comment, comment text is placed between /* and */
For ex:
/* This is a
multiline comment
that ends here */
Page | 112
7.3 PL/SQL BLOCK STRUCTURE:
PL/SQL is a block-structured language. A Program can be divided in to
logical blocks. The block structure gives modularity to a PL/SQL program and each
object within a block has ―Scope‖.
Blocks are of two types:
1. An Anonymous block is a block of code without a name. It can be used
anywhere in a program and is sent to the server engine for execution at run
time.
2. A Named Block is a block of code that is named.
A Subprogram is a named block that can be called and can take arguments.
A Procedure is a subprogram that can perform an action and it cannot
return a value.
A Function is a subprogram that returns a value.
A Package is formed from a group of procedures and functions.
A trigger is a block that is called implicitly by a DML statement.
A PL/SQL block consist of three sections:
1. A declaration section.
2. An Executable section.
3. An exception-handling section.
Declaration:
An optimal section to declare variables, constants, cursors, PL/SQL
composite data types and user-defined exceptions, which are referred in executable
and exception-handling sections.
Executable:
A mandatory section that contains PL/SQL statements to manipulate data
in the block and SQL statements to manipulate the database.
Exception Handling:
Specifies action statement to perform when an error condition exists in the
executable section.
It is also an optional section.
Page | 113
General Syntax:
[DECLARE
Declaration of constants, variables, Cursors and Exceptions]
BEGIN
Executable PL/SQL and SQL statements
[EXCEPTION
Action for error conditions]
END;
The DECLARE and EXCEPTION keywords are optional, but the BEGIN
and END keywords are mandatory. The declarations made with on a block are local
to the block.
A block is the ―Scope‖ of objects declared in that block. When blocks are
nested within each other, the declarations made in the outer block are global to the
inner block.
The object declarations made in the inner block are local to it and cannot be
referred by the outer block.
A basic PL/SQL block can be embedded in any other PL/SQL block named
or unnamed.
Blocks available in the oracle server environment are,
Anonymous block:
An unnamed block that is independent or embedded with in an
application.
Procedure/Function:
A named block that is stored on the oracle server can be called by its name,
and can take arguments.
Package:
A named PL/SQL module that is a group of functions, procedures and
identifiers.
Trigger:
A block that is associated with a database table or a view. It is executed
when automatically fixed by a DML statement.
Page | 114
7.4 DATA TYPES:
Each constant and variable in the program needs a data type.
The data type decides the type of value that can be stores in a variable.
PL/SQL has following data types:
1. Scalar
2. Composite
3. LOB
7.4.1 SCALAR:
A Scalar data types is not made up of a group of elements. It is atomic in
nature. It stores single values with no components.
There are four major categories of Scalar data type:
Character
Number
Boolean
Date
CHARACTER:
Variables with a character data type can store text. The text may include
letters, numbers and special characters.
The text in character type variables can be manipulated with build in
character functions.
Character data types include
CHAR - Fixed-length character string with maximum size of 32,767
bytes
VARCHAR2 - Variable-length character string with maximum size of 32,767
bytes
RAW - Variable-length binary or byte string with maximum size of
32,767 bytes, not interpreted by PL/SQL
NCHAR - Fixed-length national character string with maximum size of
32,767 bytes
NVARCHAR2 - Variable-length national character string with maximum size
of 32,767 bytes
Page | 115
LONG - Variable-length character string with maximum size of 32,760
bytes
LONG RAW - Variable-length binary or byte string with maximum size of
32,760 bytes, not interpreted by PL/SQL
ROWID - Physical row identifier, the address of a row in an ordinary
table
UROWID - Universal row identifier (physical, logical, or foreign row
identifier)
NUMBER:
PL/SQL has a variety of numeric data types, whole number (OR) integer
values can be handled by following data types.
BINARY_INTEGER
INTEGER
INT
SMALLINT
POSITIVE
NATURAL
Similarly there are various data types for decimal numbers,
NUMBER
DEC (fixed-point number)
DECIMAL (fixed-point number)
NUMERIC (fixed-point number)
FLOAT (floating-point number)
REAL (floating-point number)
DOUBLE PRECISION (floating-point number)
BOOLEAN:
PL/SQL has a logical data type. Boolean that is not available in SQL
It is used for Boolean data TRUE, FALSE or NULL.
These values are not enclosed in single quotation marks.
DATE:
The date type is a special data type that stores date and time information.
Page | 116
A user can enter a date in many different formats with the TO_DATE
function.
SYSDATE function is used to return the system‘s current date.
7.4.2 LOB:
Large Object (LOB) data types refer to large data items such as text,
graphic images, video clips, and sound waveforms. LOB data types allow efficient,
random, piecewise access to this data. Following are the predefined PL/SQL LOB
data types −
Data Type Description Size
BFILE Used to store large binary objects in System-dependent. Cannot
operating system files outside the database. exceed 4 gigabytes (GB)
BLOB Used to store large binary objects in the 8 to 128 terabytes (TB)
database.
CLOB Used to store large blocks of character data 8 to 128 TB
in the database.
NCLOB Used to store large blocks of NCHAR data 8 to 128 TB
in the database.
Page | 117
A CONSTANT is an identifier that must be initialized and the value of
which cannot be changed in the program body.
A NOT NULL constraint can be used for variables and such variables must
be initialized.
: = can be used to initialize a constant or a variable to a value.
Example:
DECLARE
v_number NUMBER (2);
v_count NUMBER (1):=1;
c_pi CONSTANT NUMBER:=3.14;
Page | 118
A host variable is declared at the SQL>prompt with the SQL*plus
VARIABLE statement.
The syntax of a host variable declaration is,
VARIABLE variable-name data type
For Example,
SQL>VARIABLE double NUMBER
Using a host variable in a PL\SQL block
SQL > VARIABLE g_double NUMBER
SQL > DECLARE
2 v_num NUMBER(2);
3 BEGIN
4 v_num:=5;
5 :g_double := v_num * 2;
6 END;
7.8 SUBSTITUTION VARAIBLES IN PL/SQL:
PL/SQL does not have any input capabilities in terms of having an input
statement.
There are no explicit input/output statements but substitution variables of
SQL are available in PL/SQL.
Example, SQL> VARAIBLE g_double NUMBER
SQL> DECLARE
2 v_num NUMBER(2);
3 BEGIN
4 v_num:=&p_num;
5 :g_double := v_num *2;
6 END;
7 /
10
SQL> PRINT g_double
Page | 119
7.9 PRINTING IN PL/SQL:
There is no explicit output statement in PL/SQL. Oracle does have a built-
in package called DBMS_OUTPUT with the procedure PUT_LINE to print.
The DBMS_OUTPUT is the most frequently used package because of its
capabilities to get lines from a file and to put lines in to the buffer.
The PUT_LINE procedure displays information passed to the buffer and
puts a new_line marker at the end.
For Example,
DBMS_OUTPUT.PUT_LINE (‗This line will be displayed‘);
SQL > VARIABLE NUM NUMBER
SQL > SET SERVEROUTPUT ON
SQL> DECLARE
2 DOUBLE NUMBER;
3 BEGIN
4 : NUM:=5;
5 DOUBLE: =: NUM*2;
6 DBMS_OUTPUT.PUT_LINE ((‗DOUBLE IS‘ || TO_CHAR (DOUBLE));
7
8 END;
OUTPUT:
DOUBLE IS 10.
Another procedure, DBMS_OUTPUt.PUT, it display the output on the same
line.
7.10 ARITHMETIC OPERATORS:
Five standard arithmetic operators are available in PL/SQL for calculations.
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
Page | 120
The following order of precedence is used,
Exponentiation is performed first, multiplication and division are
performed next and addition and subtraction are performed last.
If more than one operator of the same priority is present, they are
performed from left to right.
Whatever is in parenthesis is performed first
7.11 CONTROL STRUCTURES AND EMBEDDED SQL:
7.11 CONTROL STRUCTURE:
In PL/SQL, there are three basic programming control structures;
1. Sequential Structure
2. Selection structure
3. Looping Structure
7.11.1 SEQUENTIAL STRUCTURE:
In a sequential structure, a series of instructions are performed from the
beginning to the end in a linear order.
None of the instructions is skipped and none of the instructions is repeated.
Example program:
SQL > VARIABLE NUM NUMBER
SQL > DECLARE
2 DOUBLE NUMBER
3 BEGIN
4 : NUM: =5;
5 DOUBLE: =: NUM*2;
6 DBMS_OUTPUT.PUT_LINE ((‗DOUBLE OF‘||
7 (: NUM) || ‗IS‘ || (DOUBLE));
8 END;
9 /
DOUBLE OF 5 IS 10
PL/SQL Procedure successfully completed.
Page | 121
7.11.2 SELECTION STRUCTURE:
The selection sequence is also known as a decision structure or an IF
structure.
It involves conditions with a TRUE or FALSE outcome. Based on the
outcome, one of the option is performed and other option is skipped.
Selection statements are also available for multiple options.
There are three selection or conditional statements in PL/SQL.
Relational operators, logical operators and other special operators are used
to create Boolean expression on conditions.
= Equal to
< > or ! = Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
IS NULL,
IN,
LIKE
BETWEEN...AND
Page | 122
PL/SQL has five conditional or selection statements for decision making.
1. IF...THEN...END IF
2. IF...THEN...ELSE...END IF
3. IF...THEN....ELSIF....END IF
4. CASE...END CASE
5. Searched CASE
IF...THEN...END IF:
It is also known as a simple IF statements. A simple IF statement performs
action statements if the result of the condition is TRUE. If the condition is FALSE,
no action is performed.
Syntax:
IF condition(s) THEN
Action statements
END IF;
Example:
SQL >DECLARE
2 V_DAY VARCAR2 (9):=‘&DAY‘;
3 BEGIN
4 IF (V_DAY) =‘SUNDAY‘) THEN
5 DBMS_OUTPUT.PUT_LINE (‗SUNDAY IS A HOLIDAY‘)
6 END IF;
7 END;
8 /
O/P:
Enter the value for day: SUNDAY
SUNDAY IS A HOLDAY
PL/SQL Procedure Successfully completed
Page | 123
IF...THEN...ELSE...END IF:
It is an extension of the simple IF statement. It provides action statement for
the TRUE out come as well as for the false outcome.
General syntax:
IF Condition(s) THEN
Action Statement1
ELSE
Action Statement2
END IF;
If the condition‘s outcome is TRUE ACTION Statement are performed. If
the outcome is FALSE, action statements are performed. One set of statement is
skipped in any case
Example:
SQL > DECLARE
2 V_AGE NUMBER (2):=‘&AGE‘;
3 BEGIN
4 IF (V_AGE>=18) THEN
5 DBMS_OUTPUT.PUT_LINE (‗AGE:‘ ||V_AGE||‘-ADULT‘);
6 ELSE
7 DBMS_OUTPUT.PUT_LINE (‗AGE:‘ ||V_AGE||‘-MINOR‘);
8 END IF;
9 END;
10 /
O/P:
Enter value of age: 21
AGE: 21 – ADULT.
Page | 124
IF...THEN....ELSIF....END IF:
General structure:
IF condition(s)1 THEN
Action statements1
ELSIF condition(s)2 THEN
Action statements2
….
ELSIF condition(s)N THEN
Action statementsN
[ELSE
Else Action statements]
END IF;
The simple IF statement is less efficient. Suppose we will need five simple
IF statements to accomplish the same task as that performed by a single compound
ELSIF statement.
The word ELSIF, which does not have the last E in ELSE. ELSIF is a single
word, but END IF uses two words.
Example program:
DECLARE
v_pos NUMBER (1) : = &position
BEGIN
IF v_pos = 1 THEN
DBMS_OUTPUT.PUT_LINE (‗20% increase‘);
ELSIF v_pos = 2 THEN
DBMS_OUTPUT.PUT_LINE (‗15% increase‘);
ELSIF v_pos = 3 THEN
DBMS_OUTPUT.PUT_LINE (‗10% increase‘);
ELSIF v_pos = 4 THEN
DBMS_OUTPUT.PUT_LINE (‗5% increase‘);
ELSE
DBMS_OUTPUT.PUT_LINE (‗No increase‘);
END IF;
Page | 125
END;
O/P:
Enter value of position: 2
15% increase.
CASE:
The CASE statement is an alternative to the IF...THEN....ELSIF....END IF
statement.
The CASE statement begins with keyword CASE and ends with the
keywords END CASE.
The body of the CASE statement contains WHEN clauses, with values or
conditions and action statements.
When a WHEN clause‘s value/condition evaluates to TRUE, its action
statements are executed.
Syntax:
CASE [variable-name]
WHEN value1/condition1 THEN action_statement1;
WHEN value2/condition2 THEN action_statement2;
.
.
WHEN valueN/conditionN THEN action_statementN;
ELSE action_statement;
END CASE;
A statement with a value is known as a CASE statement.
Example:
DECLARE
V_NUM NUMBER:= &ANY_NUM;
V_RES NUMBER;
BEGIN
V_RES:=MOD(V_NUM,2);
CASE V_RES
WHEN 0 THEN DBMS_OUTPUT.PUT_LINE
(V_NUM||‘IS EVEN‘);
Page | 126
ELSE DBMS_OUTPUT.PUT_LINE (V_NUM||‘IS ODD‘);
END CASE;
END;
O/P:
Enter value for any_num: 5
5 IS ODD.
Searched CASE:
A statement with a condition is known as a searched CASE statement.
A CASE statement uses variable-name as a selector, but searched CASE
does not use variable-name as a selector.
Example:
DECLARE
V_NUM NUMBER:= &ANY_NUM;
BEGIN
CASE
WHEN MOD(V_NUM,2)=0 THEN
DBMS_OUTPUT.PUT_LINE (V_NUM||‘IS
EVEN‘);
ELSE
DBMS_OUTPUT.PUT_LINE (V_NUM||‘IS
ODD‘);
END CASE;
END;
O/P:
Enter value for any_num: 5
5 IS ODD.
Nested IF:
The nested IF statement contains an IF statement within another IF
statement. If the condition in the outer IF statement is TRUE, the inner IF statement
is performed.
Page | 127
Any IF statement with a compound condition can be written as a nested IF
statement.
Example:
DECLARE
V_GENDER CHAR:=‘&GEN‘;
V_AGE NUMBER:=‘&AGE‘;
V_CHARGE NUMBER(3,2);
BEGIN
IF(V_GENDER=‘M‘ AND V_AGE>=25) THEN /*MALE*/
V_CHARGE:=0.05;
ELSE
V_CHARGE:=0.10;
END IF;
ELSE /*FEMALE*/
IF(V_AGE>=25) THEN
V_CHARGE:=0.03;
ELSE
V_CHARGE:=0.06;
END IF;
END IF;
DBMS_OUTPUT.PUT_LINE (‗GENDER:‘||V_GENDER);
DBMS_OUTPUT.PUT_LINE (‗AGE:‘||V_ AGE);
DBMS_OUTPUT.PUT_LINE (‗SURCHARGE:‘||V_ CHARGE);
END;
O/P:
Enter the value for sex: F
Enter the value for age: 18
GENDER: F
AGE: 18
SURCHARGE: 0.06.
Page | 128
7.11.3 LOOPING STRUCTURE:
In a looping structure, a series of instructions is performed repeatedly.
Looping means iterations, a loop repeats a statement or a series of
statements a specific number of times.
There are three types of looping statements are available in PL/SQL:
Basic loop
WHILE loop
FOR loop
Basic loop:
A basic loop is a loop that is performed repeatedly. Once a loop is entered,
all statements in the loop are performed.
When the bottom of the loop is reached, control shifts back to the top of the loop.
The loop will continue infinitely. An infinite loop or a never ending loop is
a logical error in programming.
The only way to terminate a basic loop is by adding an EXIT statement
inside the loop.
General structure:
Loop
Looping statement1;
Looping statement2;
.
.
Looping statementN;
EXIT[WHEN Condition];
END Loop;
Example:
DECLARE
COUNT NUMBER (2);
SUM NUMBER (2):=0;
AVG NUMBER (3, 1);
BEGIN
COUNT: =1;
Page | 129
LOOP
SUM:=SUM+COUNT;
COUNT:=COUNT+1;
EXIT WHEN COUNT>10
END LOOP;
AVG:=SUM/(COUNT-1);
DBMS_OUTPUT.PUT_LINE (‗AVERAGE OF 1 TO 10
IS‘||AVG);
END;
O/P:
AVERAGE OF 1 TO 10 IS 5.5
WHILE Loop:
The WHILE loop is an alternative to the basic loop and is performed as long
as the condition is true. It terminates when the condition becomes false.
If the condition is false at the beginning of the loop, the loop is not
performed at all. The WHILE loop does not need an EXIT statement to terminate.
Syntax:
WHILE condition Loop
Looping statement1;
Looping statement2;
…..
Looping statementN;
END Loop;
Example program:
DECLARE
COUNT NUMBER (2);
SUM NUMBER (2):=0;
AVG NUMBER (3, 1);
BEGIN
COUNT: =1;
WHILE COUNT <=10 LOOP
Page | 130
SUM:=SUM+COUNT;
COUNT:=COUNT+1;
END LOOP;
AVG:=SUM/(COUNT-1);
DBMS_OUTPUT.PUT_LINE (‗AVERAGE OF 1 TO 10
IS‘||AVG);
END;
O/P:
AVERAGE OF 1 TO 10 IS 5.5
Differences between a basic loop and a WHILE loop:
Basic loop WHILE loop
FOR Loop:
The FOR loop is the simplest loop, unlike the basic and WHILE loop.
There is no need to use an EXIT statement and the counter need not be
declared.
The counter used in the loop is implicitly declared as an integer and it is
destroyed on the loop‘s termination.
General syntax:
FOR counter IN [REVERSE] lower…..upper Loop
Looping statement1;
Looping statement2;
….
Looping statementN;
END Loop
Page | 131
The counter varies from the lower value to the upper value, incrementing
by one with every loop execution.
The loop can also be used with the counter starting at a higher value and
decrementing by one with every loop execution.
The keyword REVERSE is used for varying the counter in the reverse order
or from a higher to a lower value.
Example:
DECLARE
COUNT NUMBER (2);
SUM NUMBER (2):=0;
AVG NUMBER (3, 1);
BEGIN
FOR COUNT IN 1…10 LOOP
SUM:=SUM+COUNT;
END LOOP;
AVG:=SUM/10;
DBMS_OUTPUT.PUT_LINE (‗AVERAGE OF 1 TO 10
IS‘||AVG);
END;
O/P:
AVERAGE OF 1 TO 10 IS 5.5
Nested loops:
A loop within another loop. Loops can be nested to many levels. When the
inner loop ends, it does not automatically end the outer loop. We can end an outer
loop from within the inner loop by labeling each loop and then using the EXIT
statement.
General syntax:
Loop
…..
EXIT WHEN condition;
Loop
…..
EXIT out_loop WHEN condition;
EXIT WHEN condition
END Loop in_loop;
END Loop out_loop;
Page | 132
7.12 NESTED BLOCKS:
PL/SQL block may contain another PL/SQL block, i.e. PL/SQL blocks can
be nested.
The execution starts with the outer block and continues with the inner
block.
The variables declared in the outer block are global to the inner block and
they are accessible in the inner block.
The variables declared in the inner block, are not accessible in the outer
block.
Example:
DECLARE
Var1 NUMBER;
BEGIN
…….
DECLARE
Var2 NUMBER;
BEGIN
……
END;
END;
7.13 SQL IN PL/SQL:
The PL/SQL statements have control structures for calculations, decision
making and iteration.
We need to use SQL to interface with the oracle database when changes are
necessary in the database, SQL can be used to retrieve and change information.
PL/SQL supports all DML statements such as INSERT, UPDATE and
DELETE.
It also supports the Transaction Control Language statements ROLLBACK,
COMMIT and SAVEPOINT.
We can retrieve data using the data retrieval statement SELECT.
More than one row can be retrieved and processed using cursors.
Page | 133
PL/SQL statements can use single row functions, but group functions are
not available for PL/SQL statement. SQL statement in the PL/SQL block can utilize
those group functions.
PL/SQL does not support DDL statement such as CREATE, ALTER and
DROP.
Data Control Language (DCL) statements GRANT and REVOKE are also
not available in PL/SQL.
SELECT statement in PL/SQL:
The SELECT statement retrieves data from oracle tables.
Syntax:
SELECT Column-names
INTO Variable-names/Record-name
FROM table-name
WHERE Condition;
where column-names must contain at least on column, variable-names
must contain a list of local or host variables to hold values retrieved by the SELECT
clause.
All the features of SELECT in SQL are available with an added mandatory
INTO clause.
Attribute %TYPE is used to avoid any data-type mismatches.
The SQL statement in PL/SQL ends with a semicolon (;).
Example:
DECLARE
V_LAST EMP.LNAME%TYPE;
V_FIRST EMP.FNAME%TYPE;
SAL EMP.SALARY%TYPE;
BEGIN
SELECT LNAME, FNAME, SALARY
INTO V_LAST, V_FIRST, SAL
FROM EMP
WHERE EMPID=200;
Page | 134
DBMS_OUTPUT.PUT_LINE (‗EMPNAME:‘ || V_FIRST || ‗ ‘ ||
V_LAST);
DBMS_OUTPUT.PUT_LINE (‗SALARY:‘ || SAL);
END;
O/P:
EMPNAME: JOHN SHAW
SALARY: 25000.
7.14 DATA MANIPULATION IN PL/SQL:
There are three DML statements to manipulate data are,
INSERT statement to add a new row in a table.
DELETE statement to remove a row or rows.
UPDATE statement to change values in a row or rows.
INSERT Statement:
INSERT statement used to add a new employee in the EMP table.
Example:
BEGIN
INSERT INTO EMP
(EMPID, LNAME, FNAME, SALARY, DEPTID)
VALUES
(‗E105‘,‘KUMAR‘,‘SIVA‘,9000,100);
COMMIT;
END;
DELETE Statement:
DELETE statement is used to remove some rows.
Example:
DECLARE
DEPTID DEPT.DEPTID1%TYPE;
BEGIN
SELECT DEPTID1
INTO DEPTID
FROM DEPT
Page | 135
WHERE UPPER(DEPTNAME)=‘&DEPT_NAME‘
DELETE FROM EMP
WHERE DEPTID1=DEPTID;
COMMIT;
END;
7.15 TRANSACTION CONTROL STATEMENT:
All transaction control statements are allowed in PL/SQL, they are
COMMIT statement to commit the current transaction
SAVEPOINT statement to mark a point in transaction
ROLLBACK statement to discard all or past of the transaction.
Page | 136
CHAPTER - 8
PL/SQL CURSORS & EXPECTIONS
8.1 CURSORS:
When we execute an SQL statement from a PL/SQL block, oracle assigns a
private work area for that statement. The work area called a cursor.
Cursor stores the statement and the results returned by execution of that
statement.
A cursor is created either implicitly or explicitly.
Types of cursors:
The cursor in PL/SQL is of two types.
Static cursor
Dynamic cursor
In a static cursor, the contents are known at compile time. The cursor object
for such an SQL statement is always based on one SQL statement.
In a dynamic cursor, a cursor variable that can change its value is used. The
variable can refer to difficult SQL statements at different times.
The static cursors are of two types as well,
1. We do not declare an implicit cursor. PL/SQL declares, manages and
closes it for every DML statement such as INSERT, UPDATE or
DELETE.
2. We declare an explicit cursor when we have an SQL statement in an
PL/SQL block that returns more than one row from an underlying
table.
8.2 IMPLICIT CURSORS:
Implicit cursors are automatically created by Oracle whenever an SQL
statement is executed, when there is no explicit cursor for the statement.
Programmers cannot control the implicit cursors and the information in it.
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.
Page | 137
8.2.1 IMPLICIT CURSOR ATTRIBUTES:
An implicit cursor cannot be opened, fetched from, or closed with a
statement.
The cursor attributes are available for an implicit cursor with the name SQL
as a prefix.
Four attributes for an implicit cursor are,
1. SQL%ISOPEN
2. SQL%FOUND
3. SQL%NOTFOUND
4. SQL%ROWCOUNT
%ISOPEN - Always returns FALSE for implicit cursors, because Oracle
closes the SQL cursor automatically after executing its
associated SQL statement.
Page | 138
We can declare an explicit cursor for a SELECT statement but cannot use an
explicit cursor for DML statements.
Four actions can be performed on an explicit cursor.
1. Declare it
2. Open it
3. Fetch row(s) from it
4. Close it.
Declaring an Explicit cursor:
A cursor is declared as a SELECT statement. The SELECT statement must
not have an INTO clause in a cursor‘s declaration.
If we want to retrieve rows in a specific order into a cursor, an ORDER BY
clause can be used in the SELECT statement.
Syntax:
DECLARE
CURSOR cursor-name IS
SELECT statement;
Where cursor-name is the name of the cursor. The cursor declaration is
done in the DECLARE section of the PL/SQL block.
Example:
DECLARE
CURSOR DEPT_CUR IS
SELECT * FROM DEPT
WHERE DEPTID=20;
BEGIN
Opening a Cursor:
When a cursor is opened, its SELECT query is executed. The active set is
created using all tables in the query; the data retrieved by the SELECT is brought
into the cursor, the cursor points to the first row in the active set.
PL/SQL uses an OPEN statement to open a cursor.
General syntax:
OPEN cursor-name;
Page | 139
For example,
OPEN DEPT_CUR.
Fetching Data from a Cursor:
The SELECT statement creates an active set based on tables in the FROM
clause, column names in the SELECT clause and rows based on conditions in the
WHERE clause.
After reading values from a row into variables, the cursor pointer moves to
the next row in the active set.
General syntax:
FETCH cursor-name INTO variable list/record name;
Where variable list may include a local variable, a table, or a bind variable
and record name is the name of a record structure.
For example:
FETCH employee_cur INTO v_empnum, v_sal
(Or)
FETCH employee_cur INTO emp_rec;
Closing a Cursor:
All operations are done with a cursor, we should close it.
A closed cursor can be reopened again.
If we terminate PL/SQL program without closing an open cursor, it will
not result in an exception. The local cursor declared in a PL/SQL block is closed
automatically, when the block terminates.
Syntax:
CLOSE cursor-name;
For example:
CLOSE Dept_Cur;
8.3.1 EXPLICIT CURSOR ATTRIBUTES:
Actions can be performed on cursors with OPEN, FETCH and CLOSE
statements.
We can get information about the cursor status of a cursor or the result of
the last fetch by using cursor attribute.
Page | 140
The four explicit cursor attributes are,
%NOTFOUND It returns TRUE if the last fetch did not return a row;
otherwise it returns FALSE. It complements the %FOUND
attributes.
Example:
DECLARE
V_LAST EMP.LNAME%TYPE;
V_FIRST EMP.FNAME%TYPE;
V_SAL EMP.SALARY%TYPE;
CURSOR EMP_CUR IS
SELECT LNAME, FNAME, SALARY FROM EMP
WHERE EMPID=20;
BEGIN
IF NOT EMP_CUR%ISOPEN THEN
OPEN EMP_CUR;
END IF;
LOOP
FETCH EMP_CUR
INTO V_LAST, V_FIRST, V_SAL;
EXIT WHEN NOT EMP_CUR%FOUND;
DBMS_OUTPUT.PUT_LINE (V_FIRST ||‘ ‗||V_LAST ||‘
‗||V_SAL);
END LOOP;
DBMS_OUTPUT.PUT_LINE (EMP_CUR%ROWCOUNT);
Page | 141
END;
O/P:
SIVA KUMAR 75000
JAYA PRAKASH 80000
2
8.4 CURSOR FOR LOOPS:
The cursor FOR loop is the easiest way to write a loop foe explicit cursor.
The cursor is opened implicitly when the loop starts. A row is then fetched
into the record from the cursor with every iteration of the loop.
The cursor is closed automatically when the loop ends. FOR loop automates
all the cursor actions.
Syntax:
FOR recordname IN cursorname Loop
Loop statements;
END Loop;
Example:
DECLARE
CURSOR EMP_CUR IS SELECT LNAME, FNAME, SALARY
FROM EMP;
BEGIN
FOR EMP_REC IN EMP_CUR LOOP
IF EMP_REC.SALARY>75000 THEN
DBMS_OUTPUT.PUT_LINE (EMP_REC.FNAME);
DBMS_OUTPUT.PUT_LINE (EMP_REC.LNAME);
DBMS_OUTPUT.PUT_LINE (EMP_REC.SALARY);
END IF;
END LOOP;
END;
8.5 SELECT FOR….UPDATE CURSOR:
When we type a SELECT query, the result is returned without locking any
rows in the table.
Page | 142
We can explicitly lock rows for update before changing them in the
program.
The FOR UPDATE clause is used with the SELECT query for row locking.
The locked rows are not available to other users for DML statements until we
release them with COMMIT or ROLLBACK commands.
Rows that are locked for update do not have to be updated.
Syntax:
CURSOR cursorname IS
SELECT columnnames
FROM tablename(s)
[WHERE condition]
FOR UPDATE [OF columnnames][NOWAIT];
The optional part of a FOR UPDATE clause is OF columnname, which
enables to specify columns to be updated.
The optional word NOWAIT tells if another user has already locked the
table.
If we do not use NOWAIT and one or more rows are already locked by
another user.
8.6 WHERE CURRENT OF CLAUSE:
The WHERE CURRENT OF clause allows to perform data manipulation
only on a recently fetched row.
Syntax:
1. UPDATE tablename
SET clause
WHERE CURRENT OF cursorname;
2. DELETE FROM tablename
WHERE CURRENT OF cursorname;
Page | 143
8.7 CURSOR WITH PARAMETERS:
A cursor can be declared with parameters, which allow to pass values to
the cursor.
These values are passed to the cursor when it is opened and they are used
in the query when it is executed.
Syntax:
CURSOR cursorname
[(Parameter1 datatype, parameter2 datatype, …)] IS
SELECT query;
Example:
DECLARE
V_FIRST EMP.FNAME%TYPE;
V_LAST EMP.LNAME%TYPE;
D_ID NUMBER(2):=&DEPT_ID;
CURSOR EMP_CUR(DEPT_NUM EMP.DEPTID%TYPE) IS
SELECT LNAME, FNAME
FROM EMP
WHERE DEPTID=DEPT_NUM;
BEGIN
OPEN EMP_CUR(D_ID);
DBMS_OUTPUT.PUT_LINE(D_ID);
LOOP
FETCH EMP_CUR INTO V_LAST, V_FIRST;
EXIT WHEN EMP_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (V_FIRST);
END LOOP;
CLOSE EMP_CUR;
END;
Page | 144
8.8 CURSOR VARIABLES:
A cursor variable is a reference to the work area. A cursor is based on one
specific query. We can also use the action statements OPEN, FETCH and CLOSE
with cursor variables.
The cursor attributes %ISOPEN, %FOUND, %NOTFOUND and
%ROWCOUNT are available for cursor variables.
Cursor variable can be used in an assignment statement. A cursor variable
can also be assigned to another cursor variable.
REF Cursor Type:
We can create variable with referenced cursor type.
TYPE cursortypename IS REF CURSOR [RETURN returntype];
cursorname cursortypename;
cursortypename is the name of the type of cursor.
8.9 EXCEPTIONS:
Errors are known as exceptions.
An exception occurs when an unwanted situation arises during the
exception of a program.
Exceptions can result from a system error, a user error, or an application
error.
When an exception occurs, control of the current program block shifts to
another section of the program known as the exception section, to handle
exceptions.
PL/SQL provides ways to trap and handle errors, and it is possible to
create PL/SQL programs with full protection against errors.
General syntax of an exception section,
EXCEPTION
WHEN exceptionname1 [OR exceptionname2, ….] THEN
Executable statements
[WHEN exceptionname3 [OR exceptionname4, ….] THEN
Executable statements]
[WHEN OTHERS THEN
Executable statements]
Page | 145
An exception is handled when the exception name matches the name of the
raised exception.
The exceptions are trapped by name. If an exception is raised but no
handler for it is present, the WHEN OTHERS clause is performed.
There is no handler for an error and no WHEN OTHERS clause, the error
number and associated message are displayed to the user.
8.9.1 TYPES OF EXCEPTIONS:
There are three types of exceptions in PL/SQL:
Predefined Oracle Server Exception
Non Predefined Oracle Server Exception
User-defined Exception
8.9.1.1 Predefined Oracle Server Exceptions:
This exception is named by PL/SQL and is raised implicitly when a
PL/SQL or DBMS error occurs.
These are approximately 20 such exceptions. Each has a name and
associated error number.
The user does not have to declare or raise predefined server exceptions.
Some Examples,
EXCEPTION NAME Error Description
Number
1. NO_DATA_FOUND ORA-01403 Single-row SELECT returned no
data
2. TOO_MANY_ROWS ORA-01422 Single-row SELECT returned
more than one row.
3. ZERO_DIVIDE ORA-01476 Attempted to divide by zero.
4. VALUE_ERROR ORA-06502 Arithmetic, conservation,
truncation.
5. STORAGE_ERROR ORA-06500 PL/SQL ran out of memory (or)
memory is corrupted.
6. LOGIN_DENIED ORA-01017 Logging on to oracle with an
invalid username or password.
Page | 146
7. PROGRAM_ERROR ORA-06501 PL/SQL has an internal
problem.
8. CURSOR_ALREADY_OPEN ORA-06511 Attempted to open an already-
open cursor.
9. INVALID_CURSOR ORA-01001 Illegal cursor operation control.
10. DUP_VAL_ON_INDEX ORA-00001 Attempted to insert a duplicate
value.
11. INVALID_NUMBER ORA-01722 Conversion of a character string
to number failed.
12. ROWTYPE_MISMATCH ORA-06504 Host cursor variable and
PL/SQL cursor variable.
13. TIMEOUT_OV_RESOURCE ORA-00051 Timeout occurred while oracle
is waiting for a resource.
Example program:
DECLARE
V_FIRST EMP.FNAME%TYPE;
V_LAST EMP.LNAME%TYPE;
D_ID NUMBER (2):=&DEPT_ID;
BEGIN
SELECT LNAME, FNAME INTO V_LAST, V_FIRST
FROM EMP WHERE DEPTID-D_ID;
DBMS_OUTPUT.PUT_LINE (‗‗);
DBMS_OUTPUT.PUT_LINE (V_LAST ||‘, ‗||V_FIRST);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE (‗NO SUCH DEPARTMENT WITH
EMPLOYEES‘);
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE (‗MORE THAN ONE EMPLOYEE IN
DEPT‘||D_ID);
END;
Page | 147
8.9.1.2 Non Predefined Oracle Server Exceptions:
A non-predefined oracle server exception has an attached oracle error code,
but it is not named by oracle.
We can trap such exceptions with a WHEN OTHERS clause or by declaring
them with names in the DECLARE section. The declared error is raised implicitly
by oracle.
Pragma Exception_Init:
PRAGMA is a compiler directive that associates an exception name with an
internal oracle error code.
The PRAGMA directive is not processed with the execution of a PL/SQL
block but it directs the PL/SQL compiler to associate a name with the error code.
We can use more than one PRAGMA EXCEPTION_INIT directive in
DECLARE section to assign names to different error codes.
Naming and associating are two separate statements in the declaration
section.
An exception name is declared as an EXCEPTION.
The declared name is associated with an internal error code returned by
SQLCODE with the PRAGMA directive.
Syntax:
exceptionname EXCEPTION;
PRAGMA EXCEPTION_INIT (exceptionname, errornumber);
Example:
DECLARE
emp_remain EXCEPTION;
PRAGMA EXCEPTION_INIT (emp_remain, -2292);
v_deptid dept.Deptid%TYPE:=&p_depenum;
BEGIN
DELETE FROM dept
WHERE Deptid=v_deptid;
COMMIT;
EXCEPTION
Page | 148
WHEN emp_remain THEN
DBMS_OUTPUT.PUT_LINE (‗DEPARTMENT‘||v_deptid);
DBMS_OUTPUT.PUT_LINE (‗cannot be removed-‘);
DBMS_OUTPUT.PUT_LINE (‗Employees in department‘);
END;
O/P:
p_deptnum=10
DEPARTMENT 10 cannot be removed- Employees in department.
8.9.1.3 User-Defined Exceptions:
We are allowed to define exceptions in PL/SQL. We must perform three
steps for exceptions.
1. We must declare the exception in the DECLARE section, there is no need to
use a PRAGMA directive because there is no standard error numbers to
associate.
2. We must raise the exception in the exception section fo the program with an
explicit RAISE statement.
3. We must write the handler for the exception.
Example:
DECLARE
invalid_commision EXCEPTION;
no_commision EXCEPTION;
v_comm employee.commission%TYPE;
BEGIN
SELECT commission INTO v_comm
FROM employee
WHERE Employeeid=&emp_id;
IF v_comm<0 THEN
RAISE invalid_commission;
ELSIF v_comm IS NULL THEN
RAISE no_commission;
ELSE
Page | 149
DBMS_OUTPUT.PUT_LINE (v_comm);
END IF;
EXCEPTION
WHEN invalid_commission THEN
DBMS_OUTPUT.PUT_LINE (‗commission is negative‘);
WHEN no_commission THEN
DBMS_OUTPUT.PUT_LINE (‗No commission value‘);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE (‗No such ID‘);
END;
8.10 RAISE_APPLICATION_ERROR Procedure:
It allows to display nonstandard error codes and user-defined error
messages from a stored subprogram.
Syntax:
RAISE_APPLICATION_ERROR (error-code, error-message [,TRUE/FALSE]);
Page | 150
CHAPTER - 9
COMPOSITE DATA TYPES
Composite data types are groups or collections.
The composite data types are,
RECORDS
TABLE
Nested TABLE
VARRAYS
9.1 PL/SQL RECORDS:
PL/SQL records are similar in structure to a row in a database table.
A record consists of components of any scalar, PL/SQL record, or PL/SQL
table type. These components are known as fields and they have their own values.
PL/SQL records are similar in structure to ―Struct‖ in the C language.
The record does not have a value as a whole, instead, it enables to access
these components as a group.
A PL/SQL record is based on a cursor, a table‘s row.
A record can be explicitly declared based on a cursor or a table.
CURSOR cursorname IS
SELECT query
Recordname cursorname%ROWTYPE;
9.1.1 CREATING A PL/SQL RECORD:
We create a RECORD type first, and then declare a record with that
RECORD type.
Syntax:
TYPE recordtypename IS RECORD
(fieldname1 datatype | variable%TYPE | table.column%TYPE |
table%ROWTYPE[[NOTNULL]:=|DEFAULT Expression]
[fieldname2…..
,fieldname3….);
recordname recordtypename;
Page | 151
For example,
TYPE employee_rectype IS RECORD
(e_last VARCHAR(15),
e_first VARCHAR(15),
e_sal NUMBER(8,2));
employee_rec employee_rectype;
In this declaration, employee_rectype is the user defined RECORD type.
Three fields are included in its structure; e_last, e_first, e_sal.
The record employee_rec is a record declared with the user-defined record
type employee_rectype.
9.1.2 REFERENCING FIELDS IN A RECORD:
A field in a record has a name that is given in the RECORD type definition.
We cannot reference a field by its name only. We must use the record name
as a qualifier.
recordname.fieldname
The recordname and fieldname are joined by a dot (.). We can reference the
e_sal field from the previous declaration as,
employee_rec.e_sal.
We can use a field in an assignment statement to assign a value to it.
For example,
employee_rec.e_sal:=1000;
employee_rec.e_last:=‘Jordan‘;
9.1.3 WORKING WITH RECORDS:
A record is known in the block where it is declared. When the block ends,
the record from columns in a row by using the SELECT statement or the FETCH
statement. The order of fields in a record must match the order of columns in the
row. A record can be assigned to another record if both records have the same
structure. The record declared with %ROWTYPE has the same structure as the
table‘s row.
For example,
emp_rec employee%ROWTYPE;
Page | 152
Example:
DECLARE
TYPE STU_REC_TYPE IS RECORD
(SNAME VARCHAR(15),
SPHONE VARCHAR(15),
SADDR VARCHAR(20));
STU_REC STU_REC_TYPE;
BEGIN
SELECT NAME, PHONE, ADDR INTO STU_REC
FROM STUDENT WHERE STUDENTID=‘&STUDID‘;
DBMS_OUTPUT.PUT_LINE (STU_REC.SNAME ||‘,‘ ||
STU_REC.SPHONE||‘,‘
||STU_REC.SADDR);
END;
9.1.4 NESTED RECORDS:
We can create a nested record by including a record into another record as a
field. The record that contains another record as a field is called the enclosing
record.
For example,
DECLARE
TYPE add_rectype IS RECORD
(first VARCHAR(12),
last VARCHAR(12),
street VARCHAR(25),
city VARCHAR(15));
TYPE all_add_rectype IS RECORD
(home_addr add_rectype,
vocation_addr add_rectype);
add_rec all_add_rectype;
all_add_rectype nestsadd_rectype as a field type.
Page | 153
9.2 PL/SQL TABLES:
A PL/SQL table is a single-dimensional structure with a collection of
elements that store the same type of value. It is like an array in other programming
languages. A table is a dynamic structure that is not constrained.
9.2.1 DECLARING A PL/SQL TABLE:
A PL/SQL TABLE declaration is done in two steps,
1. Declare a PL/SQL table type with a TYPE statement the structure could use
any of the scalar data type.
2. Declare an actual table based on the type declared in the previous step.
Syntax:
TYPE tabletypename IS TABLE OF
datatype | variablename%TYPE | tablename.columnname%TYPE
[NOTNULL] INDEX BY BINARY_INTEGER;
For example,
TYPE dept_table_type IS TABLE OF dept.DeptName%TYPE
INDEX BY BINARY_INTEGER;
TYPE major_table_type IS TABLE OF VARCHAR(50)
INDEX BY BINARY_INTEGER;
We can define the actual table based on the table type declared.
Syntax:
tablename tabletypename;
Example:
dept_table dept_table_type;
major_table major_table_type
9.2.2 PL/SQL TABLE STRUCTURE:
Primary key column Data column
…. …..
1 Sales
2 Marketing
3 Information System
4 Finance
…. ….
Page | 154
It contains a primary key column and a data column. The primary key has
the type BINARY_INTEGER and the data column is any valid type.
There is no limit on the number of elements, but cannot initialize elements
of a table at declaration time.
9.2.3 REFERENCING TABLE ELEMENTS/ROWS:
The rows in a table are referenced in the same way that an element in an
array is referenced. We cannot reference a table by its name only.
We must use the primary key value in a pair of parentheses as its subscript
or index.
tablename (primary key value)
valid assignments for the table‘s row.
deptname_table(5):=‘Human Resources‘;
major_table(100):=v_major.
9.2.4 ASSIGNING VALUES TO ROWS IN A PL/SQL TABLE:
We can assign values to the rows in a table in three ways,
Direct assignment
Assignment in a loop
Aggregate assignment
Direct assignment:
We can assign a value to a row with an assignment statement, this is
preferable if only a few assignments are to be made.
Example,
dept_name(5):=‘Resource‘;
Assignment in a loop:
We can use any of the three PL/SQL loops to assign values to rows in a
table.
Example:
DECLARE
TYPE DATE_TABLE_TYPE IS TABLE OF DATE
INDEX BY BINARY_INTEGER;
SUNDAY_TABLE_TYPE;
Page | 155
V_DAY BINARY_INTEGER:-1;
V_DATE DATE;
V_COUNT NUMBER(3):=1;
BEGIN
V_DATE:=‘01-JAN-2013‘;
WHILE V_COUNT<=365 LOOP
IF UPPER(TO_CHAR(V_DATE,‘DAY‘)) LIKE‘%SUNDAY%‘ THEN
SUNDAY_TABLE(V_DAY):=V_DATE;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SUNDAY_TABLE(V_DAY),
‗MONTH,DD,YYYY‘));
V_DAY:=V_DAY+1;
END IF;
V_COUNT:=V_COUNT+1;
V_DATE:=V_DATE+1;
END LOOP;
END;
Aggregate Assignment:
We can assign a table‘s values to another table. The data types of both
tables must be compatible.
When we assign a table‘s values to another table, the table receiving those
values loses all its previous primary key values as well as its data column values.
9.2.5 BUILT-IN TABLE METHODS:
The built-in table methods are procedures or functions that provide
information about a PL/SQL table.
Page | 156
9.2.6 TABLE OF RECORDS:
The PL/SQL table type is declared with a datatype. We may use a record
type as a table‘s datatype.
When a table is based on a record, the record must consists of fields with
scalar datatype.
The record must not contain a nested record.
Examples:
A PL/SQL table type based on a programmer-defined record:
TYPE stud_rec_type IS
RECORD (stu_id NUMBER (3), stu_name VARCHAR(30));
TYPE stud_table_type IS TABLE OF stud_rec_type
INDEX BY BINARY_INTEGER;
student_table stud_table_type;
A PL/SQL table type based on a database table,
TYPE emp_table_type IS TABLE OF employee%ROWTYPE
INDEX BY BINARY_INTEGER;
Employee_table emp_table_type.
The fields of a PL/SQL table based on a record are referenced with
following syntax:
tablename (index).fieldname
Example:
Student_table (10).stu_name:=‘kannan‘;
9.3 PL/SQL VARRAYS:
Varray stands for variable-size array. They are single-dimensional,
bounded collection of elements with the same datatype.
They retain their ordering and subscripts when stored in and retrieved
from a database table.
They are similar to a PL/SQL table, and each element is assigned a
subscript/index starting with 1.
Page | 157
A PL/SQL VARRAY declaration is done in two steps,
1. Declare a PL/SQL VARRAY type with a TYPE statement. The TYPE
declaration includes a size to set the upper bound of a Varray. The lower
bound is always one.
2. Declare an actual Varray based on the type declared in the previous step.
Syntax:
DECLARE
TYPE varraytypename IS VARRAY (size) OF ElementType
varrayname varraytypename;
For example,
DECLARE
TYPE Lname_varray_type IS VARRAY(5) OF employee.Lname%TYPE;
Lname_varray Lname_vaary_type:=Lname_varray_TYPE();
Example program for VARRAY:
DECLARE
CURSOR COURSE_CUR IS
ELECT COURSEID FROM COURSE WHERE ROWNUM<=3;
TYPE COURSEID_VARRAY_TYPE IS VARRAY (10) OF
COURSE.COURSEID%TYPE;
COURSEID_VARRAY COURSEID_VARRAY_TYPE:=
COURSEID_VARRAY_TYPE();
VCOUNT NUMBER (1):=1;
BEGIN
FOR CURSOR_REC IN COURSE_CUR LOOP
COURSEID_VARRAY.EXTEND;
COURSEID_VARRAY (V_COUNT):=COURSE_REC.COURSEID;
DBMS_OUTPUT.PUT_LINE (‗courseid (‘|| V_COUNT ||‘).‘||
COURSEID_VARRAY (V_COUNT));
V_COUNT:=V_COUNT+1;
END LOOP;
Page | 158
DBMS_OUTPUT.PUT_LINE (‗NO OF ELEMENTS:‘ ||
COURSEID_VARRAY.COUNT);
DBMS_OUTPUT.PUT_LINE (‗LIMIT OF ELEMENTS:‘ ||
COURSEID_VARRAY.LIMIT);
DBMS_OUTPUT.PUT_LINE (‗FIRST ELEMENT:‘ ||
COURSEID_VARRAY.FIRST);
DBMS_OUTPUT.PUT_LINE (‗LAST ELEMENT:‘ ||
COURSEID_VARRAY.LAST);
END;
O/P:
courseid (1):=EN100
courseid (2):=LA123
courseid (3):=CIS253
NO OF ELEMENTS: 3
LIMIT OF ELEMENTS: 10
FIRST ELEMENT: 1
LAST ELEMENT: 3
Page | 159
CHAPTER - 10
NAMED BLOCKS
10.1 PROCEDURES:
A procedure is a named PL/SQL program block that can perform one or
more tasks. A procedure is the building block of modular programming.
Syntax:
CREATE [OR REPLACE] PROCEDURE procedurename
[(parameter1 [,parameter2….])]
IS
[constant/variable declarations]
BEGIN
Executable statements
[EXCEPTION
Exception handling statements]
END [procedurename];
Where procedurename is a user-supplied name.
Parameter list has the names of parameters passed to the procedure by
calling program as well as the information passed from the procedure to the calling
program.
The local variables, constants are declared after the reserved word IS.
The executable statements are written after BEGIN and before EXCEPTION
or END.
The reserved word EXCEPTION and the exception handling statements are
optional.
10.1.1 CALLING A PROCEDURE:
The procedure is called by specifying its name along with the list of
parameters in parenthesis. The call statement ends with a semicolon (;).
Syntax:
procedurename [(parameter1,……)];
Example:
monthly_salary (v_salary);
Page | 160
10.1.2 PROCEDURE HEADER:
The procedure definition that comes before the reserved word it is called
the procedure header. The procedure header contains the name of the procedure
and the parameter list with data types.
Example:
CREATE OR REPLACE PROCEDURE monthly_sal
(v_sal_in IN emp.salary%TYPE)
10.1.3 PROCEDURE BODY:
The procedure body contains declaration, executable and
exception_handling sections.
The declaration and exception_handling sections are optional. The
executable section contains action statements and it must contain at least one.
The procedure body starts after the reserved word IS.
10.4.4 PARAMETERS:
Parameters are used to pass values back and from the calling environment
to oracle server.
The values passed are processed and returned with a procedure execution.
There are three types of parameters,
IN : Passes a value into the program, read only type of value, it cannot be changed.
OUT : Passes a value back from the program, write only type of value, we cannot
assign a default values.
INOUT: Passes a value in and returns a value back, value is read and then written.
Actual and Formal Parameters:
The parameters passed in a call statement are called the actual parameter.
The parameter names in the header of a module are called the formal
parameters. The actual parameters and their matching formal parameters must
have the same data type.
In a procedure call, the parameters are passed without data types. The
procedure header contains formal parameters with data types.
Procedure call
SEARCH_EMP (543, LAST)
Page | 161
Procedure Header
PROCEDURE SEARCH_EMP (EMPNO IN NUMBER, LAST OUT
VARCHAR2);
Procedure with parameters:
Program:
CREATE OR REPLACE PROCEDURE EMP1
(EMPID IN NUMBER,
O_LAST OUT VARCHAR,
O_FIRST OUT VARCHAR)
IS
BEGIN
SELECT LNAME, FNAME INTO O_LAST, O_FIRST
FROM EMP WHERE EMPID1=1_EMPID;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE (‗Empid:‘|| 1_EMPID)
END EMP1;
10.2 FUNCTIONS:
A function is a named PL/SQL block like a procedure. It is also a stored
block.
The main difference between a function and a procedure is that a function
always returns a value to the calling block.
A function is characterized as follows,
A function can be passed zero or more parameters.
A function must have an explicit RETURN statement in the
executable section to return a value.
The data type of the return value must be declared in the function‘s
header.
A function cannot be executed as a stand-alone program.
A function may have parameters of the IN, OUT and INOUT types, but the
primary use of a function is to return a value with an explicit RETURN statement.
Page | 162
General syntax:
CREATE [OR REPLACE] FUNCTION functionname
[(parameter1 [,parameter2…..])]
RETURN DataType
IS
[constant/variable declarations]
BEGIN
Executable statements
RETURN returnvalue
[EXCEPTION
Exception_handling statements
RETURN returnvalue]
END [functionname];
The body may contain more than one RETURN statement but only one is
executed with each function call.
10.2.1 FUNCTION HEADER:
The function header comes before the reserved word IS.
The header contains the name of the function, the list of parameters and the
RETURN data type.
10.2.2 FUNCTION BODY:
The body of a function must contain at least one executable statement.
If there is no declaration, the reserved word BEGIN follows IS.
If there is no exception handler, we can omit the word EXCEPTION.
RETURN Data Types:
A function can return a value with a scalar data type such as VARCHAR2,
NUMBER, BINARY_INTEGER or BOOLEAN.
It can also return a composite (or) complex data types such as, PL/SQL
table, a PL/SQL record, VARRAY (or) LOB.
Page | 163
10.2.3 CALLING A FUNCTION:
A function call is similar to procedure call. Call a function by mentioning its
name along with its parameters.
The parameter list is enclosed within parentheses. A function does return a
value, so the function call is made via an executable statement such as assignment,
selection or output statement.
For example,
v_sal:=get_sal (&emp_id);
IF emp_exists (v_empid)…….
In the first example,
The function get_sal is called from an assignment statement with the
substitution variable emp_id as its actual parameter.
The function returns the employee‘s salary, which is assigned to the
variable v_sal.
In the second example,
The function call to the function emp_exists is made from an IF statement,
the function searches for the employee and returns a Boolean TRUE or FALSE to
the statement.
Example program:
` CREATE OR REPLACE FUNCTION Get_DeptName
(I_DEPTID IN NUMBER)
RETURN VARCHAR2
IS
V_DEPTNAME VARCHAR (12);
BEGIN
SELECT DEPTNAME INTO V_DEPTNAME
FROM DEPT WHERE DEPTID=I_DEPTID;
RETURN V_DEPTNAME;
END GET_DEPTNAME;
/
Function created.
Page | 164
10.2.4 FUNCTION CALL:
DECLARE
V_DEPTID EMP.DEPTID%TYPE;
V_NAME VARCHAR (12);
V_EMPID EMP.EMPID%TYPE:=&EMP_ID;
BEGIN
SELECT DEPTID INTO V_DEPTID FROM EMP
WHERE EMPID=V_EMPID;
V_DEPT_NAME:=GET_DEPTNAME (V_DEPTID);
DBMS_OUTPUT.PUT_LINE (‗Employee:‘||V_EMPID);
DBMS_OUTPUT.PUT_LINE (‗Department Name:‘||V_NAME);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE (V_EMPID||‘Not found‘);
END;
/
O/P:
Enter value for emp_id: 200
Employee: 200
Department Name: Sales.
10.3 PACKAGES:
A package is a collection of PL/SQL objects. The objects in a package are
grouped within BEGIN and END blocks.
A package may contain objects from the following list:
Cursors
Scalar variable
Composite variables
Constants
Exception names
TYPE declarations for records and tables
Procedures
Functions
Page | 165
Packages are modular in nature, and oracle has many built-in packages.
DBMS_OUTPUT is a built-in package.
A package STANDARD contains definitions of many operators used in
oracle. The objects in a package can be declared as public objects which can be
referenced from outside (or) as private objects which are known only to the
package.
A package compiles successfully even without a body if the specification
compiles. When an object in the package is referenced for the first time, the entire
package is loaded into memory.
The package also follows top-down design.
10.3.1 STRUCTURE OF A PACKAGE:
A package provides an extra layer to a module. A module has a header and
a body, whereas a package has a specification and a body.
A module‘s header specifies the name and the parameter which tells how to
call that module.
Similarly, the package specification tells how to call different modules
within a package.
10.3.2 PACKAGE SPECIFICATION:
A package specification does not contain any code, but it does contain
information about the elements of the package. It contains definitions of functions
and procedures, declarations of global (or) public variables and anything else that
can declared in a PL/SQL block‘s declaration section. The objects in the
specification section of a package are called public objects.
Syntax:
CREATE [OR REPLACE] PACKAGE packagename
IS
[constant, variable and type declaration]
[exception declarations]
[cursor specifications]
[function specifications]
[procedure specifications]
END [packagename];
Page | 166
Example:
PACKAGE bb_team
IS tot_players CONSTANT INTEGER:=12;
player EXCEPTION;
FUNCTION team_avg (points IN NUMBER, players IN NUMBER)
RETURN NUMBER;
END bb_team;
Program:
CREATE OR REPLACE PACKAGE COURSE_INFO
AS
PROCEDURE FIND_TITLE
(I_ID IN COURSE.COURSEID%TYPE, O_TITLE OUT COURSE.TITLE%TYPE);
FUNCTION HAS_PREREQ
(I_ID IN COURSE.COURSEID%TYPE)
RETURN BOOLEAN;
END COURSE_INFO;
10.3.3 PACKAGE BODY:
A package body contains actual programming code foe the modules
described in the specification section.
The module code in the body without a description in the specification is
called a private module or a hidden module and it is not visible outside the body of
the package.
Syntax:
PACKAGE BODY packagename
IS
[variable and type declarations]
[cursor specifications and SELECT queries]
[header and body of functions]
[header and body of procedures]
[BEGIN
executable statements]
[EXCEPTION
exception handlers]
END [packagename];
Page | 167
Rules for writing a package‟s body:
1. The variables, constants, exceptions and so on declared in the specification
must not be declared again in the package body.
2. The number of cursor and module definitions in the specification must
match the number of cursor and module headers in the body.
3. Any elements declared in the specification can be referenced in the body.
Program for package body:
CREATE OR REPLACE PACKAGE BODY COURSE_INFO AS
PROCEDURE FIND_TITLE
(I_ID IN COURSE.COURSEID%TYPR,
O_TITLE OUT COURSE.TITLE%TYPE) IS
BEGIN
SELECT TITLE INTO O_TITLE FROM COURSE WHERE
COURSEID=I_ID;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE (I_ID||‘Not found‘);
END FIND_TITLE;
FUNCTION HAS_PREREQ
(I_ID IN COURSE.COURSEID%TYPE) RETURN BOOLEAN
IS
V_PREQ VARCHAR (6);
BEGIN
SELECT NVL (PREREQ, ‗NONE‘) INTO V_PREQ
FROM COURSE WHERE COURSEID=I_ID;
IF V_PREQ=‘NONE‘ THEN
DBMS_OUTPUT.PUT_LINE (‗No prerequisite‘);
RETURN FALSE;
ELSE RETURN TRUE;
END IF;
EXCEPTION
Page | 168
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINT (‗Course:‘||I_ID||‘does not exist‘);
RETURN FALSE;
END HAS_PREREQ;
10.4 TRIGGERS:
A database trigger or trigger is a PL/SQL block. It is stored in the database
and is called automatically when a triggering event occurs.
A user cannot call a trigger explicitly. The triggering event is based on a
Data Manipulation Language statements, such as INSERT, UPDAT or DELETE.
A trigger can be created to fire before or after the triggering event.
For example,
If we design a trigger to execute after INSERT a new employee in the EMP
table, the trigger executes after the INSERT statement.
The execution of a trigger is also known as firing the trigger.
Syntax:
CREATE [OR REPLACE] TRIGGER triggername
BEFORE / AFTER / INSTEAD OF triggeringevent ON table/view
[For Each Row]
[WHEN condition]
DECLARE
Declaration statements
BEGIN
Executable statements
EXCEPTION
Exception_handling statements
END;
Where CREATE means creating a new trigger and REPLACE means
replacing an existing trigger.
The keyword REPLACE is optional and only uses it to modify a trigger.
Page | 169
10.4.1 USES OF TRIGGER:
Generating values for derived columns.
Keeping track of table access.
Preventing invalid entries.
Performing validity checks or maintaining security.
10.4.2 RESTRICTIONS FOR CREATING A TRIGGER:
A trigger cannot use a Transaction Control Language statement such as
COMMIT, ROLLBACK or SAVEPOINT.
A procedure or function called by a trigger cannot perform Transaction
Control Language statements.
A variable in a trigger cannot be declared with LONG or LONG RAW
data types.
10.4.3 BEFORE Triggers:
The BEFORE trigger is fired before execution of a DML statement.
BEFORE trigger is useful when we want to plug into some values in a new
row, insert a calculated column into a new row.
Example:
CREATE OR RTEPLACE TRIGGER EMP_TRIG
BEFORE INSERT ON EMP
FOR EACH ROW
DECLARE
V_EMPID EMP.EMPID%TYPE;
BEGIN
SELECT EMPID_SRQ.NEXTVAL
INTO V_EMPID FROM DUAL;
:NEW.EMPID:=V_EMPID;
:NEW.HIREDDATE:=SYSDATE;
END;
O/P:
SQL> INSERT INTO EMP (EMPID, NAME, POSID, SUPERVISOR, SALARY)
VALUES (546, SONIA, 3, 543, 10000);
Page | 170
SELECT * FROM EMP;
EMPID NAME POSID SUPERVISOR SALARY
546 SONIA 3 543 10000.
10.4.4 AFTER Triggers:
An AFTER trigger fires after a DML statement is executed. It utilizes the
built-in Boolean functions INSERTING, UPDATING and DELETING.
If the triggering event is one of the three DML statement returns TRUE and
other two return FALSE.
For example,
If the current DML statement is INSERT then INSERTING returns TRUE,
but DELETING and UPDATING return FALSE.
Program:
CREATE OR REPLACE TRIGGER EMP_TRIG1
AFTER DELETE OR UPDATE ON EMPLOYEE
DECLARE
V_TRANTYPE VARCHAR (10);
BEGIN
IF DELETING THEN V_TRANTYPE:=‘DELETE‘;
ELSIF UPDATING THEN V_TRANTYPE:=‘UPDATE‘;
END IF;
INSERT INTO TRANSHISTORY VALUES (‗EMPLOYEE‘,
V_TRANTYPE,
USER, SYSDATE);
END;
/
Trigger created.
O/P:
SQL>DELETE FROM EMP WHERE UPPER (NAME)=‘VIQUEZ‘;
SELECT * FROM TRANSHISTORY;
TABLENAME TRANTYPE USER_NAME TRAN_DATE
EMPLOYEE DELETE NITHYA 05-FEB-10.
Page | 171
10.4.5 INSTEAD OF Trigger:
The BEFORE and AFTER triggers are based on database tables. From
version 8i onwards, oracle provides another type of trigger called an INSTEAD OF
trigger which is not based on table but is based on a view.
Example:
CREATE OR REPLACE TRIGGER facultytrig
INSTEAD OF DELETE ON STUDENT
FOR EACH ROW
BEGIN
DELETE FROM Faculty
WHERE FacultyId=:OLD.FacultyId;
END;
10.5 DATA DICTIONARY VIEWS:
Oracle maintains a very informative Data Dictionary. A few Data
Dictionary Views are useful for getting information about stored PL/SQL blocks.
Examples:
USER_PROCEDURES (for named block)
USER_TRIGGERS (for trigger only)
USER_SOURCE (for all source code)
USER_OBJECTS (for any objects)
USER_ERRORS (for current errors).
SELECT Object_Name, Procedure_Name FROM USER_PROCEDURES;
SELECT Name, Type, Line, Text FROM USER_SOURCE;
Page | 172
REFERENCES
Websites:
1. https://en.wikipedia.org/wiki/Category:Database_management_systems
2. https://www.tutorialspoint.com/dbms/index.htm
3. https://www.w3schools.in/dbms/
4. https://www.w3schools.com/sql/sql_intro.asp
5. https://www.w3schools.in/dbms/intro/
6. https://www.tutorialspoint.com/plsql/index.htm
7. https://docs.oracle.com/database/121/TTPLS/toc.htm
Page | 173