Tải bản đầy đủ (.pdf) (40 trang)

OCA Oracle Database 11g SQL Fundamentals I Exam Guide P1

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 (1.49 MB, 40 trang )


®

OCA Oracle Database 11g:
SQL Fundamentals I
Exam Guide (Exam 1Z0-051)


ABOUT THE AUTHORS

John Watson (Oxford, UK) works for BPLC Management Consultants, teaching
and consulting throughout Europe and Africa. He was with Oracle University for
several years in South Africa, and before that worked for a number of companies,
government departments, and NGOs in England and Europe. He is OCP qualified in
both database and Application Server administration. John is the author of several
books and numerous articles on technology and has 25 years of experience in IT.
Roopesh Ramklass (South Africa), OCP, is an independent Oracle specialist with
over 10 years of experience in a wide variety of IT environments. These include
software design and development, systems analysis, courseware development, and
lecturing. He has worked for Oracle Support and taught at Oracle University in
South Africa for several years. Roopesh is experienced in managing and executing
IT development projects, including infrastructure systems provisioning, software
development, and systems integration.

About the Technical Editor
Bruce Swart (South Africa) works for 2Cana Solutions and has over 14 years of
experience in IT. Whilst maintaining a keen interest for teaching others, he has
performed several roles including developer, analyst, team leader, administrator,
project manager, consultant, and lecturer. He is OCP qualified in both database and
developer roles. He has taught at Oracle University in South Africa for several years
and has also spoken at numerous local Oracle User Group conferences. His passion


is helping others achieve greatness.

Copyright © 2008 by The McGraw-Hill Companies, Inc. Click here for terms of use.


®

OCA Oracle Database 11g:
SQL Fundamentals I
Exam Guide (Exam 1Z0-051)
John Watson
Roopesh Ramklass

This publication and CD may be used in assisting students to prepare for the OCP
Oracle Database 11g: SQL Fundamentals I exam. Neither Oracle Corporation nor
The McGraw-Hill Companies warrant that use of this publication and CD will ensure
passing the relevant exam.
New York Chicago San Francisco Lisbon London Madrid
Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto


Copyright © 2008 by The McGraw-Hill Companies, Inc. All rights reserved. Manufactured in the United States of America. Except as
permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or
by any means, or stored in a database or retrieval system, without the prior written permission of the publisher.
0-07-164380-X
The material in this eBook also appears in the print version of this title: 0-07-159786-7.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked
name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the
trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate

training programs. For more information, please contact George Hoare, Special Sales, at or (212)
904-4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the
work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve
one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon,
transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use
the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may
be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS
TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,
INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,
AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not
warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or
error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless
of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information
accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental,
special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been
advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim
or cause arises in contract, tort or otherwise.
DOI: 10.1036/0071597867


With thanks to Silvia for making life worth living.
—John
Ameetha, you have been wonderfully supportive and I want to
thank you for every moment that you share with me.
—Roopesh



This page intentionally left blank


CONTENTS AT A GLANCE

1

Oracle Server Technologies and the Relational Paradigm

..........

1

2

Data Retrieval Using the SQL SELECT Statement

...............

51

3

Restricting and Sorting Data

.................................

103


4

Single-Row Functions

......................................

169

5

Using Conversion Functions and Conditional Expressions

6

Reporting Aggregated Data Using the Group Functions

7

.........

227

...........

273

Displaying Data from Multiple Tables

..........................


309

8

Using Subqueries to Solve Problems

...........................

357

9

Using the Set Operators

.....................................

381

10

Manipulating Data

.........................................

403

11

Using DDL Statements to Create and Manage Tables


12

Creating Other Schema Objects

.............

449

..............................

487

................................................

533

.................................................

537

....................................................

555

Appendix
Glossary
Index

vii



This page intentionally left blank


For more information about this title, click here

CONTENTS

Introduction

1

........................................

Oracle Server Technologies and
the Relational Paradigm . . . . . . . . . . . . . . . . . . . . . . . .
Position the Server Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Oracle Server Architecture . . . . . . . . . . . . . . . . . . . . . . .
The Oracle Application Server . . . . . . . . . . . . . . . . . . . . . . . .
Oracle Enterprise Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Grid Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 1-1: Investigate Your Database and
Application Environment . . . . . . . . . . . . . . . . . . . . . . . . . .
Development Tools and Languages . . . . . . . . . . . . . . . . . . . . .
Understand Relational Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rows and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 1-2: Perform an Extended
Relational Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summarize the SQL Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SQL Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Set-oriented Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use the Client Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SQL*Plus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SQL Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create the Demonstration Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . .
Users and Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The HR and OE Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Demonstration Schema Creation . . . . . . . . . . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xix

1
3
3
5
7
8
9
9
10
11
14
20
23
23
23

25
26
26
33
37
38
38
42
45
46

ix


x

OCA Oracle Database 11g: SQL Fundamentals I Exam Guide (Exam 1Z0-051)

Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

Data Retrieval Using the SQL SELECT Statement

...

List the Capabilities of SQL SELECT Statements . . . . . . . . . . . . . . . .
Introducing the SQL SELECT Statement . . . . . . . . . . . . . . . .

The DESCRIBE Table Command . . . . . . . . . . . . . . . . . . . . . .
Exercise 2-1: Describing the Human
Resources Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Capabilities of the SELECT Statement . . . . . . . . . . . . . . . . . .
Execute a Basic SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . .
Syntax of the Primitive SELECT Statement . . . . . . . . . . . . . .
Rules Are Meant to be Followed . . . . . . . . . . . . . . . . . . . . . . .
Exercise 2-2: Answering Our First
Questions with SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SQL Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . .
NULL Is Nothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 2-3: Experimenting with
Expressions and the DUAL Table . . . . . . . . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

Restricting and Sorting Data

48
49
50

51
52
52

53
55
57
59
59
64
67
70
81
86
90
92
94
96
98

. . . . . . . . . . . . . . . . . . . . . 103

Limit the Rows Retrieved by a Query . . . . . . . . . . . . . . . . . . . . . . . . . .
The WHERE clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 3-1: Using the LIKE Operator . . . . . . . . . . . . . . .
Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Precedence Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sort the Rows Retrieved by a Query . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ORDER BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 3-2: Sorting Data Using
the ORDER BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . .

104

104
113
124
126
132
136
136
141


Contents

Ampersand Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Substitution Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Define and Verify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 3-3: Using Ampersand Substitution . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

Single-Row Functions

142
143
149
155

158
160
162
164
166

. . . . . . . . . . . . . . . . . . . . . . . . . . . 169

Describe Various Types of Functions Available in SQL . . . . . . . . . . . .
Defining a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use Character, Number, and Date Functions
in SELECT Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Character Case Conversion Functions . . . . . . . . . . . . .
Exercise 4-1: Using the Case Conversion Functions . . . . .
Using Character Manipulations Functions . . . . . . . . . . . . . . .
Exercise 4-2: Using the Case Manipulation Functions . . .
Using Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Date Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 4-3: Using the Date Functions . . . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

xi


170
170
174
177
177
181
183
194
196
202
206
210
219
221
223
224
225

Using Conversion Functions and
Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . 227
Describe Various Types of Conversion
Functions Available in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use the TO_CHAR, TO_NUMBER,
and TO_DATE Conversion Functions . . . . . . . . . . . . . . . . . . . . . . .
Using the Conversion Functions . . . . . . . . . . . . . . . . . . . . . . .

228
228

231
232


xii

OCA Oracle Database 11g: SQL Fundamentals I Exam Guide (Exam 1Z0-051)

Exercise 5-1: Converting Dates into Characters
Using the TO_CHAR Function . . . . . . . . . . . . . . . . . . . . .
Apply Conditional Expressions in a SELECT Statement . . . . . . . . . . .
Nesting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 5-2: Using NULLIF and NVL2
for Simple Conditional Logic . . . . . . . . . . . . . . . . . . . . . . .
Conditional Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 5-3: Using the DECODE Function . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

239
245
245
251
254
260

263
265
267
268
269

Reporting Aggregated Data Using
the Group Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Describe the Group Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Definition of Group Functions . . . . . . . . . . . . . . . . . . . . . . . . .
Types and Syntax of Group Functions . . . . . . . . . . . . . . . . . . .
Identify the Available Group Functions . . . . . . . . . . . . . . . . . . . . . . . .
Using the Group Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 6-1: Using the Group Functions . . . . . . . . . . . . .
Nested Group Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Group Data Using the GROUP BY Clause . . . . . . . . . . . . . . . . . . . . . .
Creating Groups of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The GROUP BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Grouping by Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 6-2: Grouping Data Based
on Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Include or Exclude Grouped Rows Using the HAVING Clause . . . . . .
Restricting Group Results . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The HAVING Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 6-3: Using the HAVING Clause . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

274
274

275
279
279
284
285
287
287
288
291
292
294
294
296
298
301
303


Contents

Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

Displaying Data from Multiple Tables

xiii
305

306
308

. . . . . . . . . . . . . . 309

Write SELECT Statements to Access Data from
More Than One Table Using Equijoins and Nonequijoins . . . . . . . .
Types of Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Joining Tables Using SQL:1999 Syntax . . . . . . . . . . . . . . . . . .
Qualifying Ambiguous Column Names . . . . . . . . . . . . . . . . . .
The NATURAL JOIN Clause . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 7-1: Using the NATURAL JOIN . . . . . . . . . . . .
The Natural JOIN USING Clause . . . . . . . . . . . . . . . . . . . . .
The Natural JOIN ON Clause . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 7-2: Using the NATURAL
JOIN…ON Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
N-Way Joins and Additional Join Conditions . . . . . . . . . . . . .
Nonequijoins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Join a Table to Itself Using a Self-Join . . . . . . . . . . . . . . . . . . . . . . . . . .
Joining a Table to Itself Using the JOIN…ON Clause . . . . . .
Exercise 7-3: Performing a Self-Join . . . . . . . . . . . . . . . . . .
View Data that Does Not Meet a Join Condition
by Using Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inner versus Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Left Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Right Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Full Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 7-4: Performing an Outer-Join . . . . . . . . . . . . . . .
Generate a Cartesian Product of Two or More Tables . . . . . . . . . . . . . .
Creating Cartesian Products Using Cross Joins . . . . . . . . . . . .

Exercise 7-5: Performing a Cross-Join . . . . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

310
311
316
317
319
320
321
322
324
327
329
331
331
332
334
335
336
337
338
340
342
342
344

348
350
352
354
355


xiv

OCA Oracle Database 11g: SQL Fundamentals I Exam Guide (Exam 1Z0-051)

8

Using Subqueries to Solve Problems

. . . . . . . . . . . . . . 357

Define Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 8-1: Types of Subquery . . . . . . . . . . . . . . . . . . . . .
Describe the Types of Problems That the Subqueries Can Solve . . . . .
Use of a Subquery Result Set for Comparison Purposes . . . . .
Star Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generate a Table from Which to SELECT . . . . . . . . . . . . . . .
Generate Values for Projection . . . . . . . . . . . . . . . . . . . . . . . .
Generate Rows to be Passed to a DML Statement . . . . . . . . .
Exercise 8-2: More Complex Subqueries . . . . . . . . . . . . . .
List the Types of Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Single- and Multiple-Row Subqueries . . . . . . . . . . . . . . . . . . .
Correlated Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 8-3: Investigate the Different

Types of Subquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Write Single-Row and Multiple-Row Subqueries . . . . . . . . . . . . . . . . .
Exercise 8-4: Write a Query That Is Reliable
and User Friendly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

Using the Set Operators

358
359
360
360
361
362
362
363
363
364
365
366
367
369
370
374

375
378
379
380

. . . . . . . . . . . . . . . . . . . . . . . . 381

Describe the Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sets and Venn Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Set Operator General Principles . . . . . . . . . . . . . . . . . . . . . . .
Exercise 9-1: Describe the Set Operators . . . . . . . . . . . . . .
Use a Set Operator to Combine Multiple Queries
into a Single Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The UNION ALL Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .
The UNION Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The INTERSECT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MINUS Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More Complex Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 9-2: Using the Set Operators . . . . . . . . . . . . . . . .

382
383
384
385
386
387
387
388
389
390

392


Contents

xv

Control the Order of Rows Returned . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 9-3: Control the Order of Rows Returned
.....
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

393
394
396
397
398
400
401

10 Manipulating Data

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403

Describe Each Data Manipulation Language (DML) Statement . . . . .
INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MERGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
TRUNCATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DML Statement Failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Insert Rows into a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 10-1: Use the INSERT Command . . . . . . . . . . . .
Update Rows in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 10-2: Use the UPDATE Command . . . . . . . . . . .
Delete Rows from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Removing Rows with DELETE . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 10-3: Use the DELETE Command . . . . . . . . . . .
Removing Rows with TRUNCATE . . . . . . . . . . . . . . . . . . . . .
MERGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Control Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Database Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Transaction Control Statements . . . . . . . . . . . . . . . . . . .
Exercise 10-4: Use the COMMIT and
ROLLBACK Commands . . . . . . . . . . . . . . . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

404
405
406
407
408

409
409
413
417
419
421
423
423
424
425
426
427
428
431
433
439
441
444
446
448


xvi

OCA Oracle Database 11g: SQL Fundamentals I Exam Guide (Exam 1Z0-051)

11 Using DDL Statements to
Create and Manage Tables

. . . . . . . . . . . . . . . . . . . . . 449


Categorize the Main Database Objects . . . . . . . . . . . . . . . . . . . . . . . . .
Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Users and Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Naming Schema Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 11-1: Determine What Objects
Are Accessible to Your Session . . . . . . . . . . . . . . . . . . . . . .
Review the Table Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 11-2: Investigate Table Structures . . . . . . . . . . . .
List the Data Types That Are Available for Columns . . . . . . . . . . . . . .
Exercise 11-3: Investigate the Data Types
in the HR schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a Simple Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Tables with Column Specifications . . . . . . . . . . . . .
Creating Tables from Subqueries . . . . . . . . . . . . . . . . . . . . . . .
Altering Table Definitions after Creation . . . . . . . . . . . . . . . .
Dropping and Truncating Tables . . . . . . . . . . . . . . . . . . . . . . .
Exercise 11-4: Create Tables . . . . . . . . . . . . . . . . . . . . . . .
Explain How Constraints Are Created
at the Time of Table Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Types of Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 11-5: Work with Constraints . . . . . . . . . . . . . . . .
✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


12 Creating Other Schema Objects

450
450
452
453
455
455
456
457
457
461
462
462
464
465
466
467
469
470
473
476
478
479
482
483
484

. . . . . . . . . . . . . . . . . . 487


Create Simple and Complex Views . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Use Views at All? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple and Complex Views . . . . . . . . . . . . . . . . . . . . . . . . . . .
CREATE VIEW, ALTER VIEW, and DROP VIEW . . . . . . .
Exercise 12-1: Create Views . . . . . . . . . . . . . . . . . . . . . . . .

488
489
492
493
496


Contents

Retrieve Data from Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 12-2: Use Views . . . . . . . . . . . . . . . . . . . . . . . . . .
Create Private and Public Synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 12-3: Create and Use Synonyms . . . . . . . . . . . . .
Create, Maintain, and Use Sequences . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 12-4: Create and Use Sequences . . . . . . . . . . . . .
Create and Maintain Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Indexes Are For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Index
.....................................
Creating and Using Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying and Dropping Indexes . . . . . . . . . . . . . . . . . . . . . . .
Exercise 12-5: Creating Indexes . . . . . . . . . . . . . . . . . . . . .

✓ Two-Minute Drill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q&A Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Self Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lab Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xvii
497
497
498
500
501
502
504
507
509
510
511
515
517
518
521
523
527
529
531

Appendix

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533


Glossary

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537

Index

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555


This page intentionally left blank


INTRODUCTION

T

here is an ever-increasing demand for staff with IT industry certification. The benefits
to employers are significant—they can be certain that staff have a certain level of
competence—and the benefits to the individuals, in terms of demand for their services,
are equally great. Many employers are now requiring technical staff to have certifications, and many
IT purchasers will not buy from firms that do not have certified staff. The Oracle certifications
are among the most sought after. But apart from rewards in a business sense, knowing that you
are among a relatively small pool of elite Oracle professionals and that you have proved your
competence is a personal reward well worth attaining.
There are several Oracle certification tracks—this book is concerned with the
Oracle Database Administration certification track, specifically for release 11g of the
database. There are three levels of DBA certification: Certified Associate (OCA),
Certified Professional (OCP), and Certified Master (OCM). The OCA qualification
is based on two examinations, the first of which is covered in this book. The OCP

qualification requires passing a third examination. These examinations can be taken
at any Prometric Center and consist of 60 to 70 questions to be completed in 90
minutes. The OCM qualification requires completing a further two-day evaluation
at an Oracle testing center, involving simulations of complex environments and use
of advanced techniques.
The exam content is also likely to be the starting point if you intend to study for
the Oracle PL/SQL and Oracle Forms Developer track, though (as of the time of
writing) this is at release 10g with no announcement of an 11g release.
To prepare for the first OCA examination, you can attend an Oracle University
instructor-led training course, you can study Oracle University online learning
material, or you can read this book. In all cases, you should also refer to the Oracle
Documentation Library for details on syntax. This book will be a valuable addition
to other study methods, but it is also sufficient by itself. It has been designed
with the examination objectives in mind, though it also includes a great deal
of information that will be useful to you in the course of your work. For readers
working in development, the subject matter of this book is also the starting point for
studying Oracle Corporation’s development tools: SQL, PL/SQL, and the Internet
application development kits shipped with the Oracle Application Server.

xix
Copyright © 2008 by The McGraw-Hill Companies, Inc. Click here for terms of use.



×