notes dbms2
notes dbms2
RELATIONAL MODEL
What is a relational database?
Developed by E.F. Codd from IBM in the 1970s, the relational database model allows any table to be
related to another table using a common attribute. Instead of using hierarchical structures to
organize data, Codd proposed a shift to using a data model where data is stored, accessed, and
related in tables without reorganizing the tables that contain them.
Think of the relational database as a collection of spreadsheet files that help businesses organize,
manage, and relate data. In the relational database model, each “spreadsheet” is a table that stores
information, represented as columns (attributes) and rows (records or tuples).
Attributes (columns) specify a data type, and each record (or row) contains the value of that specific
data type. All tables in a relational database have an attribute known as the primary key, which is
a unique identifier of a row, and each row can be used to create a relationship between different
tables using a foreign key—a reference to a primary key of another existing table.
The Customer table contains data about the customer:
In the Customer table, the customer ID is a primary key that uniquely identifies who the customer
is in the relational database. No other customer would have the same Customer ID.
Here, the primary key to identify a specific order is the Order ID. You can connect a customer with
an order by using a foreign key to link the customer ID from the Customer table.
The two tables are now related based on the shared customer ID, which means you can query both
tables to create formal reports or use the data for other applications. For instance, a retail branch
manager could generate a report about all customers who made a purchase on a specific date or
figure out which customers had orders that had a delayed delivery date in the last month.
The above explanation is meant to be simple. But relational databases also excel at showing very
complex relationships between data, allowing you to reference data in more tables as long as the
data conforms to the predefined relational schema of your database.
As the data is organized as pre-defined relationships, you can query the data declaratively. A
declarative query is a way to define what you want to extract from the system without expressing
how the system should compute the result. This is at the heart of a relational system as opposed to
other systems.
Examples of relational databases
Now that you understand how relational databases work, you can begin to learn about the many
relational database management systems that use the relational database model. A relational
database management system (RDBMS) is a program used to create, update, and manage relational
databases. Some of the most well-known RDBMSs include MySQL, PostgreSQL, MariaDB, Microsoft
SQL Server, and Oracle Database.
Cloud-based relational databases like Cloud SQL, Cloud Spanner and AlloyDB have become
increasingly popular as they offer managed services for database maintenance, patching, capacity
management, provisioning and infrastructure support.
Relational Algebra is a procedural query language that provides a set of operations to manipulate
and retrieve data stored in a relational database. It forms the theoretical foundation for SQL
(Structured Query Language) and is widely used in Database Management Systems (DBMS).
Here’s an overview of relational algebra in DBMS:
Key Concepts
Importance in DBMS
Foundation for SQL: Relational algebra operations correspond to SQL operations such as
SELECT, JOIN, UNION, and WHERE.
Query Optimization: DBMSs often optimize SQL queries by converting them into relational
algebra expressions to determine the most efficient way to retrieve data.
Data Manipulation: Relational algebra defines how data is manipulated (inserted, updated,
deleted) in a relational database.
Basic operations:
1. Selection (σ) Selects a subset of rows from relation.
2. Projection (π) Selects a subset of columns from relation.
3. Cross-product (×) Allows us to combine two relations.
4. Set-difference ( ) Tuples in relation. 1, but not in relationn. 2.
5. Union (U) Tuples in reln. 1 and in reln. 2.
6. Rename( ρ) Use new name for the Tables
or fields. Additional operations:
7. Intersection (∩), Join( ), Division(÷): Not essential, but (very!)
useful. Since each operation returns a relation, operations can be
composed! (Algebra is
―closed‖.)
Projection
Title
πTitle(BOOK) = ―DBMS‖
―COMPILER‖
―OS‖
Selection
All of these operations take two input relations, which must be union-compatible:
o Same number of fields.
o Corresponding‘ fields have the same type.
What is the schema of
result? Consider:
Borrower Depositor
Cust- Loan-no Cust-name Acc-no
name Suleman A-100
Ram L-13 Radheshyam A-300
Shyam L-30 Ram A-401
Suleman L-42
Cust-name
Ram
Shyam
Suleman Customers who are both borrowers and depositors =
Radeshyam πCust-name (Borrower) ∩ πCust-name (Depositor)=
Cust-
name
Ram
Suleman
Customers who are borrowers but not depositors = πCust-name (Borrower)
πCust-
name (Depositor)=
Cust-name
Shyam
Borrower: Loan:
Cust-name Loan-no Loan-no Amount
Ram L-13 L-13 1000
Shyam L-30 L-30 20000
Suleman L-42 L-42 40000
The rename operation can be used to rename the fields to avoid confusion when two field
names are same in two participating tables:
Loan-borrower:
Cust Loan-No- Loan Amount
- 1 - No-
nam 2
e
Ram L-13 L-13 1000
Ram L-13 L-30 20000
Ram L-13 L-42 40000
Shyam L-30 L-13 1000
Shyam L-30 L-30 20000
Shyam L-30 L-42 40000
Suleman L-42 L-13 1000
Suleman L-42 L-30 20000
Suleman L-42 L-42 40000
Rename Operation
The rename operator ρ is one of the unary operators in relational algebra and is used to rename
relations in a DBMS. In other words, some relations or attributes may have complex names and can be
changed to make writing queries easier.
This relational operator is a base for SQL (Structured Query Language) and is used to rename relations.
Syntax
ρnew_relation_name(new_attribute_name_list)(R)ρnew_relation_name(new_attribute_name_list)(R)
The new_relation_namenew_relation_name contains the name we want to replace with the original
relation RR and the new_attribute_name_listnew_attribute_name_list contains the changed attribute
names of RR.
Why is it needed?
Types
Renaming a relation
Renaming an attribute
Renaming both
Rename a relation
ρFinalYrStudents(Students)ρFinalYrStudents(Students)
Rename an attribute
ρ(SID,SName)(Students)ρ(SID,SName)(Students)
The attributes will be ordered as same as the tables and must be renamed in the same order. If only
selective attributes are to be changed, rewrite the original attribute names of those that are supposed
to be unchanged.
Rename both
Next, we'll change both the relation name and attributes of the StudentsStudents class:
ρFinalYrStudents(SID,SName)(Students)ρFinalYrStudents(SID,SName)(Students)
What is Relational Algebra?
Relational Algebra is a system used to model data stored in relational databases. It’s largely
used as the theoretical basis for which query languages like SQL are built around.
Core Concepts
Additionally, a row can be represented as a tuple, which is simply a comma delimited list of all
attributes in the row enclosed in parenthesis. As an example, for John in the relation Person above,
he can be represented as a tuple of (John, 15, Pizza).
Set Operations
Relational Algebra uses set operations from set theory, but with added constraints.
Union: A union of two relations (R1 U R2) can only be performed if the two relations are union
compatible. This essentially means that both relations have the exact same attributes. A union
combines the rows of the two relations and outputs a new relation that has both input relations’ rows
in it. Suppose, taking the example table above, we want to union with this new table below:
The end result would look like this:
Difference: The difference between two relations (R1- R2) is, as the name implies, all rows from the
first relation that are not present in the second relation. This operation also relies on the two tables
being union compatible. Why is this the case? If you think about it, a difference operation is essentially
a reverse union. Difference ‘takes away’ rows from a table.
If we have another table, Person3, who are all under 30, we can take the difference of the Person
relation to Person3(under 30), to result in a single row table with Stacy.
Cartesian ( or sometimes, Cross) Product: This operation does not need the two tables being
operated on to be union compatible. However, without some sort of condition associated with taking
the cross product of two tables, we are likely unable to glean any data of value. Taking the cross
product of two relations essentially taking every combination of the two tables’ tuples.
If we take the cross product of this table with Person, we end up with a new table of 5 columns, with
each row getting a distinct combination of the tuples from both rows. As you can probably tell from
just that, there would be no use of this data.
Alright we took the cross product, but why?
So you’re probably confused why this operation exists. We will see in the next section, about Joins,
that once we take a cross product with a condition, we can start to gather meaningful data from this.
Join Operation
When we have two relations that have a single column (let’s just call this column C) in common, we
can perform what is known as a natural join. Essentially two tables are naturally joined by taking
the cross product of the two tables, then eliminating all combinations in which the column C from
table 1 and column C from table 2 are not equal.
So let’s take a look at a table of food, and the corresponding best way to obtain them.
If perform natural join on this table and Person, we will end up with a result of the Person table, along
with the corresponding location at which their favorite food is obtained from.
Natural Join ( )
Eliminate rows that does not satisfy the selection criteria ―σBorrower.Loan-no = Loan.Loan-no”
from Borrower
× Loan =
Borrower.Cu Borrower.Loa Loan.Loa Loan.Amount
st- name n- no n- no
Ram L-13 L-13 1000
Ram L-13 L-30 20000
Ram L-13 L-42 40000
Shyam L-30 L-13 1000
Shyam L-30 L-30 20000
Shyam L-30 L-42 40000
Suleman L-42 L-13 1000
Suleman L-42 L-30 20000
Suleman L-42 L-42 40000
i.e. =
Cust-name Loan-no Amount
Ram L-13 1000
Shyam L-30 20000
Suleman L-42 40000
Division Operation:
denoted by ÷ is used for queries that include the phrase ―for all‖.
For example ―Find customers who has an account in all branches in
branch city Agra‖. This query can be solved by following statement.
ΠCustomer-name. branch-name ( ) ÷ Πbranch-name (σBranch-
city=”Agra”(Branch)
The division operations can be specified by using only basic operations as follows:
Let r(R) and s(S) be given relations for schema R
and S with r ÷ s = ΠR-S(r) - ΠR-S ((ΠR-S (r) ×
s) - ΠR-S,S (r))
Tuple Relational Calculus
The formula P(t) is formed using atoms which uses the relations, tuples of
relations and fields of tuples and following symbols
Set operations
Set operations in DBMS allow combining the results of two or more queries in relational
databases. These operations are derived from set theory and include UNION, INTERSECT,
and MINUS (also called EXCEPT in some systems). Here's a breakdown:
1. UNION: Combines the result sets of two queries and returns all unique rows from
both queries.
o Example: SELECT column1 FROM table1 UNION SELECT column1 FROM
table2;
o This returns unique rows from both table1 and table2.
2. UNION ALL: Similar to UNION, but it does not eliminate duplicate rows.
o Example: SELECT column1 FROM table1 UNION ALL SELECT column1
FROM table2;
o This includes all duplicates from both table1 and table2.
3. INTERSECT: Returns only the rows that are common to both queries.
o Example: SELECT column1 FROM table1 INTERSECT SELECT column1
FROM table2;
o Only the rows present in both table1 and table2 are returned.
4. MINUS (EXCEPT): Returns the rows from the first query that are not present in
the second query.
o Example: SELECT column1 FROM table1 MINUS SELECT column1 FROM
table2;
o This returns rows in table1 that are not in table2.
These set operations help in performing operations such as combining results, finding
common records, or identifying differences between datasets.
Here are examples of set operations in SQL using a relational database system. Let's
assume we have two tables: TableA and TableB.
TableA:
ID Name
1 John
2 Alice
3 Bob
TableB:
ID Name
2 Alice
3 Charlie
4 David
1. UNION Example
The UNION operator combines the result sets of two queries, returning distinct values.
Result:
Name
John
Alice
Name
Bob
Charlie
David
The UNION ALL operator combines the result sets of two queries, returning all values,
including duplicates.
Result:
Name
John
Alice
Bob
Alice
Charlie
David
Explanation: This result includes all names from both tables, including the duplicate
"Alice."
3. INTERSECT Example
The INTERSECT operator returns only the rows that are common to both queries.
SELECT Name FROM TableA
INTERSECT
SELECT Name FROM TableB;
Result:
Name
Alice
Explanation: Only "Alice" is common to both tables, so it’s the only row returned.
The MINUS (or EXCEPT) operator returns the rows from the first query that are not in
the second query.
Result:
Name
John
Bob
Explanation: "John" and "Bob" exist in TableA but not in TableB, so they are returned.
SQL Aggregate functions are functions where the values of multiple rows are grouped
as input on certain criteria to form a single value result of more significant meaning.
It is used to summarize data, by combining multiple values to form a single result.
SQL Aggregate functions are mostly used with the GROUP BY clause of the SELECT
statement.
Various Aggregate Functions
1. Count()
2. Sum()
3. Avg()
4. Min()
5. Max()
Aggregate Functions in SQL
Below is the list of SQL aggregate functions, with examples
Count():
Count(*): Returns the total number of records .i.e 6.
Count(salary): Return the number of Non-Null values over the column salary. i.e 5.
Count(Distinct Salary): Return the number of distinct Non-Null values over the
column salary .i.e 5.
Sum():
sum(salary): Sum all Non-Null values of Column salary i.e., 3120.
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 3120..
Avg():
Avg(salary) = Sum(salary) / count(salary) = 3120 / 5 = 624
Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 3120 / 5 =
624
Min():
Min(salary): Minimum value in the salary column except NULL i.e., 403.
Max():
Max(salary): Maximum value in the salary i.e., 802.
SQL Database
Id Name Salary
1 A 802
2 B 403
3 C 604
Id Name Salary
4 D 705
5 E 606
6 F NULL
Output
TotalEmployees
TotalSalary
3120
AverageSalary
624
HighestSalary
802
LowestSalary
403
Key Takeaways :
Aggregate functions in SQL operate on a group of values and return a single result.
They are often used with the GROUP BY clause to summarize the grouped data.
Aggregate function operates on non-NULL values only (except COUNT).
Commonly used aggregate functions are – MIN(), MAX(), COUNT(), AVG(),
and SUM().
In SQL there may be some records in a table that do not have values or data for every
field and those fields are termed as a NULL value.
NULL values could be possible because at the time of data entry information is not
available. So SQL supports a special value known as NULL which is used to represent the
values of attributes that may be unknown or not apply to a tuple. SQL places a NULL
value in the field in the absence of a user-defined value. For example, the
Apartment_number attribute of an address applies only to addresses that are in
apartment buildings and not to other types of residences.
So, NULL values are those values in which there is no data value in the particular field
in the table.
Query:
CREATE TABLE Employee (
Fname VARCHAR(50),
Lname VARCHAR(50),
SSN VARCHAR(11),
Phoneno VARCHAR(15),
Salary FLOAT
);
VALUES
('Shubham', 'Thakur', '123-45-6789', '9876543210', 50000.00),
Output:
Query:
SELECT Fname, Lname FROM Employee WHERE SSN IS NULL;
Output:
Query:
SELECT COUNT(*) AS Count FROM Employee WHERE SSN IS NOT NULL;
Output:
Updating NULL Values in a Table
We can update the NULL values present in a table using the UPDATE statement in SQL.
To do so, we can use the IS NULL operator in the WHERE clause to select the rows with
NULL values and then we can set the new value using the SET keyword.
Let’s suppose that we want to update SSN in the row where it is NULL.
Query:
UPDATE Employee
Output:
NESTED QUERY
In SQL, a nested query, also called a subquery, is a query embedded inside another
query. It allows you to use the result of one query as input for another. The subquery
can be placed in various clauses such as SELECT, FROM, WHERE, and HAVING.
Types of Subqueries:
Basic Structure:
Here’s a breakdown:
SELECT column_name
FROM table_name
WHERE column_name = (SELECT column_name FROM another_table WHERE condition);
In this example, the subquery (SELECT AVG(salary) FROM Employees) calculates the
average salary, and the outer query selects employees with a salary greater than the
average.
Find departments where there are employees with salaries greater than 50,000.
SELECT department_id
FROM Departments
WHERE department_id IN (SELECT department_id FROM Employees WHERE salary >
50000);
Here, the subquery returns a list of department_ids from the Employees table where
salary exceeds 50,000, and the outer query retrieves all matching departments.
Find employees who earn more than the average salary in their department.
“Nested queries are powerful tools in SQL that allow you to write more dynamic and
complex queries by using the results of one query as inputs to another. They are essential
when working with multiple tables or filtering based on aggregate results.”
Views
Views in SQL are a kind of virtual table. A view also has rows and columns like tables,
but a view doesn’t store data on the disk like a table. View defines a customized query
that retrieves data from one or more tables, and represents the data as if it was coming
from a single source.
We can create a view by selecting fields from one or more tables present in the
database. A View can either have all the rows of a table or specific rows based on
certain conditions.
In this article, we will learn about creating, updating, and deleting views in SQL.
Demo SQL Database
We will be using these two SQL tables for examples.
StudentDetails
StudentMarks
You can create these tables on your system by writing the following SQL query:
-- Create StudentDetails table
CREATE VIEWS
We can create a view using CREATE VIEW statement. A View can be created from a
single table or multiple tables.
Statement Examples
In this example, we will create a View named DetailsView from the table StudentDetails.
In this example, we will create a view named StudentNames from the table Student
Details.
Output:
In this example we will create a View named MarksView from two tables StudentDetails
and StudentMarks.
To create a View from multiple tables we can simply include multiple tables in the
SELECT statement.
Output:
Uses of a View
A good database should contain views for the given reasons:
1. Restricting data access – Views provide an additional level of table security by
restricting access to a predetermined set of rows and columns of a table.
2. Hiding data complexity – A view can hide the complexity that exists in multiple
joined tables.
3. Simplify commands for the user – Views allow the user to select information
from multiple tables without requiring the users to actually know how to perform a
join.
4. Store complex queries – Views can be used to store complex queries.
5. Rename Columns – Views can also be used to rename the columns without
affecting the base tables provided the number of columns in view must match the
number of columns specified in a select statement. Thus, renaming helps to hide the
names of the columns of the base tables.
6. Multiple view facility – Different views can be created on the same table for
different users.
Embedded SQL
Popular DBMS that support Embedded SQL are Altibase, IBM Db2, Microsoft SQL Server,
Mimer SQL, Oracle Database, PostgreSQL, and SAP Sybase.
The goal to make database interactions simpler for application developers and end users
determines the demand for embedded SQL. Users often enter values or submit requests
while interacting with an application, which requires accessing and changing data
contained in a database. Developers may conduct these database tasks without
burdening the user with SQL complexities by integrating SQL queries directly into the
application code.
The first step in using embedded SQL is establishing a connection to the database. This
is done using the CONNECT keyword, preceded by EXEC SQL to indicate that it is a SQL
statement
Example
EXEC SQL CONNECT HR_USER;
Declaration Section
The declaration section is used to declare variables that will be used in SQL queries and
to capture the results of those queries. The host language and its variable will get and
capture the result. Types of variables used in the host language are:
Host Variable: Host variables are variables of the host language that are used to supply
data and store the results of SQL queries. The BEGIN DECLARE and END DECLARE
sections are defined within EXEC SQL and should be used to declare the variables within
the SQL code and;
Example
EXEC SQL BEGIN DECLARE SECTION;
int id;
int roll_no;
char name[10];
char address[30];
EXEC SQL END DECLARE SECTION;
Indicator variables: Indicator variables are also host variables but they are used to
capture NULL values which are returned by queries or to set NULL values in INSERT or
UPDATE statements. Indicator variables are always of the 2−byte short type. With
SELECT statement, indicators capture any NULL values returned for a column and with
INSERT OR UPDATE statement, the common value is set to be NULL regardless of the
value present for a host variable.
Example
EXEC SQL SELECT NAME INTO :NAME :IND_NAME FROM STUDENT WHERE ID = :ID;
INSERT INTO STUDENT (ID, SNAME) VALUES (:ID, :NAME :IND_NAME);
UPDATE STUDENT SET ADDRESS = :ADDRESS :IND_ADDRESS;
Execution Section
The execution section contains all the SQL queries and statements that are prefixed by
EXEC SQL. This is where you can write your SQL queries and perform database
operations.
Example
EXEC SQL SELECT * FROM STUDENT WHERE STUDENT_ID = :STD_ID;
Error Handling
Error handling is essential in embedded SQL. In embedded SQL, error handling is based
on the host language. In C programs, error handling is typically done using labels and
the WHENEVER statement. The WHENEVER statement is used to define the action to be
taken when a certain condition occurs. The condition can be SQLWARNING, SQLERROR,
or NOT FOUND. The action can be CONTINUE, DO <function>, GOTO , or STOP.
Example
EXEC SQL WHENEVER SQLWARNING DO display_warning();
EXEC SQL WHENEVER SQLERROR STOP;
EXEC SQL WHENEVER NOT FOUND GOTO lbl_no_records;
Accessing databases is made simple with embedded SQL, which eliminates the need for
lengthy coding or other programs. It offers a simple method for getting data by feeding
it into the database, doing away with the need for complex and complicated
programming.
By permitting a direct connection between the user interface and the database,
embedded SQL makes it easier to integrate the front end and back end of an application.
This improves the overall user experience by enabling real−time data retrieval and
changes.
4. Error prevention
Developers can prevent logical mistakes from happening when processing database
transactions by integrating SQL queries into the application code. The integration of SQL
with the host language guarantees security and improves the application's dependability.
Developers must have a solid understanding of the host language in which the SQL is
contained to use embedded SQL effectively. This can require more programming
language knowledge or training.
By using embedded SQL, the development process becomes even more difficult. It may
be difficult for developers to handle the integration of SQL statements within host
language code, especially for larger systems.
SQL statements must be declared at design time for embedded SQL, which means that
only predefined queries may be used in application code. The application's ability to react
to shifting requirements may be hampered by the absence of dynamic SQL flexibility.
4. Dynamic SQL
Dynamic SQL involves the creation and execution of SQL statements at runtime. Dynamic
SQL allows developers to generate SQL statements dynamically based on runtime
conditions or user input. By combining changeable data, conditions, and dynamic
database or column names, developers may quickly construct SQL queries using dynamic
SQL. Because of its adaptability, dynamic SQL is a good choice when the SQL statements
need to change in response to evolving needs or user inputs. Dynamic SQL queries are
built at execution time so the system chooses how to access the database and conduct
the SQL queries. Performance could be affected as a result of this lack of preparation
because the system must create an execution plan on the spot. Dynamic SQL, however,
provides outstanding adaptability and versatility.
DECLARE
@var1 NVARCHAR(MAX),
@var2 NVARCHAR(MAX);
This example will show how the data of a table named 'student' is selected using a
dynamic process.
Input Table
Student
Id Name City
1 Monu Gonda
2 Aman Lucknow
3 Naman Dehradun
Example
DECLARE
@var1 NVARCHAR(100),@var2 NVARCHAR(120);
SET @var1 = N'student';
SET @var2 = N'SELECT * FROM' + @var1;
EXEC sp_executesql @var2;
Output
Student
Id Name City
1 Monu Gonda
2 Aman Lucknow
3 Naman Dehradun
Advantages of Dynamic SQL
Integrity Constraints
Integrity constraints are the set of predefined rules that are used to maintain the quality
of information. Integrity constraints ensure that the data insertion, data updating, data
deleting and other processes have to be performed in such a way that the data integrity
is not affected. They act as guidelines ensuring that data in the database remain accurate
and consistent. So, integrity constraints are used to protect databases.
1. Domain Constraints
These are defined as the definition of valid set of values for an attribute. The data type
of domain include string, char, time, integer, date, currency etc. The value of the attribute
must be available in comparable domains.
Example:
21CSE103 Mukesh 20
Entity integrity constraints state that primary key can never contain null value because
primary key is used to determine individual rows in a relation uniquely, if primary key
contains null value then we cannot identify those rows. A table can contain null value in
it except primary key field.
Example:
It is not allowed because it is containing primary key as NULL value.
Mukesh 5th 20
4. Key Constraints
Keys are the entity set that are used to identify an entity within its entity set uniquely.
An entity set can contain multiple keys, bit out of them one key will be primary key. A
primary key is always unique, it does not contain any null value in table.
Example:
Example:
Here, in below example Block_No 22 entry is not allowed because it is not present in 2nd
table.
20 Chandigarh
21 Punjab
25 Delhi
Integrity constraints act as the backbone of reliable and robust database. They ensure
that the data stored is reliable, worthy, consistent and accurate within the database. By
implement integrity constraints we can improve the quality of the data stored in database.
So, as the database continues to grow it will not become inconsistent and inaccurate.
Procedures
Local Procedures vs. Stored Procedures are both types of procedures used in SQL,
but they differ in terms of scope, storage, and how they are used within the database
environment. Below is a detailed comparison of both types:
1. Local Procedures
Definition:
A local procedure is a procedure that is defined and used within the scope of a single
session, batch, or block of code (like a script or a PL/SQL block). These procedures are
not stored permanently in the database; they exist only while the code is being executed.
Key Points:
Scope: Local procedures are defined within a block of code such as an anonymous
PL/SQL block, a function, or another procedure. They are not accessible outside
of this scope.
Temporary Nature: They exist only during the execution of the script or block
where they are defined. Once the block finishes executing, the local procedure is
discarded.
Use Cases: Local procedures are useful for modularizing code within a specific
task, reducing redundancy in scripts, and enhancing readability without the need
to persist the procedure in the database.
Performance: Since they are compiled at runtime, local procedures may have
slightly lower performance compared to stored procedures which are precompiled.
sql
Copy code
DECLARE
-- Define a local procedure within an anonymous PL/SQL block
PROCEDURE PrintMessage IS
BEGIN
DBMS_OUTPUT.PUT_LINE('This is a local procedure!');
END;
BEGIN
-- Call the local procedure
PrintMessage;
END;
In this example, the PrintMessage procedure is defined and used within the scope of the
anonymous block.
2. Stored Procedures
Definition:
A stored procedure is a named set of SQL statements and procedural logic stored
permanently in the database. It is precompiled, reusable, and can be invoked multiple
times from any session or application connected to the database.
Key Points:
Scope: Stored procedures are stored and managed by the database and can be
called from anywhere within the database environment by users with appropriate
permissions.
Persistence: They are permanently stored in the database, meaning they persist
beyond the session and can be reused across multiple applications and sessions.
Precompiled: Stored procedures are precompiled and stored in an optimized
form in the database, resulting in faster execution compared to scripts or local
procedures.
Security and Maintenance: Stored procedures can be secured with access
permissions and provide a centralized point of maintenance for commonly used
code, reducing duplication.
Performance: Due to their precompiled nature and optimized execution plan,
stored procedures often provide better performance.
sql
Copy code
CREATE PROCEDURE GetCustomerDetails(IN custID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = custID;
END;
sql
Copy code
CALL GetCustomerDetails(101);
Key Differences Between Local and Stored Procedures:
Summary:
Local Procedures are best for temporary, localized tasks within a single script or
block, providing modularity without storing code permanently.
Stored Procedures offer a robust, reusable, and secure way to encapsulate
business logic within the database, optimizing performance and maintainability.
The following are the key differences between triggers and stored procedures:
Key Points:
Types of Triggers:
o BEFORE Trigger: Executes before the event occurs (e.g., before inserting
a row).
o AFTER Trigger: Executes after the event has occurred (e.g., after
updating a row).