Sie sind auf Seite 1von 10

Introduction to Oracle9i for

Experienced SQL Users

Student Guide • Volume 1

40059GC10
Production 1.0
January 2002
D34327
Authors Copyright © Oracle Corporation 2000,2001,2002 All rights reserved.

Priya Nathan This documentation contains proprietary information of Oracle Corporation. It is


provided under a license agreement containing restrictions on use and disclosure and
is also protected by copyright law. Reverse engineering of the software is prohibited.
If this documentation is delivered to a U.S. Government Agency of the Department of
Technical Contributors Defense, then it is delivered with Restricted Rights and the following legend is
and Reviewers applicable:

Helen Robertson Restricted Rights Legend


Isabelle Marchand
Use, duplication or disclosure by the Government is subject to restrictions for
Leslo Wanders
commercial computer software and shall be deemed to be Restricted Rights software
Ligia Jasmin Robayo under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013,
Lilian Hobbs Rights in Technical Data and Computer Software (October 1988).
Martin Jensen
This material or any portion of it may not be copied in any form or by any means
Piet Van Zon without the express prior written permission of Oracle Corporation. Any other copying
Roland Maile is a violation of copyright law and may result in civil and/or criminal penalties.
Ruediger Steffan
Sander Rekveld If this documentation is delivered to a U.S. Government Agency not within the
Department of Defense, then it is delivered with “Restricted Rights,” as defined in
Sunitha Patel FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987).
Trinh N Tran
Yanti Chang The information in this document is subject to change without notice. If you find any
problems in the documentation, please report them in writing to Oracle Education
Products, Oracle Corporation, 500 Oracle Parkway, Box SB-6, Redwood Shores, CA
Publisher 94065. Oracle Corporation does not warrant that this document is error-free.
Shane Mattimoe Oracle and all references to Oracle products are trademarks or registered trademarks
of Oracle Corporation.

All other products or company names are used for identification purposes only, and
may be trademarks of their respective owners.
Contents

Preface

Introduction
Objectives I-2
Oracle Database Overview I-3
Oracle9i I-5
Oracle9i Application Server I-7
Oracle9i Database I-8
Oracle Architectural Components I-9
Oracle Database Files I-11
Other Key Physical Structures I-12
Oracle Instance I-13
Processing a SQL Statement I-15
Connecting to an Instance I-16
Using SQL to Communicate with an RDBMS I-18
SQL Statements I-19
Tables Used in the Course I-20
Summary I-21

1 The Oracle SELECT Statement


Objectives 1-2
Oracle SELECT Statement 1-3
Using the WHERE Clause 1-5
Defining and Using a Column Alias 1-7
SQL Operators 1-8
Arithmetic Expressions 1-10
Using Parentheses 1-11
Defining a Null Value 1-12
Null Values in Arithmetic Expressions 1-13
Using the NVL2 Function 1-14
Using the Concatenation Operator 1-15
Using Pseudocolumns 1-16
ORDER BY Clause 1-18
Joining Tables Using Oracle Syntax 1-20
Retrieving Records with Equijoins 1-21
Qualifying Ambiguous Column Names 1-22
Non-Equijoins 1-24
Outer Join Syntax 1-25

iii
Using Outer Joins 1-26
Self Joins 1-27
Joins: Comparing SQL:1999 to Oracle Syntax 1-28
SQL and iSQL*Plus Interaction 1-29
SQL Statements versus iSQL*Plus Commands 1-30
Overview of iSQL*Plus 1-31
Logging In to iSQL*Plus 1-32
The iSQL*Plus Environment 1-34
Interacting with Script Files 1-35
Saving Output to a File 1-37
Displaying Table Structure 1-38
Data Types 1-39
Oracle9i DateTime Support 1-42
DateTime Data Types 1-44
INTERVAL Data Types 1-46
Identification of Nonstandard SQL 1-49
Summary 1-50
Practice 1 Overview 1-51
2 Single-Row Functions
Objectives 2-2
SQL Functions 2-3
Two Types of SQL Functions 2-4
Single-Row Functions 2-5
Character Functions 2-6
Case Manipulation Functions 2-8
Character Manipulation Functions 2-10
Number Functions 2-12
Using the ROUND and TRUNCATE Functions 2-13
Working with Dates 2-15
RR Date Format 2-16
Arithmetic Operators with Dates 2-17
Date Functions 2-18
Using Date Functions 2-19
Conversion Functions 2-21
Explicit Data Type Conversion 2-23
The TO_CHAR Function with Dates 2-24
Date Format Model Elements 2-25
Using the TO_CHAR Function with Dates and Numbers 2-27
TO_NUMBER and TO_DATE Functions 2-29
TO_LOB Functions 2-30
Using the COALESCE Function 2-31
Conditional Expressions 2-33
The CASE Expression 2-34

iv
DECODE Function 2-35
Using the DECODE Function 2-37
Nesting Functions 2-39
Practice 2, Part 1: Overview 2-41
Time Zones 2-42
TZ_OFFSET 2-43
CURRENT_DATE 2-45
CURRENT_TIMESTAMP 2-46
LOCALTIMESTAMP 2-47
DBTIMEZONE and SESSIONTIMEZONE 2-48
EXTRACT 2-49
TIMESTAMP Conversion Using FROM_TZ 2-50
STRING to TIMESTAMP Conversion Using TO_TIMESTAMP and
TO_TIMESTAMP_TZ 2-51
Time Interval Conversion with TO_YMINTERVAL 2-52
Summary 2-53
Practice 2 2-54

3 Enhancements to the GROUP BY Clause


Objectives 3-2
Review of Group Functions 3-3
Review of the GROUP BY and HAVING Clauses 3-4
GROUP BY with ROLLUP and CUBE Operators 3-6
ROLLUP Operator 3-7
ROLLUP Operator: Example 3-8
CUBE Operator 3-9
CUBE Operator: Example 3-10
GROUPING Function 3-11
GROUPING Function: Example 3-12
GROUPING SETS 3-13
GROUPING SETS: Example 3-15
Composite Columns 3-17
Composite Columns: Example 3-19
Concatenated Groupings 3-21
Concatenated Groupings: Example 3-22
Summary 3-23
Practice 3 Overview 3-24

4 Using Set Operators


Objectives 4-2
The Set Operators 4-3

v
Tables Used in This Lesson 4-4
The UNION Set Operator 4-7
Using the UNION Operator 4-8
The UNION ALL Operator 4-10
Using the UNION ALL Operator 4-11
The INTERSECT Operator 4-12
Using the INTERSECT Operator 4-13
The MINUS Operator 4-14
Set Operator Guidelines 4-16
The Oracle Server and Set Operators 4-17
Matching the SELECT Statements 4-18
Controlling the Order of Rows 4-20
Summary 4-21
Practice 4 Overview 4-22

5 Advanced Subqueries
Objectives 5-2
Subqueries 5-3
Using a Subquery 5-5
Multiple-Column Subqueries 5-6
Column Comparisons 5-7
Pairwise Comparison Subqueries 5-8
Nonpairwise Comparison Subqueries 5-9
Using a Subquery in the FROM Clause 5-11
Scalar Subquery Expressions 5-12
Correlated Subqueries 5-15
Using the EXISTS Operator 5-18
Using the NOT EXISTS Operator 5-20
Correlated UPDATE Operations 5-21
Correlated DELETE Operations 5-23
The WITH Clause 5-25
WITH Clause: Example 5-26
Summary 5-28
Practice 5 Overview 5-29

vi
6 Hierarchical Retrieval
Objectives 6-2
Hierarchical Queries 6-3
Sample Data from the EMPLOYEES Table 6-4
Natural Tree Structure 6-5
Hierarchical Queries 6-6
“Walking” the Tree 6-8
Walking the Tree: From the Bottom Up 6-10
Walking the Tree: From the Top Down 6-11
Ranking Rows with the LEVEL Pseudocolumn 6-12
Formatting Hierarchical Reports Using LEVEL and LPAD 6-13
Pruning Branches 6-15
Summary 6-16
Practice 6 Overview 6-17
7 Creating and Managing Tables and Constraints
Objectives 7-2
What Is a Schema? 7-3
Referencing Another User’s Tables 7-5
The CREATE TABLE Statement 7-6
Creating a Table by Using a Subquery 7-8
The ALTER TABLE Statement 7-10
Adding a Column 7-11
Modifying and Dropping Columns 7-12
SET UNUSED Option 7-13
Data Integrity 7-14
Types of Constraint 7-16
Defining Constraints 7-17
Guidelines for Defining Constraints 7-20
Adding a Constraint 7-22
Disabling and Enabling Constraints 7-23
Constraint Checking 7-26
Changing the Enforcement of Constraints 7-27
Primary and Unique Key Enforcement 7-28
Foreign Key Considerations 7-29
Dropping a Constraint 7-31
Cascading Constraints 7-32
Changing the Name of an Object 7-34
Truncating a Table 7-35
Dropping a Table 7-36
Adding Comments to a Table 7-37
Tables in the Oracle Database 7-38
Querying the Data Dictionary 7-40
Viewing Constraints 7-41
Summary 7-42
Practice 7 Overview 7-47

vii
8 Other Oracle Database Objects
Objectives 8-2
Database Objects 8-3
Overview of Views 8-4
Simple Views and Complex Views 8-5
Creating a View 8-6
Creating a Simple View 8-7
Creating a Complex View 8-9
Querying a View 8-11
Modifying a View 8-12
Rules for Performing DML Operations on a View 8-13
Using the WITH CHECK OPTION Clause 8-14
Denying DML Operations 8-16
Removing a View 8-17
Inline Views 8-18
Top-N Analysis 8-19
Example of Top-N Analysis 8-20
What Is a Sequence? 8-21
The CREATE SEQUENCE Statement 8-22
Confirming Sequences 8-24
Using a Sequence 8-25
Using a Sequence: Example 8-27
Caching Sequence Values 8-28
Altering a Sequence 8-29
Guidelines for Modifying Sequences 8-30
Removing a Sequence 8-31
What Is an Index? 8-32
How Are Indexes Created? 8-34
Creating an Index 8-35
Guidelines for Creating an Index 8-36
Confirming Indexes 8-37
Function-Based Indexes 8-38
The CREATE INDEX Clause with the CREATE TABLE Statement 8-39
Removing an Index 8-40
Synonyms 8-41
Creating and Removing Synonyms 8-42
Summary 8-43
Practice 8 Overview 8-45
9 Controlling Transactions
Objectives 9-2
Database Transactions 9-3
COMMIT and ROLLBACK Statements 9-5
Controlling Transactions 9-7

viii
State of the Data Before COMMIT or ROLLBACK 9-8
State of the Data after COMMIT 9-9
State of the Data after ROLLBACK 9-10
Rolling Back Changes to a Marker 9-11
Statement-Level Rollbacks 9-12
Read Consistency Without Locking 9-13
Implementation of Read Consistency 9-14
Read Consistency: Example 9-16
Implicit Locking 9-17
Explicit Locking 9-18
Summary 9-19
Practice 9 Overview 9-20
10 Controlling User Access
Objectives 10-2
Controlling User Access 10-3
System Privileges 10-4
Creating Users 10-5
User System Privileges 10-6
Granting System Privileges 10-7
What Is a Role? 10-8
Creating and Granting Privileges to a Role 10-9
Changing Your Password 10-10
Object Privileges 10-11
Granting Object Privileges 10-13
Using the WITH GRANT OPTION and PUBLIC Keywords 10-14
Confirming Privileges Granted 10-15
Revoking Object Privileges 10-16
Database Links 10-17
Summary 10-20
Practice 10 Overview 10-21
11 Oracle9i Extensions to DML and DDL Statements
Objectives 11-2
Overview of Multitable INSERT Statements 11-3
Types of Multitable INSERT Statements 11-5
Multitable INSERT Statements 11-6
Unconditional INSERT ALL 11-8
Conditional INSERT ALL 11-9
Conditional FIRST INSERT 11-11
Pivoting INSERT 11-13
External Tables 11-16
Creating an External Table 11-17
Example of Creating an External Table 11-18

ix
Querying External Tables 11-21
Summary 11-22
Practice 11 Overview 11-23
Appendix A: Practice Solutions
Appendix B: Table Descriptions and Data
Appendix C: iSQL*Plus
Appendix D: Using SQL to Generate SQL
Appendix E: ANSI Joins
Additional Practices
Additional Practice Solutions

Das könnte Ihnen auch gefallen