Rdbms Notes
Rdbms Notes
Rdbms Notes
UNIT - I
Introduction to DBMS – Data and Information - Database – Database Management
System – Objectives - Advantages – Components - Architecture. ER Model: Building
blocks of ER Diagram – Relationship Degree – Classification – ER diagram to Tables –
ISA relationship – Constraints – Aggregation and Composition – Advantages
.
UNIT - II
Relational Model: CODD’s Rule- Relational Data Model - Key - Integrity – Relational
Algebra Operations – Advantages and limitations – Relational Calculus – Domain
Relational Calculus - QBE.
UNIT - III
Structure of Relational Database. Introduction to Relational Database Design -
Objectives – Tools – Redundancy and Data Anomaly – Functional Dependency -
Normalization – 1NF – 2NF – 3NF – BCNF. Transaction Processing – Database
Security.
UNIT - IV
SQL: Commands – Data types – DDL - Selection, Projection, Join and Set Operations –
Aggregate Functions – DML – Modification - Truncation - Constraints – Sub query.
UNIT - V
PL/SQL: Structure - Elements – Operators Precedence – Control Structure – Iterative
Control - Cursors - Procedure - Function - Packages – Exceptional Handling - Triggers.
TEXT BOOK:
1. S. Sumathi, S. Esakkirajan, “Fundamentals of Relational Database Management
System”, Springer International Edition 2007.
REFERENCE BOOKS:
1. Abraham Silberchatz, Henry F. Korth, S. Sudarshan, “Database System Concepts”,
McGrawHill 2019, 7th Edition.
2. Alexis Leon & Mathews Leon, “Fundamentals of DBMS”, Vijay Nicole Publications
2014, 2nd Edition.
WEB REFERENCES:
NPTEL & MOOC courses titled Relational Database Management Systems
https://nptel.ac.in/courses/106106093/
https://nptel.ac.in/courses/106106095/
UNIT - I
Introduction to DBMS:
For example: MySQL, Oracle, etc are a very popular commercial database which
is used in different applications.
Data:
Information:
Database:
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.
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
Objective of DBMS:
Advantages of DBMS:
Reduction of redundancies:
Sharing of Data:
A database allows the sharing of data under its control by any number of
application programs or users.
Data Integrity:
Data integrity means that the data contained in the database is both accurate
and consistent. Therefore data values being entered for storage could be
checked to ensure that they fall within a specified range and are of the correct
format.
Data Security:
The DBA who has the ultimate responsibility for the data in the dbms can
ensure that proper access procedures are followed including proper
authentication to access to the Data Base System and additional check before
permitting access to sensitive data.
Conflict Resolution:
DBA resolve the conflict on requirements of various user and applications. The
DBA chooses the best file structure and access method to get optional
performance for the application.
Data Independence:
Components of DBMS:
The database system consists of many components. Each component
performs very significant tasks in the database management system
environment. A database environment is a collective system of components
that comprise and regulates the group of data, management, and use of data,
which consist of software, hardware, people, techniques of handling
database, and the data also.
Storage engine
Query language
Query processor
Optimization engine
Metadata catalog
Log manager
Data utilities
Storage engine
The storage engine is the core component of the DBMS that interacts with the
file system at an OS level to store data. All SQL queries which interact with the
underlying data go through the storage engine.
Query language
Query processor
This is the intermediary between the user queries and the database. The query
processor interprets the queries of users and makes them actionable
Optimization engine
The optimization Engine allows the DBMS to provide insights into the
performance of the database in terms of optimizing the database itself and
queries. When coupled with database monitoring tools, it can provide a
powerful toolset to gain the best performance out of the database.
Metadata catalog
This is the centralized catalog of all the objects within the database. When an
object is created, the DBMS keeps a record of that object with some metadata
about it using the metadata catalog. Then, this record can be used to:
Log manager
This component will keep all the logs of the DBMS. These logs will consist of
user logins and activity, database functions, backups and restore functions, etc.
The log manager ensures all these logs are properly recorded and easily
accessible.
Data utilities
In addition to all the above, most DBMS software comes with additional inbuilt
utilities to provide functionality such as:
Architecture of DBMS:
o The DBMS design depends upon its architecture. The basic client/server
architecture is used to deal with a large number of PCs, web servers,
database servers and other components that are connected with
networks.
o The client/server architecture consists of many PCs and a workstation
which are connected via the network.
o DBMS architecture depends upon how users are connected to the
database to get their request done.
1-Tier Architecture
2-Tier Architecture
with the database at the server side. For this interaction, API's
like: ODBC, JDBC are used.
o The user interfaces and application programs are run on the client-side.
o The server side is responsible to provide the functionalities like: query
processing and transaction management.
o To communicate with the DBMS, client-side application establishes a
connection with the server side.
3-Tier Architecture
o The 3-Tier architecture contains another layer between the client and
server. In this architecture, client can't directly communicate with the
server.
o The application on the client-end interacts with an application server
which further communicates with the database system.
o End user has no idea about the existence of the database beyond the
application server. The database also has no idea about any other user
beyond the application.
o The 3-Tier architecture is used in case of large web application.
DBMS 3-tier architecture divides the complete system into three inter-related
but independent modules as shown below
1. Physical Level: At the physical level, the information about the location of
database objects in the data store is kept. Various users of DBMS are unaware
of the locations of these objects.
2.Conceptual Level: At conceptual level, data is represented in the form of
various database tables. For Example, STUDENT database may contain
STUDENT and COURSE tables which will be visible to users but users are
unaware of their storage.
3.External Level: An external level specifies a view of the data in terms of
conceptual level tables. Each external level view is used to cater to the needs of
a particular category of users. For Example, FACULTY of a university is
interested in looking course details of students, STUDENTS are interested in
looking at all details related to academics, accounts, courses and hostel details
as well. So, different views can be generated for different users.
Data Independence
Data independence means a change of data at one level should not affect
another level. Two types of data independence are present in this architecture:
ER model
o ER model stands for an Entity-Relationship model. It is a high-level data
model. This model is used to define the data elements and relationship
for a specified system.
o It develops a conceptual design for the database. It also develops a very
simple and easy to design view of data.
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity
can be represented as rectangles.
a. Weak Entity
An entity that depends on another entity called a weak entity. The weak entity
doesn't contain any key attribute of its own. The weak entity is represented by
a double rectangle.
2. Attribute
For example, id, age, contact number, name, etc. can be attributes of a student.
a. Key Attribute
b. Composite Attribute
c. Multivalued Attribute
An attribute can have more than one value. These attributes are known as a
multivalued attribute. The double oval is used to represent multivalued
attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
For example, A person's age changes over time and can be derived from
another attribute like Date of birth.
3. Relationship
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it
is known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one
female.
b. One-to-many relationship
When only one instance of the entity on the left, and more than one instance of
an entity on the right associates with the relationship then this is known as a
one-to-many relationship.
For example, Scientist can invent many inventions, but the invention is done
by the only specific scientist.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance of
an entity on the right associates with the relationship then it is known as a
many-to-one relationship.
For example, Student enrolls for only one course, but a course can have many
students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one
instance of an entity on the right associates with the relationship then it is
known as a many-to-many relationship.
For example, Employee can assign by many projects and project can have
many employees.
Notation of ER diagram
Mapping Constraints
o A mapping constraint is a data constraint that expresses the number of
entities to which another entity can be related via a relationship set.
o It is most useful in describing the relationship sets that involve more than
two entity sets.
o For binary relationship set R on an entity set A and B, there are four possible
mapping cardinalities. These are as follows:
1. One to one (1:1)
2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)
One-to-one
In one-to-one mapping, an entity in E1 is associated with at most one entity in E2,
and an entity in E2 is associated with at most one entity in E1.
One-to-many
In one-to-many mapping, an entity in E1 is associated with any number of entities
in E2, and an entity in E2 is associated with at most one entity in E1.
Many-to-one
In one-to-many mapping, an entity in E1 is associated with at most one entity in
E2, and an entity in E2 is associated with any number of entities in E1.
Many-to-many
In many-to-many mapping, an entity in E1 is associated with any number of
entities in E2, and an entity in E2 is associated with any number of entities in E1.
In the database, every entity set or relationship set can be represented in tabular
form.
There are some points for converting the ER diagram to the table:
In the STUDENT table, Age is the derived attribute. It can be calculated at any point
of time by calculating the difference between current date and Date of Birth.
Using these rules, you can convert the ER diagram to tables and columns and
assign the mapping between the tables. Table structure for the given ER diagram
is as below:
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of
information.
o Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is not
affected.
o Thus, integrity constraint is used to guard against accidental damage to the
database.
1. Domain constraints
Example:
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows.
o A table can contain a null value other than the primary key field.
Example:
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set
uniquely.
o An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.
Example:
Keys
o Keys play an important role in the relational database.
o It is used to uniquely identify any record or row of data from the table. It is also used to
establish and identify relationships between tables.
For example, ID is used as a key in the Student table because it is unique for each student.
In the PERSON table, passport_number, license_number, SSN are keys since they are unique
for each person.
Types of keys:
1. Primary key
o It is the first key used to identify one and only one instance of an entity uniquely. An entity
can contain multiple keys, as we saw in the PERSON table. The key which is most suitable
from those lists becomes a primary key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for each employee. In the
EMPLOYEE table, we can even select License_Number and Passport_Number as primary keys
since they are also unique.
o For each entity, the primary key selection is based on requirements and developers.
2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify a tuple.
o Except for the primary key, the remaining attributes are considered a candidate key. The
candidate keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the
attributes, like SSN, Passport_Number, License_Number, etc., are considered a candidate key.
3. Super Key
Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a
candidate key.
4. Foreign key
o Foreign keys are the column of the table used to point to the primary key of another table.
o Every employee works in a specific department in a company, and employee and department
are two different entities. So we can't store the department's information in the employee
table. That's why we link these two tables through the primary key of one table.
o We add the primary key of the DEPARTMENT table, Department_Id, as a new attribute in the
EMPLOYEE table.
o In the EMPLOYEE table, Department_Id is the foreign key, and both the tables are related.
5. Alternate key
There may be one or more attributes or a combination of attributes that uniquely identify
each tuple in a relation. These attributes or combinations of the attributes are called the
candidate keys. One key is chosen as the primary key from these candidate keys, and the
remaining candidate key, if it exists, is termed the alternate key. In other words, the total
number of the alternate keys is the total number of candidate keys minus the primary key.
The alternate key may or may not exist. If there is only one candidate key in a relation, it does
not have an alternate key.
For example, employee relation has two attributes, Employee_Id and PAN_No, that act as
candidate keys. In this relation, Employee_Id is chosen as the primary key, so the other
candidate key, PAN_No, acts as the Alternate key.
6. Composite key
Whenever a primary key consists of more than one attribute, it is known as a composite key.
This key is also known as Concatenated Key.
For example, in employee relations, we assume that an employee may be assigned multiple
roles, and an employee may work on multiple projects simultaneously. So the primary key
will be composed of all three attributes, namely Emp_ID, Emp_role, and Proj_ID in
combination. So these attributes act as a composite key since the primary key comprises
more than one attribute.
7. Artificial key
The key created using arbitrarily assigned data are known as artificial keys. These keys are
created when a primary key is large and complex and has no relationship with many other
relations. The data values of the artificial keys are usually numbered in a serial order.
For example, the primary key, which is composed of Emp_ID, Emp_role, and Proj_ID, is large
in employee relations. So it would be better to add a new virtual attribute to identify each
tuple in the relation uniquely.
Foreign Key
The use of a foreign key is simply to link the attributes of two tables together with
the help of a primary key attribute. Thus, it is used for creating and maintaining
the relationship between the two relations.
Consider two tables Student and Department having their respective attributes
as shown in the below table structure:
In the tables, one attribute, you can see, is common, that is Stud_Id, but it has
different key constraints for both tables. In the Student table, the field Stud_Id is
a primary key because it is uniquely identifying all other fields of the Student
table. On the other hand, Stud_Id is a foreign key attribute for the Department
table because it is acting as a primary key attribute for the Student table. It means
that both the Student and Department table are linked with one another because
of the Stud_Id attribute.
In the below-shown figure, you can view the following structure of the relationship
between the two tables.
Aggregation
In aggregation, the relation between two entities is treated as a single entity. In aggregation,
relationship with its corresponding entities is aggregated into a higher level entity.
For example: Center entity offers the Course entity act as a single entity in the relationship
which is in a relationship with another entity visitor. In the real world, if a visitor visits a
coaching center then he will never enquiry about the Course only or just about the Center
instead he will ask the enquiry about both.
The UML representation of the example above (relation between employee and
department):
Composition
Composition is special type of Aggregation. It is a strong type of Aggregation. In
this type of Aggregation the child object does not have their own life cycle. The
child object's life depends on the parent's life cycle. Only the parent object has an
independent life cycle. If we delete the parent object then the child object(s) will
also be deleted. We can define the Composition as a "Part of" relationship.
For example, the company and company location, a single company has multiple
locations. If we delete the company then all the company locations are
automatically deleted. The company location does not have their independent
life cycle, it depends on the company object's life (parent object).
Advantages of ER Model
Conceptually it is very simple: ER model is very simple because if we know relationship
between entities and attributes, then we can easily draw an ER diagram.
Highly integrated with relational model: ER model can be easily converted into relational
model by simply converting ER model into tables.
Easy conversion to any data model: ER model can be easily converted into another data model
like hierarchical data model, network data model and so on.
Disadvantages of ER Model
Limited constraints and specification
Popular for high level design: ER model is very popular for designing high level design
UNIT –II
Relational schema: A relational schema contains the name of the relation and
name of all columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can
identify the row in the relation uniquely.
o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the
attributes.
o The instance of schema STUDENT has 5 tuples.
o t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
Properties of Relations
o Name of the relation is distinct from all other relations.
o Each relation cell contains exactly one atomic (single) value
o Each attribute contains a distinct name
o Attribute domain has no significance
o tuple has no duplicate value
o Order of tuple can have a different sequence
STUDENT
ROLL_N ADDRES AG BRANCH_COD
O NAME S PHONE E E
945512345
1 RAM DELHI 1 18 CS
915625313
3 SUJIT ROHTAK 1 20 ECE
BRANCH
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
CV CIVIL ENGINEERING
BRANCH_CODE of STUDENT can only take the values which are present in
BRANCH_CODE of BRANCH which is called referential integrity constraint.
The relation which is referencing to other relation is called REFERENCING
RELATION (STUDENT in this case) and the relation to which other
relations refer is called REFERENCED RELATION (BRANCH in this case).
Codd's Rules
Every database has tables, and constraints cannot be referred to as a rational
database system. And if any database has only relational data model, it cannot
be a Relational Database System (RDBMS). So, some rules define a database
to be the correct RDBMS. These rules were developed by Dr. Edgar F. Codd
(E.F. Codd) in 1985, who has vast research knowledge on the Relational Model
of database Systems. Codd presents his 13 rules for a database to test the
concept of DBMS against his relational model, and if a database follows the rule,
it is called a true relational database (RDBMS). These 13 rules are popular in
RDBMS, known as Codd's 12 rules.
suppose a table either split into two tables, or two table joins to create a single table, these
changes should not be impacted on the user view application.
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process
to obtain the result of the query. It uses operators to perform queries.
1. Select Operation:
1. Notation: σ p(r)
Where:
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in
the result. Rest of the attributes are eliminated from the table.
o It is denoted by ∏.
Where
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the
tuples that are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Input:
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each
row in the other table. It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted
by rho (ρ).
1. ρ(STUDENT1, STUDENT)
Join Operations:
A Join operation combines related tuples from different relations, if and only if a
given join condition is satisfied. It is denoted by ⋈.
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
EMP_CODE
EMP_CODE EMP_NAME
SALARY SALARY
SALARY
Result:
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal
on their common attribute names.
o It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal
with missing information.
Example:
EMPLOYEE
FACT_WORKERS
Input:
1. (EMPLOYEE ⋈ FACT_WORKERS)
Output:
o Left outer join contains the set of tuples of all combinations in R and S that
are equal on their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by ⟕.
Input:
1. EMPLOYEE ⟕ FACT_WORKERS
o Right outer join contains the set of tuples of all combinations in R and S that
are equal on their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by ⟖.
Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
o Full outer join is like a left or right join except that it contains all rows from
both tables.
o In full outer join, tuples in R that have no matching tuples in S and tuples in
S that have no matching tuples in R in their common attribute name.
o It is denoted by ⟗.
Input:
1. EMPLOYEE ⟗ FACT_WORKERS
Output:
3. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched
data as per the equality condition. The equi join uses the comparison operator(=).
Example:
CUSTOMER RELATION
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
Input:
1. CUSTOMER ⋈ PRODUCT
Output:
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
Relational Calculus
There is an alternate way of formulating queries known as Relational Calculus.
Relational calculus is a non-procedural query language. In the non-procedural
query language, the user is concerned with the details of how to obtain the end
results. The relational calculus tells what to do but never explains how to do. Most
commercial relational languages are based on aspects of relational calculus
including SQL-QBE and QUEL.
Relational Calculus
It is based on Predicate calculus, a name derived from branch of symbolic
language. A predicate is a truth-valued function with arguments. On substituting
values for the arguments, the function result in an expression called a proposition.
It can be either true or false. It is a tailored version of a subset of the Predicate
Calculus to communicate with the relational database.
Many of the calculus expressions involves the use of Quantifiers. There are
two types of quantifiers:
Before using the concept of quantifiers in formulas, we need to know the concept
of Free and Bound Variables.
Free and bound variables may be compared with global and local variable of
programming languages.
the desired information without giving a specific procedure for obtaining that
information. The tuple relational calculus is specified to select the tuples in a
relation. In TRC, filtering variable uses the tuples of a relation. The result of the
relation can have one or more tuples.
Notation:
Where
For example:
Output: This query selects the tuples from the AUTHOR relation. It returns a tuple
with 'name' from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃)
and Universal Quantifiers (∀).
For example:
Output: This query will yield the same result as the previous one.
Notation:
Where
For example:
Output: This query will yield the article, page, and subject from the relational
javatpoint, where the subject is a database.
Name, Phone_Number, and Branch fields. And we want to get the name of
the SAC-Representative name who belongs to the MCA Branch. If we write
this query in SQL we have to write it like
SELECT NAME
FROM SAC
WHERE BRANCH = 'MCA'"
And definitely, we will get our correct result. But in the case of QBE, it may
be done as like there is a field present and we just need to fill it with “MCA”
and then click on the SEARCH button we will get our required result.
Points about QBE:
UNIT – III
Basic Structure
1. Figure 3.1 shows the deposit and customer tables for our banking example.
We will use the terms relation and tuple in place of table and row from
now on.
Database Scheme
Note that customers are identified by name. In the real world, this would not be allowed, as
two or more customers might share the same name.
4. The relation schemes for the banking example used throughout the text are:
o Branch-scheme = (bname, assets, bcity)
o Customer-scheme = (cname, street, ccity)
o Deposit-scheme = (bname, account#, cname, balance)
o Borrow-scheme = (bname, loan#, cname, amount)
Note: some attributes appear in several relation schemes (e.g. bname, cname). This
is legal, and provides a way of relating tuples of distinct relations.
Keys
1. The notions of superkey, candidate key and primary key all apply to the
relational model.
2. For example, in Branch-scheme,
o {bname} is a superkey.
o {bname, bcity} is a superkey.
o {bname, bcity} is not a candidate key, as the superkey {bname} is contained in
it.
o {bname} is a candidate key.
o {bcity} is not a superkey, as branches may be in the same city.
o We will use {bname} as our primary key.
3. The primary key for Customer-scheme is {cname}.
Redundancy in DBMS is the problem that arises when the database is not normalized. It is
the concept of storing multiple copies of the same data in different parts of the database.
Let's understand the concept of redundancy in DBMS with a simple student table.
In this student table, we have repeated the same department details, dept_id, dept_name,
and dept_head in every student record. This causes redundancy in the student table.
Data redundancy in DBMS may occur due to any of the below reasons.
Redundancy in DMBS can cause several problems while performing operations on data
such as insert, delete, and update. Let's use the below student table to
understand insertion, updation, and deletion anomalies.
Insertion Anomaly
An insertion anomaly occurs when specific details cannot be inserted into the database
without the other details.
Example: Without knowing the department details, we cannot insert the student details in
the above table. The student details (student_id, student_name, and student_age) depends
on the department details (dept_id, dept_name, and dept_head).
Deletion Anomaly
Deletion anomaly occurs when deleting specific details loses some unrelated information
from the database.
Example:- If we delete the student with student_id 3 from the above student table, we also
lose the department details with dept_id 101. Deleting student details result in losing
unrelated department details.
Updation Anomaly
Updation anomaly occurs when there is data inconsistency resulting from a partial data
update.
Example:- We wanted to update the dept_head to Peter Parker for dept_id 101; we need
to update it in all places. If the update didn't occur in all the places (partial update), it may
result in data inconsistency.
• Data redundancy can help disaster recovery by backing up the data in a different
place.
• Data redundancy can help during malicious attacks. Data integrity can be verified if
we have multiple copies of the same data.
• Data redundancy can cause an increase in storage space due to duplicate data which
may increase the cost of the data storage.
• Data redundancy increase the size of the database, which increases the complexity
of performing operations on the data.
• Data redundancy can cause inconsistency in data due to partial updates to the
database.
42 abc CO A4
43 pqr IT A3
44 xyz CO A4
45 xyz IT A3
46 mno EC B2
47 jkl ME B2
42 abc 17
43 pqr 18
44 xyz 18
42 abc 17
43 pqr 18
44 xyz 18
42 abc 17
43 pqr 18
44 xyz 18
45 abc 19
42 abc CO 4
43 pqr EC 2
44 xyz IT 1
45 abc EC 2
Normalization
A large database defined as a single relation may result in data duplication. This
repetition of data may result in:
So to handle these problems, we should analyze and decompose the relations with
redundant data into smaller, simpler, and well-structured relations that are satisfy
What is Normalization?
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of
relations. It is also used to eliminate undesirable characteristics like
Insertion, Update, and Deletion Anomalies.
o Normalization divides the larger table into smaller and links them using
relationships.
o The normal form is used to reduce redundancy from the database table.
The main reason for normalizing the relations is removing these anomalies.
Failure to eliminate anomalies leads to data redundancy and can cause data
integrity and other problems as the database grows. Normalization consists of a
series of guidelines that helps to guide you in creating a good database structure.
Normal Description
Form
2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are
fully functional dependent on the primary key.
4NF A relation will be in 4NF if it is in Boyce Codd's normal form and has
no multi-valued dependency.
5NF A relation is in 5NF. If it is in 4NF and does not contain any join
dependency, joining should be lossless.
Advantages of Normalization
o Normalization helps to minimize data redundancy.
o Greater overall database organization.
o Data consistency within the database.
o Much more flexible database design.
o Enforces the concept of relational integrity.
Disadvantages of Normalization
o You cannot start building the database before knowing what the user needs.
o The performance degrades when normalizing the relations to higher normal
forms, i.e., 4NF, 5NF.
o It is very time-consuming and difficult to normalize relations of a higher
degree.
o Careless decomposition may lead to a bad database design, leading to
serious problems.
EMPLOYEE table:
14 John 7272826385, UP
9064738238
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
o In the second normal form, all non-key attributes are fully functional
dependent on the primary key
Example: Let's assume, a school can store the data of teachers and the subjects
they teach. In a school, a teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
A relation is in third normal form if it holds atleast one of the following conditions
for every non-trivial function dependency X → Y.
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are
non-prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Example: Let's assume there is a company where employees work in more than
one department.
EMPLOYEE table:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
264 India
EMP_DEPT table:
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
Now, this is in BCNF because left side part of both the functional dependencies is
a key
Transaction
o The transaction is a set of logically related operation. It contains a group of
tasks.
o A transaction is an action or series of actions. It is performed by a single user
to perform operations for accessing the contents of the database.
Example: Suppose an employee of bank transfers Rs 800 from X's account to Y's
account. This small transaction contains several low-level tasks:
X's Account
1. Open_Account(X)
2. Old_Balance = X.balance
3. New_Balance = Old_Balance - 800
4. X.balance = New_Balance
5. Close_Account(X)
Y's Account
1. Open_Account(Y)
2. Old_Balance = Y.balance
3. New_Balance = Old_Balance + 800
4. Y.balance = New_Balance
5. Close_Account(Y)
Operations of Transaction:
Following are the main operations of transaction:
Read(X): Read operation is used to read the value of X from the database and
stores it in a buffer in main memory.
Write(X): Write operation is used to write the value back to the database from the
buffer.
1. 1. R(X);
2. 2. X = X - 500;
3. 3. W(X);
o The first operation reads X's value from database and stores it in a buffer.
o The second operation will decrease the value of X by 500. So buffer will
contain 3500.
o The third operation will write the buffer's value to the database. So X's final
value will be 3500.
But it may be possible that because of the failure of hardware, software or power,
etc. that transaction may fail before finished all the operations in the set.
For example: If in the above transaction, the debit transaction fails after executing
operation 2 then X's value will remain 4000 in the database which is not
acceptable by the bank.
Transaction property
The transaction has the four properties. These are used to maintain consistency in
a database, before and after the transaction.
Property of Transaction
1. Atomicity
2. Consistency
3. Isolation
4. Durability
Atomicity
o It states that all operations of the transaction take place at once if not, the
transaction is aborted.
o There is no midway, i.e., the transaction cannot occur partially. Each
transaction is treated as one unit and either run to completion or is not
executed at all.
Abort: If a transaction aborts then all the changes made are not visible.
Commit: If a transaction commits then all the changes made are visible.
T1 T2
Read(A) Read(B)
A:= A-100 Y:= Y+100
Write(A) Write(B)
Consistency
o The integrity constraints are maintained so that the database is consistent
before and after the transaction.
o The execution of a transaction will leave a database in either its prior stable
state or a new stable state.
o The consistent property of database states that every transaction sees a
consistent database instance.
o The transaction is used to transform the database from one consistent state
to another consistent state.
For example: The total amount must be maintained before or after the
transaction.
Isolation
o It shows that the data which is used at the time of execution of a transaction
cannot be used by the second transaction until the first one is completed.
o In isolation, if the transaction T1 is being executed and using the data item
X, then that data item can't be accessed by any other transaction T2 until the
transaction T1 ends.
o The concurrency control subsystem of the DBMS enforced the isolation
property.
Durability
o The durability property is used to indicate the performance of the
database's consistent state. It states that the transaction made the
permanent changes.
o They cannot be lost by the erroneous operation of a faulty transaction or by
the system failure. When a transaction is completed, then the database
reaches a state known as the consistent state. That consistent state cannot
be lost, even in the event of a system's failure.
o The recovery subsystem of the DBMS has the responsibility of Durability
property.
States of Transaction
In a database, the transaction can be in one of the following states -
Active state
o The active state is the first state of every transaction. In this state, the
transaction is being executed.
o For example: Insertion or deletion or updating a record is done here. But all
the records are still not saved to the database.
Partially committed
Committed
A transaction is said to be in a committed state if it executes all its operations
successfully. In this state, all the effects are now permanently saved on the
database system.
Failed state
o If any of the checks made by the database recovery system fails, then the
transaction is said to be in the failed state.
o In the example of total mark calculation, if the database is not able to fire a
query to fetch the marks, then the transaction will fail to execute.
Aborted
o If any of the checks fail and the transaction has reached a failed state then
the database recovery system will make sure that the database is in its
previous consistent state. If not then it will abort or roll back the transaction
to bring the database into a consistent state.
o If the transaction fails in the middle of the transaction then before executing
the transaction, all the executed transactions are rolled back to its
consistent state.
o After aborting the transaction, the database recovery module will select one
of the two operations:
1. Re-start the transaction
2. Kill the transaction
Database Security
Security of databases refers to the array of controls, tools, and procedures
designed to ensure and safeguard confidentiality, integrity, and accessibility. This
tutorial will concentrate on confidentiality because it's a component that is most
at risk in data security breaches.
Security for databases must cover and safeguard the following aspects:
Insider Dangers
An insider threat can be an attack on security from any three sources having an
access privilege to the database.
Insider dangers are among the most frequent sources of security breaches to
databases. They often occur as a consequence of the inability of employees to have
access to privileged user credentials.
Human Error
The unintentional mistakes, weak passwords or sharing passwords, and other
negligent or uninformed behaviours of users remain the root causes of almost half
(49 percent) of all data security breaches.
Malware
Malware is software designed to exploit vulnerabilities or cause harm to
databases. Malware can be accessed via any device that connects to the databases
network.
Attacks on Backups
Companies that do not protect backup data using the same rigorous controls
employed to protect databases themselves are at risk of cyberattacks on backups.
o Data volumes are growing: Data capture, storage, and processing continue
to increase exponentially in almost all organizations. Any tools or methods
must be highly flexible to meet current as well as far-off needs.
o The infrastructure is sprawling: Network environments are becoming
more complicated, especially as companies shift their workloads into
multiple clouds and hybrid cloud architectures and make the selection of
deployment, management, and administration of security solutions more
difficult.
o More stringent requirements for regulatory compliance: The
worldwide regulatory compliance landscape continues to increase by
complexity. This makes the compliance of every mandate more challenging.
UNIT – IV
SQL Commands
o SQL commands are instructions. It is used to communicate with the
database. It is also used to perform specific tasks, functions, and queries of
data.
o SQL can perform various tasks like create a table, add data to tables, drop
the table, modify the table, set permission for users.
o DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
o All the command of DDL are auto-committed that means it permanently
save all the changes in the database.
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
Example:
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
Example
c. ALTER: It is used to alter the structure of the database. This change could be
either to modify the characteristics of an existing attribute or probably to add a
new attribute.
Syntax:
EXAMPLE
d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.
Syntax:
Example:
o DML commands are used to modify the database. It is responsible for all
form of changes in the database.
o The command of DML is not auto-committed that means it can't
permanently save all the changes in the database. They can be rollback.
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
Syntax:
Or
For example:
Syntax:
For example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Syntax:
For example:
o Grant
o Revoke
Example
Example
These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.
o COMMIT
o ROLLBACK
o SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
COMMIT;
Example:
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax:
ROLLBACK;
Example:
Syntax:
SAVEPOINT SAVEPOINT_NAME;
o SELECT
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT emp_name
FROM employee
WHERE age > 20;
SQL Datatype
o SQL Datatype is used to define the values that a column can contain.
o Every column is required to have a name and data type in the database table.
Datatype of SQL:
1. Binary Datatypes
There are Three types of binary Datatypes which are given below:
Data Description
Type
binary It has a maximum length of 8000 bytes. It contains fixed-length binary data.
varbinary It has a maximum length of 8000 bytes. It contains variable-length binary data.
image It has a maximum length of 2,147,483,647 bytes. It contains variable-length binary data.
float -1.79E + 308 1.79E + 308 It is used to specify a floating-point value e.g. 6.2, 2.9 etc.
Data Description
type
char It has a maximum length of 8000 characters. It contains Fixed-length non-unicode characters.
Datatype Description
timestamp It stores the year, month, day, hour, minute, and the second value.
Select all the students of department ECE whose fees is greater then equal
to 10000 and belongs to Team other than A.
σ Fees >= 10000(σClass != 'A' (Student))
Projection operation
It displays the specific column of a table. It is denoted by pie (∏). It is a vertical
subset of the original relation. It eliminates duplicate tuples.
Syntax
The syntax is as follows −
∏regno(student)
Example
Consider the student table:
1 CSE A
2 ECE B
3 CIVIL B
4 IT A
To display regno column of student table, we can use the following command −
∏regno(student)
Output
RegNo
To display branch, section column of student table, use the following command −
∏branch,section(student)
The result is as follows −
Branch Section
CSE A
ECE B
CIVIL B
IT A
Output
Regno Section
2 B
1. Union
o The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
o In the union operation, all the number of datatype and columns must be
same in both the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
Syntax
Example:
ID NAME
1 Jack
2 Harry
3 Jackson
ID NAME
3 Jackson
4 Stephan
5 David
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without
removing duplication and sorting the data.
Syntax:
UNION ALL
SELECT * FROM Second;
ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
3. Intersect
Syntax
Example:
ID NAME
3 Jackson
4. Minus
Syntax:
Example
ID NAME
1 Jack
2 Harry
Join Operations:
A Join operation combines related tuples from different relations, if and only if a
given join condition is satisfied. It is denoted by ⋈.
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Result:
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal
on their common attribute names.
o It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal
with missing information.
Example:
EMPLOYEE
FACT_WORKERS
Input:
1. (EMPLOYEE ⋈ FACT_WORKERS)
Output:
o Left outer join contains the set of tuples of all combinations in R and S that
are equal on their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by ⟕.
Input:
1. EMPLOYEE ⟕ FACT_WORKERS
o Right outer join contains the set of tuples of all combinations in R and S that
are equal on their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by ⟖.
Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
o Full outer join is like a left or right join except that it contains all rows from
both tables.
o In full outer join, tuples in R that have no matching tuples in S and tuples in
S that have no matching tuples in R in their common attribute name.
o It is denoted by ⟗.
Input:
1. EMPLOYEE ⟗ FACT_WORKERS
Output:
3. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched
data as per the equality condition. The equi join uses the comparison operator(=).
Example:
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
1. CUSTOMER ⋈ PRODUCT
Output:
1 John 1 Delhi
2 Harry 2 Mumb
3 Harry 3 Noida
Aggregate functions
Aggregate functions in DBMS take multiple rows from the table and return a value
according to the query.
All the aggregate functions are used in Select statement.
Syntax −
SELECT <FUNCTION NAME> (<PARAMETER>) FROM <TABLE NAME>
AVG Function
This function returns the average value of the numeric column that is supplied as
a parameter.
Example: Write a query to select average salary from employee table.
Select AVG(salary) from Employee
COUNT Function
The count function returns the number of rows in the result. It does not count the
null values.
Example: Write a query to return number of rows where salary > 20000.
Select COUNT(*) from Employee where Salary > 20000;
Types −
• COUNT(*): Counts all the number of rows of the table including null.
• COUNT( COLUMN_NAME): count number of non-null values in column.
• COUNT( DISTINCT COLUMN_NAME): count number of distinct values in a
column.
MAX Function
The MAX function is used to find maximum value in the column that is supplied
as a parameter. It can be used on any type of data.
Example − Write a query to find the maximum salary in employee table.
Select MAX(salary) from Employee
SUM Function
This function sums up the values in the column supplied as a parameter.
Example: Write a query to get the total salary of employees.
Select SUM(salary) from Employee
STDDEV Function
The STDDEV function is used to find standard deviation of the column specified
as argument.
Example − Write a query to find standard deviation of salary in Employee table.
Select STDDEV(salary) from Employee
VARIANCE Function
The VARIANCE Function is used to find variance of the column specified as
argument.
Example −
Select VARIANCE(salary) from Employee
Important Rule:
Syntax
1. SELECT column_name
2. FROM table_name
3. WHERE column_name expression operator
4. ( SELECT column_name from table_name WHERE ... );
Example
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
1. SELECT *
2. FROM EMPLOYEE
3. WHERE ID IN (SELECT ID
4. FROM EMPLOYEE
5. WHERE SALARY > 4500);
4 Alina 29 UK 6500.00
Syntax:
Example
Now use the following syntax to copy the complete EMPLOYEE table into the
EMPLOYEE_BKP table.
Syntax
1. UPDATE table
Example
1. UPDATE EMPLOYEE
2. SET SALARY = SALARY * 0.25
3. WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
4. WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have the
following records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
Syntax
Example
This would impact three rows, and finally, the EMPLOYEE table would have the
following records.
1 John 20 US 2000.00
UNIT – V
PL/SQL Structure:
In PL/SQL, the code is not executed in single line format, but it is always
executed by grouping the code into a single element called Blocks. In this
tutorial, you are going to learn about these blocks.
Block Structure
PL/SQL blocks have a pre-defined structure in which the code is to be
grouped. Below are different sections of PL/SQL blocks.
1. Declaration section
2. Execution section
3. Exception-Handling section
The below picture illustrates the different PL/SQL block and their
section order.
Declaration Section
This is the first section of the PL/SQL blocks. This section is an optional
part. This is the section in which the declaration of variables, cursors,
exceptions, subprograms, pragma instructions and collections that are
needed in the block will be declared. Below are few more characteristics
of this part.
Execution Section
Execution part is the main and mandatory part which actually executes
the code that is written inside it. Since the PL/SQL expects the executable
statements from this block this cannot be an empty block, i.e., it should
have at least one valid executable code line in it. Below are few more
characteristics of this part.
Exception-Handling Section:
The exception is unavoidable in the program which occurs at run-time
and to handle this Oracle has provided an Exception-handling section in
blocks. This section can also contain PL/SQL statements. This is an
optional section of the PL/SQL blocks.
DECLARE --optional
<declarations>
BEGIN --mandatory
<executable statements. At least one executable statement is mandatory>
EXCEPTION --optional
<exception handles>
END; --mandatory
/
Note: A block should always be followed by ‘/’ which sends the
information to the compiler about the end of the block.
1. Anonymous blocks
2. Named Blocks
Anonymous blocks:
Anonymous blocks are PL/SQL blocks which do not have any names
assigned to them. They need to be created and used in the same session
because they will not be stored in the server as database objects.
Since they need not store in the database, they need no compilation
steps. They are written and executed directly, and compilation and
execution happen in a single process.
• These blocks don’t have any reference name specified for them.
• These blocks start with the keyword ‘DECLARE’ or ‘BEGIN’.
• Since these blocks do not have any reference name, these cannot be
stored for later purpose. They shall be created and executed in the
same session.
• They can call the other named blocks, but call to anonymous block
is not possible as it is not having any reference.
• It can have nested block in it which can be named or anonymous. It
can also be nested in any blocks.
• These blocks can have all three sections of the block, in which
execution section is mandatory, the other two sections are
optional.
Named blocks:
Named blocks have a specific and unique name for them. They are stored
as the database objects in the server. Since they are available as database
objects, they can be referred to or used as long as it is present on the
server. The compilation process for named blocks happens separately
while creating them as a database objects.
1. Procedure
2. Function
Elements of PL/SQL
• PL/SQL block
• Function
• Package
• Package body
• Procedure
• Trigger
• Type
• Type body
PL/SQL OPERATORS:
An operator is a symbol that tells the compiler to perform specific mathematical
or logical manipulation. PL/SQL language is rich in built-in operators and provides
the following types of operators −
• Arithmetic operators
• Relational operators
• Comparison operators
• Logical operators
• String operators
Here, we will understand the arithmetic, relational, comparison and logical
operators one by one. The String operators will be discussed in a later chapter
− PL/SQL - Strings.
Arithmetic Operators
Following table shows all the arithmetic operators supported by PL/SQL. Let us
assume variable A holds 10 and variable B holds 5, then −
Show Examples
Relational Operators
Relational operators compare two expressions or values and return a Boolean
result. Following table shows all the relational operators supported by PL/SQL.
Let us assume variable A holds 10 and variable B holds 20, then –
Show Examples
Checks if the values of two operands are equal or not, if yes then (A = B) is not
=
condition becomes true. true.
!=
Checks if the values of two operands are equal or not, if values are (A != B) is
<> not equal then condition becomes true. true.
~=
Checks if the value of left operand is greater than the value of right (A > B) is not
>
operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than the value of right (A < B) is true.
<
operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to the (A >= B) is not
>=
value of right operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than or equal to the value (A <= B) is
<=
of right operand, if yes then condition becomes true. true
Comparison Operators
Comparison operators are used for comparing one expression to another. The
result is always either TRUE, FALSE or NULL.
Show Examples
The LIKE operator compares a character, If 'Zara Ali' like 'Z% A_i' returns a
string, or CLOB value to a pattern and returns Boolean true, whereas, 'Nuha Ali'
LIKE
TRUE if the value matches the pattern and like 'Z% A_i' returns a Boolean
FALSE if it does not. false.
The IN operator tests set membership. x IN If x = 'm' then, x in ('a', 'b', 'c')
IN (set) means that x is equal to any member of returns Boolean false but x in ('m',
set. 'n', 'o') returns Boolean true.
Logical Operators
Following table shows the Logical operators supported by PL/SQL. All these
operators work on Boolean operands and produce Boolean results. Let us
assume variable A holds true and variable B holds false, then −
Show Examples
Called the logical AND operator. If both the operands are true then (A and B) is
and
condition becomes true. false.
Called the logical NOT Operator. Used to reverse the logical state of not (A and B)
not its operand. If a condition is true then Logical NOT operator will is true.
make it false.
Operator Operation
** exponentiation
+, - identity, negation
*, / multiplication, division
comparison
AND conjunction
OR inclusion
PL/SQL IF Statements
1. IF-THEN Statement
Syntax
IF condition
THEN
Statement;
END IF;
This syntax is used when user needs to execute statements when condition is true.
2. IF-THEN-ELSE Statement
Syntax
IF condition
THEN
[Statements to execute when condition is TRUE]
ELSE
[Statements to execute when condition is FALSE]
END IF;
This syntax is used to execute one set of statements when condition is TRUE or
different set of statements when condition is FALSE.
3. IF-THEN-ELSIF statement
Syntax
IF Condition1
THEN
Statements to execute when condition1 is TRUE
ELSIF condition2
THEN
Statements to execute when condition2 is TRUE
END IF;
This syntax is used to execute one set of statements when condition1 is TRUE or a
different set of statements when condition is FALSE.
4. IF-THEN-ELS-IF-ELSE Statement
Syntax
IF condition1
THEN
Statements to execute when condition1 is TRUE
ELSIF condition2
THEN
Statements to execute when condition2 is TRUE
ELSE
Statements to execute when both condition1 and condition2 are FALSE
END IF;
This syntax is used to execute one set of statements if condition1 is TRUE, a different
set of statements when condition2 is TRUE or a third set of statements when both
condition1 and condition2 are false.
Example
DECLARE
a number(3) := 200;
BEGIN
-- check the boolean condition using if statement
IF( a < 10 ) THEN
-- if condition is true then print the following
dbms_output.put_line('a is less than 10 ' );
ELSE
dbms_output.put_line('a is not less than 10 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
Output:
a is not less than 10
value of a is : 200
Syntax
CASE [expression]
WHEN condition1 THEN result1
WHEN condition2 THEN result2
…....................
WHEN condition_n THEN result_n
ELSE result
END;
Example
DECLARE
grade char(1) := 'C';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Distinction');
when 'B' then dbms_output.put_line('First class');
when 'C' then dbms_output.put_line('Second class');
when 'D' then dbms_output.put_line('Pass class');
else dbms_output.put_line('Failed');
END CASE;
END;
/
Output:
Second class
PL/SQL Loop
Syntax
LOOP
Sequence of statements;
END LOOP;
LOOP
Statements;
EXIT;
[or EXIT WHEN condition;]
END LOOP;
Example
DECLARE
i NUMBER := 1;
BEGIN
LOOP
EXIT WHEN i>5;
dbms_output.put_line(i);
i := i+1;
END LOOP;
END;
Output:
1
2
3
4
5
WHILE [condition]
LOOP Statements;
END LOOP;
Example
DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 5 LOOP
dbms_output.put_line(i);
i := i+1;
END LOOP;
END;
Output:
1
2
3
4
5
Example
BEGIN
FOR k IN 1..5 LOOP
dbms_output.put_line(k)
END LOOP;
END;
Output:
1
2
3
4
Syntax
I) GOTO label_name;
In the above syntax, the label declaration contains the label_name is encapsulated
within the << >> symbol and it should be followed by at least one statement to execute.
Example
DECLARE
a number(2) := 50;
BEGIN
<<loopstart>>
-- while loop execution
WHILE a < 60LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 55 THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;
/
Output:
value of a: 50
value of a: 51
value of a: 52
value of a: 53
value of a: 54
value of a: 56
value of a: 57
value of a: 58
value of a: 59
Cursor function:
A cursor is a pointer to this context area. PL/SQL controls the context area
through a cursor. A cursor holds the rows (one or more) returned by a SQL
statement. The set of rows the cursor holds is referred to as the active set.
You can name a cursor so that it could be referred to in a program to fetch and
process the rows returned by the SQL statement, one at a time. There are two types
of cursors −
• Implicit cursors
• Explicit cursors
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.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor,
which always has attributes such as %FOUND, %ISOPEN, %NOTFOUND,
and %ROWCOUNT. The SQL cursor has additional
attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed for use
with the FORALL statement. The following table provides the description of the
most used attributes −
%FOUND
1 Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more
rows or a SELECT INTO statement returned one or more rows. Otherwise, it returns
FALSE.
%NOTFOUND
2 The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or DELETE
statement affected no rows, or a SELECT INTO statement returned no rows.
Otherwise, it returns FALSE.
3 %ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
automatically after executing its associated SQL statement.
%ROWCOUNT
4
Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement,
or returned by a SELECT INTO statement.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
The following program will update the table and increase the salary of each
customer by 500 and use the SQL%ROWCOUNT attribute to determine the
number of rows affected −
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
/
When the above code is executed at the SQL prompt, it produces the following
result −
6 customers selected
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2500.00 |
| 2 | Khilan | 25 | Delhi | 2000.00 |
| 3 | kaushik | 23 | Kota | 2500.00 |
| 4 | Chaitali | 25 | Mumbai | 7000.00 |
| 5 | Hardik | 27 | Bhopal | 9000.00 |
| 6 | Komal | 22 | MP | 5000.00 |
+----+----------+-----+-----------+----------+
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over
the context area. An explicit cursor should be defined in the declaration section
of the PL/SQL Block. It is created on a SELECT Statement which returns more than
one row.
The syntax for creating an explicit cursor is −
CURSOR cursor_name IS select_statement;
Working with an explicit cursor includes the following steps −
END LOOP;
CLOSE c_customers;
END;
/
When the above code is executed at the SQL prompt, it produces the following
result −
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
PL/SQL Procedure
The PL/SQL stored procedure or simply a procedure is a PL/SQL block which
performs one or more specific tasks. It is just like procedures in other
programming languages.
o Header: The header contains the name of the procedure and the
parameters or variables passed to the procedure.
o Body: The body contains a declaration section, execution section and
exception section similar to a general PL/SQL block.
Table creation:
Procedure Code:
Output:
Procedure created.
1. BEGIN
2. insertuser(101,'Rahul');
3. dbms_output.put_line('record inserted successfully');
4. END;
5. /
Now, see the "USER" table, you will see one record is inserted.
ID Name
101 Rahul
PL/SQL Function
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference
between procedure and a function is, a function must always return a value, and
on the other hand a procedure may or may not return a value. Except this, all the
other things of PL/SQL procedure are true for PL/SQL function too.
Here:
o RETURN clause specifies that data type you are going to return from the
function.
o Function_body contains the executable part.
o The AS keyword is used instead of the IS keyword for creating a standalone
function.
1. DECLARE
2. n3 number(2);
3. BEGIN
4. n3 := adder(11,22);
5. dbms_output.put_line('Addition is: ' || n3);
6. END;
7. /
Output:
Addition is: 33
Statement processed.
0.05 seconds
1. DECLARE
2. a number;
3. b number;
4. c number;
5. FUNCTION findMax(x IN number, y IN number)
6. RETURN number
7. IS
8. z number;
9. BEGIN
10. IF x > y THEN
11. z:= x;
12. ELSE
13. Z:= y;
14. END IF;
15.
16. RETURN z;
17.END;
18.BEGIN
19. a:= 23;
20. b:= 45;
21.
22. c := findMax(a, b);
23. dbms_output.put_line(' Maximum of (23,45): ' || c);
24.END;
25./
Output:
Maximum of (23,45): 45
Statement processed.
0.02 seconds
Customers
Create Function:
After the execution of above code, you will get the following result.
Function created.
While creating a function, you have to give a definition of what the function has to
do. To use a function, you will have to call that function to perform the defined
task. Once the function is called, the program control is transferred to the called
function.
After the successful completion of the defined task, the call function returns
program control back to the main program.
To call a function you have to pass the required parameters along with function
name and if function returns a value then you can store returned value. Following
program calls the function totalCustomers from an anonymous block:
1. DECLARE
2. c number(2);
3. BEGIN
4. c := totalCustomers();
After the execution of above code in SQL prompt, you will get the following result.
Total no. of Customers: 4
PL/SQL procedure successfully completed.
1. DECLARE
2. num number;
3. factorial number;
4.
5. FUNCTION fact(x number)
6. RETURN number
7. IS
8. f number;
9. BEGIN
10. IF x=0 THEN
11. f := 1;
12. ELSE
13. f := x * fact(x-1);
14. END IF;
15.RETURN f;
16.END;
17.
18.BEGIN
19. num:= 6;
20. factorial := fact(num);
21. dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
22.END;
23./
After the execution of above code at SQL prompt, it produces the following result.
Factorial 6 is 720
PL/SQL procedure successfully completed.
If you want to remove your created function from the database, you should use the
following syntax.
Packages:
Packages are schema objects that groups logically related PL/SQL types, variables,
and subprograms.
A package will have two mandatory parts −
• Package specification
• Package body or definition
Package Specification
The specification is the interface to the package. It just DECLARES the types,
variables, constants, exceptions, cursors, and subprograms that can be referenced
from outside the package. In other words, it contains all information about the
content of the package, but excludes the code for the subprograms.
All objects placed in the specification are called public objects. Any subprogram
not in the package specification but coded in the package body is called
a private object.
The following code snippet shows a package specification having a single
procedure. You can have many global variables defined and multiple procedures
or functions inside a package.
CREATE PACKAGE cust_sal AS
PROCEDURE find_sal(c_id customers.id%type);
END cust_sal;
/
When the above code is executed at the SQL prompt, it produces the following
result −
Package created.
Package Body
The package body has the codes for various methods declared in the package
specification and other private declarations, which are hidden from the code
outside the package.
The CREATE PACKAGE BODY Statement is used for creating the package body.
The following code snippet shows the package body declaration for
the cust_sal package created above. I assumed that we already have CUSTOMERS
table created in our database as mentioned in the PL/SQL - Variables chapter.
CREATE OR REPLACE PACKAGE BODY cust_sal AS
FROM customers
WHERE id = c_id;
dbms_output.put_line('Salary: '|| c_sal);
END find_sal;
END cust_sal;
/
When the above code is executed at the SQL prompt, it produces the following
result −
Package body created.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
-- Removes a customer
PROCEDURE delCustomer(c_id customers.id%TYPE);
--Lists all customers
PROCEDURE listCustomer;
END c_package;
/
When the above code is executed at the SQL prompt, it creates the above package
and displays the following result −
Package created.
Creating the Package Body
CREATE OR REPLACE PACKAGE BODY c_package AS
PROCEDURE addCustomer(c_id customers.id%type,
c_name customers.Name%type,
c_age customers.age%type,
c_addr customers.address%type,
c_sal customers.salary%type)
IS
BEGIN
INSERT INTO customers (id,name,age,address,salary)
VALUES(c_id, c_name, c_age, c_addr, c_sal);
END addCustomer;
WHERE id = c_id;
END delCustomer;
PROCEDURE listCustomer IS
CURSOR c_customers is
SELECT name FROM customers;
TYPE c_list is TABLE OF customers.Name%type;
name_list c_list := c_list();
counter integer :=0;
BEGIN
FOR n IN c_customers LOOP
counter := counter +1;
name_list.extend;
name_list(counter) := n.name;
dbms_output.put_line('Customer(' ||counter|| ')'||name_list(counter));
END LOOP;
END listCustomer;
END c_package;
/
The above example makes use of the nested table. We will discuss the concept of
nested table in the next chapter.
When the above code is executed at the SQL prompt, it produces the following
result −
Package body created.
Using The Package
The following program uses the methods declared and defined in the
package c_package.
DECLARE
code customers.id%type:= 8;
BEGIN
c_package.addcustomer(7, 'Rajnish', 25, 'Chennai', 3500);
c_package.addcustomer(8, 'Subham', 32, 'Delhi', 7500);
c_package.listcustomer;
c_package.delcustomer(code);
c_package.listcustomer;
END;
/
When the above code is executed at the SQL prompt, it produces the following
result −
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish
Customer(8): Subham
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish
Exception Handling
An exception is an error condition during a program execution. PL/SQL supports
programmers to catch such conditions using EXCEPTION block in the program
and an appropriate action is taken against the error condition. There are two types
of exceptions −
• System-defined exceptions
• User-defined exceptions
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
When the above code is executed at the SQL prompt, it produces the following
result −
No such customer!
Raising Exceptions
Exceptions are raised by the database server automatically whenever there is any
internal database error, but exceptions can be raised explicitly by the programmer
by using the command RAISE. Following is the simple syntax for raising an
exception −
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;
END;
You can use the above syntax in raising the Oracle standard exception or any user-
defined exception. In the next section, we will give you an example on raising a
user-defined exception. You can raise the Oracle standard exceptions in a similar
way.
User-defined Exceptions
PL/SQL allows you to define your own exceptions according to the need of your
program. A user-defined exception must be declared and then raised explicitly,
using either a RAISE statement or the
procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR.
The syntax for declaring an exception is −
DECLARE
my-exception EXCEPTION;
Example
The following example illustrates the concept. This program asks for a customer
ID, when the user enters an invalid ID, the exception invalid_id is raised.
DECLARE
c_id customers.id%type := &cc_id;
c_name customerS.Name%type;
c_addr customers.address%type;
-- user defined exception
ex_invalid_id EXCEPTION;
BEGIN
IF c_id <= 0 THEN
RAISE ex_invalid_id;
ELSE
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
END IF;
EXCEPTION
WHEN ex_invalid_id THEN
dbms_output.put_line('ID must be greater than zero!');
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
When the above code is executed at the SQL prompt, it produces the following
result −
Enter value for cc_id: -6 (let's enter a value -6)
old 2: c_id customers.id%type := &cc_id;
new 2: c_id customers.id%type := -6;
ID must be greater than zero!
Pre-defined Exceptions
PL/SQL provides many pre-defined exceptions, which are executed when any
database rule is violated by a program. For example, the predefined exception
NO_DATA_FOUND is raised when a SELECT INTO statement returns no rows. The
following table lists few of the important pre-defined exceptions −
Oracle
Exception SQLCODE Description
Error
Triggers
Triggers are stored programs, which are automatically executed or fired when
some events occur. Triggers are, in fact, written to be executed in response to any
of the following events −
• A database manipulation (DML) statement (DELETE, INSERT, or
UPDATE)
• A database definition (DDL) statement (CREATE, ALTER, or DROP).
• A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).
Triggers can be defined on the table, view, schema, or database with which the
event is associated.
Benefits of Triggers
Triggers can be written for the following purposes −
Creating 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)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Where,
• CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an
existing trigger with the trigger_name.
• {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be
executed. The INSTEAD OF clause is used for creating trigger on a view.
• {INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML operation.
• [OF col_name] − This specifies the column name that will be updated.
• [ON table_name] − This specifies the name of the table associated with the
trigger.
• [REFERENCING OLD AS o NEW AS n] − This allows you to refer new and old
values for various DML statements, such as INSERT, UPDATE, and DELETE.
• [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected. Otherwise the trigger will execute just
once when the SQL statement is executed, which is called a table level
trigger.
• WHEN (condition) − This provides a condition for rows for which the trigger
would fire. This clause is valid only for row-level triggers.
Example
To start with, we will be using the CUSTOMERS table we had created and used in
the previous chapters −
Select * from customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
The following program creates a row-level trigger for the customers table that
would fire for INSERT or UPDATE or DELETE operations performed on the
CUSTOMERS table. This trigger will display the salary difference between the old
values and new values −
Triggering a Trigger
Let us perform some DML operations on the CUSTOMERS table. Here is one
INSERT statement, which will create a new record in the table −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (7, 'Kriti', 22, 'HP', 7500.00 );
When a record is created in the CUSTOMERS table, the above create
trigger, display_salary_changes will be fired and it will display the following
result −
Old salary: