Download lecture 5

Document related concepts

Concurrency control wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Relational algebra wikipedia , lookup

DBase wikipedia , lookup

Team Foundation Server wikipedia , lookup

Database wikipedia , lookup

Microsoft Access wikipedia , lookup

Ingres (database) wikipedia , lookup

Tandem Computers wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Btrieve wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

Oracle Database wikipedia , lookup

Null (SQL) wikipedia , lookup

Relational model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
SQLPLUS
Know more about the tool you rely on!
• show user
Oracle Basics
• You must have an existing database instance before you
can create an oracle relation (table).
• If you use an Oracle account, you are already given a
database instance when DBA opens the account for you.
• Note that the following discussion is generic, and not
specific to Oneonta account.
3
How to talk to Oracle server?
• Various Client software
Oracle system client server architecture
•Oracle server and Oracle client
Oracle server
Oracle client
Oracle client
data management
transaction control
recovery
security
Oracle client
Client software provides interface to manipulate data
tools to support development of application.
SQL*PLUS is one of the client tools allowing developers to
communicate with Oracle server.
Tools of Oracle
• The tools provided by Oracle are so user friendly that
a person with minimum skills in the field of
computers can access them with ease.
• The main tools are:
–
–
–
–
SQL *Plus
PL/SQL
Forms
Reports
6
SQL*Plus
SQL*Plus is frequently used by DBAs and Developers to
interact with the Oracle database.
SQL*Plus is an Oracle specific program which accepts
SQL commands and PL/SQL blocks and executes
them. SQL*Plus enables manipulation of SQL
commands and PL/SQL blocks. It performs many
additional tasks as well.
7
Oracle is a client-server architecture and SQL*Plus is the client.
SQL*Plus
• Interface to manipulate Oracle databases
• Tool to support the development of application
- SQL*Plus as an interface
What is SQL*PLUS
• SQL*Plus is a client end interface tool and
reporting tool that ships with the Oracle
Database Server.
• SQL*Plus is a client terminal software allowing
users to interact with Oracle server to
manipulate data and data structures.
• It is a client agent to bridge the user and the
server. It is usually used interactively.
– Users type in SQL statements in SQL*Plus that
send statements to Oracle server, with simple
grammar check.
– Oracle server then validates and executes the
statements on its databases.
– The query results are returned to SQL*Plus and
displayed to the user.
• Besides sending SQL statements to the server,
SQL*Plus also saves them into a local buffer
and allow users to view and change the
statements. The following figure illustrates the
process.
What commands does SQL*PLUS deal
with?
• It can take SQL and PL/SQL commands from
users in command line mode and send those
commands to Oracle server.
• Besides bypassing SQL and PL/SQL, SQL*PLUS
supports also a set of SQL*PLUS commands to
help format query result and preprocess SQL
query.
• Therefore, when you use SQL*PLUS, you
potentially will mingle several set of
commands when you type:
– SQL commands intended to pass to oracle server.
– PL/SQL commands also for oracle server to run
– SQL*PLUS commands for pre-processing or postprocessing for SQL*PLUS itself
- To start SQL*Plus, enter Oracle username and password:
– $> sqlplus jason/athena
(from a command line
operating system such UNIX)
– click: Start  Program  Oracle (for Windows SQL*Plus)
– At SUCO, we get access to SQL PLUS through a batch
file.
Exiting SQL*Plus
• Three ways to exit SQL*Plus:
– Type exit at the SQL prompt
– lick File on the menu bar, and then click Exit
– Click the Close button on the program window title bar
• Database session ends when SQL*Plus exits
• After you login into SQL*Plus, at the SQL prompt, you can
begin typing any SQL command.
• Upon hitting return (i.e., enter key) the SQL prompt will
change to line number prompts.
• When you are finished typing a command, type / or RUN to
execute the SQL command.
• Also, a semicolon at the end of the SQL command will execute
the command immediately after hitting return.
• In addition to SQL commands, /, and RUN, you can also
executes SQL*Plus commands.
SQL*Plus file commands
• SQL*Plus file command allow you to execute
commands (or programs) stored in an external
file, input or output data from/to a file, and
save SQL commands typed during current
session.
Some SQL*Plus file commands
• SAVE filename.
– This allows you to save buffer contents into a file.
• SPOOL filename.
– This allows you save SQL statements together with their outputs to a
file.
• GET filename.
– This retrieve a file and places it into the buffer.
• START filename.
– This allows you to execute a batch of SQL statements stored in a file.
• @ filename.
– This allows you to execute a PL/SQL procedure(s) stored in a file.
Retrieving a File
• To retrieve SQL command from a file use GET
filename or START filename.
• GET filename places the file into the buffer.
• START filename executes the commands in the file.
• The @ (at symbol) is equivalent to the START
command and is used to run SQL*Plus command
scripts.
Using Scripts to Create Database Tables
• One or more SQL commands saved in a text file
• Usually have .sql extension
• To run from SQL*Plus:
– Start full file path (c:\temp\myfile.sql)
– @full file path (@c:\temp\myfile.sql)
– Extension can be omitted if it is .sql
Sending the Results to a File
using spool
• To send queries and their results to a file for later
printing (e.g., turning in a running session for
homework assignments), the spool command is
used.
• On the menu go to FILE/Spool to begin spooling--it
will ask for a file name. To quit spooling, go to
FILE/Spool and then click on end Spool.
• Or you can always issue appropriate SQLPLUS
commands to activate the …
Example of using spool
• spool test.out
• select * from employee;
• spool off
Line-by-line Editing
• The previously executed commands (in current SQL*Plus session) are
stored in the local buffer. One way to change an SQL statement in the
buffer is by using the line editor. The following are a list of line edit
commands.
– LIST or L--Lists the contents of the buffer
– LIST n or L n--Lists the contents of line number n in the buffer and makes the
line current
– LIST * or L *--Lists the current line
– LIST m n--Lists the range from m to n line
– Append text or A text--Adds to the end of the current line (e.g., "A ," adds a
comma to the end of line
– INPUT or I--Adds one or more lines after the current line so you can begin
adding the text.
– CHANGE /text--Deletes text from the current line
– CHANGE /oldtext/newtext--Replaces oldtext with newtext in the current line
– DEL -- Deletes the current line
• I would not recommend you spend time on this topic, for the best way is
to directly use a text editor to edit your script then copy paste or run the
script file as a whole directly.
Editing Prior Commands
• Using a Text editor
• EDIT--Places you in an editor (Notepad or
textpad depending the default editor setting).
The buffer contents are put into the editor .
Using Notepad
• Useful to use Notepad to edit sql commands
– Commands can be edited without retyping
– Commands can be saved
– Saving multiple sql commands in a file creates a script
SQL*PLUS variables
• There are two types of variables that can be
used in SQL*Plus:
– Substitution Variables
• which are declared with define
• A kind of more like a constant
– bind variables
• Using command variable (var)
• Real variables
Substitution variables
• You can define substitution variables, for
repeated use in a single script by using the
SQL*Plus DEFINE command.
• define sets a user variable or displays its value.
– & symbol
– Accept allows sqlplus use more user friendly
prompts for variables, instead of variable names.
– Like define in c. The defined symbolic name will be
replaced by the affiliated value.
• The following command sets the variable
cname to ONEONTA.
• define cname=ONEONTA
• From now on, whenever SQL*Plus encounters
a &cname, it replaces it with ONEONTA.
• The ampersand & is the default prefix for
defined variables.
• The value of cname can be displayed with
(assuming the default escape character was
set to the ampersand (&) and hasn't been
changed.
– prompt &cname
• A defined variable can be undefined with
undefine.
• By default, when SQL*Plus encounters a defined variable, it prints the
original line and the line with the substitued values:
• define s="4+3'"
• define t=dual
• select &s from &t;
• old 1: select &s from &t
• new 1: select 4+3 from dual
•
4+3
• --------•
7
Suppressing old and new values
•
•
•
•
•
•
•
•
•
This behavior can be turned with setting verify to off:
set verify off
define thousand=1000
define twelve = 12
define plus = +
select &thousand &plus &twelve from dual;
1000+12
---------1012
•
•
•
•
The ampersand is the default prefix for defined variables.
However, default can be changed with set define.
set define x
set define x specifies the prefix-character for substitution
variables. The default is the ampersand (&).
• set define +
• select * from dba_objects where object_name like
'%+object_name%';
Accept
• A user variable can (mostly useful in a script) be set
interactively with accept.
– Setting a value of a variable interactively
• With accept, it is possible to interactively set a value for a user
variable in SQL*Plus.
– accept foo prompt 'Enter value for foo'
• Inputting passwords
– accept pw prompt 'Enter your secret password' hide
Accept
•
•
•
•
•
•
•
accept variable
accept variable number
accept variable char
accept variable date
accept variable binary_float
accept variable binary_double
accept variable format format-string accept variable default
default-value accept variable prompt prompt-string accept
variable noprompt
• accept variable hide
Output format enhancement
• Present data result more friendly.
• Produce more readable Output.
• Formatting column Headings
Binding variables
• In SQL*Plus, a binding variable declares a bind
variable that can be embedded in subsequent
SQL statements.
• a bind variable is declared with variable:
• var cenrollment number
• var cname varchar2(15)
• After the declaration, a value can be assigned
to the variable
• begin
• select 'oneonta', 44 into :cname,
:cenrollment from dual;
• end;
• /
• The value of the bind variable can then be
printed with print:
• print cname
•
•
•
•
•
begin
select 55 into :cage from dual;
end;
/
• Assigning a value to a bind variable with
execute
– Since an execute is basically a wrapper around a
begin .. end PL/SQL block, a variable can be
assigned a value like
• exec :cname:='oneonta'
• exec insert into sunycampus values (:cname,
:cenrollment)
• select * from sunycampus;
•
•
•
•
•
•
•
•
•
variable bind_var_name number
variable bind_var_name char
variable bind_var_name char(n)
variable bind_var_name nchar
variable bind_var_name nchar(n) variable bind_var_name
varchar2 variable bind_var_name varchar2(n) variable
bind_var_name nvarchar2(n) variable bind_var_name clob
variable bind_var_name nclob
variable bind_var_name refcursor
variable bind_var_name binary_float
variable bind_var_name binary_double
Formatting Output in SQL*Plus
• To change default column headings:
– Specify alternate column headings: SELECT fieldname1
"heading1_text", fieldname2 "heading2_text", ...
– Use an alias for column headings: SELECT fieldname1 AS
alias_name1...
• To change SQL*Plus line and page size settings
– Select Options/Environment on menu bar
– Modify linesize and pagesize to desired values
• Please go through chapter 4
Simple formatting query results
• To format a number attribute to a dollar format, use the column
<attribute> format <format>:
• SQL> COLUMN salary FORMAT $999,999
• To indicate the displayed width of a character string attribute, use the
column <attribute> format <A'format>.
• For example, set the width of the name attribute to 8 characters. SQL>
COLUMN name FORMAT A8
• If a name is longer than 8 characters, the remaining is displayed at the
second line (or several lines)
• column c1 heading "First Name" Format a15
•
•
•
•
COLUMN column_name CLEAR
COLUMN column_name FORMAT model
COLUMN SALARY FORMAT 99,990
COLUMN SALARY FORMAT $99,990
set
• The set command can be used to change the default
number of lines per page (14) and the number of
characters per line (80).
• For example, to set the number of lines per page to
60, use the following command:
• SQL> SET PAGESIZE 60
• All formatting remain active until they are cleared or
reset or after you exit from SQL*Plus.
• SQL> CLEAR COLUMN
• Sometimes when you get something fuzzy, you can
try the following
• SQL> SET SERVEROUTPUT ON
• SQL> set verify off
• SQL> set timing on
• SQL> create table tff(f1 int);
• Table created.
• real: 188
• SQL> set timing off
• It is measured in Milliseconds
Errors
• When an error occurs error information is displayed:
– Line number
– Position on line
– Error code
– Description of error
• Error codes
– 3 letter prefix (I.e. ORA)
– 5 digit code
– More information on errors can be found at
http://otn.oracle.com
The role of SQLPLUS
• If you are familiar with other databases,
sqlplus is equivalent to
– "sql" in Ingres,
– "isql" or in Sybase
– “sql analyzer” for SQLServer,
– "db2" in IBM DB2,
– "psql" in PostgresQL,
– "mysql" in MySQL.
Chapter 2
How to use
Oracle SQL Developer
and other tools
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 51
Objectives
Applied
 Start or stop the Oracle database server and listener.
 Use SQL*Plus to run a SQL statement.
 Use Oracle SQL Developer to do any of the following:
Create a database connection
Export or import database connections
Navigate through the objects of a database
View the column definitions for a table
View the data for a table
Edit the column definitions for a table
 Use Oracle SQL Developer to enter, run, open, and save SQL
statements and scripts.
 Use the SQL Reference manual to look up information about SQL
statements.
Objectives (continued)
Knowledge
 Briefly describe the function of each of these products:
Database Home Page
SQL*Plus
Oracle SQL Developer
SQL Reference manual
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 53
How to stop the database
 StartAll Programs
Oracle Database 10g Express Edition
Stop Database
The DOS window when the database is stopped
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 54
How to start the database
 StartAll Programs
Oracle Database 10g Express Edition
Start Database
The DOS window when the database is started
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 55
Terms to know
 Database service (database server, database engine)
 Database listener
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 56
The Database Home Page
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 57
How to start the Database Home Page from the
Windows Start menu
 All Programs
Oracle Database 10g Express Edition
Go To Database Home Page.
 Use the Database Login page to log in.
How to use the Database Home Page to create a
user for a database
 Log in as the system user.
 Administration
Database Users
Create User.
 Use the resulting web page to create the user.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 58
The SQL*Plus tool
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 59
How to start SQL*Plus
 Select the Run command from the Start menu.
 Enter “sqlplus”, and click the OK button.
How to connect to a database with SQL*Plus
 Enter the username and password.
How to run a SQL statement with SQL*Plus
 Type the statement.
 Type a semicolon, and press the Enter key.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 60
The SQL Developer dialog box for creating
database connections
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 61
How to create a database connection
 Right-click on the Connections node.
 Select the New Connection command.
 Enter a connection name, username, and password.
 Click the Test button to test the connection.
 Click the Save button to save the connection.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 62
How to export database connections
 Right-click the Connections node.
 Select the Export Connections command.
 Select the connections that you want to export.
 Specify the path and filename for the XML file for the connections.
How to import database connections
 Right-click the Connections node.
 Select the Import Connections command.
 Navigate to the XML file for the connections.
 Select the connections that you want to import.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 63
The Connection Information dialog box
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 64
The tables available to the AP user
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 65
The column definitions for a table
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 66
How to view the column definitions for a table
 Click on the name of the table in the Connections window.
 By default, the columns are displayed in the sequence in which
they were created.
How to view the data for a table
 Click on the Data tab.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 67
Editing the column definition of a table
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 68
A SELECT statement and its results
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 69
How to change the connection for a worksheet
 Use the Connections list
How to comment out or uncomment a line
 Press Ctrl+/.
Two ways to execute a SQL statement
 Press the F9 key.
 Click the Execute Statement button in the toolbar.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 70
Using the snippets window
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 71
Handling syntax errors
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 72
Common causes of errors
 Misspelling the name of a table or column
 Misspelling a keyword
 Omitting the closing quotation mark
 Being connected as the wrong user
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 73
The Open File dialog box
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 74
Three ways to open a SQL file
 Click the Open button in the toolbar.
 Press Ctrl+O.
 Select the FileOpen command.
How to switch between open statements
 Select the appropriate tab.
How to cut, copy, and paste code
 Use the standard Windows techniques.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 75
Three ways to save a SQL file
 Click the Save button in the toolbar.
 Press Ctrl+S.
 Select the FileSave command.
How to change the default directory
for new statements
 Use the ToolsPreferences command.
 Expand the Database node.
 Click on the Worksheet Parameters node.
 Change the default path for scripts.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 76
A SQL script and its results
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 77
How to run an entire SQL script
 Press F5 or click the Run Script button.
How to run one statement within a script
1. Move the cursor into the statement you want to execute.
2. Press F9 or click the Execute Statement button.
Note
 The results of a statement are displayed in the Results tab.
 The results of a script are displayed in the Script Output tab.
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 78
The Oracle Database SQL Reference manual
Murach’s Oracle SQL and
PL/SQL, C2
© 2008, Mike Murach & Associates, Inc.
Slide 79
http://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_five.htm
#i1211130