Tải bản đầy đủ (.pdf) (1,508 trang)

Oracle® Database SQL Language Reference pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.22 MB, 1,508 trang )


Oracle® Database
SQL Language Reference
11g Release 2 (11.2)
E10592-02
August 2009
Oracle Database SQL Language Reference, 11g Release 2 (11.2)
E10592-02
Copyright © 1996, 2009, Oracle and/or its affiliates. All rights reserved.
Primary Authors: Diana Lorentz, Mary Beth Roeser
Contributors: Sundeep Abraham, Angela Amor, Geeta Arora, Vikas Arora, Lance Ashdown, Hermann
Baer, Shrikanth Bellamkonda, Donna Carver, Dan Chiba, Timothy Chien, Alan Choi, Thierry Cruanes,
George Eadon, Amit Ganesh, Barb Glover, Naveen Gopal, Mike Hallas, Min-Hank Ho, Chandrasekharan
Iyer, Mark Jaeger, Vikram Kapoor, Peter Knaggs, Srinath Krishnaswamy, Andre Kruglikov, Paul Lane,
Huagang Li, Yunrui Li, Vince Liang, Bryn Llewellyn, Rich Long, Vineet Marwah, Jun Matsuzawa, Robert
McGuirk, Rahil Mir, Gopal Mulagund, Sujatha Muthulingam, Hanlin Qian, Ashish Ray, John Russell,
Laurent Schneider, Vivian Schupmann, Jia Shi, Ajeet Singh, Wayne Smith, Shanshan Song, Vinay Srihari, Jim
Stenoish, Sankar Subramanian, Seema Sundara, Mark van de Wiel, Badhri Varanasi, William Waddington,
Peter Wahl, Charles Wetherell, Sergiusz Wolicki, Daniel Wong, Tsae-feng Yu, Mohamed Zait, Fred Zemke,
Wei Zhang, Weiran Zhang
This software and related documentation are provided under a license agreement containing restrictions on
use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your
license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license,
transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse
engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is
prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If
you find any errors, please report them to us in writing.
If this software or related documentation is delivered to the U.S. Government or anyone licensing it on
behalf of the U.S. Government, the following notice is applicable:
U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data


delivered to U.S. Government customers are "commercial computer software" or "commercial technical data"
pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As
such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and
license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of
the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software
License (December 2007). Oracle USA, Inc., 500 Oracle Parkway, Redwood City, CA 94065.
This software is developed for general use in a variety of information management applications. It is not
developed or intended for use in any inherently dangerous applications, including applications which may
create a risk of personal injury. If you use this software in dangerous applications, then you shall be
responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure the safe use
of this software. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of
this software in dangerous applications.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks
of their respective owners.
This software and documentation may provide access to or information on content, products, and services
from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all
warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and
its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of
third-party content, products, or services.
iii
Contents
Preface xxi
Audience xxi
Documentation Accessibility xxi
Related Documents xxii
Conventions xxii
What's New in the SQL Language Reference? xxiii
Oracle Database 11g Release 2 New Features in the SQL Language Reference xxiii
Oracle Database 11g Release 1 New Features in the SQL Language Reference xxv
1 Introduction to Oracle SQL

History of SQL 1-1
SQL Standards 1-1
How SQL Works 1-2
Common Language for All Relational Databases 1-2
Using Enterprise Manager 1-3
Lexical Conventions 1-3
Tools Support 1-3
2 Pseudocolumns
Hierarchical Query Pseudocolumns 2-1
CONNECT_BY_ISCYCLE Pseudocolumn 2-1
CONNECT_BY_ISLEAF Pseudocolumn 2-2
LEVEL Pseudocolumn 2-2
Sequence Pseudocolumns 2-3
Where to Use Sequence Values 2-3
How to Use Sequence Values 2-4
Version Query Pseudocolumns 2-6
COLUMN_VALUE Pseudocolumn 2-6
OBJECT_ID Pseudocolumn 2-7
OBJECT_VALUE Pseudocolumn 2-8
iv
ORA_ROWSCN Pseudocolumn 2-8
ROWID Pseudocolumn 2-9
ROWNUM Pseudocolumn 2-9
XMLDATA Pseudocolumn 2-10
3 Basic Elements of Oracle SQL
Data Types 3-1
Oracle Built-in Data Types 3-6
CHAR Data Type 3-9
NCHAR Data Type 3-9
NVARCHAR2 Data Type 3-9

VARCHAR2 Data Type 3-10
VARCHAR Data Type 3-10
NUMBER Data Type 3-10
FLOAT Data Type 3-12
Floating-Point Numbers 3-12
BINARY_FLOAT 3-13
BINARY_DOUBLE 3-13
Numeric Precedence 3-14
DATE Data Type 3-17
Using Julian Days 3-17
TIMESTAMP Data Type 3-18
TIMESTAMP WITH TIME ZONE Data Type 3-18
TIMESTAMP WITH LOCAL TIME ZONE Data Type 3-19
INTERVAL YEAR TO MONTH Data Type 3-19
INTERVAL DAY TO SECOND Data Type 3-20
Datetime/Interval Arithmetic 3-20
Support for Daylight Saving Times 3-22
Datetime and Interval Examples 3-23
RAW and LONG RAW Data Types 3-24
BFILE Data Type 3-26
BLOB Data Type 3-26
CLOB Data Type 3-26
NCLOB Data Type 3-27
Rowid Data Types 3-27
ROWID Data Type 3-27
UROWID Data Type 3-28
ANSI, DB2, and SQL/DS Data Types 3-28
User-Defined Types 3-30
Object Types 3-30
REF Data Types 3-30

Varrays 3-31
Nested Tables 3-31
Oracle-Supplied Types
3-31
Any Types 3-32
ANYTYPE 3-32
ANYDATA 3-32
v
ANYDATASET 3-32
XML Types 3-32
XMLType 3-32
URI Data Types 3-33
URIFactory Package 3-34
Spatial Types 3-34
SDO_GEOMETRY 3-34
SDO_TOPO_GEOMETRY 3-34
SDO_GEORASTER 3-35
Media Types 3-35
ORDAudio 3-35
ORDImage 3-35
ORDVideo 3-35
ORDDoc 3-35
ORDDicom 3-36
SI_StillImage 3-36
SI_Color 3-36
SI_AverageColor 3-36
SI_ColorHistogram 3-36
SI_PositionalColor 3-36
SI_Texture 3-36
SI_FeatureList 3-36

ORDImageSignature 3-36
Expression Filter Type 3-36
Expression 3-37
Data Type Comparison Rules 3-37
Numeric Values 3-37
Date Values 3-37
Character Values 3-37
Object Values 3-40
Varrays and Nested Tables 3-40
Data Type Precedence 3-40
Data Conversion 3-40
Implicit and Explicit Data Conversion 3-40
Implicit Data Conversion 3-40
Implicit Data Conversion Examples 3-43
Explicit Data Conversion 3-43
Security Considerations for Data Conversion 3-44
Literals 3-45
Text Literals 3-46
Numeric Literals 3-47
Integer Literals 3-47
NUMBER and Floating-Point Literals 3-48
Datetime Literals 3-50
Interval Literals 3-52
INTERVAL YEAR TO MONTH 3-53
INTERVAL DAY TO SECOND 3-54
vi
Format Models 3-55
Number Format Models 3-56
Number Format Elements 3-57
Datetime Format Models 3-59

Datetime Format Elements 3-60
Uppercase Letters in Date Format Elements 3-60
Punctuation and Character Literals in Datetime Format Models 3-60
Datetime Format Elements and Globalization Support 3-64
ISO Standard Date Format Elements 3-64
The RR Datetime Format Element 3-64
RR Datetime Format Examples 3-65
Datetime Format Element Suffixes 3-65
Format Model Modifiers 3-66
Format Model Examples 3-67
String-to-Date Conversion Rules 3-68
XML Format Model 3-69
Nulls 3-70
Nulls in SQL Functions 3-70
Nulls with Comparison Conditions 3-71
Nulls in Conditions 3-71
Comments 3-71
Comments Within SQL Statements 3-72
Comments on Schema and Nonschema Objects 3-72
Hints 3-73
Alphabetical Listing of Hints 3-77
ALL_ROWS Hint 3-77
APPEND Hint 3-78
APPEND_VALUES Hint 3-78
CACHE Hint 3-79
CHANGE_DUPKEY_ERROR_INDEX Hint 3-79
CLUSTER Hint 3-80
CURSOR_SHARING_EXACT Hint 3-80
DRIVING_SITE Hint 3-80
DYNAMIC_SAMPLING Hint 3-81

FACT Hint 3-81
FIRST_ROWS Hint 3-81
FULL Hint
3-82
HASH Hint 3-82
IGNORE_ROW_ON_DUPKEY_INDEX Hint 3-83
INDEX Hint 3-83
INDEX_ASC Hint 3-84
INDEX_COMBINE Hint 3-84
INDEX_DESC Hint 3-85
INDEX_FFS Hint 3-85
INDEX_JOIN Hint 3-85
INDEX_SS Hint 3-86
INDEX_SS_ASC Hint 3-86
vii
INDEX_SS_DESC Hint 3-87
LEADING Hint 3-87
MERGE Hint 3-87
MODEL_MIN_ANALYSIS Hint 3-88
MONITOR Hint 3-88
NATIVE_FULL_OUTER_JOIN Hint 3-88
NOAPPEND Hint 3-89
NOCACHE Hint 3-89
NO_EXPAND Hint 3-89
NO_FACT Hint 3-89
NO_INDEX Hint 3-90
NO_INDEX_FFS Hint 3-90
NO_INDEX_SS Hint 3-90
NO_MERGE Hint 3-91
NO_MONITOR Hint 3-91

NO_NATIVE_FULL_OUTER_JOIN Hint 3-91
NO_PARALLEL Hint 3-91
NOPARALLEL Hint 3-92
NO_PARALLEL_INDEX Hint 3-92
NOPARALLEL_INDEX Hint 3-92
NO_PUSH_PRED Hint 3-92
NO_PUSH_SUBQ Hint 3-93
NO_PX_JOIN_FILTER Hint 3-93
NO_QUERY_TRANSFORMATION Hint 3-93
NO_RESULT_CACHE Hint 3-93
NO_REWRITE Hint 3-93
NOREWRITE Hint 3-94
NO_STAR_TRANSFORMATION Hint 3-94
NO_UNNEST Hint 3-94
NO_USE_HASH Hint 3-94
NO_USE_MERGE Hint 3-94
NO_USE_NL Hint 3-95
NO_XML_QUERY_REWRITE Hint 3-95
NO_XMLINDEX_REWRITE Hint 3-95
OPT_PARAM Hint 3-95
ORDERED Hint 3-96
PARALLEL Hint 3-96
PARALLEL_INDEX Hint 3-98
PQ_DISTRIBUTE Hint
3-98
PUSH_PRED Hint 3-101
PUSH_SUBQ Hint 3-101
PX_JOIN_FILTER Hint 3-101
QB_NAME Hint 3-101
RESULT_CACHE Hint 3-102

RETRY_ON_ROW_CHANGE Hint 3-102
REWRITE Hint 3-103
STAR_TRANSFORMATION Hint 3-103
viii
UNNEST Hint 3-104
USE_CONCAT Hint 3-104
USE_HASH Hint 3-104
USE_MERGE Hint 3-105
USE_NL Hint 3-105
USE_NL_WITH_INDEX Hint 3-106
Database Objects 3-106
Schema Objects 3-106
Nonschema Objects 3-107
Database Object Names and Qualifiers 3-107
Database Object Naming Rules 3-107
Schema Object Naming Examples 3-111
Schema Object Naming Guidelines 3-111
Syntax for Schema Objects and Parts in SQL Statements 3-111
How Oracle Database Resolves Schema Object References 3-112
References to Objects in Other Schemas 3-113
References to Objects in Remote Databases 3-113
Creating Database Links 3-114
Database Link Names 3-114
Username and Password 3-114
Database Connect String 3-114
References to Database Links 3-115
References to Partitioned Tables and Indexes 3-115
References to Object Type Attributes and Methods 3-117
4 Operators
About SQL Operators 4-1

Unary and Binary Operators 4-2
Operator Precedence 4-2
Arithmetic Operators 4-3
Concatenation Operator 4-4
Hierarchical Query Operators 4-5
PRIOR 4-5
CONNECT_BY_ROOT 4-5
Set Operators 4-5
Multiset Operators 4-6
MULTISET EXCEPT 4-6
MULTISET INTERSECT 4-7
MULTISET UNION 4-8
User-Defined Operators 4-9
5 Functions
About SQL Functions 5-2
Single-Row Functions 5-3
Numeric Functions 5-3
Character Functions Returning Character Values 5-4
NLS Character Functions 5-5
ix
Character Functions Returning Number Values 5-5
Datetime Functions 5-5
General Comparison Functions 5-6
Conversion Functions 5-6
Large Object Functions 5-7
Collection Functions 5-7
Hierarchical Function 5-7
Data Mining Functions 5-7
XML Functions 5-8
Encoding and Decoding Functions 5-9

NULL-Related Functions 5-9
Environment and Identifier Functions 5-9
Aggregate Functions 5-9
Analytic Functions 5-11
Object Reference Functions 5-16
Model Functions 5-16
Alphabetical Listing of SQL Functions 5-17
ABS 5-18
ACOS 5-18
ADD_MONTHS 5-19
APPENDCHILDXML 5-19
ASCII 5-20
ASCIISTR 5-21
ASIN 5-21
ATAN 5-22
ATAN2 5-22
AV G 5-23
BFILENAME 5-24
BIN_TO_NUM 5-25
BITAND 5-26
CARDINALITY 5-27
CAST 5-28
CEIL 5-31
CHARTOROWID 5-31
CHR 5-32
CLUSTER_ID 5-33
CLUSTER_PROBABILITY 5-34
CLUSTER_SET 5-36
COALESCE 5-38
COLLECT 5-39

COMPOSE 5-40
CONCAT 5-40
CONVERT 5-41
CORR 5-43
CORR_* 5-44
CORR_S 5-45
CORR_K 5-46
x
COS 5-46
COSH 5-47
COUNT 5-47
COVAR_POP 5-49
COVAR_SAMP 5-50
CUBE_TABLE 5-51
CUME_DIST 5-53
CURRENT_DATE 5-54
CURRENT_TIMESTAMP 5-55
CV 5-56
DATAOBJ_TO_PARTITION 5-57
DBTIMEZONE 5-57
DECODE 5-58
DECOMPOSE 5-59
DELETEXML 5-60
DENSE_RANK 5-61
DEPTH 5-63
DEREF 5-63
DUMP 5-64
EMPTY_BLOB, EMPTY_CLOB 5-65
EXISTSNODE 5-66
EXP 5-67

EXTRACT (datetime) 5-68
EXTRACT (XML) 5-70
EXTRACTVALUE 5-71
FEATURE_ID 5-72
FEATURE_SET
5-73
FEATURE_VALUE 5-75
FIRST 5-77
FIRST_VALUE 5-79
FLOOR 5-80
FROM_TZ 5-81
GREATEST 5-81
GROUP_ID 5-82
GROUPING 5-83
GROUPING_ID 5-84
HEXTORAW 5-85
INITCAP 5-85
INSERTCHILDXML 5-86
INSERTCHILDXMLAFTER 5-87
INSERTCHILDXMLBEFORE 5-88
INSERTXMLAFTER 5-89
INSERTXMLBEFORE 5-90
INSTR 5-91
ITERATION_NUMBER 5-92
LAG 5-94
LAST 5-95
xi
LAST_DAY 5-95
LAST_VALUE 5-96
LEAD 5-98

LEAST 5-99
LENGTH 5-100
LISTAGG 5-101
LN 5-102
LNNVL 5-103
LOCALTIMESTAMP 5-104
LOG 5-105
LOWER 5-105
LPAD 5-106
LTRIM 5-106
MAKE_REF 5-107
MAX 5-108
MEDIAN 5-109
MIN 5-111
MOD 5-112
MONTHS_BETWEEN 5-113
NANVL 5-114
NCHR 5-114
NEW_TIME 5-115
NEXT_DAY 5-116
NLS_CHARSET_DECL_LEN 5-116
NLS_CHARSET_ID 5-117
NLS_CHARSET_NAME 5-117
NLS_INITCAP
5-118
NLS_LOWER 5-119
NLS_UPPER 5-119
NLSSORT 5-120
NTH_VALUE 5-122
NTILE 5-123

NULLIF 5-124
NUMTODSINTERVAL 5-125
NUMTOYMINTERVAL 5-126
NVL 5-126
NVL2 5-127
ORA_DST_AFFECTED 5-128
ORA_DST_CONVERT 5-129
ORA_DST_ERROR 5-130
ORA_HASH 5-130
PATH 5-131
PERCENT_RANK 5-132
PERCENTILE_CONT 5-133
PERCENTILE_DISC 5-135
POWER 5-137
POWERMULTISET 5-137
xii
POWERMULTISET_BY_CARDINALITY 5-138
PREDICTION 5-139
PREDICTION_BOUNDS 5-141
PREDICTION_COST 5-142
PREDICTION_DETAILS 5-144
PREDICTION_PROBABILITY 5-146
PREDICTION_SET 5-147
PRESENTNNV 5-150
PRESENTV 5-151
PREVIOUS 5-152
RANK 5-153
RATIO_TO_REPORT 5-155
RAWTOHEX 5-155
RAWTONHEX 5-156

REF 5-157
REFTOHEX 5-157
REGEXP_COUNT 5-158
REGEXP_INSTR 5-160
REGEXP_REPLACE 5-162
REGEXP_SUBSTR 5-164
REGR_ (Linear Regression) Functions 5-166
REMAINDER 5-171
REPLACE 5-172
ROUND (date) 5-172
ROUND (number) 5-173
ROW_NUMBER 5-174
ROWIDTOCHAR 5-176
ROWIDTONCHAR
5-176
RPAD 5-177
RTRIM 5-178
SCN_TO_TIMESTAMP 5-178
SESSIONTIMEZONE 5-180
SET 5-180
SIGN 5-181
SIN 5-181
SINH 5-182
SOUNDEX 5-182
SQRT 5-183
STATS_BINOMIAL_TEST 5-184
STATS_CROSSTAB 5-185
STATS_F_TEST 5-186
STATS_KS_TEST 5-187
STATS_MODE 5-188

STATS_MW_TEST 5-189
STATS_ONE_WAY_ANOVA 5-190
STATS_T_TEST_* 5-191
STATS_T_TEST_ONE 5-192
xiii
STATS_T_TEST_PAIRED 5-193
STATS_T_TEST_INDEP and STATS_T_TEST_INDEPU 5-193
STATS_WSR_TEST 5-194
STDDEV 5-195
STDDEV_POP 5-196
STDDEV_SAMP 5-197
SUBSTR 5-199
SUM 5-200
SYS_CONNECT_BY_PATH 5-201
SYS_CONTEXT 5-202
SYS_DBURIGEN 5-208
SYS_EXTRACT_UTC 5-209
SYS_GUID 5-209
SYS_TYPEID 5-210
SYS_XMLAGG 5-211
SYS_XMLGEN 5-211
SYSDATE 5-212
SYSTIMESTAMP 5-213
TAN 5-213
TANH 5-214
TIMESTAMP_TO_SCN 5-214
TO_BINARY_DOUBLE 5-215
TO_BINARY_FLOAT 5-216
TO_BLOB 5-217
TO_CHAR (character) 5-218

TO_CHAR (datetime) 5-219
TO_CHAR (number) 5-221
TO_CLOB 5-222
TO_DATE 5-223
TO_DSINTERVAL 5-224
TO_LOB 5-225
TO_MULTI_BYTE 5-226
TO_NCHAR (character) 5-227
TO_NCHAR (datetime) 5-227
TO_NCHAR (number) 5-228
TO_NCLOB 5-229
TO_NUMBER 5-229
TO_SINGLE_BYTE 5-230
TO_TIMESTAMP 5-230
TO_TIMESTAMP_TZ 5-231
TO_YMINTERVAL 5-232
TRANSLATE 5-233
TRANSLATE USING 5-234
TREAT 5-235
TRIM 5-236
TRUNC (date) 5-237
TRUNC (number) 5-238
xiv
TZ_OFFSET 5-238
UID 5-239
UNISTR 5-239
UPDATEXML 5-240
UPPER 5-241
USER 5-242
USERENV 5-242

VA L U E 5-243
VA R _ P O P 5-244
VA R _ S A M P 5-245
VARIANCE 5-246
VSIZE 5-247
WIDTH_BUCKET 5-248
XMLAGG 5-249
XMLCAST 5-250
XMLCDATA 5-251
XMLCOLATTVAL 5-252
XMLCOMMENT 5-252
XMLCONCAT 5-253
XMLDIFF 5-254
XMLELEMENT 5-255
XMLEXISTS 5-257
XMLFOREST 5-258
XMLISVALID 5-259
XMLPARSE 5-259
XMLPATCH
5-260
XMLPI 5-261
XMLQUERY 5-262
XMLROOT 5-263
XMLSEQUENCE 5-264
XMLSERIALIZE 5-266
XMLTABLE 5-267
XMLTRANSFORM 5-269
ROUND and TRUNC Date Functions 5-270
About User-Defined Functions 5-271
Prerequisites 5-272

Name Precedence 5-272
Naming Conventions 5-272
6 Expressions
About SQL Expressions 6-1
Simple Expressions 6-3
Compound Expressions 6-4
CASE Expressions 6-5
Column Expressions 6-6
CURSOR Expressions 6-7
Datetime Expressions 6-8
xv
Function Expressions 6-10
Interval Expressions 6-10
Model Expressions 6-11
Object Access Expressions 6-13
Placeholder Expressions 6-14
Scalar Subquery Expressions 6-14
Type Constructor Expressions 6-14
Expression Lists 6-16
7 Conditions
About SQL Conditions 7-1
Condition Precedence 7-3
Comparison Conditions 7-4
Simple Comparison Conditions 7-5
Group Comparison Conditions 7-6
Floating-Point Conditions 7-7
Logical Conditions 7-7
Model Conditions 7-9
IS ANY Condition 7-9
IS PRESENT Condition 7-10

Multiset Conditions 7-11
IS A SET Condition 7-11
IS EMPTY Condition 7-12
MEMBER Condition 7-12
SUBMULTISET Condition 7-13
Pattern-matching Conditions 7-14
LIKE Condition 7-14
REGEXP_LIKE Condition 7-17
Null Conditions 7-19
XML Conditions 7-19
EQUALS_PATH Condition 7-19
UNDER_PATH Condition 7-20
Compound Conditions 7-20
BETWEEN Condition 7-21
EXISTS Condition 7-22
IN Condition 7-22
IS OF type Condition 7-24
8 Common SQL DDL Clauses
allocate_extent_clause 8-2
constraint 8-4
deallocate_unused_clause 8-27
file_specification 8-29
logging_clause 8-38
parallel_clause 8-41
physical_attributes_clause 8-44
xvi
size_clause 8-47
storage_clause 8-48
9 SQL Queries and Subqueries
About Queries and Subqueries 9-1

Creating Simple Queries 9-2
Hierarchical Queries 9-3
Hierarchical Query Examples 9-5
The UNION [ALL], INTERSECT, MINUS Operators 9-8
Sorting Query Results 9-10
Joins 9-11
Join Conditions 9-11
Equijoins 9-11
Self Joins 9-11
Cartesian Products 9-12
Inner Joins 9-12
Outer Joins 9-12
Antijoins 9-13
Semijoins 9-14
Using Subqueries 9-14
Unnesting of Nested Subqueries 9-15
Selecting from the DUAL Table 9-15
Distributed Queries 9-16
10 SQL Statements: ALTER CLUSTER to ALTER JAVA
Types of SQL Statements 10-1
Data Definition Language (DDL) Statements 10-2
Data Manipulation Language (DML) Statements 10-2
Transaction Control Statements 10-3
Session Control Statements 10-3
System Control Statement 10-3
Embedded SQL Statements 10-3
How the SQL Statement Chapters are Organized 10-4
ALTER CLUSTER 10-5
ALTER DATABASE 10-9
ALTER DATABASE LINK 10-46

ALTER DIMENSION 10-48
ALTER DISKGROUP 10-51
ALTER FLASHBACK ARCHIVE 10-73
ALTER FUNCTION 10-76
ALTER INDEX 10-77
ALTER INDEXTYPE 10-96
ALTER JAVA 10-99
11 SQL Statements: ALTER LIBRARY to ALTER SYSTEM
ALTER LIBRARY 11-2
xvii
ALTER MATERIALIZED VIEW 11-3
ALTER MATERIALIZED VIEW LOG 11-18
ALTER OPERATOR 11-25
ALTER OUTLINE 11-28
ALTER PACKAGE 11-30
ALTER PROCEDURE 11-31
ALTER PROFILE 11-32
ALTER RESOURCE COST 11-35
ALTER ROLE 11-38
ALTER ROLLBACK SEGMENT 11-40
ALTER SEQUENCE 11-43
ALTER SESSION 11-45
Initialization Parameters and ALTER SESSION 11-50
Session Parameters and ALTER SESSION 11-51
ALTER SYSTEM 11-58
12 SQL Statements: ALTER TABLE to ALTER TABLESPACE
ALTER TABLE 12-2
ALTER TABLESPACE 12-89
13 SQL Statements: ALTER TRIGGER to COMMIT
ALTER TRIGGER 13-2

ALTER TYPE 13-4
ALTER USER 13-5
ALTER VIEW 13-12
ANALYZE 13-15
ASSOCIATE STATISTICS 13-23
AUDIT 13-27
CALL 13-40
COMMENT 13-44
COMMIT 13-47
14 SQL Statements: CREATE CLUSTER to CREATE JAVA
CREATE CLUSTER 14-2
CREATE CONTEXT 14-9
CREATE CONTROLFILE 14-12
CREATE DATABASE 14-19
CREATE DATABASE LINK 14-32
CREATE DIMENSION 14-37
CREATE DIRECTORY 14-42
CREATE DISKGROUP 14-44
CREATE EDITION 14-52
CREATE FLASHBACK ARCHIVE 14-56
CREATE FUNCTION 14-59
CREATE INDEX 14-61
CREATE INDEXTYPE 14-87
xviii
CREATE JAVA 14-91
15 SQL Statements: CREATE LIBRARY to CREATE SPFILE
CREATE LIBRARY 15-2
CREATE MATERIALIZED VIEW 15-3
CREATE MATERIALIZED VIEW LOG 15-26
CREATE OPERATOR 15-34

CREATE OUTLINE 15-37
CREATE PACKAGE 15-41
CREATE PACKAGE BODY 15-43
CREATE PFILE 15-45
CREATE PROCEDURE 15-47
CREATE PROFILE 15-49
CREATE RESTORE POINT 15-55
CREATE ROLE 15-58
CREATE ROLLBACK SEGMENT 15-61
CREATE SCHEMA 15-64
CREATE SEQUENCE 15-66
CREATE SPFILE 15-70
16 SQL Statements: CREATE SYNONYM to CREATE TRIGGER
CREATE SYNONYM 16-2
CREATE TABLE 16-6
CREATE TABLESPACE 16-80
CREATE TRIGGER 16-94
17 SQL Statements: CREATE TYPE to DROP ROLLBACK SEGMENT
CREATE TYPE 17-3
CREATE TYPE BODY 17-5
CREATE USER 17-7
CREATE VIEW 17-14
DELETE 17-26
DISASSOCIATE STATISTICS 17-34
DROP CLUSTER 17-36
DROP CONTEXT 17-38
DROP DATABASE 17-39
DROP DATABASE LINK 17-40
DROP DIMENSION 17-41
DROP DIRECTORY 17-42

DROP DISKGROUP 17-43
DROP EDITION 17-45
DROP FLASHBACK ARCHIVE 17-46
DROP FUNCTION 17-47
DROP INDEX 17-49
DROP INDEXTYPE 17-51
D R OP J AVA 17-52
xix
DROP LIBRARY 17-53
DROP MATERIALIZED VIEW 17-54
DROP MATERIALIZED VIEW LOG 17-56
DROP OPERATOR 17-58
DROP OUTLINE 17-59
DROP PACKAGE 17-61
DROP PROCEDURE 17-63
DROP PROFILE 17-64
DROP RESTORE POINT 17-65
DROP ROLE 17-66
DROP ROLLBACK SEGMENT 17-67
18 SQL Statements: DROP SEQUENCE to ROLLBACK
DROP SEQUENCE 18-2
DROP SYNONYM 18-3
DROP TABLE 18-5
DROP TABLESPACE 18-9
DROP TRIGGER 18-12
DROP TYPE 18-13
DROP TYPE BODY 18-15
DROP USER 18-16
DROP VIEW 18-18
EXPLAIN PLAN 18-20

FLASHBACK DATABASE 18-24
FLASHBACK TABLE 18-27
GRANT 18-33
INSERT 18-54
LOCK TABLE 18-72
MERGE 18-75
NOAUDIT 18-80
PURGE 18-84
RENAME 18-86
REVOKE 18-88
ROLLBACK 18-97
19 SQL Statements: SAVEPOINT to UPDATE
SAVEPOINT 19-2
SELECT 19-4
SET CONSTRAINT[S] 19-58
SET ROLE 19-60
SET TRANSACTION 19-62
TRUNCATE CLUSTER 19-65
TRUNCATE TABLE 19-67
UPDATE 19-71
xx
A How to Read Syntax Diagrams
Graphic Syntax Diagrams A-1
Required Keywords and Parameters A-2
Optional Keywords and Parameters A-3
Syntax Loops A-3
Multipart Diagrams A-4
Database Objects A-4
B Automatic and Manual Locking Mechanisms During SQL Operations
Automatic Locks in DML Operations B-1

Automatic Locks in DDL Operations B-4
Exclusive DDL Locks B-4
Share DDL Locks B-5
Breakable Parse Locks B-5
Manual Data Locking B-5
C Oracle and Standard SQL
ANSI Standards C-1
ISO Standards C-2
Oracle Compliance To Core SQL:2008 C-3
Oracle Support for Optional Features of SQL/Foundation:2008 C-9
Oracle Compliance with SQL/CLI:2008 C-21
Oracle Compliance with SQL/PSM:2008 C-21
Oracle Compliance with SQL/MED:2008 C-21
Oracle Compliance with SQL/OLB:2008 C-21
Oracle Compliance with SQL/JRT:2008 C-21
Oracle Compliance with SQL/XML:2008 C-22
Oracle Compliance with FIPS 127-2 C-27
Oracle Extensions to Standard SQL C-28
Oracle Compliance with Older Standards C-29
Character Set Support C-29
D Oracle Regular Expression Support
Multilingual Regular Expression Syntax D-1
Regular Expression Operator Multilingual Enhancements D-2
Perl-influenced Extensions in Oracle Regular Expressions D-3
E Oracle SQL Reserved Words
F Extended Examples
Using Extensible Indexing F-1
Using XML in SQL Statements F-8
Index
xxi

Preface
This reference contains a complete description of the Structured Query Language
(SQL) used to manage information in an Oracle Database. Oracle SQL is a superset of
the American National Standards Institute (ANSI) and the International Standards
Organization (ISO) SQL:1999 standard.
This Preface contains these topics:
■ Audience
■ Documentation Accessibility
■ Related Documents
■ Conventions
Audience
The Oracle Database SQL Language Reference is intended for all users of Oracle SQL.
Documentation Accessibility
Our goal is to make Oracle products, services, and supporting documentation
accessible to all users, including users that are disabled. To that end, our
documentation includes features that make information available to users of assistive
technology. This documentation is available in HTML format, and contains markup to
facilitate access by the disabled community. Accessibility standards will continue to
evolve over time, and Oracle is actively engaged with other market-leading
technology vendors to address technical obstacles so that our documentation can be
accessible to all of our customers. For more information, visit the Oracle Accessibility
Program Web site at />Accessibility of Code Examples in Documentation
Screen readers may not always correctly read the code examples in this document. The
conventions for writing code require that closing braces should appear on an
otherwise empty line; however, some screen readers may not always read a line of text
that consists solely of a bracket or brace.
Accessibility of Links to External Web Sites in Documentation
This documentation may contain links to Web sites of other companies or
organizations that Oracle does not own or control. Oracle neither evaluates nor makes
any representations regarding the accessibility of these Web sites.

xxii
Deaf/Hard of Hearing Access to Oracle Support Services
To reach Oracle Support Services, use a telecommunications relay service (TRS) to call
Oracle Support at 1.800.223.1711. An Oracle Support Services engineer will handle
technical issues and provide customer support according to the Oracle service request
process. Information about TRS is available at
and a list of phone
numbers is available at />Related Documents
For more information, see these Oracle resources:
■ Oracle Database PL/SQL Language Reference for information on PL/SQL, the
procedural language extension to Oracle SQL
■ Pro*C/C++ Programmer's Guide, Oracle SQL*Module for Ada Programmer's Guide, and
the Pro*COBOL Programmer's Guide for detailed descriptions of Oracle embedded
SQL
Many of the examples in this book use the sample schemas, which are installed by
default when you select the Basic Installation option with an Oracle Database
installation. Refer to Oracle Database Sample Schemas for information on how these
schemas were created and how you can use them yourself.
Conventions
The following text conventions are used in this document:
Convention Meaning
boldface Boldface type indicates graphical user interface elements associated
with an action, or terms defined in text or the glossary.
italic Italic type indicates book titles, emphasis, or placeholder variables for
which you supply particular values.
monospace Monospace type indicates commands within a paragraph, URLs, code
in examples, text that appears on the screen, or text that you enter.
xxiii
What's New in the SQL Language
Reference?

This section describes new features of Oracle Database 11g and provides pointers to
additional information.
For information on features that were new in earlier versions of Oracle Database, refer
to the documentation for the earlier release.
Oracle Database 11g Release 2 New Features in the SQL Language
Reference
Structural Changes in the SQL Language Reference
A number of sections of this book that were make up primarily of PL/SQL were
moved to Oracle Database PL/SQL Language Reference in Oracle Database 11g Release 1.
Please refer to "Structural Changes in the SQL Language Reference" on page xxv for
details on this migration of material.
New Features in the SQL Language Reference
The following top-level SQL statements are new or enhanced in this release:
■ A new top-level SQL statement ALTER DATABASE LINK on page 10-46 lets you
update the fixed user password in a database link when the password of a
connection or authentication user has changed.
■ The ALTER DISKGROUP statement has the following changes:
– A new disk_region_clause on page 10-65 lets you determine the Intelligent Data
Placement attribute of the disk group file.
– New diskgroup_volume_clauses on page 10-67 let you manipulate logical Oracle
Automatic Storage Management (Oracle ASM) volumes corresponding to
physical volume devices.
– Several new clauses let you control access to Oracle ASM files: usergroup_
clauses on page 10-68, user_clauses on page 10-69, file_permissions_clause on
page 10-69, and file_owner_clause on page 10-69.
■ AUDIT and NOAUDIT contain a new clause ALL STATEMENTS on page 13-30 that
lets you enable and disable auditing of all top-level SQL statements executed. In
AUDIT only, a new clause IN SESSION CURRENT on page 13-31 allows you to
limit auditing to the current session.
■ CREATE DISKGROUP and ALTER DISKGROUP have the following changes:

xxiv
– A new clause QUORUM | REGULAR on page 14-46 let you designate a disk
or failure group as a quorum disk or failure group, which can contain the
voting file for Cluster Synchronization Services (CSS).
■ Two new statements, CREATE EDITION on page 14-52 and DROP EDITION on
page 17-45, let you use editions. An edition makes it possible to have two or more
versions of the same editionable objects in the database.
■ CREATE INDEXTYPE on page 14-87 and ALTER INDEXTYPE on page 10-96 have
a new clause WITH SYSTEM MANAGED STORAGE TABLES. This clause makes it
possible to create domain indexes in both range- and list-partitioned tables.
■ ALTER SESSION contains a new clause SYNC WITH PRIMARY on page 11-48 that
lets you synchronize the physical standby database with the primary database. A
new session parameter STANDBY_MAX_DATA_DELAY on page 11-53 lets you
specify a session-specific apply lag tolerance for queries to a physical standby
database that is in real-time query mode.
■ CREATE MATERIALIZED VIEW LOG has a new clause COMMIT SCN on
page 15-30 that instructs the database to use commit SCN data instead of
timestamps to refresh the materialized view, which improves the speed of the
refresh.
■ CREATE MATERIALIZED VIEW LOG and ALTER MATERIALIZED VIEW LOG have a
new clause mv_log_purge_clause on page 15-32 that lets you specify the purge time
for the materialized view log.
■ CREATE TABLE and ALTER TABLE are enhanced in the following ways:
– A new clause deferred_segment_creation on page 16-32 lets you postpone
creation of the table segment until the first row of data is inserted into the
table. This clause is also applicable to materialized views.
– The clause table_compression on page 16-34 has new syntax and terminology.
Use COMPRESS FOR OLTP to specify OLTP table compression. (In earlier
releases, the syntax was COMPRESS FOR ALL OPERATIONS.) Use COMPRESS
BASIC to specify basic table compression. (In earlier releases, the syntax was

COMPRESS FOR DIRECT_LOAD OPERATIONS and this type of compression
was called DSS table compression.)
– A new RESULT_CACHE Clause on page 16-61 lets you specify whether the
results of queries that name the table are considered for result caching.
– The nested_table_col_properties on page 16-47 provides a LOCAL keyword to
equipartition a nested table with partitioned base table. This is the default
behavior in this release. The default in earlier releases was not to equipartition
the nested table with the partitioned base table. Now you must specify the
GLOBAL keyword to store an unpartitioned nested table with a partitioned
base table.
■ The CREATE VIEW has a new keyword EDITIONING on page 17-17 that lets you
create an editioning view.
■ The statement GRANT on page 18-33 has a new EXECUTE object privilege on
directory objects. The ORACLE_LOADER access driver for external tables references
this privilege when deciding whether to execute a preprocessor program.
The following built-in functions are new or enhanced in this release:
■ For a specified measure, the function LISTAGG on page 5-101 orders data within
each group specified in an ORDER BY clause and then concatenates the values of
the measure column.
xxv
■ A new NTH_VALUE on page 5-122 function returns the value of a measure in a
specified row of a window of data.
■ Three new functions are useful when you are changing the time zone data file for
your database: ORA_DST_AFFECTED on page 5-128, ORA_DST_CONVERT on
page 5-129, and ORA_DST_ERROR on page 5-130.
The following miscellaneous features are new or enhanced in this release:
■ Hints, which were introduced in Oracle7, are now superseded by several Oracle
tools, including the SQL Tuning Advisor, SQL plan management, and SQL
Performance Analyzer. See "Hints" on page 3-73 for more information.
■ Beginning with Oracle Database 11g Release 2, the PARALLEL, PARALLEL_INDEX,

NO_PARALLEL, and NO_PARALLEL_INDEX hints are statement-level hints and
supersede the earlier object-level hints. See "Note on Parallel Hints" on page 3-96.
■ A new APPEND_VALUES Hint on page 3-78 lets you use direct-path INSERT
with the VALUES clause.
■ When specifying a redo log file, you can use the new keyword BLOCKSIZE Clause
on page 8-34 to override the operating system-dependent sector size.
■ The LOB_compression_clause on page 16-45 now has a new LOW setting, which
results in significantly higher decompression and compression speeds, at the cost
of a slightly lower compression ratio.
■ The subquery_factoring_clause on page 19-13 now supports recursive subquery
factoring (recursive WITH), which lets you query hierarchical data. This feature is
more powerful than CONNECT BY in that it provides depth-first search and
breadth-first search, and supports multiple recursive branches. A new search_clause
on page 19-14 and cycle_clause on page 19-14 let you specify an ordering for the
rows and mark cycles in the recursion.
Oracle Database 11g Release 1 New Features in the SQL Language
Reference
Structural Changes in the SQL Language Reference
A number of SQL statements are constructed almost entirely of PL/SQL elements.
Those statements continue to appear in this reference, but the bulk of their syntax and
semantics has been moved to Oracle Database PL/SQL Language Reference. The following
table contains links to both the abbreviated SQL syntax and semantics in this book and
to the full syntax and semantics in Oracle Database PL/SQL Language Reference.
Abbreviated SQL Section Full Syntax and Semantics
CREATE FUNCTION on page 14-59 CREATE FUNCTION
CREATE PACKAGE on page 15-41 CREATE PACKAGE
CREATE PACKAGE BODY on page 15-43 CREATE PACKAGE BODY
CREATE PROCEDURE on page 15-47 CREATE PROCEDURE
CREATE TRIGGER on page 16-94 CREATE TRIGGER
CREATE TYPE on page 17-3 CREATE TYPE

CREATE TYPE BODY on page 17-5 CREATE TYPE BODY
ALTER FUNCTION on page 10-76 ALTER FUNCTION

×