Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQLPLUS Know more about the tool you rely on!. Oracle Basics You must have an existing database instance before you can create an oracle relation (table).

Similar presentations


Presentation on theme: "SQLPLUS Know more about the tool you rely on!. Oracle Basics You must have an existing database instance before you can create an oracle relation (table)."— Presentation transcript:

1 SQLPLUS Know more about the tool you rely on!

2 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. 2

3 How to talk to Oracle server? Various Client software

4 Oracle system client server architecture Oracle server and Oracle client Oracle server Oracle client data management transaction control recovery security 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.

5 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 5

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. 6

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

8 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.

9 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.

10 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.

11

12 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.

13 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 post- processing for SQL*PLUS itself

14 -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.

15 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

16 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.

17 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.

18 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.

19 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.

20 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

21 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 …

22 Example of using spool spool test.out select * from employee; spool off

23 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.

24 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.

25 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

26 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

27 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.

28 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.

29 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.

30 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

31 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

32 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%';

33 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

34 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

35 Output format enhancement Present data result more friendly. Produce more readable Output. Formatting column Headings

36 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)

37 After the declaration, a value can be assigned to the variable begin select 'oneonta', 44 into :cname, :cenrollment from dual; end; /

38 The value of the bind variable can then be printed with print: print cname

39 begin select 55 into :cage from dual; end; /

40 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;

41 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

42 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

43 Simple formatting query results To format a number attribute to a dollar format, use the column format : SQL> COLUMN salary FORMAT $999,999 To indicate the displayed width of a character string attribute, use the column 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)

44 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

45 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

46 Sometimes when you get something fuzzy, you can try the following SQL> SET SERVEROUTPUT ON SQL> set verify off

47 SQL> set timing on SQL> create table tff(f1 int); Table created. real: 188 SQL> set timing off It is measured in Milliseconds

48 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

49 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.

50 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 50

51

52 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 52

53 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 53

54 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 54

55 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 55

56 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 56

57 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 57

58 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 58

59 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 59

60 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 60

61 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 61

62 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 62

63 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 63

64 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 64

65 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 65

66 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 66

67 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 67

68 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 68

69 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 69

70 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 70

71 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 71

72 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 72

73 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 73

74 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 74

75 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 75

76 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 76

77 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 77

78 Murach’s Oracle SQL and PL/SQL, C2 © 2008, Mike Murach & Associates, Inc. Slide 78

79 http://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_five.htm #i1211130


Download ppt "SQLPLUS Know more about the tool you rely on!. Oracle Basics You must have an existing database instance before you can create an oracle relation (table)."

Similar presentations


Ads by Google