Dbms Notes
Dbms Notes
Relational Algebra
These are the basic/fundamental operators used in Relational Algebra.
1. Selection(ς) 2.Projection(π) 3.Union(U) 4.Set Difference(-) 5.Set Intersection(∩)
6. Rename(ρ) 7.Cartesian Product(X)
1.Selection(ς): It is used to select required tuples of the relations.2.Projection(π): It is
used to project required column data from a relation. 3.Union(U): Union operation in
relational algebra is the same as union operation in set theory.4.Set Difference(-): Set
Difference in relational algebra is the same set difference operation as in set
theory.5.Set Intersection(∩): Set Intersection in relational algebra is the same set
intersection operation in set theory.6.Rename(ρ): Rename is a unary operation used
for renaming attributes of a relation.7.Cross Product(X): Cross-product between two
relations. Let’s say A and B, so the cross product between A X B will result in all the
attributes of A followed by each attribute of B. Each record of A will pair with every
record of B.
What does a Transaction mean in DBMS?
Transaction in Database Management Systems (DBMS) can be defined as a set of logically
related operations.
It is the result of a request made by the user to access the contents of the database and perform
operations on it.
It consists of various operations and has various states in its completion journey.
It also has some specific properties that must be followed to keep the database consistent.
Operations of Transaction
i) Read(X)
A read operation is used to read the value of X from the database and store it in a buffer
in the main memory for further actions such as displaying that value.
Such an operation is performed when a user wishes just to see any content of the
database and not make any changes to it. For example, when a user wants to
check his/her account’s balance, a read operation would be performed on user’s account
balance from the database.
ii) Write(X)
A write operation is used to write the value to the database from the buffer in the main
memory. For a write operation to be performed, first a read operation is performed to
bring its value in buffer, and then some changes are made to it, e.g. some set of
arithmetic operations are performed on it according to the user’s request, then to store
the modified value back in the database, a write operation is performed.
iii) Commit
This operation in transactions is used to maintain integrity in the database. Due to some
failure of power, hardware, or software, etc., a transaction might get interrupted before
all its operations are completed. This may cause ambiguity in the database, i.e. it might
get inconsistent before and after the transaction.
To ensure that further operations of any other transaction are performed only after work
of the current transaction is done, a commit operation is performed to the changes made
by a transaction permanently to the database.
iv) Rollback
This operation is performed to bring the database to the last saved state when any
transaction is interrupted in between due to any power, hardware, or software failure.
In simple words, it can be said that a rollback operation does undo the operations of
transactions that were performed before its interruption to achieve a safe state of the
database and avoid any kind of ambiguity or inconsistency.
Properties of Transaction
As transactions deal with accessing and modifying the contents of the database, they
must have some basic properties which help maintain the consistency and integrity of the
database before and after the transaction. Transactions follow 4 properties,
namely, Atomicity, Consistency, Isolation, and Durability.
i) Atomicity
This property ensures that either all operations of a transaction are executed or it is
aborted. In any case, a transaction can never be completed partially.
Each transaction is treated as a single unit (like an atom). Atomicity is achieved through
commit and rollback operations, i.e. changes are made to the database only if all
operations related to a transaction are completed, and if it gets interrupted, any changes
made are rolled back using rollback operation to bring the database to its last saved state.
ii) Consistency
This property of a transaction keeps the database consistent before and after a
transaction is completed.
Execution of any transaction must ensure that after its execution, the database is either in
its prior stable state or a new stable state.
In other words, the result of a transaction should be the transformation of a database
from one consistent state to another consistent state.
Consistency, here means, that the changes made in the database are a result of logical
operations only which the user desired to perform and there is not any ambiguity.
iii) Isolation
This property states that two transactions must not interfere with each other, i.e. if some
data is used by a transaction for its execution, then any other transaction can not
concurrently access that data until the first transaction has completed.
It ensures that the integrity of the database is maintained and we don’t get any
ambiguous values. Thus, any two transactions are isolated from each other.
This property is enforced by the concurrency control subsystem of DBMS.
iv) Durability
This property ensures that the changes made to the database after a transaction is
completely executed, are durable.
It indicates that permanent changes are made by the successful execution of a
transaction.
In the event of any system failures or crashes, the consistent state achieved after the
completion of a transaction remains intact. The recovery subsystem of DBMS is
responsible for enforcing this property.
Normalization is the process of minimizing redundancy from a relation or set of relations.
Redundancy in relation may cause insertion, deletion, and update anomalies. So, it helps to minimize
the redundancy in relations. Normal forms are used to eliminate or reduce redundancy in database
tables.
Important Points Regarding Normal Forms in DBMS
First Normal Form (1NF): This is the most basic level of normalization. In 1NF, each table cell
should contain only a single value, and each column should have a unique name. The first normal
form helps to eliminate duplicate data and simplify queries.
Second Normal Form (2NF): 2NF eliminates redundant data by requiring that each non-key
attribute be dependent on the primary key. This means that each column should be directly
related to the primary key, and not to other columns.
Third Normal Form (3NF): 3NF builds on 2NF by requiring that all non-key attributes are
independent of each other. This means that each column should be directly related to the
primary key, and not to any other columns in the same table.
Fourth Normal Form (4NF): 4NF is a further refinement of BCNF that ensures that a table does
not contain any multi-valued dependencies.
Fifth Normal Form (5NF): 5NF is the highest level of normalization and involves decomposing a
table into smaller tables to remove data redundancy and improve data integrity.
Normal forms help to reduce data redundancy, increase data consistency, and improve database
performance. However, higher levels of normalization can lead to more complex database designs
and queries. It is important to strike a balance between normalization and practicality when
designing a database.
Advantages of Normal Form
Reduced data redundancy
Improved data consistency
Simplified database design
Improved query performance
Easier database maintenance
Query By Example (QBE) . QBE stands for Query By Example and it was developed in 1970 by
Moshe Zloof at IBM. It is a graphical query language where we get a user interface and then
we fill some required fields to get our proper result. In SQL we will get an error if the query is
not correct but in the case of QBE if the query is wrong either we get a wrong answer or the
query will not be going to execute but we will never get any error. In QBE we don’t write
complete queries like SQL or other database languages it comes with some blank so we need
to just fill that blanks and we will get our required result. SELECT NAME FROM
COLLEGE_DEPTc WHERE BRANCH = 'MCA'" Points about QBE: • Supported by most of the
database programs. • It is a Graphical Query Language. • Created in parallel to SQL
development. Features: – 2-D relation language. – Display results. – Fill up templates of
relations for query formulation. – Example of a possible answer is entered by the user.
SUBQUERY
You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause,
FROM clause. Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements
along with expression operator. It could be equality operator or comparison operator
such as =, >, =, <= and Like operator.
A subquery is a query within another query. The outer query is called as main query and
inner query is called as subquery.
The subquery generally executes first when the subquery doesn’t have any co-
relation with the main query, when there is a co-relation the parser takes the decision on
the fly on which query to execute on precedence and uses the output of the subquery
accordingly.
Subquery must be enclosed in parentheses.
Subqueries are on the right side of the comparison operator.
Use single-row operators with single row Subqueries. Use multiple-row operators with
multiple-row Subqueries.
Syntax: There is not any general syntax for Subqueries. However, Subqueries are seen to be
used most frequently with SELECT statement as shown below:
SELECT column_name
FROM table_name
WHERE column_name expression operator
(SELECT column_name FROM table_name WHERE ...);
Decision-making statements in programming languages decide the direction of the flow of
program execution. Conditional Statements available in PL/SQL are defined below:
1. IF THEN
2. IF THEN ELSE
3. NESTED-IF-THEN
4. IF THEN ELSIF-THEN-ELSE Ladder
1. IF THEN
if then the statement is the most simple decision-making statement. It is used to decide
whether a certain statement or block of statements will be executed or not i.e if a certain
condition is true then a block of statement is executed otherwise not.
Syntax:
declare
-- declare the values here
begin
if condition then
dbms_output.put_line('output');
end if;
dbms_output.put_line('output2');
end;
2. IF THEN ELSE
The if statement alone tells us that if a condition is true it will execute a block of statements
and if the condition is false it won’t. But what if we want to do something else if the
condition is false. Here comes the else statement. We can use the else statement with if
statement to execute a block of code when the condition is false.
Syntax:-
declare
num1 number:= 10;
num2 number:= 20;
begin
if num1 < num2 then
dbms_output.put_line('i am in if block');
ELSE
dbms_output.put_line('i am in else Block');
end if;
dbms_output.put_line('i am not in if or else Block');
end;
3. NESTED-IF-THEN
A nested if-then is an if statement that is the target of another if statement. Nested if-then
statements mean an if statement inside another if statement. Yes, PL/SQL allows us to nest if
statements within if-then statements. i.e, we can place an if then statement inside another if
then statement.
Syntax:-
declare
num1 number:= 10;
num2 number:= 20;
num3 number:= 20;
begin
if num1 < num2 then
dbms_output.put_line('num1 small num2');
if num1 < num3 then
dbms_output.put_line('num1 small num3 also');
end if;
end if;
dbms_output.put_line('after end if');
end;
A Cursor in PL/SQL is a pointer to a context area that stores the result set of a query.
PL/SQL Cursors
The cursor is used to retrieve data one row at a time from the results set, unlike other SQL
commands that operate on all rows at once.
Cursors update table records in a singleton or row-by-row manner.
Cursor Actions
Key actions involved in working with cursors in PL/SQL are:
1. Declare Cursor: A cursor is declared by defining the SQL statement that returns a result
set.
2. Open: A Cursor is opened and populated by executing the SQL statement defined by the
cursor.
3. Fetch: When the cursor is opened, rows can be fetched from the cursor one by one or in a
block to perform data manipulation.
4. Close: After data manipulation, close the cursor explicitly.
5. Deallocate: Finally, delete the cursor definition and release all the system resources
associated with the cursor.
Types of Cursors in PL/SQL
Cursors are classified depending on the circumstances in which they are opened.
Implicit Cursor: If the Oracle engine opened a cursor for its internal processing it is known
as an Implicit Cursor. It is created “automatically” for the user by Oracle when a query is
executed and is simpler to code.
Explicit Cursor: A Cursor can also be opened for processing data through a PL/SQL block,
on demand. Such a user-defined cursor is known as an Explicit Cursor.
Explicit cursor
An explicit cursor is defined in the declaration section of the PL/SQL Block. It is created on
a SELECT Statement which returns more than one row.
Where,
o Header: The header contains the name of the procedure and the parameters or variables
passed to the procedure.
o Body: The body contains a declaration section, execution section and exception section similar
to a general PL/SQL block.
1. IN parameters: The IN parameter can be referenced by the procedure or function. The value of the
parameter cannot be overwritten by the procedure or the function.
2. OUT parameters: The OUT parameter cannot be referenced by the procedure or function, but the
value of the parameter can be overwritten by the procedure or function.
3. INOUT parameters: The INOUT parameter can be referenced by the procedure or function and the
value of the parameter can be overwritten by the procedure or function.