RDBMS
RDBMS
2
[
Relational database management systems
rajanna ]
Unit-I
DBMS (Database Management Systems)
Introduction:
Database-System Applications:
1. Enterprise Information
3
[
Relational database management systems
rajanna ]
Data redundancy and inconsistency: Since different programmers create the files and application
programs over a long period, the various files are likely to have different structures and the programs
may be written in several programming languages. Moreover, the same information may be duplicated
in several places (files).
Data isolation: Because data are scattered in various files, and files may be in different formats,
writing new application programs to retrieve the appropriate data is difficult.
Integrity problems: The data values stored in the database must satisfy certain types of consistency
constraints. Suppose the university maintains an account for each department, and records the balance
amount in each account. Suppose also that the university requires that the account balance of a
department may never fall below zero. Developers enforce these constraints in the system by adding
appropriate code in the various application programs.
Concurrent-access anomalies: For the sake of overall performance of the system and faster response,
many systems allow multiple users to update the data simultaneously. Indeed, today, the largest Internet
retailers may have millions of accesses per day to their data by shoppers. In such an environment,
interaction of concurrent updates is possible and may result in inconsistent data.
Security problems: Not every user of the database system should be able to access all the data. For
example, in a university, payroll personnel need to see only that part of the database that has financial
information. They do not need access to information about academic records. But, since application
programs are added to the file-processing system in an ad hoc manner, enforcing such security
constraints is difficult.
FILE ORIENTED APPROACH:
The earliest business computer systems were used to process business records and produce
information. They were generally faster and more accurate than equivalent manual systems. These
systems stored groups of records in separate files, and so they were called file
Processing systems.
1. File system is a collection of data. Any management with the file system, user has to write
the procedures
[Link] system gives the details of the data representation and Storage of data.
[Link] File system storing and retrieving of data cannot be done efficiently.
4. Concurrent access to the data in the file system has many problems like a Reading the file
while other deleting some information, updating some information
[Link] system doesn’t provide crash recovery mechanism.
Eg. While we are entering some data into the file if System crashes then content of the file is
lost.
4
[
Relational database management systems
rajanna ]
5
[
Relational database management systems
rajanna ]
Programmers wrote these application programs to meet the needs of the bank. New application
programs are added to the system as the need arises. For example, suppose that the savings bank
decides to offer checking accounts.
As a result, the bank creates new permanent files that contain information about all the checking
accounts maintained in the bank, and it may have to write new application programs to deal with
situations that do not arise in savings accounts, such as overdrafts. Thus, as time goes by, the system
acquires more files and more application programs. 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. Before
database management systems (DBMS) came along, organizations usually stored information in such
systems. Organizational information in a file-processing system has a number of major disadvantages:
1. Data Redundancy and Inconsistency:
The address and telephone number of a particular customer may appear in a file that consists of
savings-account records and in a file that consists of checking-account records. This redundancy leads
to higher storage and access cost. In, it may lead to data inconsistency; that is, the various copies of the
same data may no longer agree. For example, a changed customer address may be reflected in savings-
account records but not elsewhere in the system.
2. Difficulty in Accessing Data:
Suppose that one of the bank officers 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. Because there is no application program to generate that. The bank officer has now two choices:
either obtain the list of all customers and extract the needed information manually or ask a system
programmer to write the necessary application program. Both alternatives are obviously unsatisfactory.
3. Data Isolation:
Because data are scattered in various files and files may be in different formats, writing new
application programs to retrieve the appropriate data is difficult.
4. Integrity Problems:
The balance of a bank account may never fall below a prescribed amount (say, $25).
Developers enforce these constraints in the system by adding appropriate code in the various
application programs. However, when new constraints are added, it is difficult to change the programs
to enforce them. The problem is compounded when constraints involve several data items from
different files.
5. Atomicity Problems:
A computer system, like any other mechanical or electrical device, 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. Consider 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, resulting in an inconsistent database state. Clearly, it is essential to
database consistency that either both the credit and debit occur, or that neither occur. That is, the funds
transfer must be atomic—it must happen in its entirety or not at all. It is difficult to ensure atomicity in
a conventional file-processing system.
6. Concurrent-Access Anomalies:
For the sake of overall performance of the system and faster response, many systems allow
multiple users to update the data simultaneously. In such an environment, interaction of concurrent
6
[
Relational database management systems
rajanna ]
updates may result in inconsistent data. Consider bank account A, containing $500. If two customers
withdraw funds (say $50 and $100 respectively) from account A at about the same time, the result of
the concurrent executions may leave the account in an incorrect (or inconsistent) state. Suppose that the
programs executing on behalf of each withdrawal read the old balance, reduce that value by the amount
being withdrawn, and write the result back. If the two programs run concurrently, they may both read
the value $500, and write back $450 and $400, respectively. Depending on which one writes the value
last, the account may contain $450 or $400, rather than the correct value of $350.
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 that have not been coordinated previously.
7. Security Problems:
Not every user of the database system should be able to access all the data. For example, in a
banking system, payroll personnel 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,
since application programs are added to the system in an ad hoc manner, enforcing such security
constraints is difficult. These difficulties, among others, prompted the development of database
systems.
The data models are as follows:
7
[
Relational database management systems
rajanna ]
In this the main concept of many-many relationships got introduced. But this also followed the
same technology of pointers to define relationships with a difference in this made in the introduction if
grouping of data items as sets.
In order to overcome all the drawbacks of the previous systems, the Relational Database
System got introduced in which data get organized as tables and each record forms a row with many
fields or attributes in it. Relationships between tables are also formed in this system.
8
[
Relational database management systems
rajanna ]
Database Administrator:
One of the main reasons for using DBMSs is 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).
Database Administrator Functions/Roles:
• Schema definition. The DBA creates the original database schema by executing a set of data
definition statements in the DDL.
• Storage structure and access-method definition.
• Schema and physical-organization modification. TheDBAcarries out changes to the schema and
physical organization to reflect the 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. Examples of the database administrator’s routine maintenance activities are:
◦ Periodically backing up the database, either onto tapes or onto remote servers, to prevent loss of data
in case of
disasters such as flooding.
◦ Ensuring that enough free disk space is available for normal operations, and upgrading disk space as
required.
◦ Monitoring jobs running on the database and ensuring that performan
Database Architecture:
The architecture of a database system is greatly influenced by the underlying computer system
on which it runs, in particular by such aspects of computer architecture as networking, parallelism, and
distribution:
•Networking of computers allows some tasks to be executed on a server system and some tasks to be
executed on client systems. This division of work has led to client –server database systems.
•Parallel processing within a computer system allows database-system activities to be speeded up,
allowing faster response to transactions, as well as more transactions per second. Queries can be
processed in a way that exploits the parallelism offered by the underlying computer system. The need
for parallel query processing has led to parallel database systems.
•Distributing data across sites in an organization allows those data to reside where they are generated
or most needed, but still to be accessible from other sites and from other departments. Keeping
multiple copies of the database across different sites also allows large organizations to continue their
database operations even when one site is affected by a natural disaster, such as flood, fire, or
earthquake. Distributed database systems handle geographically or administratively
9
[
Relational database management systems
rajanna ]
In two-tier architecture, the application resides at the client machine, where it invokes database
system functionality at the server machine through query language statements. Application program
interface standards are used for interaction between the client and the server.
In contrast, in three-tier architecture, the client machine acts as merely a front end and does not
contain any direct database calls. Instead, the client end communicates with an application server,
usually through a forms interface. The application server in turn communicates with a database system
to access data. Three-tier applications are more appropriate for large applications, and for applications
that run on the World Wide Web.
Three Level Architecture:
For the system to be usable, it must retrieve data efficiently. The need for efficiency has led
designers to use complex data structures to represent data in the database. Since many database-system
users are not computer trained, developers hide the complexity from users through several levels of
abstraction, to simplify users interactions with the system:
• Physical level. The lowest level of abstraction describes how the data are actually stored.
The physical level 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 relationships exist among those data. The logical level thus describes the entire database in terms
of a small number of relatively simple structures. Although implementation of the simple structures at
the logical level may involve complex physical-level structures, the user of the logical level does not
need to be aware of this complexity. This is referred to as physical data independence. Database
administrators, who 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. Even though
the logical level uses simpler structures, complexity remains because of the variety of information
stored in a large database. Many users of the database system do not need all this information; instead,
they need to access only a part of the database. The view level of abstraction exists to simplify their
interaction with the system. The system may provide many views for the same database.
10
[
Relational database management systems
rajanna ]
Key constraints
Domain constraints
Referential integrity constraints
Key Constraints:
There must be at least one minimal subset of attributes in the relation, which can identify a tuple
uniquely. This minimal subset of attributes is called key for that relation. If there are more than one
such minimal subsets, these are called candidate keys.
Key constraints force that −
in a relation with a key attribute, no two tuples can have identical values for key attributes.
11
[
Relational database management systems
rajanna ]
UNION: It combines all of the rows in one table with all of the rows in another table except for the
duplicate tuples. The tables are required to have the same attribute characteristics for the Union
command to work. The tables must be union-compatible which means that two tables being used have
the same amount of columns and the columns have the same names, and also need to share the same
domain.
INTERSECT is the second SQL command that takes two tables and combines only the rows that
appear in both tables. The tables must be union-compatible to be able to use the Intersect command or
else it won't work.
DIFFERENCE in another SQL command that gets all rows in one table that are not found in the other
table. Basically it subracts one table from the other table to leave only the attributes that are not the
same in both tables. For this command to work both tables must be union-compatible.
PRODUCT command would show all possible pairs of rows from both tables being used. This
command can also be referred to as the Cartesian Product.
SELECT is the command to show all rows in a table. It can be used to select only specific data from
the table that meets certain criteria. This command is also referred to as the Restrict command.
PROJECT is the command that gives all values for certian attributes specified after the command. It
shows a vertical view of the given table.
JOIN takes two or more tables and combines them into one table. This can be used in combination
with other commands to get specific information. There are several types of the Join command. The
Natural Join, Equijion, Theta Join, Left Outer Join and Right Outer Join.
DIVIDE has specific requirements of the table. One of the tables can only have one column and the
other table must have two columns only.
The Entity-Relationship Model:
The entity-relationship (E-R) data model uses a collection of basic objects, called entities, and
relationships among these objects. An entity is a “thing” or “object” in the real world that is
distinguishable from other objects. For example, each person is an entity, and bank accounts can be
considered as entities.
Entities are described in a database by a set of attributes. For example, the attributes dept
name, building, and budget may describe one particular department in a university, and they form
12
[
Relational database management systems
rajanna ]
attributes of the department entity set. Similarly, attributes ID, name, and salary may describe an
instructor entity.
Arelationship is an association among several entities. For example, a member relationship
associates an instructor with her department. The set of all entities of the same type and the set of all
relationships of the same type are termed an entity set and relationship set, respectively.
The overall logical structure (schema) of a database can be expressed graphically by an entity-
relationship (E-R) diagram. There are several ways in which to draw these diagrams. One of the most
popular is to use the Unified Modeling Language (UML). In the notation we use, which is based on
UML, an E-R diagram is represented as follows:
1. Entity
An entity is an object or component of data. An entity is represented as rectangle in an ER diagram.
For example: In the following ER diagram we have two entities Student and College and these two
entities have many to one relationship as many students study in a single college. We will read more
about relationships later, for now focus on entities.
Strong Entity: which is an entity that cannot depend others so it is called strong [Link] is represented
by rectangle box.
Ex:
ENTITY
Weak Entity:
An entity that cannot be uniquely identified by its own attributes and relies on the relationship
with other entity is called weak entity. The weak entity is represented by a double rectangle.
For example – a bank account cannot be uniquely identified without knowing the bank to which the
account belongs, so bank account is a weak entity.
WEAKENTITY
Attributes:
Entities are represented by means of their properties, called attributes. All attributes have
values.
For example, a student entity may have name, class, and age as attributes. they are represented by
Ellipse of oval
There exists a domain or range of values that can be assigned to attributes. For example, a
student's name cannot be a numeric value. It has to be alphabetic. A student's age cannot be negative,
etc.
Types of Attributes
Simple attribute − Simple attributes are atomic values, which cannot be divided further.
For example, a student's phone number is an atomic value of 10 digits.
9441170915
13
[
Relational database management systems
rajanna ]
Composite attribute − Composite attributes are made of more than one simple attribute.
For example, a student's complete name may have first_name and last_name.
Derived attribute − Derived attributes are the attributes that do not exist in the physical
database, but their values are derived from other attributes present in the database.
For example, average_salary in a department should not be saved directly in the
database, instead it can be derived. For another example, age can be derived from
data_of_birth.
Multi-value attribute − Multi-value attributes may contain more than one values.
For example, a person can have more than one phone number, email_address, etc.
Relationship:
The association among entities is called a relationship. For example, an employee works_at a
department, a student enrolls in a course. Here, Works_at and Enrolls are called relationships.
Relationship Set:
A set of relationships of similar type is called a relationship set. Like entities, a relationship too
can have attributes. These attributes are called descriptive attributes.
Degree of Relationship:
Degree of Relationship:
The number of participating entities in a relationship defines the degree of the relationship.
Binary = degree 2
Ternary = degree 3
n-ary = degree
Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be associated with the number
of entities of other set via relationship set.
One-to-one − One entity from entity set A can be associated with at most one entity of entity
set B and vice versa.
14
[
Relational database management systems
rajanna ]
One-to-many − One entity from entity set A can be associated with more than one entities of
entity set B however an entity from entity set B, can be associated with at most one entity.
Many-to-one − More than one entities from entity set A can be associated with at most one
entity of entity set B, however an entity from entity set B can be associated with more than one
entity from entity set A.
Many-to-many − One entity from A can be associated with more than one entity from B and
vice versa.
15
[
Relational database management systems
rajanna ]
ER Diagram Representation
Let us now learn how the ER Model is represented by means of an ER diagram. Any object, for
example, entities, attributes of an entity, relationship sets, and attributes of relationship sets, can be
represented with the help of an ER diagram.
Entity:
Entities are represented by means of rectangles. Rectangles are named with the entity set they
represent.
Attributes
Attributes are the properties of entities. Attributes are represented by means of ellipses. Every ellipse
represents one attribute and is directly connected to its entity (rectangle).
If the attributes are composite, they are further divided in a tree like structure. Every node is then
connected to its attribute. That is, composite attributes are represented by ellipses that are connected
with an ellipse.
16
[
Relational database management systems
rajanna ]
17
[
Relational database management systems
rajanna ]
Relationship:
Relationships are represented by diamond-shaped box. Name of the relationship is written inside the
diamond-box. All the entities (rectangles) participating in a relationship, are connected to it by a line.
Binary Relationship and Cardinality:
A relationship where two entities are participating is called a binary relationship. Cardinality is the
number of instance of an entity from a relation that can be associated with the relation.
One-to-one − When only one instance of an entity is associated with the relationship, it is
marked as '1:1'. The following image reflects that only one instance of each entity should be
associated with the relationship. It depicts one-to-one relationship.
18
[
Relational database management systems
rajanna ]
One-to-many − When more than one instance of an entity is associated with a relationship, it
is marked as '1:N'. The following image reflects that only one instance of entity on the left and
more than one instance of an entity on the right can be associated with the relationship. It
depicts one-to-many relationship.
Many-to-one − When more than one instance of entity is associated with the relationship, it is
marked as 'N:1'. The following image reflects that more than one instance of an entity on the
left and only one instance of an entity on the right can be associated with the relationship. It
depicts many-to-one relationship.
Many-to-many − The following image reflects that more than one instance of an entity on the
left and more than one instance of an entity on the right can be associated with the
relationship. It depicts many-to-many relationship.
19
[
Relational database management systems
rajanna ]
Generalization Aggregation
The ER Model has the power of expressing database entities in a conceptual hierarchical
manner. As the hierarchy goes up, it generalizes the view of entities, and as we go deep in the
hierarchy, it gives us the detail of every entity included.
Going up in this structure is called generalization, where entities are clubbed together to represent a
more generalized view. For example, a particular student named Mira can be generalized along with
all the students. The entity shall be a student, and further, the student is a person. The reverse is
called specialization where a person is a student, and that student is Mira.
Generalization:
As mentioned above, the process of generalizing entities, where the generalized entities contain the
properties of all the generalized entities, is called generalization. In generalization, a number of
entities are brought together into one generalized entity based on their similar characteristics. For
example, pigeon, house sparrow, crow and dove can all be generalized as Birds.
20
[
Relational database management systems
rajanna ]
There are several processes and algorithms available to convert ER Diagrams into Relational Schema.
Some of them are automated and some of them are manual. We may focus here on the mapping
diagram contents to relational basics.
ER diagrams mainly comprise of −
Mapping Entity:
An entity is a real-world object with some attributes.
Unit-II
Relational Database Integrity:
21
[
Relational database management systems
rajanna ]
The keys:
Types of keys in DBMS
Primary Key – A primary is a column or set of columns in a table that uniquely identifies tuples
(rows) in that table.
Super Key – A super key is a set of one of more columns (attributes) to uniquely identify rows in a
table.
Candidate Key – A super key with no redundant attribute is known as candidate key
Alternate Key – Out of all candidate keys, only one gets selected as primary key, remaining keys are
known as alternate or secondary keys.
Composite Key – A key that consists of more than one attribute to uniquely identify rows (also known
as records & tuples) in a table is called composite key.
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.
constraints/key constraints:-
Entity integrity constraints / key constraints specifies the condition and restricts the data that
can be stored, only in one table.
Definitions :
Key constraint: a key constraint is a statement that a certain minimal fields of a relation has a
unique identifier for all tuples. Actually key constraint is the general term, the term candidate key is
used for satisfying the constraints according to a key constraints.
Database Schema:
When we talk about a database, we must differentiate between the database schema, which is
the logical design of the database, and the database instance, which 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, while the concept of a relation schema corresponds to the
programming language notion of type definition.
In general, a relation schema consists of a list of attributes and their corresponding domains.
We shall not be concerned about the precise definition of the domain of each attribute until we discuss
the QL language. The concept of a relation instance corresponds to the programming-language notion
of a value of a 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.
Ex: 1. Student (sno,rollno,std_name,addr).
2. Department (dept_name, building, budget).
Schema Diagrams:
A database schema, along with primary key and foreign key dependencies, can be depicted by
schema diagrams. Figure 2.8 shows the schema diagram for our university organization. Each relation
appears as a box, with the relation name at the top in blue, and the attributes listed inside the box.
Primary key attributes are shown underlined. Foreign key dependencies appear as arrows from the
foreign key attributes of the referencing relation to the primary key of the referenced relation.
Relational Query Languages:
22
[
Relational database management systems
rajanna ]
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 standard programming language. Query
languages can be categorized as either procedural or nonprocedural. 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 nonprocedural language, the user describes the desired information without giving a specific
procedure for obtaining that information.
Ex:
1. Classroom (building, room number, capacity)
2. Department (dept name, building, budget)
3. course (course id, title, dept name, credits)
4. instructor ( ID, name, dept name, salary)
5. section (course id, sec id, semester, year, building, room number, time slot id)
6. teaches ( ID, course id, sec id, semester, year)
7. student ( ID, name, dept _name, tot_ credit)
DBMS - Normalization
Functional Dependency:
Functional dependency (FD) is a set of constraints between two attributes in a relation.
Functional dependency says that if two tuples have same values for attributes A1, A2,..., An, then
those two tuples must have to have same values for attributes B1, B2, ..., Bn.
Functional dependency is represented by an arrow sign (→) that is, X→Y, where X functionally
determines Y. The left-hand side attributes determine the values of attributes on the right-hand side.
Armstrong's Axioms:
If F is a set of functional dependencies then the closure of F, denoted as F +, is the set of all functional
dependencies logically implied by F. Armstrong's Axioms are a set of rules, that when applied
repeatedly, generates a closure of functional dependencies.
Reflexive rule − If alpha is a set of attributes and beta is_subset_of alpha, then alpha holds
beta.
Augmentation rule − If a → b holds and y is attribute set, then ay → by also holds. That is
adding attributes in dependencies, does not change the basic dependencies.
Transitivity rule − Same as transitive rule in algebra, if a → b holds and b → c holds, then a
→ c also holds. a → b is called as a functionally that determines b.
Trivial Functional Dependency:
Trivial − If a functional dependency (FD) X → Y holds, where Y is a subset of X, then it is
called a trivial FD. Trivial FDs always hold.
Non-trivial − If an FD X → Y holds, where Y is not a subset of X, then it is called a non-
trivial FD.
Completely non-trivial − If an FD X → Y holds, where x intersect Y = Φ, it is said to be a
completely non-trivial FD.
Normalization:
If a database design is not perfect, it may contain anomalies, which are like a bad dream for any
database administrator. Managing a database with anomalies is next to impossible.
23
[
Relational database management systems
rajanna ]
Update anomalies − If data items are scattered and are not linked to each other properly, then
it could lead to strange situations. For example, when we try to update one data item having its
copies scattered over several places, a few instances get updated properly while a few others
are left with old values. Such instances leave the database in an inconsistent state.
Deletion anomalies − We tried to delete a record, but parts of it was left undeleted because of
unawareness, the data is also saved somewhere else.
Insert anomalies − We tried to insert data in a record that does not exist at all.
Normalization is a method to remove all these anomalies and bring the database to a consistent state.
First Normal Form:
First Normal Form is defined in the definition of relations (tables) itself. This rule defines that all the
attributes in a relation must have atomic domains. The values in an atomic domain are indivisible
units.
Each attribute must contain only a single value from its pre-defined domain.
Second Normal Form:
Before we learn about the second normal form, we need to understand the following −
Prime attribute − An attribute, which is a part of the candidate-key, is known as a prime
attribute.
Non-prime attribute − An attribute, which is not a part of the prime-key, is said to be a non-
prime attribute.
If we follow second normal form, then every non-prime attribute should be fully functionally
dependent on prime key attribute. That is, if X → A holds, then there should not be any proper subset
Y of X, for which Y → A also holds true.
24
[
Relational database management systems
rajanna ]
We see here in Student_Project relation that the prime key attributes are Stu_ID and Proj_ID.
According to the rule, non-key attributes, i.e. Stu_Name and Proj_Name must be dependent upon both
and not on any of the prime key attribute individually. But we find that Stu_Name can be identified by
Stu_ID and Proj_Name can be identified by Proj_ID independently. This is called partial
dependency, which is not allowed in Second Normal Form.
We broke the relation in two as depicted in the above picture. So there exists no partial dependency.
Third Normal Form:
For a relation to be in Third Normal Form, it must be in Second Normal form and the following must
satisfy −
We find that in the above Student_detail relation, Stu_ID is the key and only prime key attribute. We
find that City can be identified by Stu_ID as well as Zip itself. Neither Zip is a superkey nor is City a
prime attribute. Additionally, Stu_ID → Zip → City, so there exists transitive dependency.
To bring this relation into third normal form, we break the relation into two relations as follows −
25
[
Relational database management systems
rajanna ]
26
[
Relational database management systems
rajanna ]
27
[
Relational database management systems
rajanna ]
Operation:
Insertion − When a record is required to be entered using static hash, the hash
function h computes the bucket address for search key K, where the record will be stored.
Bucket address = h(K)
Search − When a record needs to be retrieved, the same hash function can be used to retrieve
the address of the bucket where the data is stored.
Delete − This is simply a search followed by a deletion operation.
*****
28
[
Relational database management systems
rajanna ]
UNIT III
30
[
Relational database management systems
rajanna ]
31
[
Relational database management systems
rajanna ]
Operator Description
BETWEEN The BETWEEN operator is used to search for values that are
within a set of values, given the minimum value and the
maximum value.
EXISTS The EXISTS operator is used to search for the presence of a row
in a specified table that meets certain criteria.
NOT The NOT operator reverses the meaning of the logical operator
with which it is used. Eg. NOT EXISTS, NOT BETWEEN,
NOT IN etc. This is negate operator.
32
[
Relational database management systems
rajanna ]
Operator Description
1 ~ (Bitwise NOT)
4 =, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)
5 NOT
6 AND
8 = (Assignment)
33
[
Relational database management systems
rajanna ]
SCHEMA
In the SQL environment, a schema is a group of database objects such as tables and indexes,
which are related to each other. Usually the schema belongs to a single user or application. A
single database can hold multiple schemas belonging to different users or applications.
Schemas are used to define a portion of a database that a particular user owns. Schemas are
dependent on a catalog. Schemas enforce a first level of security by allowing each user to see
only the tables that belong to that user.
The syntax of the SQL command to create a database schema:
34
[
Relational database management systems
rajanna ]
CREATE TABLE
table_name (
column_name1
data_type,
column_name2
data_type,
column_name3
data_type,
Example:
CREATE TABLE VENDOR (
V_CODE INTEGER NOT NULL UNIQUE,
V_NAME VARCHAR(35) NOT NULL,
V_CONTACT VARCHAR(15) NOT NULL,
V_AREACODE CHAR(3) NOT NULL,
35
[
Relational database management systems
rajanna ]
SQL CONSTRAINTS
Constraints are the rules that can not be violated by the user. Constraints are used to ensure
proper data entry in tables. They should be written when creating or altering the table
structures. Constraints used in SQL are listed below.
Primary key constraint:
o Primary key attributes contain both a NOT NULL and a UNIQUE
specification
Foreign key constraint:
o RDBMS will automatically enforce referential integrity for foreign keys. It
indicates that the proper foreign key values must match the primary key
values of other table other wise null
NOT NULL constraint
o Ensures that a column does not accept nulls
UNIQUE constraint
o Ensures that all values in a column are unique
DEFAULT constraint
o Assigns a value to an attribute when a new row is added to a table
CHECK constraint
o Validates data when an attribute value is entered
ON UPDATE CASCADE
o Ensures that a change in pk will automatically be applied to all FK references
throughout the system
o Also have ON DELETE CASCADE and ON UPDATE CASCADE
Example:
SQL> create table emp99
(emp_num number(3),
emp_name varchar2(15) constraint emp99_emp_name_nn not null, emp_city
varchar2(10) default 'hyd',
emp_basic number(5) constraint emp99_emp_basic_ck
36
[
Relational database management systems
rajanna ]
check (emp_basic>10000),
deptno number(2),
constraint emp99_emp_num_pk primary key(emp_num),
constraint emp99_deptno_fk foreign key(deptno) references dept99(deptno));
The DDL commands are used to define a database including creating, altering, and
dropping tables and establishing constraints. DDL deals with meta data.
The commands are explained below.
Create command:
The CREATE TABLE Statement is used to create tables to store data.
CREATE TABLE
table_name
(column_name1 datatype,
column_name2 datatype,
... column_nameN datatype
);
table_name - is the name of the table.
column_name1, column_name2.... - is the name of the columns
datatype - is the datatype for the column like char, date, number etc. For Example:
If you want to create the employee table, the statement would be like,
CREATE TABLE
employee ( id
number(5),
name
char(20),
dept
char(10), age
number(2),
salary
number(10),
location char(10)
);
ALTER COMMAND:
The SQL ALTER TABLE command is used to modify the definition
(structure) of a table by modifying the definition of its columns. The ALTER
37
[
Relational database management systems
rajanna ]
TRUNCATE command:
It is used to delete rows with auto commit
Syntax :
TRUNCATE table table_name;
Example:
col1, col2,...colN -- the names of the columns in the table into which you want to
insert data.
Example:
INSERT INTO employee (id, name, dept, age, salary location) VALUES (105,
columnN)]
40
[
Relational database management systems
rajanna ]
SELECT column1,
column2, ...columnN FROM
table_name [WHERE condition];
Example
INSERT INTO employee
SELECT * FROM temp_employee;
SQL UPDATE STATEMENT
The UPDATE Statement is used to modify the existing rows in a table.
Syntax
UPDATE table_name
SET column_name1 =
value1, column_name2 =
value2, ... [WHERE
condition]
table_name - the table name which has to be updated.
column_name1, column_name2.. - the columns that gets changed.
value1, value2... - are the new values.
Example:
UPDATE employee
SET location
='Mysore' WHERE
id = 101;
Delete command:
The DELETE Statement is used to delete rows from a table.
Syntax:
DELETE FROM table_name [WHERE condition];
table_name -- the table name which has to be updated.
Example: To delete an employee with id 100 from the employee table, the sql delete query
would be like,
DELETE FROM employee WHERE id = 100;
To delete all the rows from the employee table, the query would be like,
DELETE FROM employee;
DATA CONTROL LANGUAGE (DCL) COMMANDS.
DCL commands are used to control a database. It deals with accessibilitlity and transaction
changes. It includes a set of privileges that can be granted to or revoked from a user. The
commands are : GRANT, REVOKE, COMMIT, ROLLBACK and SAVEPOINT.
41
[
Relational database management systems
rajanna ]
GRANT COMMAND:
GRANT is a command used to provide access or privileges on the database objects to the
users.
Syntax:
GRANT
privilege_name ON
object_name
TO {user_name |PUBLIC |
role_name} [WITH GRANT
OPTION];
privilege_name is the access right or privilege granted to the user. Some of the
access rights are ALL, EXECUTE, and SELECT.
object_name is the name of an database object like TABLE, VIEW, STORED
PROC and SEQUENCE.
user_name is the name of the user to whom an access right is being granted.
PUBLIC is used to grant access rights to all users.
ROLES are a set of privileges grouped together.
WITH GRANT OPTION - allows a user to grant access rights to other users.
For Eample: GRANT SELECT ON employee TO user1;
This command grants a SELECT permission on employee table to user1.
REVOKE Command:
The REVOKE command removes user access rights or privileges to the database objects.
Syntax:
REVOKE
privilege_name ON
object_name
FROM {user_name |PUBLIC |role_name}
Example: REVOKE SELECT ON employee FROM user1;
This commmand will REVOKE a SELECT privilege on employee table from user1.
COMMIT Command
To make the changes done in a transaction permanent issue the COMMIT statement.
syntax
COMMIT ;
Example
insert into emp (empno,ename,sal) values (101,’Abid’,2300);
commit;
ROLLBACK
To rollback the changes done in a transaction give rollback statement. Rollback restore the
state of the database to the last commit point.
42
[
Relational database management systems
rajanna ]
Example :
delete from emp;
rollback; /* undo the changes */
SAVEPOINT
Specify a point in a transaction to which later you can roll back.
Example:
insert into emp (empno,ename,sal) values (109,’Sami’,3000);
savepoint a;
insert into dept values (10,’Sales’,’Hyd’);
Now if you give
rollback to a;
Then row dept will be roll backed. Now you can commit the row inserted into emp table or
rollback the transaction.
43
[
Relational database management systems
rajanna ]
Rahul Sharma 10
Anajali Bhagwat 12
Shekar Gowda 15
SQL IN Operator:
The IN operator is used when you want to compare a column with more than one value. It is
similar to an OR condition.
For example: If you want to find the names of students who are studying either Maths or
Science, the query would be like,
SELECT first_name, last_name,
subject FROM student_details
WHERE subject IN ('Maths', 'Science');
The output would be similar to:
44
[
Relational database management systems
rajanna ]
45
[
Relational database management systems
rajanna ]
46
[
Relational database management systems
rajanna ]
• 'A_Z': All string that starts with 'A', another character, and end with 'Z'. For example,
'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there
are two characters between A and Z instead of one).
• 'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would
both satisfy the condition.
name salary
---------- ----------
Soumya 20000
Ramesh 25000
Priya 30000
Hrithik 35000
Harsha 35000
If you want to sort the data in descending order, you must explicitly specify it as shown
below.
47
[
Relational database management systems
rajanna ]
SELECT name,
salary FROM
employee
ORDER BY name, salary DESC;
The above query sorts only the column 'salary' in descending order and the column
'name' by ascending order.
If you want to select both name and salary in descending order, the query would be as given
below.
SELECT name,
salary FROM
employee
ORDER BY name DESC, salary DESC;
SQL CLAUSES
The clauses in SQL are explained below:
SELECT CLAUSE
The most commonly used SQL command is SELECT statement. The SQL SELECT
statement is used to query or retrieve data from a table in the database.
WHERE CLAUSE
The WHERE Clause is used when you want to retrieve specific information from a table
excluding other irrelevant data.
ORDER BY CLAUSE
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or
descending order. Oracle sorts query results in ascending order by default.
GROUP BY CLAUSE
The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped
according to one or more columns.
HAVING CLAUSE
Having clause is used to filter data based on the group functions. This is similar to WHERE
condition but is used with group functions. Group functions cannot be used in WHERE
Clause but can be used in HAVING clause.
UNION Clause
Returns all distinct rows selected by either query.
UNION ALL Clause
Returns all rows selected by either query, including all duplicates.
INTERSECT Clause
Returns all distinct rows selected by both queries.
MINUS Clause Returns all distinct rows selected by the first query but not the second.
48
[
Relational database management systems
rajanna ]
COUNT (): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition. If the WHERE condition is not specified,
then the query returns the total number of rows in the table.
WHERE dept =
'Electronics';
MAX(): This function is used to get the maximum value from a column. To get
the maximum salary drawn by an employee, the query would be:
MIN(): This function is used to get the minimum value from a column. To get
the minimum salary drawn by an employee, he query would be:
AVG(): This function is used to get the average value of a numeric column. To get the
average salary, the query would be
SUM(): This function is used to get the sum of a numeric column To get
the total salary given out to the employees,
49
[
Relational database management systems
rajanna ]
TYPES OF AGGREGATIONS
Scalar aggregate: If a single value is returned from an SQL query that includes an aggregate
function then it is scalar aggregate. The following example returns single value. i.e. sum of
the salaries of all employees.
SELECT SUM(SAL) FROM EMP;
Vector aggregate: If multiple values are returned from an SQL query that includes an
aggregate function then it is vector aggregation. The following example returns multiple
values i.e. sum of the salaries of all employees in each department.
SELECT DEPTNO, SUM(SAL) FROM EMP GROUP BY DEPTNO;
GROUPING ON DATA.
SQL GROUP BY CLAUSE
The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped
according to one or more columns.
For Example: If you want to know the total amount of salary spent on each department, the
query would be:
SELECT dept, SUM
(salary) FROM employee
GROUP BY dept;
The output would be like:
dept salary
--------------- ----------
- ----
Electrical 25000
Electronics 55000
NOTE: The group by clause should contain all the columns in the select list expect those
used along with the group functions.
SELECT location, dept, SUM
(salary) FROM employee
GROUP BY location, dept;
The output would be like:
location dept salary
----------
------------ -------------- -
- -
Bangalore Electrical 25000
50
[
Relational database management systems
rajanna ]
dept salary
------------- -------------
Electronics 55000
Aeronautics 35000
InfoTech 30000
When WHERE, GROUP BY and HAVING clauses are used together in a SELECT
statement, the WHERE clause is processed first, then the rows that are returned after the
WHERE clause is executed are grouped based on the GROUP BY clause. Finally, any
conditions on the group functions in the HAVING clause are applied to the grouped rows
before the final output is displayed.
3.18 VIEW
A VIEW is a virtual table, through which a selective portion of the data from one or more
tables can be seen. Views do not contain data of their own. They are used to restrict access
to the database or to hide data complexity. A view is stored as a SELECT statement in the
database. DML operations on a view like INSERT, UPDATE, DELETE affects the data in
the original table upon which the view is based.
The Syntax to create a sql view is
CREATE VIEW view_name
AS
51
[
Relational database management systems
rajanna ]
SELECT column_list
Advantages of views:
1. View the data without storing the data into the object.
2. Provides the most current data from table
3. Restrict the view of a table i.e. can hide some of columns in the tables.
4. Join two or more tables and show it as one object to user.
5. Restrict the access of a table so that nobody can insert the rows into the table.
Disadvantages:
1. Can not use DML operations on this.
2. When table is dropped view becomes inactive.. it depends on the table objects.
3. It is an object, so it occupies space.
52
[
Relational database management systems
rajanna ]
SQL JOINS:
SQL Joins are used to relate information in different tables. A Join condition is a part of the
sql query that retrieves rows from two or more tables. A SQL Join condition is used in the
SQL WHERE Clause of select, update, delete statements.
The Syntax for joining two tables is:
SELECT col1, col2, col3...
FROM table_name1, table_name2
WHERE table_name1.col2 = table_name2.col1;
Equijoins
An equijoin is a join with a join condition containing an equality operator ( = ). An equijoin
combines rows that have equivalent values for the specified columns.
Example :
select [Link], [Link], [Link], [Link], [Link], [Link] from emp
e, dept d where [Link]=[Link];
The above query can also be written like given below without using table qualifiers.
select empno,ename,sal,dname,city from emp,dept
where [Link]=[Link];
Non Equi Joins.
Non equi joins is used to return result from two or more tables where exact join is not
possible.
For example we have emp table and salgrade table. The salgrade table contains grade and
their low salary and high salary. Suppose you want to find the grade of employees based on
their salaries then you can use NON EQUI join.
select [Link], [Link], [Link], [Link] from emp e, salgrade s
where [Link] between [Link] and [Link]
Self Joins(recursive join)
A self join is a join of a table to itself. This table appears twice in the FROM clause and is
followed by table aliases that qualify column names in the join condition. To perform a self
join, Oracle combines and returns rows of the table that satisfy the join condition.
For example the following query returns employee names and their manager names for whom
they are working.
Select [Link], [Link], [Link] “Manager” from emp e,
53
[
Relational database management systems
rajanna ]
54
[
Relational database management systems
rajanna ]
The different SQL join operators and their meanings and examples are listed below:
55
[
Relational database management systems
rajanna ]
Subquery or Inner query or Nested query is a query in a query. A subquery is usually added
in the WHERE Clause of the sql statement. Most of the time, a subquery is used when you
know how to search for a value using a SELECT statement, but do not know the exact value.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the following sql statements along with the comparision
operators like =, <, >, >=, <= etc.
SELECT
INSERT
UPDATE
DELETE
Properties of Sub-Query
A sub query is select statement inside a query.
A sub query must be enclosed in the parenthesis.
A sub query must be put in the right hand of the comparison operator
56
[
Relational database management systems
rajanna ]
The first query in the SQL statement is known as the outer query.
The query inside the SQL statement is known as the inner query.
The inner query is executed first.
The output of the inner query is used as input for the outer query.
A sub query cannot contain a ORDER-BY clause.
A query can contain more than one sub-queries. Example:
the following are the examples of nested queries.
To find the second max salary of the employees in emp table.
Select max(sal) from emp where sal< (select max(sal) from
emp);
To print the name of the employee who draws maximum salary.
Select ename form emp where sal = (select max(sal) from emp);
Multiple row subquery returns one or more rows to the outer SQL statement.
The outer query may use the IN, ANY, or ALL operator to handle a sub query that
returns multiple rows
Using IN operator:
57
[
Relational database management systems
rajanna ]
Example:
SELECT p.product_name FROM product p
WHERE p.product_id = (SELECT o.product_id FROM order_items
o WHERE o.product_id = p.product_id);
NOTE:
1) You can nest as many queries you want but it is recommended not to nest more than
16 subqueries in oracle.
2) If a subquery is not dependent on the outer query it is called a non- correlated
subquery.
More Examples on Correlated Sub queries:
The following query lists the names of the employees who joined on a same date.
Select ename from emp e where 1 < ( select count(*) from emp where
hiredate = [Link]
The following query lists the names of the employees who draw first 3 maximum salary
Select * from emp e where 3 > (select count(*) from emp where sal >
[Link]);
2.25 DUAL TABLE
This is a single row and single column dummy table provided by oracle. This is used to
perform mathematical calculations without using a table.
Output:
DUMMY
-------
X
group of rows.
59
[
Relational database management systems
rajanna ]
ROUND (x, y) Rounded off value of the number 'x' up to the number 'y' decimal places
The following examples explains the usage of the above numeric functions .
60
[
Relational database management systems
rajanna ]
FLOOR (2.83) 2
FLOOR (x)
FLOOR (2.49) 2
61
[
Relational database management systems
rajanna ]
The following examples explains the usage of the above character or text functions
GOOD
UPPER(string_value) UPPER('Good Morning')
MORNING
INITCAP('GOOD MORNING')
INITCAP(string_value) Good Morning
3) Date Functions:
These are functions that take values that are of datatype DATE as input and return values of
datatypes DATE, except for the MONTHS_BETWEEN function, which returns a number as
output.
62
[
Relational database management systems
rajanna ]
MONTHS_BETWEEN (x1, Returns the number of months between dates x1 and x2.
x2)
The below table provides the examples for the above functions
NEXT_DAY ('01-Jun-08',
NEXT_DAY( ) 04-JUN-08
'Wednesday')
4) Conversion Functions:
These are functions that help us to convert a value in one form to another form. For Ex: a null
value into an actual value, or a value from one datatype to another datatype like NVL,
TO_CHAR, TO_NUMBER, TO_DATE.
63
[
Relational database management systems
rajanna ]
If 'x' is NULL, replace it with 'y'. 'x' and 'y' must be of the
NVL (x, y)
same datatype.
DECODE (a, b, c, d, e, Checks the value of 'a', if a = b, then returns 'c'. If a = d, then
default_value) returns 'e'. Else, returns default_value.
The below table provides the examples for the above functions
Characteristics of Sequences:
Oracle sequences are an independent object in the database. (Sequences are not a
data type)
64
[
Relational database management systems
rajanna ]
Oracle sequences have a name and can be used anywhere a value is expected.
Oracle sequences are not tied to a table or a column.
Oracle sequences generate a numeric value that can be assigned to any column in
any table.
The table attribute to which you assigned a value based on a sequence can be edited
and modified.
An oracle sequence can be created and deleted anytime. The
syntax for a sequence is:
For example:
This would create a sequence object called supplier_seq. The first sequence number that it
would use is 1 and each subsequent number would increment by 1 (ie: 2,3,4,...}. It will
cache up to 20 values for performance.
Each PL/SQL program consists of SQL and PL/SQL statements which form a PL/SQL
block.
A PL/SQL Block consists of three sections:
The Declaration section (optional).
The Execution section (mandatory).
65
[
Relational database management systems
rajanna ]
DECLARE
Variable declaration BEGIN
Program Execution EXCEPTION
Exception handling END;
Example
DECLARE
A Number := 50 ;
B Number := 0 ;
BEGIN
DBMS_OUTPUT.PUT_LINE('A+B= ' || (A+B));
END;
66
[
Relational database management systems
rajanna ]
3. 31 COMMENTS IN PL/SQL
Comments are non-executable statements in a program. The comments are used to increase
the understandability of the program. Two different ways to comments in pl-sql are given
below:
We use two hyphens ( - - ) for single comments. We
use /* */ for multiline comment in programs
67
[
Relational database management systems
rajanna ]
68
[
Relational database management systems
rajanna ]
IF..ELSE Statement
Syntax Example:
IF <condition> THEN IF a > 40 THEN
<statements>; b := a - 40;
ELSE ELSE
<statements>; b := 0;
END IF; END IF;
If – elsif Statement
Syntax IF Score >= 90 THEN
IF <condition-1> THEN LetterGrade := 'A';
<statements>; ELSIF Score >= 80 THEN
ELSIF <condition-2> THEN LetterGrade := 'B';
<statements>; ELSIF Score >= 70 THEN
ELSIF <condition-3> THEN LetterGrade := 'C';
<statements>; ELSIF Score >= 60 THEN
.................... LetterGrade := 'D';
.................... ELSE
ELSE LetterGrade := 'E';
<statements>; END IF;
END IF;
69
[
Relational database management systems
rajanna ]
While Loop
Syntax: Example:
WHILE <condition> WHILE i<5
LOOP LOOP
<action> dbms_output.put_line('Hello');
END LOOP; i:=i+1;
END LOOP;
FOR Loop
Syntax: Example FOR
FOR variable IN [REVERSE] i IN 1..5 LOOP
start..end
LOOP
<action>
END LOOP;
70
[
Relational database management systems
rajanna ]
SWITCH Statement
Syntax Example Code:
CASE
DECLARE
WHEN <condition>
a number := 5; BEGIN
THEN <statement> WHEN <condition>
CASE
THEN <statement> ELSE <statement>
WHEN a = 5 THEN
END CASE;
dbms_output.put_line('Execute Me'); WHEN a = 4 T
dbms_output.put_line('Not Execute Me');
ELSE
dbms_output.put_line('Else Statements');
END CASE; END;
EXCEPTION
In PL/SQL, a warning or error is called an exception. Handling run time errors is called
exception handling Exceptions can be internally defined (by the run- time system) or user
defined. Examples of internally defined exceptions include division by zero and out of
memory. Some common internal exceptions have predefined names, such as
ZERO_DIVIDE and STORAGE_ERROR.
71
[
Relational database management systems
rajanna ]
72
[
Relational database management systems
rajanna ]
When an error occurs, an exception is raised. That is, normal execution stops and control
transfers to the exception-handling part of your PL/SQL block or subprogram. Internal
exceptions are raised implicitly (automatically) by the run-time system. User-defined
exceptions must be raised explicitly by RAISE statements, which can also raise predefined
exceptions.
To handle raised exceptions, you write separate routines called exception handlers.
Buit-In Exception Syntax:
EXCEPTION
WHEN <Buit-in Exception> THEN
<User Defined Action to be taken>
Example Code:
DECLARE
V_NAME VARCHAR2(20);
BEGIN
SELECT EMP_NAME
INTO V_NAME FROM
EMP99
WHERE EMP_CITY = 'DELHI';
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('NO EMP EXIST WITH THE GIVEN
CITY');
END;
73
[
Relational database management systems
rajanna ]
Example:
CREATE OR REPLACE PROCEDURE UPDATESAL1(N NUMBER, A NUMBER) IS
BEGIN
UPDATE EMP_NUM=N;
EMP99 SET EMP_BASIC=EMP_BASIC+A WHERE
END UPDATESAL1;
74
[
Relational database management systems
rajanna ]
FUNCTION IN PL/SQL
A function is a named PL/SQL Block which is similar to a procedure. The major difference
between a procedure and a function is, a function must always return a value, but a
procedure may or may not return a value.
Example:
CREATE OR REPLACE FUNCTION FACT (N NUMBER)
RETURN NUMBER
IS
I NUMBER(10);
F
NUMBER :=1;
BEGIN
FOR I IN 1.. N LOOP
F:= F*I;
END LOOP;
RETURN F;
75
[
Relational database management systems
rajanna ]
TRIGGER
A trigger is a pl/sql block structure which is fired when a DML statements like Insert,
Delete, Update is executed on a database table. A trigger is triggered automatically when
an associated DML statement is executed.
Syntax of Triggers
The Syntax for creating a trigger is:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;
CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger
with the given name or overwrites an existing trigger with the same name.
{BEFORE | AFTER | INSTEAD OF } - This clause indicates at what time should the
trigger get fired. i.e for example: before or after updating a table. INSTEAD OF is
used to create a trigger on a view. before and after cannot be used to create a trigger
on a view.
{INSERT [OR] | UPDATE [OR] | DELETE} - This clause determines the triggering
event. More than one triggering events can be used together separated by OR
keyword. The trigger gets fired at all the specified triggering event.
[OF col_name] - This clause is used with update triggers. This clause is used
when you want to trigger an event only when a specific column is updated.
[ON table_name] - This clause identifies the name of the table or view to which
the trigger is associated.
[REFERENCING OLD AS o NEW AS n] - This clause is used to reference the old
and new values of the data being changed. By default, you reference the values
as :old.column_name or :new.column_name. The reference names can also be
changed from old (or new) to any other user- defined name. You cannot reference
old values when inserting a record, or new values when deleting a record, because
they do not exist.
76
[
Relational database management systems
rajanna ]
[FOR EACH ROW] - This clause is used to determine whether a trigger must fire
when each row gets affected ( i.e. a Row Level Trigger) or just once when the
entire sql statement is executed([Link] level Trigger).
WHEN (condition) - This clause is valid only for row level triggers. The trigger is
fired only for rows that satisfy the condition specified.
We can create a trigger to update the 'product_price_history' table when the price of the
product is updated in the 'product' table.
77
[
Relational database management systems
rajanna ]
:old.product_name,
:old.supplier_name,
:old.unit_price
);
4) If you ROLLBACK the transaction before committing to the database, the data inserted
to the table is also rolled back.
3. 39 TYPES OF TRIGGERS
Types of PL/SQL Triggers
There are two types of triggers based on the which level it is triggered.
1) Row level trigger - An event is triggered for each row upated, inserted or deleted.
2) Statement level trigger - An event is triggered for each sql statement executed
78
[
Relational database management systems
rajanna ]
Unit-IV
79
[
Relational database management systems
rajanna ]
Inconsistent retrievals occur when a transaction accesses data before and after another
transaction(s) finish working with such data.
*****
Concurrency Control with Locking Methods
A lock guarantees exclusive use of a data item to a current transaction.
Lock Granularity it indicates the level of lock use. It is of following levels:
Database Level Locks
Table Level Locks
Page Level Locks
Row Level Locks
Field (attribute) Level Locks
Database Level
In a database-level lock, the entire database is locked. It prevents other transactions to use
any tables in the database. The following figure shows a database level lock:
Table Level
In a table-level lock, the entire table is locked. It prevents other transactions to use any row
in the tables. The following figure shows a database level lock:
Page Level
In a page-level lock, an entire diskpage is locked. It prevents other transactions to use any
page in the tables The following figure shows a page level lock:
80
[
Relational database management systems
rajanna ]
Row Level
A row-level lock allows the transactions to access different rows of the same table. The
following figure illustrates row-level lock:
Field Level
The field-level lock allows the transactions to access different fields (attributes) within a
row. Field-level locking requires high overhead. It can be rarely implemented.
***
Lock Types
The DBMS may use different lock types, such as:
Binary Locks
Shared/exclusive.
Binary Locks
A binary lock has only two states: locked (1) or unlocked (0). It specifies that every transaction
requires a lock and unlock operation for each of its data items.
If a transaction locks any database object then the other transactions are not allowed until it
unlocks that object.
Shared/Exclusive Locks
Exclusive Locks
An exclusive lock reserves access only for the transaction that locked the object. An
exclusive lock is issued when a transaction wants to update (write) a data item and only if
no other locks are held on the data item.
Shared Locks:
A shared lock grants read access on the object. A shared lock is issued when a
transaction wants to read data from the database.
***
81
[
Relational database management systems
rajanna ]
Deadlock
A database deadlock is caused when two or more transactions wait for each other to
unlock data.
Two-phase locking increases the transaction processing cost and might cause
deadlocks.
***
Deadlocks
A deadlock occurs when two transactions wait indefinitely for each other to unlock data. For
example, a deadlock occurs when two transactions, T1 and T2, exist in the following mode:
82
[
Relational database management systems
rajanna ]
83
[
Relational database management systems
rajanna ]
85
[
Relational database management systems
rajanna ]
Database recovery
Database recovery restores a database from an inconsistent) to a consistent state. Some
Critical events can cause damage to a database. Examples of critical events are:
Hardware/software failures: A hard disk failure, memory failure, etc are known as the Hardware
failures. The application program or operating system errors are known as software errors.
Human-caused incidents: This type of event can be of unintentional or intentional.
· An unintentional failure is caused by carelessness by end-users. Such errors include
deleting the wrong rows from a table, pressing the wrong key on the keyboard, or shutting
down the main database server by accident.
· Intentional events are of a more severe nature and are very serious. The security
threats caused by hackers and virus attacks comes under this category
Natural disasters: This category includes fires, earthquakes, floods, and power
failures.
Transaction Recovery
Database transaction recovery is the process to recover a database from an inconsistent state
to a consistent state. There are four important concepts that affect the recovery process:
1) Write-ahead-log Protocol
2) Redundant transaction logs
3) Database Buffers
4) Database checkpoints
The write-ahead-log protocol ensures that transaction logs are always written before
any update.
Redundant transaction logs Maintains several copies of the transaction log to ensure
DBMS’s ability to recover data.
Database buffers are temporary storage areas in primary memory. These are useful to
speed up disk operations. It saves a lot of time.
For any transaction that had a ROLLBACK operation after the last checkpoint, nothing
needs to be done. Because the database was never updated. Database checkpoints are
operations in which the DBMS writes all of its updated buffers to disk.
Transaction recovery
Transaction recovery procedures will use two techniques:
1. Deferred-Write technique
2. Write-Through technique.
Transaction recovery procedures: Transaction recovery procedures will use two
techniques:
1. Deferred-Write technique
2. Write-Through technique.
Deferred-Write technique:
86
[
Relational database management systems
rajanna ]
In a deferred–write technique only the transaction log is updated. The recovery process in a
deferred-write technique has the following steps:
1. Identify the last checkpoint in the transaction log.
2. For a transaction that started and was committed before the last checkpoint, nothing needs
to be done.
3. For a transaction that performed a commit operation after the last checkpoint, the
DBMS uses the transaction log to update the database.
Write-through technique
In a write-through technique the database is immediately updated.
The recovery process in a write-through technique has the following steps:
1. Identify the last checkpoint in the transaction log.
2. For a transaction that started and was committed before the last checkpoint, nothing needs
to be done
3. For a transaction that was committed after the last checkpoint, the DBMS redoes the
transaction.
4. For any transaction that had a ROLLBACK operation after the last checkpoint, the DBMS
uses the transaction log to ROLLBACK the operations by using the “before” values in the
transaction log.
***
87
[
Relational database management systems
rajanna ]
Unit-V
DDBMS: In a distributed database, there are a number of databases that may be
geographically distributed all over the world. A distributed DBMS manages the distributed database
in a manner so that it appears as one single database to users. In the later part of the chapter, we go on
to study the factors that lead to distributed databases, its advantages and disadvantages.
A distributed database is a collection of multiple interconnected databases, which are spread
physically across various locations that communicate via a computer network.
Features
Databases in the collection are logically interrelated with each other. Often they represent a
single logical database.
Data is physically stored across multiple sites. Data in each site can be managed by a DBMS
independent of the other sites.
The processors in the sites are connected via a network. They do not have any multiprocessor
configuration.
A distributed database is not a loosely connected file system.
A distributed database incorporates transaction processing, but it is not synonymous with a
transaction processing system.
Distributed Database Management System
A distributed database management system (DDBMS) is a centralized software system that manages a
distributed database in a manner as if it were all stored in a single location.
Features
It is used to create, retrieve, update and delete distributed databases.
It synchronizes the database periodically and provides access mechanisms by the virtue of
which the distribution becomes transparent to the users.
It ensures that the data modified at any site is universally updated.
It is used in application areas where large volumes of data are processed and accessed by
numerous users simultaneously.
It is designed for heterogeneous database platforms.
It maintains confidentiality and data integrity of the databases.
Factors Encouraging DDBMS:
The following factors encourage moving over to DDBMS −
Distributed Nature of Organizational Units − Most organizations in the current times are
subdivided into multiple units that are physically distributed over the globe. Each unit requires
its own set of local data. Thus, the overall database of the organization becomes distributed.
Need for Sharing of Data − The multiple organizational units often need to communicate with
each other and share their data and resources. This demands common databases or replicated
databases that should be used in a synchronized manner.
Support for Both OLTP and OLAP − Online Transaction Processing (OLTP) and Online
Analytical Processing (OLAP) work upon diversified systems which may have common data.
Distributed database systems aid both these processing by providing synchronized data.
88
[
Relational database management systems
rajanna ]
Database Recovery − One of the common techniques used in DDBMS is replication of data
across different sites. Replication of data automatically helps in data recovery if database in
any site is damaged. Users can access data from other sites while the damaged site is being
reconstructed. Thus, database failure may become almost inconspicuous to users.
Support for Multiple Application Software − Most organizations use a variety of application
software each with its specific database support. DDBMS provides a uniform functionality for
using the same data among different platforms.
Advantages of Distributed Databases:
Following are the advantages of distributed databases over centralized databases.
Modular Development − If the system needs to be expanded to new locations or new units, in
centralized database systems, the action requires substantial efforts and disruption in the existing
functioning. However, in distributed databases, the work simply requires adding new computers and
local data to the new site and finally connecting them to the distributed system, with no interruption in
current functions.
More Reliable − In case of database failures, the total system of centralized databases comes to a
halt. However, in distributed systems, when a component fails, the functioning of the system
continues may be at a reduced performance. Hence DDBMS is more reliable.
Better Response − If data is distributed in an efficient manner, then user requests can be met from
local data itself, thus providing faster response. On the other hand, in centralized systems, all queries
have to pass through the central computer for processing, which increases the response time.
Lower Communication Cost − In distributed database systems, if data is located locally where it is
mostly used, then the communication costs for data manipulation can be minimized. This is not
feasible in centralized systems.
Adversities of Distributed Databases:
Following are some of the adversities associated with distributed databases.
Need for complex and expensive software − DDBMS demands complex and often expensive
software to provide data transparency and co-ordination across the several sites.
Processing overhead − Even simple operations may require a large number of
communications and additional calculations to provide uniformity in data across the sites.
Data integrity − The need for updating data in multiple sites pose problems of data integrity.
Overheads for improper data distribution − Responsiveness of queries is largely dependent
upon proper data distribution. Improper data distribution often leads to very slow response to
user requests.
Data Replication:
Data replication is the process of storing separate copies of the database at two or more sites. It is a
popular fault tolerance technique of distributed databases.
89
[
Relational database management systems
rajanna ]
Snapshot replication
Near-real-time replication
Pull replication
Fragmentation:
Fragmentation is the task of dividing a table into a set of smaller tables. The subsets of the table are
called fragments. Fragmentation can be of three types: horizontal, vertical, and hybrid (combination
of horizontal and vertical). Horizontal fragmentation can further be classified into two techniques:
primary horizontal fragmentation and derived horizontal fragmentation.
Fragmentation should be done in a way so that the original table can be reconstructed from the
fragments. This is needed so that the original table can be reconstructed from the fragments whenever
required. This requirement is called “reconstructiveness.”
Advantages of Fragmentation:
Since data is stored close to the site of usage, efficiency of the database system is increased.
Local query optimization techniques are sufficient for most queries since data is locally
available.
Since irrelevant data is not available at the sites, security and privacy of the database system
can be maintained.
Disadvantages of Fragmentation:
When data from different fragments are required, the access speeds may be very high.
In case of recursive fragmentations, the job of reconstruction will need expensive techniques.
90
[
Relational database management systems
rajanna ]
Lack of back-up copies of data in different sites may render the database ineffective in case of
failure of a site.
Vertical Fragmentation:
In vertical fragmentation, the fields or columns of a table are grouped into fragments. In order to
maintain reconstructiveness, each fragment should contain the primary key field(s) of the table.
Vertical fragmentation can be used to enforce privacy of data.
For example, let us consider that a University database keeps records of all registered students in a
Student table having the following schema.
STUDENT:
Now, the fees details are maintained in the accounts section. In this case, the designer will fragment
the database as follows −
CREATE TABLE STD_FEES AS
SELECT Regd_No, Fees
FROM STUDENT;
91
[
Relational database management systems
rajanna ]
92
[
Relational database management systems
rajanna ]
93
[
Relational database management systems
rajanna ]
94
[
Relational database management systems
rajanna ]
Design Alternatives:
The distribution design alternatives for the tables in a DDBMS are as follows −
Vertical fragmentation
Horizontal fragmentation
Hybrid fragmentation
95
[
Relational database management systems
rajanna ]
Mixed Distribution:
This is a combination of fragmentation and partial replications. Here, the tables are initially
fragmented in any form (horizontal or vertical), and then these fragments are partially replicated
across the different sites according to the frequency of accessing the fragments.
An example of a two tier client/server structure is a web server. It returns the required web pages to the
clients that requested them.
An illustration of the two-tier client/server structure is as follows −
96
[
Relational database management systems
rajanna ]
If the client nodes are increased beyond capacity in the structure, then the server is not able to
handle the request overflow and performance of the system degrades.
Three - Tier Client/Server Structure
The three tier architecture has three layers namely client, application and data layer. The client layer is
the one that requests the information. In this case it could be the GUI, web interface etc. The
application layer acts as an interface between the client and data layer. It helps in communication and
also provides security. The data layer is the one that actually contains the required data.
An illustration of the three-tier client/server structure is as follows −
The three tier structure provides much better service and fast performance.
The structure can be scaled according to requirements without any problem.
Data security is much improved in the three tier structure.
Disadvantages of Three - Tier Client/Server Structure
A major disadvantage of the three-tier client/server structure is −
****
97