Ch.2.advanced SQL
Ch.2.advanced SQL
ID NAME
1 abhi
2 adam
4 alex
The SQL query will be,
• INSERT INTO class VALUE(seq_1.nextval, 'anu');
• Result set table will look like,
ID NAME
1 abhi
2 adam
4 alex
1 anu
SQL Query to Create a View from the above table will be,
CREATE or REPLACE VIEW sale_view
AS
SELECT * FROM Sale WHERE customer = 'Alex';
The syntax for displaying the data in a view is similar to fetching data from a table using
a SELECT statement.
SELECT * FROM sale_view;
SYSNONYM
• The SYSNONYM is the alternative name or the alias name for the database
objects so the same database object can be referred by different names without
using the complex names that are defined before. The SYNONYM can be created
for the tables, stored procedures, user-defined functions, or any database
objects.
• The SYNONYMS becomes invalid when:
• The base table is dropped.
• The name of the base table is changed.
• Creating a synonym for a synonym is not possible.
PL/SQL Introduction
Basics of PL/SQL
• PL/SQL stands for Procedural Language extensions to the Structured
Query Language (SQL).
• PL/SQL is a combination of SQL along with the procedural features of
programming languages.
• Oracle uses a PL/SQL engine to processes the PL/SQL statements.
• PL/SQL includes procedural language elements like conditions and
loops. It allows declaration of constants and variables, procedures and
functions, types and variable of those types and triggers.
Disadvantages of SQL:
• SQL doesn’t provide the programmers with a technique of condition checking, looping and
branching.
• SQL statements are passed to Oracle engine one at a time which increases traffic and decreases
speed.
• SQL has no facility of error checking during manipulation of data.
Features of PL/SQL:
• PL/SQL is basically a procedural language, which provides the functionality of decision making,
iteration and many more features of procedural programming languages.
• PL/SQL can execute a number of queries in one block using single command.
• One can create a PL/SQL unit such as procedures, functions, packages, triggers, and types,
which are stored in the database for reuse by applications.
• PL/SQL provides a feature to handle the exception which occurs in PL/SQL block known as
exception handling block.
• Applications written in PL/SQL are portable to computer hardware or operating system where
Oracle is operational.
• PL/SQL Offers extensive error checking.
Differences between SQL and
PL/SQL:
SQL PL/SQL
SQL is a single query that is used to perform DML and DDL operations. PL/SQL is a block of codes that used to write the entire program blocks/
procedure/ function, etc.
It is declarative, that defines what needs to be done, rather than how things PL/SQL is procedural that defines how the things needs to be done.
need to be done.
Cannot contain PL/SQL code in it. It is an extension of SQL, so it can contain SQL inside it.
Structure of PL/SQL Block:
• PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a
structural language that is more powerful than SQL. The basic unit in PL/SQL is a block.
All PL /SQL programs are made up of blocks, which can be nested within each other.
A block has the following structure:
A block has the following structure: • It supports all DML commands, DDL commands and
DECLARE SQL*PLUS built-in functions as well.
declaration statements; • Exception section starts with EXCEPTION keyword.
BEGIN • This section is optional which contains statements that
executable statements EXCEPTIONS are executed when a run-time error occurs.
exception handling statements • Any exceptions can be handled in this section.
END;
• Declare section starts with DECLARE keyword in which
variables, constants, records as cursors can be declared
which stores data temporarily.
• It basically consists definition of PL/SQL identifiers. This
part of the code is optional.
• Execution section starts with BEGIN and ends
with END keyword.
• This is a mandatory section and here the program logic
is written to perform any task like loops and conditional
statements.
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20) ;
BEGIN
null;
END;
/
OUTPUT:-
PL/SQL procedure successfully completed.
PL/SQL code to print sum of two numbers taken from the user.
--SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
BEGIN
c := a + b ;
dbms_output.put_line('Sum of '||a||' and '||b||' is = '||c);
END;
/
OUTPUT:-
Enter value for a: 2
Enter value for b: 3
Sum of 2 and 3 is = 5
• A stored procedure in SQL is a group of SQL queries that can be saved and reused multiple times. It is very useful as it
reduces the need for rewriting SQL queries. It enhances efficiency, reusability, and security in database management.
• Users can also pass parameters to stored procedures so that the stored procedure can act on the passed parameter values.
• Stored Procedures are created to perform one or more DML operations on the Database. It is nothing but a group of SQL
statements that accepts some input in the form of parameters, performs some task, and may or may not return a value.
• Syntax
Syntax to Create a Stored Procedure
CREATE PROCEDURE procedure_name
(parameter1 data_type, parameter2 data_type, …)
AS
BEGIN
— SQL statements to be executed
END
• OUTPUT:-
• EXEC procedure_name parameter1_value, parameter2_value, ..
-- Create a new database named "SampleDB" (5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain');
CREATE DATABASE SampleDB; Output:
-- Create a stored procedure named
-- Switch to the new database "GetCustomersByCountry"
USE SampleDB; CREATE PROCEDURE GetCustomersByCountry
@Country VARCHAR(50)
-- Create a new table named "Customers" AS CustomerName Contact Name
CREATE TABLE Customers ( BEGIN
CustomerID INT PRIMARY KEY, SELECT CustomerName, ContactName
CustomerName VARCHAR(50), FROM Customers
ContactName VARCHAR(50), WHERE Country = @Country;
Country VARCHAR(50) END; Naveen Tulasi
);
-- Execute the stored procedure with parameter "Sri lanka"
-- Insert some sample data into the Customers table EXEC GetCustomersByCountry @Country = 'Sri lanka';
INSERT INTO Customers (CustomerID, CustomerName,
ContactName, Country)
VALUES (1, 'Shubham', 'Thakur', 'India'),
(2, 'Aman ', 'Chopra', 'Australia'),
(3, 'Naveen', 'Tulasi', 'Sri lanka'),
(4, 'Aditya', 'Arpan', 'Austria'),
SQL Trigger
3 %ISOPEN Always returns FALSE for implicit cursors, because Oracle closes the SQL
cursor automatically after executing its associated SQL statement.
Output:-