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

What’s New in SQL Reference?
This section describes new features of Oracle9i release 2 and provides pointers to additional information. New features information from previous releases is also retained to help those users upgrading to the current release.
The following sections describe the new features in the Oracle9i SQL Reference:
■Oracle9i Release 2 (9.2) New Features in the SQL Reference
■Oracle9i Release 1 (9.0.1) New Features in the SQL Reference
■Oracle8i New Features in SQL Reference
xxvii
Oracle9i Release 2 (9.2) New Features in the SQL Reference
The following built-in conditions are new to this release:
■EQUALS_PATH on page 5-13
■UNDER_PATH on page 5-20
The following built-in expression is enhanced in this release:
■The syntax for type constructor expressions now allows creation of new user-defined constructors (see "Type Constructor Expressions" on page 4-13).
The following built-in functions are new to this release:
■DEPTH on page 6-57
■EXTRACTVALUE on page 6-66
■PATH on page 6-115
■UPDATEXML on page 6-197
■XMLAGG on page 6-210
■XMLCONCAT on page 6-213
■XMLCOLATTVAL on page 6-212
■XMLELEMENT on page 6-214
■XMLFOREST on page 6-217
■XMLSEQUENCE on page 6-218
■XMLTRANSFORM on page 6-219
The following privileges are new or enhanced in this release:
■ DEBUG CONNECT SESSION system privilege on page 17-37 (new)
■DEBUG ANY PROCEDURE system privilege on page 17-37 (new)
■DEBUG object privilege on page 17-46 (new)
■EXECUTE object privilege on procedures, packages, libraries, and object types (enhanced)
■GRANT ANY OBJECT PRIVILEGE system privilege on page 17-44
The following top-level SQL statements are new or enhanced in this release:
xxviii
■ALTER DATABASE on page 9-11 has new syntax for managing standby databases, for managed standby recovery, and for logical standby.
■ALTER OPERATOR on page 9-117 lets you recompile an existing user-defined operator.
■ALTER TABLE on page 11-2 contains new clauses that let you rename a column or a constraint.
■CREATE DATABASE on page 13-23:
■Contains two new clauses for assigning passwords to the SYS and SYSTEM users
■Lets you create a locally managed SYSTEM tablespace
■CREATE SYNONYM on page 15-2 now allows creation of synonyms for object types.
■CREATE TABLE on page 15-7:
■Allows creation of a default list partition to capture rows that do not fall within any of the other list partitions
■Allows creation of range-list composite-partitioned tables
■Contains syntax for creating a table of type XMLType and for creating range-list composite-partitioned tables
■Allows data compression of data in table and partition segments
■CREATE TYPE on page 16-3 allows creation of object types with of NCHAR, NVARCHAR2, and NCLOB attributes.
■CREATE VIEW on page 16-39 now contains syntax for creating an XML view by transforming a table of type XMLType.
■SELECT on page 18-4 provides syntax for "flashback queries", which let you query data at a specified system change number or time in the past.
Oracle9i Release 1 (9.0.1) New Features in the SQL Reference
The following built-in datatypes were new or modified in this release:
■Oracle provides SQL-based interfaces for defining new types when the built-in or ANSI-supported types are not sufficient. See "Oracle-Supplied Types" on page 2-40.
xxix
■"CHAR Datatype" on page 2-10 can take the CHAR or BYTE parameter to indicate character or byte semantics, respectively.
■"INTERVAL YEAR TO MONTH Datatype" on page 2-24 and "INTERVAL DAY TO SECOND Datatype" on page 2-24 are provided for additional datetime functionality
■"TIMESTAMP Datatype" on page 2-21 is provided for additional datetime functionality.
■"VARCHAR2 Datatype" on page 2-11 can take the CHAR or BYTE parameter to indicate character or byte semantics, respectively.
The following expression forms were new or enhanced in this release:
■"CASE Expressions" on page 4-6 (enhanced with searched case expression)
■"CURSOR Expressions" on page 4-7 (enhanced; they can be passed as REF CURSOR arguments to functions)
■"Datetime Expressions" on page 4-9 (new)
■"INTERVAL Expressions" on page 4-11 (new)
■"Scalar Subquery Expressions" on page 4-13 (new)
The following condition was new in this release:
■IS OF type Conditions on page 5-19
The following built-in functions were new to this release:
■ASCIISTR on page 6-19
■BIN_TO_NUM on page 6-25
■COALESCE on page 6-33
■COMPOSE on page 6-34
■CURRENT_DATE on page 6-49
■CURRENT_TIMESTAMP on page 6-50
■DBTIMEZONE on page 6-51
■DECOMPOSE on page 6-53
■EXISTSNODE on page 6-61
■EXTRACT (datetime) on page 6-63
■EXTRACT (XML) on page 6-65
xxx
■FIRST on page 6-67
■FROM_TZ on page 6-71
■GROUP_ID on page 6-72
■GROUPING_ID on page 6-75
■LAST on page 6-81
■LOCALTIMESTAMP on page 6-90
■NULLIF on page 6-110
■PERCENTILE_CONT on page 6-118
■PERCENTILE_DISC on page 6-121
■RAWTONHEX on page 6-126
■ROWIDTONCHAR on page 6-141
■SESSIONTIMEZONE on page 6-143
■SYS_CONNECT_BY_PATH on page 6-155
■SYS_DBURIGEN on page 6-161
■SYS_EXTRACT_UTC on page 6-162
■SYS_XMLAGG on page 6-165
■SYS_XMLGEN on page 6-166
■SYSTIMESTAMP on page 6-168
■TO_CHAR (character) on page 6-170
■TO_CLOB on page 6-175
■TO_DSINTERVAL on page 6-177
■TO_NCHAR (character) on page 6-180
■TO_NCHAR (datetime) on page 6-181
■TO_NCHAR (number) on page 6-182
■TO_NCLOB on page 6-182
■TO_TIMESTAMP on page 6-185
■TO_TIMESTAMP_TZ on page 6-186
■TO_YMINTERVAL on page 6-187
xxxi
■TREAT on page 6-191
■TZ_OFFSET on page 6-195
■UNISTR on page 6-196
■WIDTH_BUCKET on page 6-208
The following functions were enhanced for this release:
■INSTR on page 6-78
■LENGTH on page 6-88
■SUBSTR on page 6-152
The following privileges were new to this release:
■ EXEMPT ACCESS POLICY system privilege on page 17-44
■RESUMABLE system privilege on page 17-44
■ |
SELECT ANY DICTIONARY system privilege on page 17-44 |
||
■ |
UNDER |
ANY |
TYPE system privilege on page 17-42 |
■ |
UNDER |
ANY |
VIEW system privilege on page 17-43 |
■UNDER object privilege on page 17-46
The following top-level SQL statements were new to this release:
■CREATE PFILE on page 14-62
■CREATE SPFILE on page 14-94
■MERGE on page 17-77
The following SQL statements had new syntax:
■ALTER DATABASE on page 9-11 has new syntax that lets you end a "hot backup" procedure while the database is mounted. It also has new syntax related to standby databases.
■ALTER INDEX on page 9-62 lets you gather statistics on index usage.
■ALTER OUTLINE on page 9-118 allows modification of both public and private outlines.
■ALTER ROLE on page 9-134 lets you identify a role using an application-specified package.
xxxii
■ALTER SESSION on page 10-2 lets you specify whether statements issued during the session can be suspended under some conditions.
■ALTER SYSTEM on page 10-20 has extended SET clause and new RESET clause; lets you put the database in quiesed state.
■ALTER TABLE on page 11-2 allows partitioning by a list of specified values.
■ALTER TYPE on page 12-6 lets you modify the attribute or method definition of an object type.
■ALTER VIEW on page 12-31 lets you add constraints to views.
■ANALYZE on page 12-34 now has ONLINE and OFFLINE clauses as part of the VALIDATE STRUCTURE syntax. In addition, you can now choose whether to collect standard statistics, user-defined statistics, or both.
■constraints on page 7-5 has been enhanced to facilitate index handling when dropping or disabling constraints.
■CREATE CONTEXT on page 13-12 has added syntax to let you initialize the context from the LDAP directory or from an OCI interface and to make the context accessible throughout an instance.
■CREATE CONTROLFILE on page 13-15 allows creation of Oracle-managed files.
■CREATE DATABASE on page 13-23 lets you create default temporary tablespaces when you create the database; lets you create undo tablespaces.
■CREATE FUNCTION on page 13-52 lets you create pipelined and parallel table functions and user-defined aggregate functions.
■CREATE OUTLINE on page 14-48 allows creation of both public and private outlines.
■CREATE ROLE on page 14-79 lets you identify a role using an application-specified package.
■CREATE TABLE on page 15-7 allows creation of external tables (tables whose data is outside the database); allows creation of Oracle-managed files; allows partitioning by a list of specified values.
■CREATE TABLESPACE on page 15-80 allows for segment space management by bitmaps as well as by free lists; allows creation of Oracle-managed files; lets you create undo tablespaces.
■CREATE TEMPORARY TABLESPACE on page 15-92 allows creation of Oracle-managed files.
xxxiii
■CREATE TYPE on page 16-3 lets you create subtypes.
■CREATE VIEW on page 16-39 lets you create subviews of object views; lets you define constraints on views.
■DROP TABLESPACE on page 17-10 has added syntax that lets you drop operating system files when you drop the contents from a dropped tablespace.
■file_specification on page 7-39 allows creation of Oracle-managed files.
■INSERT on page 17-53 has added syntax that lets you insert default column values.
■SELECT on page 18-4 lets you specify multiple groupings in the GROUP BY clause, for selective analysis across multiple dimensions; lets you assign names to subquery blocks; has new ANSI-compliant join syntax.
■SET TRANSACTION on page 18-50 lets you specify a name for a transaction.
■UPDATE on page 18-59 has added syntax that lets you update to default column values.
Oracle8i New Features in SQL Reference
The following SQL functions were new to this version:
■BITAND on page 6-25
■CORR on page 6-37
■COVAR_POP on page 6-42
■COVAR_SAMP on page 6-44
■CUME_DIST on page 6-47
■DENSE_RANK on page 6-55
■FIRST_VALUE on page 6-69
■LAG on page 6-80
■LAST_VALUE on page 6-84
■LEAD on page 6-86
■NTILE on page 6-109
■NUMTOYMINTERVAL on page 6-112
■NUMTODSINTERVAL on page 6-111
xxxiv
■
■
■
■
NVL2 on page 6-114
PERCENT_RANK on page 6-116
RANK on page 6-123
RATIO_TO_REPORT on page 6-125
■ REGR_ (Linear Regression) Functions on page 6-129
■STDDEV_POP on page 6-149
■STDDEV_SAMP on page 6-151
■VAR_POP on page 6-202
■VAR_SAMP on page 6-204
The following top-level SQL statements were new to Release 8.1.5:
■ALTER DIMENSION on page 9-56
■ALTER JAVA on page 9-87
■ALTER OUTLINE on page 9-118
■ASSOCIATE STATISTICS on page 12-50
■CALL on page 12-68
■CREATE CONTEXT on page 13-12
■CREATE DIMENSION on page 13-43
■CREATE INDEXTYPE on page 13-95
■CREATE JAVA on page 13-98
■CREATE OPERATOR on page 14-44
■CREATE OUTLINE on page 14-48
■ CREATE TEMPORARY TABLESPACE on page 15-92
■DISASSOCIATE STATISTICS on page 16-64
■DROP CONTEXT on page 16-69
■DROP DIMENSION on page 16-72
■DROP INDEXTYPE on page 16-79
■DROP JAVA on page 16-81
■DROP OPERATOR on page 16-88
xxxv
■DROP OUTLINE on page 16-90
In addition, the following features were enhanced:
■The aggregate functions have expanded functionality. See "Aggregate Functions" on page 6-8.
■When specifying LOB storage parameters, you can now specify caching of LOBs for read-only purposes. See CREATE TABLE on page 15-7.
■The section on Expressions now contains a new expression. See "CASE Expressions" on page 4-6.
■Subqueries can now be unnested. See "Unnesting of Nested Subqueries" on page 8-15.
xxxvi