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

SQL Plus: PL/SQL Blocks On The Results of A Query in The Form of Reports

SQL*Plus is a command line tool used to interact with Oracle databases. It allows users to execute SQL statements and PL/SQL blocks to manipulate database objects like tables, views and schemas. Some key functions of SQL*Plus include editing, saving, loading and running SQL commands, formatting and printing query results, and viewing metadata about database objects. It provides an interface for tasks like database administration, user management, and data retrieval, updating and deletion.

Uploaded by

hiwot kebede
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

SQL Plus: PL/SQL Blocks On The Results of A Query in The Form of Reports

SQL*Plus is a command line tool used to interact with Oracle databases. It allows users to execute SQL statements and PL/SQL blocks to manipulate database objects like tables, views and schemas. Some key functions of SQL*Plus include editing, saving, loading and running SQL commands, formatting and printing query results, and viewing metadata about database objects. It provides an interface for tasks like database administration, user management, and data retrieval, updating and deletion.

Uploaded by

hiwot kebede
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 57

SQL*Plus

•The Oracle Corporation also supplies interface tools to access data stored in
an Oracle database
•SQL * Plus is a Oracle utility that recognizes SQL commands and sends these
commands to Oracle server for execution.
•SQL*Plus – A command line tool used to manipulate tables and other
database objects in an Oracle database

Operations you can perform in SQL * Plus


editing, saving, loading and execution of SQL commands and
PL/SQL blocks;
 formatting, saving, printing, and making certain calculations
on the results of a query in the form of reports;
listing of definitions of tables;
accessing and transferring data between databases;
implementation of functions for database managing:
the users administration, namespaces table, operations management of
archiving and recovery.
The differences between SQL * Plus
commands and SQL commands
• SQL * Plus Commands
– not asking the end character;
– are not submitted in the SQL buffer;
– do not allow manipulation of the data in the
database;
- keywords may be abbreviated.
Running SQL*Plus under Windows

• To run the SQL*Plus command line program


from Windows, click on the button, Programs,
Oracle – OraHomeXX, Application
Development and then SQL*Plus.
– Enter User-name:
– Password:
Connecting to Oracle Database as
User HR
• unlock the HR account and connect to Oracle
Database as the user HR,
• To unlock the HR account and reset its
password:
1. Using SQL*Plus, connect to Oracle Database
as a user with the ALTER USER system
privilege.
2. At the SQL> prompt, unlock the HR account
and reset its password:
ALTER USER HR ACCOUNT UNLOCK IDENTIFIED
BY password;
Viewing HR Schema Objects with
SQL*Plus
• With SQL*Plus, you can view the objects that
belong to the HR schema by querying the
static data dictionary view USER_OBJECTS.
Sql>select object_name,object_type from
user_objects order by
object_type,object_name;
Viewing EMPLOYEES Table Properties using
describe sqlplus command

DESCRIBE EMPLOYEES
Viewing EMPLOYEES Table Data with
SQL*Plus
COLUMN FIRST_NAME FORMAT A20
COLUMN LAST_NAME FORMAT A25
COLUMN PHONE_NUMBER FORMAT A20

SELECT LAST_NAME, FIRST_NAME,


PHONE_NUMBER FROM EMPLOYEES ORDER
BY LAST_NAME;
SQL*Plus Commands
• to rename a column labelled LAST_NAME with
the heading "Family Name", enter the
command:
• COLUMN LAST_NAME HEADING 'Family
Name'
DESCRIBE/DESC
• to list column definitions for the EMPLOYEES
table, enter the command:

DESCRIBE EMPLOYEES
schema
• A schema is a collection of database objects that
can include: tables, views, indexes and
sequences.
• By default, each user has their own the schema
which has the same name as the Oracle
username. For example, a single Oracle database
can have separate schemas for HOLOWCZAK,
JONES, JSHIH, SMITH and GREEN
• Any object in the database must be created in
only one schema. The object name is prefixed by
the schema name as in: schema.object_name
Schema
A schema is a collection of logical structures of data,
called schema objects.

• It is owned by a database user and has the same


name of the user.

• Schema objects can be created and manipulated


with SQL

12
Schema objects
-Tables
-Indexes
-Constraints
… but also (in ORACLE)
-Links
-Views
-Triggers
-Operators
-Sequences
-Stored functions
-Stored procedures
-Synonyms

…and more

13
• By default, all objects are created in the user’s
own schema
• If JONES creates an EMPLOYEE table, the full
name of the table becomes: JONES.EMPLOYEE
Unlocking the Sample Tables

• The Human Resources (HR) Sample Schema is


installed as part of the default Oracle Database
installation. The HR account is locked by default.
• You need to unlock the HR account before you
can use the HR sample schema. To unlock the HR
account, log in as the SYSTEM user and enter the
following command, where your_password is the
password you want to define for the user HR:
• ALTER USER HR IDENTIFIED BY your_password
ACCOUNT UNLOCK;

• Unlocking scott account:


• ALTER USER scott IDENTIFIED BY tiger
ACCOUNT UNLOCK;
Connecting to Database
• Open SQLPLU
– User-name:scott
– Password:tiger

• To know to which database instance you are


connect or logged in:
• SQL>define
Logging in using a different user
• SQL > connect syst as sysdba
• Password:

• Another way of logging in


SQL> connect scott/tiger
Displaying all tables owned by a
user
• Eg: Display all table names owned by scott

1) Logg using scott


2)SQL> Select table_name from user_tables;
Describing a table
• SQL>desc emp
Selecting Table Data
SELECT select_list FROM source_list

Eg:
• Select query on emp table
SQL> select empno,ename,job,hiredate,sal
from emp
order by 2;
Changing Page size

• SQL>set pagesize 200

• Executing the query in the buffer


• SQL>/
Listing the current query in the
buffer
SQL>l List
Listing a statement on a line
• Just type the line number

• Eg
• SQL>1
• Changing column listing
Eg: Change empno to deptno
SQL>c/empno/deptno
Formatting a column
• Eg: format col sal to $999,999.00 format

• Sql > col sal format $999.999.00


Modifying a query
• Type the line number you want to modify
• Use the command c (Change)

Eg
SQL> 1
SQL > c/empno/deptno
Formatting Output
• Changing date format to mm/dd/yyyy

• SQL> alter session set


nls_date_format=‘mm/dd/yyyy’

• SQL>set heading off


Changing Column Heading
SQL>col sal heading salary
SQL>/

SQL > col ename heading ‘employee|Name’


SQL>/
SQL> set underline ‘=‘
SQL>/
Changing Column Heading
SQL>col sal heading salary
SQL>/

SQL > col ename heading ‘employee|Name’


SQL>/
SQL> set underline ‘=‘
SQL>/
How to append a new value
• Example:
query : select ename,hiredate
from emp;
Append to this query deptno
SQL>1
SQL>a ,deptno
SQL>l (list)
SQL>/
Making a column more wider

• Eg
• SQL> col job format a15

• Redoing formatting

• SQL> col job clear


Saving SQL statement in a file
• To save the current SQL statement in the
buffer:

• SQL> save filename.sql


• This saves the query in the directory
from which sqlplus is executed.
Saving in a different directory
Ex:
• SQL> save D:\ex\all.sql

• Executing The Query

• SQL>@D:\ex\all (File extension not


required)
Saving the output of a query to a file
• SQL>spool output.txt [writes the
output on the file output.txt in the
directory where sqlplus is launched]
• SQL> / [write the output of the
current query to the file output.txt]
• SQL> spool off
Looking the content of the output file
• SQL > host
> type output.txt
> exit
Appending an output to a file
• Eg:
SQL> select deptno,dname
from dept

SQL> spool output.txt append


SQL> /
SQL> spool off
SQL> host type output.txt
Editing a query
• SQL > ed
edit the query and save the file
SQL> /
Listing the editor being in use
• SQL > define _editor

Changing the editor


SQL> define _editor=“Path to the editor”
Setting top and bottom titles
• SQL> ttitle “Top title”
• SQL>btitle “Bottom title”
Complete list of SQLPlus
commands
• SQL>help index

• Getting help for a particular command

• SQL>help define
SQL
• Structured Query Language (SQL) is the set of
statements with which all programs and users
access data in an Oracle database
• SQL Standards
• industry-accepted committees are the
American National Standards Institute (ANSI)
and the International Organization for
Standardization (ISO)
• SQL provides statements for a variety of tasks,
including:
• Querying data
• Inserting, updating, and deleting rows in a table
• Creating, replacing, altering, and dropping
objects
• Controlling access to the database and its objects
• Guaranteeing database consistency and integrity
Data Manipulation Language
Statements
• Retrieve or fetch data from one or more tables or views
(SELECT);
• Add new rows of data into a table or view (INSERT).
• Change column values in existing rows of a table or view
(UPDATE).
• Update or insert rows conditionally into a table or view
(MERGE).
• Remove rows from tables or views (DELETE).
• View the execution plan for a SQL statement (EXPLAIN
PLAN).
• Lock a table or view, temporarily limiting other users'
access (LOCK TABLE).
• Some examples of DML statements are:
– SELECT last_name, manager_id, commission_pct +
salary FROM employees;
– INSERT INTO employees VALUES (1234, 'DAVIS',
'SALESMAN', 7698, '14-FEB-1988', 1600, 500, 30);
DELETE FROM employees WHERE last_name IN
('WARD','JONES');
Displaying Selected Columns Under New
Headings
• [use hr schema]

SELECT FIRST_NAME First, LAST_NAME last,


DEPARTMENT_ID DepT FROM EMPLOYEES;
Preserving Case and Including Spaces in
Column Aliases

SELECT FIRST_NAME "Given Name",


LAST_NAME "Family Name“ FROM
EMPLOYEES;
Selecting Data that Satisfies Specified
Conditions
• [Use hr schema]
Eg:Selecting Data from One Department

SELECT FIRST_NAME, LAST_NAME,


DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90;
• Eg: To select data only for employees in
departments 100, 110, and 120, use this
WHERE clause:

SELECT FIRST_NAME, LAST_NAME,


DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (100, 110, 120);
Selecting Data for Last Names that Start with
the Same Substring

SELECT FIRST_NAME, LAST_NAME


FROM EMPLOYEES
WHERE LAST_NAME LIKE 'Ma%';
Selecting Data that Satisfies Two
Conditions
SELECT FIRST_NAME, LAST_NAME, SALARY,
COMMISSION_PCT "%"
FROM EMPLOYEES
WHERE (SALARY >= 11000) AND
(COMMISSION_PCT IS NOT NULL);
Selecting Data from Multiple
Tables
SELECT EMPLOYEES.FIRST_NAME "First",
EMPLOYEES.LAST_NAME "Last",
DEPARTMENTS.DEPARTMENT_NAME "Dept. Name"
FROM EMPLOYEES, DEPARTMENTS
WHERE EMPLOYEES.DEPARTMENT_ID =
DEPARTMENTS.DEPARTMENT_ID
ORDER BY DEPARTMENTS.DEPARTMENT_NAME,
EMPLOYEES.LAST_NAME;
• Table-name qualifiers are optional for column
names that appear in only one table of a join,
but are required for column names that appear
in both tables
SELECT FIRST_NAME "First",
LAST_NAME "Last",
DEPARTMENT_NAME "Dept. Name"
FROM EMPLOYEES, DEPARTMENTS
WHERE EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
ORDER BY DEPARTMENT_NAME, LAST_NAME;
Using Arithmetic Operators in
Queries

• SQL supports the basic arithmetic operators: +


(addition), - (subtraction), * (multiplication),
and / (division).

SELECT LAST_NAME,
SALARY "Monthly Pay",
SALARY * 12 "Annual Pay"
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90
ORDER BY SALARY DESC;
Using an Arithmetic Expression in
a Query
select last_name,salary as "Monthy Pay",salary *
12 as "Annual Pay"
from employees
where department_id=90
order by salary desc
Using Numeric Functions in
Queries
select last_name,salary as "Monthy
Pay",round((salary * 12)/365,2) as "Daily Pay"
from employees
where department_id=100
order by salary desc

You might also like