
- •Contents
- •Send Us Your Comments
- •Preface
- •What’s New in SQL Reference?
- •1 Introduction to Oracle SQL
- •History of SQL
- •SQL Standards
- •Embedded SQL
- •Lexical Conventions
- •Tools Support
- •2 Basic Elements of Oracle SQL
- •Datatypes
- •Oracle Built-in Datatypes
- •ANSI, DB2, and SQL/DS Datatypes
- •Oracle-Supplied Types
- •"Any" Types
- •XML Types
- •Spatial Type
- •Media Types
- •Datatype Comparison Rules
- •Data Conversion
- •Literals
- •Text Literals
- •Integer Literals
- •Number Literals
- •Interval Literals
- •Format Models
- •Number Format Models
- •Date Format Models
- •String-to-Date Conversion Rules
- •XML Format Model
- •Nulls
- •Nulls in SQL Functions
- •Nulls with Comparison Conditions
- •Nulls in Conditions
- •Pseudocolumns
- •CURRVAL and NEXTVAL
- •LEVEL
- •ROWID
- •ROWNUM
- •XMLDATA
- •Comments
- •Comments Within SQL Statements
- •Comments on Schema Objects
- •Hints
- •Database Objects
- •Schema Objects
- •Nonschema Objects
- •Parts of Schema Objects
- •Schema Object Names and Qualifiers
- •Schema Object Naming Rules
- •Schema Object Naming Examples
- •Schema Object Naming Guidelines
- •Syntax for Schema Objects and Parts in SQL Statements
- •How Oracle Resolves Schema Object References
- •Referring to Objects in Other Schemas
- •Referring to Objects in Remote Databases
- •Referencing Object Type Attributes and Methods
- •3 Operators
- •About SQL Operators
- •Unary and Binary Operators
- •Operator Precedence
- •Arithmetic Operators
- •Concatenation Operator
- •Set Operators
- •4 Expressions
- •About SQL Expressions
- •Simple Expressions
- •Compound Expressions
- •CASE Expressions
- •CURSOR Expressions
- •Datetime Expressions
- •Function Expressions
- •INTERVAL Expressions
- •Object Access Expressions
- •Scalar Subquery Expressions
- •Type Constructor Expressions
- •Variable Expressions
- •Expression Lists
- •5 Conditions
- •About SQL Conditions
- •Condition Precedence
- •Comparison Conditions
- •Simple Comparison Conditions
- •Group Comparison Conditions
- •Logical Conditions
- •Membership Conditions
- •Range Conditions
- •Null Conditions
- •EQUALS_PATH
- •EXISTS Conditions
- •LIKE Conditions
- •IS OF type Conditions
- •UNDER_PATH
- •Compound Conditions
- •6 Functions
- •SQL Functions
- •Single-Row Functions
- •Aggregate Functions
- •Analytic Functions
- •Object Reference Functions
- •Alphabetical Listing of SQL Functions
- •ACOS
- •ADD_MONTHS
- •ASCII
- •ASCIISTR
- •ASIN
- •ATAN
- •ATAN2
- •BFILENAME
- •BITAND
- •CAST
- •CEIL
- •CHARTOROWID
- •COALESCE
- •COMPOSE
- •CONCAT
- •CONVERT
- •CORR
- •COSH
- •COUNT
- •COVAR_POP
- •COVAR_SAMP
- •CUME_DIST
- •CURRENT_DATE
- •CURRENT_TIMESTAMP
- •DBTIMEZONE
- •DECODE
- •DECOMPOSE
- •DENSE_RANK
- •DEPTH
- •DEREF
- •DUMP
- •EMPTY_BLOB, EMPTY_CLOB
- •EXISTSNODE
- •EXTRACT (datetime)
- •EXTRACT (XML)
- •EXTRACTVALUE
- •FIRST
- •FIRST_VALUE
- •FLOOR
- •FROM_TZ
- •GREATEST
- •GROUP_ID
- •GROUPING
- •GROUPING_ID
- •HEXTORAW
- •INITCAP
- •INSTR
- •LAST
- •LAST_DAY
- •LAST_VALUE
- •LEAD
- •LEAST
- •LENGTH
- •LOCALTIMESTAMP
- •LOWER
- •LPAD
- •LTRIM
- •MAKE_REF
- •MONTHS_BETWEEN
- •NCHR
- •NEW_TIME
- •NEXT_DAY
- •NLS_CHARSET_DECL_LEN
- •NLS_CHARSET_ID
- •NLS_CHARSET_NAME
- •NLS_INITCAP
- •NLS_LOWER
- •NLSSORT
- •NLS_UPPER
- •NTILE
- •NULLIF
- •NUMTODSINTERVAL
- •NUMTOYMINTERVAL
- •PATH
- •PERCENT_RANK
- •PERCENTILE_CONT
- •PERCENTILE_DISC
- •POWER
- •RANK
- •RATIO_TO_REPORT
- •RAWTOHEX
- •RAWTONHEX
- •REFTOHEX
- •REGR_ (Linear Regression) Functions
- •REPLACE
- •ROUND (number)
- •ROUND (date)
- •ROW_NUMBER
- •ROWIDTOCHAR
- •ROWIDTONCHAR
- •RPAD
- •RTRIM
- •SESSIONTIMEZONE
- •SIGN
- •SINH
- •SOUNDEX
- •SQRT
- •STDDEV
- •STDDEV_POP
- •STDDEV_SAMP
- •SUBSTR
- •SYS_CONNECT_BY_PATH
- •SYS_CONTEXT
- •SYS_DBURIGEN
- •SYS_EXTRACT_UTC
- •SYS_GUID
- •SYS_TYPEID
- •SYS_XMLAGG
- •SYS_XMLGEN
- •SYSDATE
- •SYSTIMESTAMP
- •TANH
- •TO_CHAR (character)
- •TO_CHAR (datetime)
- •TO_CHAR (number)
- •TO_CLOB
- •TO_DATE
- •TO_DSINTERVAL
- •TO_MULTI_BYTE
- •TO_NCHAR (character)
- •TO_NCHAR (datetime)
- •TO_NCHAR (number)
- •TO_NCLOB
- •TO_NUMBER
- •TO_SINGLE_BYTE
- •TO_TIMESTAMP
- •TO_TIMESTAMP_TZ
- •TO_YMINTERVAL
- •TRANSLATE
- •TRANSLATE ... USING
- •TREAT
- •TRIM
- •TRUNC (number)
- •TRUNC (date)
- •TZ_OFFSET
- •UNISTR
- •UPDATEXML
- •UPPER
- •USER
- •USERENV
- •VALUE
- •VAR_SAMP
- •VARIANCE
- •VSIZE
- •WIDTH_BUCKET
- •XMLAGG
- •XMLCOLATTVAL
- •XMLCONCAT
- •XMLELEMENT
- •XMLFOREST
- •XMLSEQUENCE
- •XMLTRANSFORM
- •ROUND and TRUNC Date Functions
- •User-Defined Functions
- •Prerequisites
- •Name Precedence
- •7 Common SQL DDL Clauses
- •allocate_extent_clause
- •constraints
- •deallocate_unused_clause
- •file_specification
- •logging_clause
- •parallel_clause
- •physical_attributes_clause
- •storage_clause
- •8 SQL Queries and Subqueries
- •About Queries and Subqueries
- •Creating Simple Queries
- •Hierarchical Queries
- •The UNION [ALL], INTERSECT, MINUS Operators
- •Sorting Query Results
- •Joins
- •Using Subqueries
- •Unnesting of Nested Subqueries
- •Selecting from the DUAL Table
- •Distributed Queries
- •9 SQL Statements: ALTER CLUSTER to ALTER SEQUENCE
- •Types of SQL Statements
- •Organization of SQL Statements
- •ALTER CLUSTER
- •ALTER DATABASE
- •ALTER DIMENSION
- •ALTER FUNCTION
- •ALTER INDEX
- •ALTER INDEXTYPE
- •ALTER JAVA
- •ALTER MATERIALIZED VIEW
- •ALTER MATERIALIZED VIEW LOG
- •ALTER OPERATOR
- •ALTER OUTLINE
- •ALTER PACKAGE
- •ALTER PROCEDURE
- •ALTER PROFILE
- •ALTER RESOURCE COST
- •ALTER ROLE
- •ALTER ROLLBACK SEGMENT
- •ALTER SEQUENCE
- •10 SQL Statements: ALTER SESSION to ALTER SYSTEM
- •ALTER SESSION
- •ALTER SYSTEM
- •ALTER TABLE
- •ALTER TABLESPACE
- •ALTER TRIGGER
- •ALTER TYPE
- •ALTER USER
- •ALTER VIEW
- •ANALYZE
- •ASSOCIATE STATISTICS
- •AUDIT
- •CALL
- •COMMENT
- •COMMIT
- •13 SQL Statements: CREATE CLUSTER to CREATE JAVA
- •CREATE CLUSTER
- •CREATE CONTEXT
- •CREATE CONTROLFILE
- •CREATE DATABASE
- •CREATE DATABASE LINK
- •CREATE DIMENSION
- •CREATE DIRECTORY
- •CREATE FUNCTION
- •CREATE INDEX
- •CREATE INDEXTYPE
- •CREATE JAVA
- •14 SQL Statements: CREATE LIBRARY to CREATE SPFILE
- •CREATE LIBRARY
- •CREATE MATERIALIZED VIEW
- •CREATE MATERIALIZED VIEW LOG
- •CREATE OPERATOR
- •CREATE OUTLINE
- •CREATE PACKAGE
- •CREATE PACKAGE BODY
- •CREATE PFILE
- •CREATE PROCEDURE
- •CREATE PROFILE
- •CREATE ROLE
- •CREATE ROLLBACK SEGMENT
- •CREATE SCHEMA
- •CREATE SEQUENCE
- •CREATE SPFILE
- •15 SQL Statements: CREATE SYNONYM to CREATE TRIGGER
- •CREATE SYNONYM
- •CREATE TABLE
- •CREATE TABLESPACE
- •CREATE TEMPORARY TABLESPACE
- •CREATE TRIGGER
- •CREATE TYPE
- •CREATE TYPE BODY
- •CREATE USER
- •CREATE VIEW
- •DELETE
- •DISASSOCIATE STATISTICS
- •DROP CLUSTER
- •DROP CONTEXT
- •DROP DATABASE LINK
- •DROP DIMENSION
- •DROP DIRECTORY
- •DROP FUNCTION
- •DROP INDEX
- •DROP INDEXTYPE
- •DROP JAVA
- •DROP LIBRARY
- •DROP MATERIALIZED VIEW
- •DROP MATERIALIZED VIEW LOG
- •DROP OPERATOR
- •DROP OUTLINE
- •DROP PACKAGE
- •DROP PROCEDURE
- •DROP PROFILE
- •DROP ROLE
- •DROP ROLLBACK SEGMENT
- •17 SQL Statements: DROP SEQUENCE to ROLLBACK
- •DROP SEQUENCE
- •DROP SYNONYM
- •DROP TABLE
- •DROP TABLESPACE
- •DROP TRIGGER
- •DROP TYPE
- •DROP TYPE BODY
- •DROP USER
- •DROP VIEW
- •EXPLAIN PLAN
- •GRANT
- •INSERT
- •LOCK TABLE
- •MERGE
- •NOAUDIT
- •RENAME
- •REVOKE
- •ROLLBACK
- •18 SQL Statements: SAVEPOINT to UPDATE
- •SAVEPOINT
- •SELECT
- •SET CONSTRAINT[S]
- •SET ROLE
- •SET TRANSACTION
- •TRUNCATE
- •UPDATE
- •Required Keywords and Parameters
- •Optional Keywords and Parameters
- •Syntax Loops
- •Multipart Diagrams
- •Database Objects
- •ANSI Standards
- •ISO Standards
- •Oracle Compliance
- •FIPS Compliance
- •Oracle Extensions to Standard SQL
- •Character Set Support
- •Using Extensible Indexing
- •Using XML in SQL Statements
- •Index

DELETE
DELETE
Purpose
Use the DELETE statement to remove rows from a table, a partitioned table, a view’s base table, or a view’s partitioned base table.
Prerequisites
For you to delete rows from a table, the table must be in your own schema or you must have DELETE privilege on the table.
For you to delete rows from the base table of a view, the owner of the schema containing the view must have DELETE privilege on the base table. Also, if the view is in a schema other than your own, you must be granted DELETE privilege on the view.
The DELETE ANY TABLE system privilege also allows you to delete rows from any table or table partition, or any view’s base table.
You must also have the SELECT privilege on the object from which you want to delete if:
■The object is on a remote database or
■The SQL92_SECURITY initialization parameter is set to TRUE and the DELETE operation references table columns (such as the columns in a where_clause).
Syntax
delete::=
hint |
FROM |
dml_table_expression_clause |
t_alias |
DELETE
ONLY (
dml_table_expression_clause
)
where_clause returning_clause
;
(DML_table_expression_clause::= on page 16-56, where_clause::= on page 16-56, returning_clause::= on page 16-56)
SQL Statements: CREATE TYPE to DROP ROLLBACK SEGMENT 16-55

DELETE
DML_table_expression_clause::=
|
|
|
|
PARTITION |
( |
partition |
) |
|
|
|
|
|
|
SUBPARTITION |
|
( |
subpartition |
) |
|
|
|
|
@ |
dblink |
|
|
|
|
|
|
schema |
. |
table |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
view |
@ |
|
dblink |
|
|
|
|
|
|
materialized view |
|
|
|
|
|
|
|
|
|
subquery_restriction_clause |
|
|
|
|
|
|
( |
subquery |
|
|
) |
|
|
|
|
|
table_collection_expression
(subquery::= on page 18-5, subquery_restriction_clause::= on page 16-56, table_collection_expression::= on page 16-56)
subquery_restriction_clause::=
READ |
ONLY |
|
WITH |
CONSTRAINT |
constraint |
|
||
CHECK |
OPTION |
|
table_collection_expression::=
( +
)
TABLE (
collection_expression
)
where_clause::=
WHERE condition
returning_clause::=
|
, |
|
, |
RETURNING |
expr |
INTO |
data_item |
16-56 Oracle9i SQL Reference

DELETE
Semantics
hint
Specify a comment that passes instructions to the optimizer on choosing an execution plan for the statement.
See Also: "Hints" on page 2-91 and Oracle9i Database Performance
Tuning Guide and Reference for the syntax and description of hints
from_clause
Use the FROM clause to specify the database objects from which you are deleting rows.
The ONLY syntax is only relevant for views. Use the ONLY clause if the view in the FROM clause belongs to a view hierarchy and you do not want to delete rows from any of its subviews.
DML_table_expression_clause
schema
Specify the schema containing the table or view. If you omit schema, Oracle assumes the table or view is in your own schema.
table | view | materialized view | subquery
Specify the name of a table or view, or the column or columns resulting from a subquery, from which the rows are to be deleted. If you specify view, Oracle deletes rows from the view’s base table.
If table (or the base table of view) contains one or more domain index columns, this statements executes the appropriate indextype delete routine.
See Also: Oracle9i Data Cartridge Developer’s Guide for more information on these routines
Issuing a DELETE statement against a table fires any DELETE triggers defined on the table.
All table or index space released by the deleted rows is retained by the table and index.
SQL Statements: CREATE TYPE to DROP ROLLBACK SEGMENT 16-57

DELETE
PARTITION (partition_name) and SUBPARTITION (subpartition_name)
Specify the name of the partition or subpartition within table targeted for deletes.
You need not specify the partition name when deleting values from a partitioned table. However, in some cases, specifying the partition name is more efficient than a complicated where_clause.
See Also: "Deleting Rows from a Partition: Example" on page 16-63
dblink
Specify the complete or partial name of a database link to a remote database where the table or view is located. You can delete rows from a remote table or view only if you are using Oracle’s distributed functionality.
See Also: "Referring to Objects in Remote Databases" on page 2-118 for information on referring to database links and
"Deleting Rows from a Remote Database: Example" on page 16-62
If you omit dblink, Oracle assumes that the table or view is located on the local database.
subquery_restriction_clause
The subquery_restriction_clause lets you restrict the subquery in one of the following ways:
WITH READ ONLY Specify WITH READ ONLY to indicate that the table or view cannot be updated.
WITH CHECK OPTION Specify WITH CHECK OPTION to indicate that Oracle prohibits any changes to the table or view that would produce rows that are not included in the subquery.
CONSTRAINT constraint Specify the name of the CHECK OPTION constraint. If you omit this identifier, Oracle automatically assigns the constraint a name of the form SYS_Cn, where n is an integer that makes the constraint name unique within the database.
See Also: "Using the WITH CHECK OPTION Clause: Example" on page 18-34
16-58 Oracle9i SQL Reference

DELETE
table_collection_expression
The table_collection_expression lets you inform Oracle that the value of collection_expression should be treated as a table for purposes of query and DML operations. The collection_expression can be a subquery, a column, a function, or a collection constructor. Regardless of its form, it must return a collection value (that is, a value whose type is nested table or varray). This process of extracting the elements of a collection is called collection unnesting.
Note: In earlier releases of Oracle, when collection_ expression was a subquery, table_collection_expression was expressed as "THE subquery". That usage is now deprecated.
You can use a table_collection_expression in a correlated subquery to delete rows with values that also exist in another table.
See Also: "Table Collections: Examples" on page 18-38
collection_expression Specify a subquery that selects a nested table column from table or view.
Note: In earlier releases of Oracle, table_collection_ expression was expressed as "THE subquery". That usage is now deprecated.
Restrictions on the dml_table_expression_clause
■You cannot execute this statement if table (or the base table of view) contains any domain indexes marked IN_PROGRESS or FAILED.
■You cannot insert into a partition if any affected index partitions are marked
UNUSABLE.
■You cannot specify the ORDER BY clause in the subquery of the dml_table_ expression_clause.
■You cannot delete from a view except through INSTEAD OF triggers if the view’s defining query contains one of the following constructs:
■A set operator
■A DISTINCT operator
SQL Statements: CREATE TYPE to DROP ROLLBACK SEGMENT 16-59

DELETE
■An aggregate or analytic function
■A GROUP BY, ORDER BY, CONNECT BY, or START WITH clause
■A collection expression in a SELECT list
■A subquery in a SELECT list
■Joins (with some exceptions). See Oracle9i Database Administrator’s Guide for details.
If you specify an index, index partition, or index subpartition that has been marked
UNUSABLE, the DELETE statement will fail unless the SKIP_UNUSABLE_INDEXES parameter has been set to true.
See Also: ALTER SESSION on page 10-2
table_collection_expression
where_clause
Use the where_clause to delete only rows that satisfy the condition. The condition can reference the table and can contain a subquery. You can delete rows from a remote table or view only if you are using Oracle’s distributed functionality.
See Also: Chapter 5, "Conditions" for the syntax of condition
Note: If this clause contains a subquery that refers to remote objects, the DELETE operation can run in parallel as long as the reference does not loop back to an object on the local database. However, if the subquery in the dml_table_expression_ clause refers to any remote objects, the UPDATE operation will run serially without notification. Please refer to the parallel_clause for CREATE TABLE on page 15-53 for additional information.
If you omit dblink, Oracle assumes that the table or view is located on the local database.
If you omit the where_clause, Oracle deletes all rows of the table or view.
t_alias Provide a correlation name for the table, view, subquery, or collection value to be referenced elsewhere in the statement. Table aliases are generally used in DELETE statements with correlated queries.
16-60 Oracle9i SQL Reference

DELETE
Note: This alias is required if the dml_table_expression_ clause references any object type attributes or object type methods.
returning_clause
The returning clause retrieves the rows affected by a DML (INSERT, UPDATE, or DELETE) statement. You can specify this clause for tables and materialized views, and for views with a single base table.
When operating on a single row, a DML statement with a returning_clause can retrieve column expressions using the affected row, rowid, and REFs to the affected row and store them in host variables or PL/SQL variables.
When operating on multiple rows, a DML statement with the returning_clause stores values from expressions, rowids, and REFs involving the affected rows in bind arrays.
expr Each item in the expr list must be a valid expression syntax. All forms are valid except scalar subquery expressions.
INTO The INTO clause indicates that the values of the changed rows are to be stored in the variable(s) specified in data_item list.
data_item Each data_item is a host variable or PL/SQL variable that stores the retrieved expr value.
For each expression in the RETURNING list, you must specify a corresponding type-compatible PL/SQL variable or host variable in the INTO list.
Restrictions on the RETURNING Clause You cannot:
■Specify the returning_clause for a multitable insert.
■Use this clause with parallel DML or with remote objects.
■Retrieve LONG types with this clause.
■Specify this clause for a view on which an INSTEAD OF trigger has been defined.
See Also: PL/SQL User’s Guide and Reference for information on using the BULK COLLECT clause to return multiple values to collection variables
SQL Statements: CREATE TYPE to DROP ROLLBACK SEGMENT 16-61

DELETE
Note: This clause lets you return values from deleted columns, and thereby eliminate the need to issue a SELECT statement following the DELETE statement.
See Also: "Using the RETURNING Clause: Example" on page 16-63
Examples
Deleting Rows: Examples The following statement deletes all rows from the sample table oe.product_descriptions:
DELETE FROM product_descriptions
WHERE language_id = ’AR’;
The following statement deletes from the sample table hr.employees purchasing clerks whose commission rate is less than 10%:
DELETE FROM employees
WHERE job_id = ’PU_CLERK’
AND commission_pct < .1;
The following statement has the same effect as the preceding example, but uses a subquery:
DELETE FROM (SELECT * FROM employees)
WHERE job_id = ’PU_CLERK’
AND commission_pct < .1;
Deleting Rows from a Remote Database: Example The following statement deletes specified rows from the locations table owned by the user hr on a database accessible by the database link remote:
DELETE FROM hr.locations@remote
WHERE location_id > 3000;
Deleting Nested Table Rows: Example The following example deletes rows of nested table projs where the department number is either 123 or 456, or the department’s budget is greater than 456.78:
DELETE TABLE(SELECT projs FROM dept d WHERE d.dno = 123) p
WHERE p.pno IN (123, 456) OR p.budgets > 456.78;
16-62 Oracle9i SQL Reference

DELETE
Deleting Rows from a Partition: Example The following example removes rows from partition sales_q1_1998 of the sh.sales table:
DELETE FROM sales PARTITION (sales_q1_1998)
WHERE amount_sold > 10000;
Using the RETURNING Clause: Example The following example returns column salary from the deleted rows and stores the result in bind variable :bnd1. (The bind variable must already have been declared.)
DELETE FROM employees WHERE job_id = ’SA_REP’
AND hire_date + TO_YMINTERVAL(’01-00’) < SYSDATE; RETURNING salary INTO :bnd1;
SQL Statements: CREATE TYPE to DROP ROLLBACK SEGMENT 16-63