You are on page 1of 2

www.PTI.

net 800-538-0453 ADVANCING THE DATA-DRIVEN WORLD

ORACLE SQL QUICK GUIDE BASIC SYNTAX AND EXAMPLES DELETE FROM staff WHERE lname = Smith; DELETE FROM staff;

FOR THE BEGINNING USER Note: There are many variations and extensions to the syntax and UPDATE staff SET lname = Plum This being the first change you execute inside your session, begins your first
examples provided below. See Oracles documentation for com- WHERE fname=Anna transaction. Any users who SELECT from staff will still see all rows in the
plete syntax diagrams and usage examples. table because you havent yet made the change permanent. You may issue
LANGUAGE FUNDAMENTALS MERGE INTO staff s USING my_staff m
additional DML statements to change the data as your transaction contin-
Structured Query Language (SQL) is the method by which we are http://tahiti.oracle.com ON (s.ssn = m.ss#)
ues. The following will end your transaction:
able to work with objects and their data inside our database. The WHEN MATCHED THEN
following list gives an overview of the commands and their DDL (Data Definition) WITH TABLES UPDATE staff SET COMMIT;
classification in SQL. s.fname = m.fname,
CREATE TABLE table_name (
s.lname = m.lname,
col1_name DATATYPE [NOT NULL] [DEFAULT expr] Be aware, in addition to an explicit COMMIT, ANY DDL issued inside this ses-
s.phone = m.ph#
DDL column_constraint spec],
WHEN NOT MATCHED THEN sion will implicitly commit everything up until that point in your session.
col2, col3, colx, INSERT (fname, lname, ssn, phone)
Data Definition Language: Commands that we use to create and alter object [table_constraint_spec] );
structures in the database. These commands do not change the actual data. VALUES (m.fname, m.lname, m.ss#, m.ph#); Prior to issuing a COMMIT or DDL statement, you may decide that you were
Each change is committed immediately and ends the transaction including all in error and want to undo the changes. As in the above example, maybe
DML issued up to that point. CREATE TABLE names ( you forgot your WHERE clause. If this is the case, issue a ROLLBACK to
fname VARCHAR2(20), lname VARCHAR2(20), DCL (Data Control) and PRIVILEGES completely undo the change.
CREATE Create a new object n the database. ssn INTEGER(9) PRIMARY KEY, ph_num INTEGER(10) NOT Privileges are required in order for a user to be able to do anything in the
ALTER Change the structure of an existing object. NULL ); database. OBJECT PRIVILEGEs enable a user to make a change to the data ROLLBACK;
or, in other words, use DML against specific objects in the database. SYS-
DROP Remove an object from the database. TEM PRIVILEGEs allow a user to carry out a particular action in the database.
CREATE TABLE names_cpy AS
SELECT * FROM names; For a complete list and descriptions, see Oracle documentation. Some Unfortunately, had you also made many other changes that were not yet
ADD Add a column, constraint, etc to an existing object.
examples follow. committed in your session, they would also be rolled back. To control this
MODIFY Change an attribute such as a column datatype. ROLLBACK behavior, you should consider using SAVEPOINTs as follows.
ALTER TABLE names ADD (bday VARCHAR2(10));
RENAME Change the name of a column or object. OBJECT PRIVILEGES SYSTEM PRIVILEGES
UPDATE staff SET lname = Greer
ALTER TABLE names MODIFY (bday DATE); SELECT ON table, sequence, view CREATE (ANY) object
WHERE ssn = 315984545;
DML INSERT ON table, view ALTER (ANY) object --many more updates
Data Manipulation Language: Allows us to retrieve and make changes to the ALTER TABLE names DROP COLUMN bday; SAVEPOINT 1_after_upd;
UPDATE ON table, view DROP (ANY) object DELETE FROM staff WHERE lname = Plum;
data in the database. Changes may be explicitly committed or rolled back.
DELETE ON table, view CREATE SESSION DELETE FROM staff;
SELECT Query data in the database. ALTER TABLE names RENAME COLUMN ph_num TO phone; --forgot WHERE clause
ALTER ON table UNLIMITED TABLESPACE SAVEPOINT 2_after_del;
INSERT Insert a new row into an existing table. INSERT INTO staff SELECT * from new_hires;
ALTER TABLE names RENAME TO staff
FLASHBACK ON table FLASHBACK (ANY) table --realize your mistake
UPDATE Change the value of existing row data in a table.
EXECUTE ON procedure, function EXECUTE (ANY) object ROLLBACK to SAVEPOINT 1_after_upd
DELETE Remove a row of data from an existing table. DROP TABLE names_cpy;
INDEX ON table RESUMABLE
MERGE Merges one or more tables by either updating the existing In this case, your changes are only rolled back to the SAVEPOINT that you
row in the target table or inserting a new one depending DML (Data Manipulation) WITH TABLES REFERENCES ON table EXPORT/IMPORT FULL DATABASE created just before you did the large DELETE operation, so your updates are
upon whether it exists already or not. preserved.
SELECT is different than other DML statements in that it does not actually
change/manipulate the data by itself. It is sometimes used with other GRANT privilege ON object GRANT privilege TO user
DCL commands to carry out DML using data retrieved from elsewhere in the TO user [WITH GRANT
OPTION];
[WITH ADMIN OPTION]; LOGGING INTO AND OUT OF SQLPLUS
database. Following is the basic query-only syntax.
Data Control Language: Allows us to control which users have privileges to
access objects or carry out certain actions in the database. Sqlplus username/ @database_name: logging into
SELECT col1, col2, colx | * FROM table_name Notice the WITH GRANT/ADMIN OPTION for each type of privilege. These sqlplus
GRANT Give a role or privilege to a user. [WHERE colx = expr] [ORDER BY colx]; options give the grantee the ability to further grant the same privileges to
REVOKE Take a role or privilege away from a user. other users. Also note that OBJECT PRIVILEGES are always granted ON a
specific object. The owner of an object automatically has all object privi- C:\app\oracle>sqlplus system@oradb
SELECT fname, lname FROM staff SQL*Plus: Release 11.1.0.7.0 - Production on Wed
leges on the objects they own. To grant all object privileges to a non-owner: Aug 26 14:04:37 2009
WHERE phone IS NOT NULL
TCL ORDER BY lname; Copyright (c) 1982, 2008, Oracle.
GRANT ALL ON object TO user; All rights reserved.
Transaction Control Language: Allows us to make changes permanent, undo Enter password:
them, or create periodic rollback-to points. Connected to:
True Data Manipulation Oracle Database 11g Enterprise Edition Release
COMMIT Makes a DML change permanent. Transaction Control 11.1.0.7.0 - Production
INSERT INTO staff (fname, lname, ssn)
With the Partitioning, OLAP, Data Mining and Real
ROLLBACK Un-does an uncommitted change rather than COMMITing it. VALUES (Chris, Plum, 318675309); Transaction Control is a very important aspect of SQL statement execution Application Testing options
Brings back the before image. to understand. When you request a change be made in the database, it is SQL>
not visible to other users or made permanent until you COMMIT your
SAVEPOINT Creates a marker in a series of statements within a INSERT INTO staff SELECT * FROM new_hires;
transaction. Consider the following which deletes all rows from the staff
transaction so that we can ROLLBACK part of a transaction table.
rather than the entire transaction.

Oracle SQL Server Quick Guide.in1 1 12/23/2010 1:37:45 PM


Exit: logs out of sqlplus, commits or rolls back any pending changes and SQL> select department_nmae from hr.departments;
returns control to the operating system. select department_nmae from hr.departments Perpetual Technologies, Inc. (PTI)
*
SQL> exit
ERROR at line 1: provides mission-critical database and
ORA-00904: DEPARTMENT_NMAE: invalid identifier
Disconnected from Oracle Database 11g Enterprise
Edition Release 11.1.0.7.0 - Production
SQL> c/department_nmae/department_name information systems support to
1* select department_name from hr.departments
With the Partitioning, OLAP, Data Mining and Real
Application Testing options
SQL> / commercial and government
DEPARTMENT_NAME
C:\app\oracle> ------------------------------ enterprises worldwide.
Administration
Marketing Focused on improving performance
BASIC EXAMPLES FOR SQLPLUS EDITING Purchasing
AND FORMATTING and lowering costs, our subject-matter
SQLPLUS Command Description
SQL> show linesize
linesize 80
experts plan, design, develop, deploy,
DESCRIBE describes table columns and manage Oracle database
<schema>.<table> SQL> show user
EDIT edits current sql statement in buffer USER is SYS environments running on UNIX and
START, RUN or @ executes stored statement on O/S Windows platforms. Perpetual
filesystem FORMATTING YOUR OUTPUT Technologies strives to create tailored,
HOST <O/S command> executes host command on O/S while
in SQLPLUS
Using commands such as the following, helps to display your output in a
more readable presentation and can be used to format reports for printing. flexible IT solutions in the areas of
SPOOL <filename.ext> saves sql statement output to Oracle database 8i, 9i, 10g, 11g,
filename on O/S filesystem col <column_name> sets the column to display alphabetic
CHANGE or c/name/name changes first input with second input
format a30 character up to specified number of Oracle RAC, capacity planning,
characters until wrapping occurs
for line in buffer
col <column_name> sets the column to display specified character
disaster recovery planning,
GET </path/filename.ext> loads the file into the sqlplus buffer format 999,999 at specified intervals on number columns performance tuning, Oracle
SAVE </path/filename.

ORACLE SQL
saves the contents in the buffer to
ext> a file
set pages 200 sets the page length to specified number of
rows until heading is displayed again
Application Server , Oracle content
SHOW <parameter> shows the current value for set lines 150 sets the line length to specified number of manager, Oracle database design,
LIST
parameter ex. (user, spool, linesize)
show current buffer contents set head off
characters until wrapping occurs
sets the heading off
complete or supplemental remote Quick Reference Guide
set trim on trims the blank spaces off ends of lines
Oracle database administration, FOR THE BEGINNING USER
SQL> describe hr.departments
Name Null? set underline sets underline of column heading to specified after-hours DBA coverage, and
Type <character> off/on
------------------- -------- ---------------------
character, on/off
Oracle database vacation support.
-- DEPARTMENT_ID NOT NULL NUM- set pause on/off sets pause for pages on/off (pauses are based
BER(4) on what pagesize is set to)
DEPARTMENT_NAME NOT NULL VAR-
set feedback on/ Sets the informational return of rows on/off
CHAR2(30)
off
MANAGER_ID NUMBER(6)
LOCATION_ID NUMBER(4)
Using this alter session statement allows you to change the date format for
SQL> host dir your session only.
Volume in drive C has no label.
Volume Serial Number is 94F3-F664 SQL> alter session set nls_date_format=DD-MON-YYYY
Directory of C:\Users\mpyle hh24:MI:SS;
08/24/2009 09:51 PM <DIR> .
08/24/2009 09:51 PM <DIR> ..
08/12/2009 12:46 PM
3 File(s) 272,445 bytes
<DIR> AppData CONCLUSION
3 Dir(s) 55,841,816,576 bytes free
There are many many good online references for SQL statement execution.
This document only bullet-points some of the very basic commands. It is
a very powerful language that, when exploited, will enable you to produce
very complex reports.
5649 Lee Road
Indianapolis, IN 46216 ADVANCING THE DATA-DRIVEN WORLD
800-538-0453
www.PTI.net

Oracle SQL Server Quick Guide.in2 2 12/23/2010 1:37:45 PM

You might also like