0% found this document useful (0 votes)
47 views

SQL Basics

This document provides an introduction to SQL (Structured Query Language). It discusses what SQL is used for, the basic concepts of SQL including queries with conditions, data types, tables, rows and columns. It also covers the three main categories of SQL: DDL for defining database objects, DML for manipulating data, and DCL for controlling access. Examples are provided of creating tables and inserting data using SQL statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

SQL Basics

This document provides an introduction to SQL (Structured Query Language). It discusses what SQL is used for, the basic concepts of SQL including queries with conditions, data types, tables, rows and columns. It also covers the three main categories of SQL: DDL for defining database objects, DML for manipulating data, and DCL for controlling access. Examples are provided of creating tables and inserting data using SQL statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

SQL BASICS

INTERNAL

February 27,2012
INTRODUCTION TO SQL
What is SQL ?
– What will an user want from a database ?

– He will need data i.e. information from


database. How can this be accomplished ?

– He should ask the data which he needs to the


database. This function of asking the database
for a particular information can be termed as
QUERY.
Concept of SQL
– A query to retrieve data or information will
always have a certain condition.
– SQL is a language that allows user to
specify these conditions in the queries that
are being given to the database.

– SQL- Structured Query Language; it is the


standard language for relational database
management systems. Oracle, Sybase,
Microsoft SQL Server, Access, Ingres, etc
–.
Basics Of SQL
• Data Types
• Objects of the Data Base
Data Types – Character String
• character-string
– CHAR(N) (or CHARACTER(N)) is a fixed-
length character string
– VARCHAR(N) (or CHAR VARYING(N), or
CHARACTER VARYING(N)) is a variable-
length character string with at most N
characters
• bit-strings
– BIT(N) isa fixed-length bit string
– VARBIT(N) (or BIT VARYING(N)) is a bit
string with at most N bits
Data Types – Time
– DATE is a date: YYYY-MM-DD
– TIME, a time of day: HH-MM-SS
– TIME(I), a time of day with I decimal
fractions of a second: HH-MM-SS-F....F
– TIME WITH TIME ZONE, a time with a
time zone added: HH-MM-SS-HH-MM
– TIME-STAMP, date, time, fractions of a
second and an optional WITH TIME
ZONE qualifier:
YYYY-MM-DD-HH-MM-SS-F...F{-HH-MM}
– INTERVAL, relativevalue used to
increment or decrement DATE, TIME, or
TIMESTAMP: YEAR/MONTH or DAY/TIME
Data Types – Numeric
• numeric
– INTEGER (or INT), SMALLINT are subsets of
the integers (machine dependent)
– REAL, DOUBLE PRECISION are floating-point
and double-precision floating-point
(machine dependent)
– FLOAT(N) is floating-point with at least N
digits
– DECIMAL(P,D) (or DEC(P,D), or NUMERIC(P,D)),
with P digits of which D are to the right
of the decimal point
What is a Table?

– A relational database system contains one or


more objects called tables

– The data or information for the database is


stored in these tables.

– Tables are uniquely identified by their


names and are comprised of columns and
rows.
Rows and Columns Sachin

Runs 100's vs Team

3000+ 20 Australia

. . .

. . .

– Columns Æ Column name, data type, and


attributes for the column

– Rows contain the data for the columns i.e.


the record sets. The rows are also called as
tuples.
Languages of SQL
• DDL
• DML
• DCL
3 Categories of SQL
– Data Definition Language – Defining
Relations,view,integrity constraints and
Triggers.

– Data Manipulation Language – For


updating and Querying

– Data Control Language – For defining Access


rights and Concurrency controls .
Basic structure of an SQL query

General SELECT, ALL / DISTINCT, *,


Structure AS, FROM, WHERE

Comparison IN, BETWEEN, LIKE "% _"

Grouping GROUP BY, HAVING,


COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )

Display Order ORDER BY, ASC / DESC

Logical AND, OR, NOT


Operators
SQL - Data Definition Language
• Create
• Alter
• Drop
SQL DDL, Table creation (simple)

– CREATE TABLE table_name


(column_name_1 data type,
column_name_to data type,
column_name_n data type)

– Other than the data types more attributes can


be added to each columns of the table which
are called as constraints (later).
Create a simple table
– Create Table Titans
(Project varchar (25),
MonthsW number (12),
Branch varchar (25),
Name varchar (25));

Titans

Project MonthsW Branch Name


Schema, View, Index

– A schema is a collection of objects in a data


base. The objects include
tables,views,synonyms, procedures etc.,

– To create a schema
Create schema schema_name Authorization
owner;

– Create schema BFS2.4 Authorization TCS;


Schema, View, Index (contd.)

– A view is very similar to a DB table. The


fields in a view can be taken from many
tables.
– A view always shows up-to-date data when
the DB recreates the data, using the view's
SQL statement, every time a user queries a
view.
– CREATE VIEW view_name AS (Select a
combination of columns from many tables).
Schema, View, Index (contd.)

– An index can be created in a table on a


column to speed up searches/queries

– CREATE INDEX index_name


ON table_name (column_name).

– Indexes are to be created on tables that are


frequently searched against and not on those
which are updated often. .
Altering an object

– One can add a column, drop or modify the


same.

– ALTER TABLE Titans DROP COLUMN Name;

– ALTER TABLE Titans ADD (Name varchar (25));

– ALTER TABLE Titans MODIFY (Name varchar


(26));
Drop an object

– Database objects such as Indexes and tables


can easily be deleted/removed with the
DROP statement.

– DROP OBJECT object_name.

– If table needs to be intact and only the data


has to be deleted. The keyword Truncate
Should be used as
TRUNCATE TABLE table_name.
SQL - Constraints
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
SQL Constraints – NOT NULL

– The NOT NULL constraint enforces a


column to NOT accept NULL value.

– This means that one cannot insert a


new record, or update a record without
adding a value to this field.

– A mandatory column that can have the same


value for many records can be defined as
NOT NULL.
SQL Constraint - UNIQUE

– If the constraint UNIQUE is applied to a


particular column then none of the values in
that column be identical.

– More than one column can be defined


as unique.

– This provides uniqueness for the records


pulled out with the help of the column that is
defined as unique.
SQL Constraint – Primary Key

– Primary Key is an attribute which identifies


a tuple or record set uniquely

– PRIMARY KEY = NOT NULL +UNIQUE

– The difference being one cannot have more


than one primary key in a table whereas
there can be more than one Unique Key..
SQL Constraint – Example

Titans
Project MonthsW Branch Employee ID
City 28Ch‐1 387010

Nielsen 11Siruseri 244356

– Create table Titans


(Project Varchar (25) NOT NULL,
MonthsW Number (12) NOT NULL,
Branch varchar (25) NOT NULL,
EmployeeId number (10) primary key);.
SQL Constraint – Foreign Key

– A column in a table is set as foreign key


when it references to a column which is a
primary key in another table.

– If a link has to be created and remain


undisrupted use foreign key.

– It prevents invalid data form being inserted


into the foreign key column, because it has
to be one of the values contained in the table
it points to.
SQL Constraint – Foreign Key
Titans TitanFort_Details
Project MonthsW Employee ID Branch Branch Place
City 28 387010Ch‐1
Ch‐1 Thoraipakkam
Nielsen 11 500560Siruseri
WolterK 10 809765Ch‐1 Siruseri SIPCOT IT Park

Create table Titans


(Project Varchar (25) NOT NULL,
MonthsW Number (12) NOT NULL,
EmployeeId number (10) Primary key
Branch REFERENCES
TitanFort_Details(Branch) );
SQL Constraint – Check & Default

– The CHECK constraint is used to limit or


define the value range that can be
placed in a column.

– The DEFAULT constraint is used to


insert a default value into a column.

– The default value will be added to all


new records, if no other value is
specified.
SQL Constraint – Check &Default
Titans
Project MonthsW Employee ID Branch
City 28 387010Ch‐1
Nielsen 11 500560Siruseri
WolterK 10 809765Ch‐1

Create table Titans


(Project Varchar (25) NOT NULL,
MonthsW Number (2) CHECK (MonthsW)>1,
EmployeeId number (10) Primary key
Branch Varchar (25) DEFAULT ‘TCS’);
SQL - DML
• INSERT
• UPDATE
• DELETE
• SELECT
SQL DML – INSERT

– The Insert Query will work in two ways.

– 1.Customized Insert – Only columns we


need can be inserted leaving out others.

– 2.Natural Insert – All the columns of the


table will be filled while inserting a record
set.
SQL INSERT – Customized Insert
client_details
Client_id Client_Name State Tax_Amount

A552 Johnson Dirk Arizona 756.9

INSERT INTO client_details


(client_id,client_name,state) VALUES
(E435,Vanessa King,Iowa);.

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa
SQL INSERT – Natural Insert
client_details
Client_id Client_Name State Tax_Amount

A552 Johnson Dirk Arizona 756.9

INSERT INTO client_details (E435,Vanessa


King,Iowa,98.78);.

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
SQL DML – UPDATE

– The UPDATE statement is used to


update existing records in a table.

– UPDATE table_name
SET column1=value,
column2=value2,...
WHERE some_column=some_value;
– The WHERE clause specifies which
records are be updated. If you omit the
WHERE clause, all records will be
updated!.
SQL DML – UPDATE
client_details
Client_id Client_Name State Tax_Amount

A552 Johnson Dirk Arizona 756.9

UPDATE client_details
SET State =‘Virginia'
WHERE client_id=‘A552' ;

client_details
Client_id Client_Name State Tax_Amount

A552 Johnson Dirk Virginia 756.9


SQL DML – DELETE

– The DELETE statement is used to


delete rows in a table.

– DELETE FROM table_name


WHERE some_column=some_value.

– To Delete all the record sets in a


particular we have to remove the where
clause.
SQL DML – DELETE
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78

DELETE FROM client_details


WHERE client_id=‘E435' ;

client_details
Client_id Client_Name State Tax_Amount

A552 Johnson Dirk Virginia 756.9


SQL – Select Query
SQL DML – SELECT

SELECT ...... FROM ......


WHERE ......

SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;


FROM tablename WHERE condition
Select - General Structure

SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;


FROM tablename WHERE condition
– The query will select rows from the source tablename
and output the result in table form.
– Expressions expr1, expr2 can be :
• (1) a column, or
• (2) an expression of functions and fields.

– And col1, col2 are their corresponding column


names in the output table.
Select - General Structure (contd.)

SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;


FROM tablename WHERE condition
– DISTINCT will eliminate duplication in the output
while ALL will keep all duplicated rows.
– condition can be :
• (1) an inequality, or
• (2) a string comparison
• using logical operators AND, OR, NOT.
SQL DML – Simple Select
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details Æ Selects all the


data from the table.

– Select * from client_details where client_id =


‘A552’ Æ Selects the row of client_id ‘A552’
alone.
SQL DML – Select Distinct
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select Distinct (State) from client_details;

State
Arizona
Iowa
Select – Where clause (AND & OR)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details where client_id =


‘A552’ and state = ‘Arizona’;

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
Select – Where clause (AND & OR)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details where client_id =


‘A552’ OR client_id = ‘E435’;

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 96.8
Select – Where clause (BETWEEN)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details where tax_amount


between 500 AND 1000;

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
X869 Maurice Green Iowa 546
Select – Where clause (IN)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details where client_id in


(‘A552’ , ‘E435’);

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 96.8
Select – Where clause (Wildcards &
LIKE)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details where state like ‘A%’;

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
Select – Where clause (Wildcards &
LIKE)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details where state like ‘_r%’;

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
Select – Where clause (ROWNUM OR
TOP)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details where rownum < = 1;

client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
Select – ORDER BY

– The ORDER BY key word is used to


sort the output by a specific column.

– Default the key word orders the result


set in ascending order.

– ‘DESC’ is the key word used to sort the


output in a descending order.
Select –ORDER BY
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select * from client_details ORDER BY


tax_amount;

client_details
Client_id Client_Name State Tax_Amount
E435 Vanessa King Iowa 96.8
X869 Maurice Green Iowa 546
A552 Johnson Dirk Arizona 756.9
SQL –Funct. & Aggregate Query
• ARITHMETIC FUNCTIONS
• GROUPING
SQL Functions – Arithmetic.

– AVG () Function is used to output the


Average of a particular Column which is
numeric .

– The MIN() function returns the smallest


value of the selected column

– MAX() function returns the largest value


of the selected column.
SQL Funct. – Arithmetic.

– The SUM () function returns the


smallest value of the selected column

– Select Function_name (column) AS


alias_name from table_name;

– The functions are used in nested


selects and in Group By Clause with
great effect.
SQL Funct. – Scalar.

– The UCASE() function converts the


value of a field to uppercase.

– The LCASE() function converts the


value of a field to lowercase.

– MID() function is used to extract


characters Æ
MID(column_name,start[,length])
SQL Functions – count.

– Type 1: SELECT
COUNT(column_name) FROM
table_name;

– The query returns the number of values


in the column specified that are not null.

– Type 2: SELECT COUNT(*) FROM


table_name;
SQL Functions – count.

– The type 2 returns the number of rows


or record sets of the particular table.

– Type 3: SELECT COUNT(DISTINCT


column_name) FROM table_name

– The returned value will be the number of


values that are unique in the specified
column.
GROUP BY
SELECT ...... FROM ...... WHERE condition
GROUP BY groupexpr [HAVING requirement]

Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )

– groupexpr specifies the related rows to be


grouped as one entry. Usually it is a column.

– WHERE condition specifies the condition of


individual rows before the rows are group.
HAVING requirement specifies the condition
involving the whole group.
Group BY
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select state, SUM (tax_amount) from


client_details;

Arizona 1399.7

Iowa 1399.7

Iowa 1399.7
Group BY
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select state, SUM (tax_amount) from


client_details GROUP BY state ;

Arizona 756.9

Iowa 642.8
Group BY
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select state, count(*) from client_details GROUP


BY state ;

Arizona 1

Iowa 2
Group BY – Why Having Clause ?

– References in the SELECT clause can


only be made to aggregates and to
attributes in the GROUP BY clause.

– Select state, count(*) from client_details


GROUP BY state; Æ In this Query You
need the result set to have the records
only when the count is greater than 1.
– Select state, count(*) from client_details
GROUP BY state where count (*) > 1;
Group BY - Having
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 98.78
X869 Maurice Green Iowa 546

– Select state, count(*) from client_details GROUP


BY state HAVING count (*) > 1;

Iowa 2
SQL – Nested & Miscellaneous
• NESTED QUERIES & INTO
• NULL
• TRANSACTION
Select – INTO
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa 96.8
X869 Maurice Green Iowa 546

SELECT * FROM client_details ORDER BY name


DESC INTO TABLE client_details_bkup;

client_details_bkup
Client_id Client_Name State Tax_Amount
E435 Vanessa King Iowa 96.8
X869 Maurice Green Iowa 546
A552 Johnson Dirk Arizona 756.9
Select – INTO

– Using INTO command selecting data


from more than one table is also
possible.

– For creating Views we can use AS


statement with select.

– CREATE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition;
NULL VALUES

– NULL represents missing data. It is not


possible to compare NULL and 0; they
are not equivalent..
– NVL() function can be used substitute
any value when a null value column is
selected.

– IS NULL and IS NOT NULL are used to


look out for null values.
NULL VALUES (contd.)
client_details
Client_id Client_Name State Tax_Amount
A552 Johnson Dirk Arizona 756.9
E435 Vanessa King Iowa
X869 Maurice Green Iowa

– SELECT client_name,NVL(tax_amount,0))
FROM client_details;.

Client_Name Tax_Amount
Vanessa King 0

Maurice Green 0
Johnson Dirk 756.9
TRANSACTION COMMANDS

– The command DESC is used to


describe the structure of the table.

– COMMIT command is used to complete


a query and make the changes
irreversible.

– ROLLBACK command is used to revert


the changes before the changes are
commited.
Nested Queries
Titans TitanFort_Details
Project MonthsW Employee ID Branch Branch Place
City 28 387010Ch‐1
Ch‐1 Thoraipakkam
Nielsen 11 500560Siruseri
WolterK 10 809765RTSC Siruseri SIPCOT IT Park

Select * from titans where branch in (Select


branch from titanfort_details );
SQL - JOINS
• INNER JOIN
• LEFT AND RIGHT OUTER JOIN
• SELF JOIN
JOINS – INNER JOIN
EMPNO ENAME HIREDATE SALARY MANAGER_ID
1 Jason 25-JUL-96 8767 2
2 John 15-JUL-97 3456 3
3 Joe 25-JAN-86 5654 3

EMPNO JOBTITLE
1 Tester
2 Accountant
ENAME JOBTITLE
3 Developer
Jason Tester
John Accountant
Joe Developer
JOINS – INNER JOIN

– The Inner Join keyword produces the


result set when there is at least one
match in both tables. [Use Alias For easier
understanding in JOINS]

SELECT
e.ename, j.jobtitle
FROM employee e
INNER JOIN job j
ON e.empno = j.empno;
JOINS – LEFT OUTER JOIN
EMPNO ENAME HIREDATE SALARY MANAGER_ID
1 Jason 25-JUL-96 8767 2
2 John 15-JUL-97 3456 3
3 Joe 25-JAN-86 5654 3
4 Sam 12-JAN-89 10000 5

EMPNO JOBTITLE
1 Tester
2 Accountant ENAME JOBTITLE
Jason Tester
3 Developer
John Accountant
Joe Developer
Sam -
JOINS – LEFT OUTER JOIN

– The LEFT JOIN keyword returns all


rows from the left table (table_name1),
even if there are no matches in the right
table (table_name2).
SELECT
e.ename, j.jobtitle
FROM employee e --- (TABLE 1)
LEFT JOIN job j ---(TABLE 2)
ON e.empno = j.empno;
ON e.empno = j.empno (+);
(FROM T1,T2)
JOINS – RIGHT OUTER JOIN
EMPNO ENAME HIREDATE SALARY MANAGER_ID
1 Jason 25-JUL-96 8767 2
2 John 15-JUL-97 3456 3
3 Joe 25-JAN-86 5654 3

EMPNO JOBTITLE
1 Tester
2 Accountant ENAME JOBTITLE
Jason Tester
3 Developer
John Accountant
4 Support
Joe Developer
Support
JOINS – RIGHT OUTER JOIN

– The RIGHT JOIN keyword returns all


the rows from the right table
(table_name2), even if there are no
matches in the left table (table_name1).
SELECT
e.ename, j.jobtitle
FROM employee e --- (TABLE 1)
RIGHT JOIN job j --- (TABLE 2)
ON e.empno = j.empno;
ON e.empno = j.empno (+);
(FROM t1,t2)
JOINS – SELF JOIN
EMPNO ENAME HIREDATE SALARY MANAGER_ID
1 Jason 25-JUL-96 8767 0
2 John 15-JUL-97 3456 1
3 Joe 25-JAN-86 5654 2
5 Sam 02-JAN-88 2134 1

SELECT w.ename || ' works for '|| ' ' || m.ename


FROM employee w, employee m
WHERE w.manager_id = m.empno;

W.ENAME||'WORKSFOR'||''||M.ENAME
Sam works for Jason
John works for Jason
Joe works for John
JOINS – USING Keyword
SELECT e.ename, j.jobtitle
FROM employee e
INNER JOIN job j
ON e.empno = j.empno;
– Instead of using the ON key word to
compare with the common column USING
keyword can be used.

SELECT e.ename, j.jobtitle


FROM employee e
INNER JOIN job j
USING empno;
INTRODUCTION TO PL/SQL
• PL SQL
• PROCEDURE AND PACKAGE
What is PL/SQL ?
– PL/SQL is Oracle's procedural language
extension to SQL, the non-procedural
relational database language?

– With PL/SQL, you can use SQL


statements to manipulate ORACLE data
and the flow of control statements to
process the data.

– We can declare constants and


variables, define subprograms
(procedures and functions), and trap
runtime errors.
What is PL/SQL ?
-- PL/SQL combines the data
manipulating power of SQL with the
data processing power of procedural
languages.
Server Server

SQL SQL SQL


SQL SQL SQL
Query1 Query2 Query3
Query1 Query2 Query3

PL-SQL Block
Client

Client

Oracle applications are built using client-server architecture. The Oracle database resides on the server.
•The program that makes requests against this database resides on the client machine.
•This program can be written in C, Java, or PL/SQL
Declare
Begin
Exception
PL SQL Block End;

Declare is optional and only


required when variables need to be
declared.

Exception is optional and required


when Error/Exception handling is
done.

Begin and End are mandatory as


all logic and queries are written
inside it.
PL SQL Block – Simple program
DECLARE
v_num1 Number;
v_num2 Number;
v_sum Number;
BEGIN
V_num1 := &Number1;
V_num2 := &Number2;
V_sum := v_num1 + v_num2 ;
Dbms_Output.Put_Line (‘The Sum of
number is :’ || v_sum);
END;
What is a PL/SQL PROCEDURE?
– PL/SQL procedure is a named block
that performs one or more actions.
PL/SQL procedure allows you to wrap
complex business logic and reuse it.
PROCEDURE [schema.]name[( parameter[, parameter...]
)]
[AUTHID DEFINER | CURRENT_USER]
IS
[--declarations statements]
--executable statements
[ EXCEPTION
---exception handlers]
BEGIN
END [name];
PL/SQL Procedure’s Header
– Schema: The optional name of the
schema that own this procedure.

– Name: The name of the procedure. It


should be always meaningful and starting
by a verb.

– Parameters: The optional list of


parameters. IN,OUT and INOUT.
PL/SQL Procedure’s BODY .
CREATE OR REPLACE PROCEDURE
adjust_salary(
in_employee_id IN,
in_percent IN
) IS
BEGIN
-- update employee's salary
UPDATE employees
SET salary = salary + salary * in_percent / 100
WHERE employee_id = in_employee_id;
END;
What is a PL/SQL PACKAGE?
– PL/SQL package is a group of related
stored functions, Procedures and
cursors.

– It is like a library once written stored in


the Oracle database and can be used
by many applications.

– The package specification is the


package’s API.
Thank You

You might also like