Sie sind auf Seite 1von 41

Ben Forta

Sams TeachYourself

Oracle

PL /SQL
in 10 Minutes

800 East 96th Street, Indianapolis, Indiana 46240


Sams Teach Yourself Oracle PL/SQL in 10 Minutes Acquisitions
Copyright 2016 by Pearson Education, Inc. Editor
All rights reserved. No part of this book shall be reproduced, Mark Taber
stored in a retrieval system, or transmitted by any means,
Managing
electronic, mechanical, photocopying, recording, or otherwise,
Editor
without written permission from the publisher. No patent
liability is assumed with respect to the use of the information Kristy Hart
contained herein. Although every precaution has been taken in
Senior Project
the preparation of this book, the publisher and author assume no
Editor
responsibility for errors or omissions. Nor is any liability assumed
for damages resulting from the use of the information contained Betsy Gratner
herein.
Copy Editor
ISBN-13: 978-0-672-32866-4 Paula Lowell
ISBN-10: 0-672-32866-6
Library of Congress Control Number: 2015910491 Indexer
Printed in the United States of America Lisa Stumpf
First Printing September 2015 Proofreader
Trademarks Sarah Kearns
All terms mentioned in this book that are known to be trademarks
or service marks have been appropriately capitalized. Sams Editorial
Publishing cannot attest to the accuracy of this information. Use Assistant
of a term in this book should not be regarded as affecting the Vanessa Evans
validity of any trademark or service mark.
Cover Designer
Warning and Disclaimer Mark Shirar
Every effort has been made to make this book as complete and
as accurate as possible, but no warranty or fitness is implied. Senior
The information provided is on an as is basis. The author and Compositor
the publisher shall have neither liability nor responsibility to any Gloria Schurick
person or entity with respect to any loss or damages arising from
the information contained in this book or from the use of the
programs accompanying it.
Special Sales
For information about buying this title in bulk quantities, or for
special sales opportunities (which may include electronic versions;
custom cover designs; and content particular to your business,
training goals, marketing focus, or branding interests), please
contact our corporate sales department atcorpsales@pearsoned.
comor(800) 382-3419.
For government sales inquiries, please contactgovernmentsales@
pearsoned.com.
For questions about sales outside the U.S., please
contactinternational@pearsoned.com.
Contents

Introduction 1

What Is This Book? ................................................................. 1

Who Is This Book For? ............................................................. 2

Companion Website ................................................................ 3

Conventions Used in This Book ................................................ 3

1 Understanding SQL 5

Database Basics ..................................................................... 5

What Is SQL? ........................................................................ 10

Try It Yourself........................................................................ 11

Summary .............................................................................. 12

2 Getting Started with Oracle and PL/SQL 13

What Is Oracle? .................................................................... 13

Getting Set Up ...................................................................... 16

Summary .............................................................................. 20

3 Working with Oracle 21

Creating a Working Environment ............................................. 21

Making the Connection .......................................................... 25

A Quick Introduction to Oracle SQL Developer ......................... 27

Creating and Populating the Example Tables ........................... 28

One More Look at Oracle SQL Developer ................................ 32

Summary .............................................................................. 32
iv Sams Teach Yourself Oracle PL/SQL in 10 Minutes

4 Retrieving Data 33

The SELECT Statement .......................................................... 33

Retrieving Individual Columns ................................................. 33

Retrieving Multiple Columns ................................................... 36

Retrieving All Columns ........................................................... 37

Retrieving Distinct Rows ........................................................ 38

Using Fully Qualified Table Names .......................................... 40

Using Comments ................................................................... 40

Summary .............................................................................. 42

5 Sorting Retrieved Data 43

Sorting Data ......................................................................... 43

Sorting by Multiple Columns................................................... 45

Specifying Sort Direction ........................................................ 47

Summary .............................................................................. 49

6 Filtering Data 51

Using the WHERE Clause ......................................................... 51

The WHERE Clause Operators .................................................. 53

Summary .............................................................................. 59

7 Advanced Data Filtering 61

Combining WHERE Clauses...................................................... 61

Using the IN Operator ........................................................... 65

Using the NOT Operator.......................................................... 67

Summary .............................................................................. 68
Contents v

8 Using Wildcard Filtering 69

Using the LIKE Operator ........................................................ 69

Tips for Using Wildcards ........................................................ 74

Summary .............................................................................. 74

9 Searching Using Regular Expressions 75


Understanding Regular Expressions ........................................ 75

Using Oracle PL/SQL Regular Expressions .............................. 76

Summary .............................................................................. 89

10 Creating Calculated Fields 91

Understanding Calculated Fields............................................. 91

Concatenating Fields ............................................................. 92

Performing Mathematical Calculations .................................... 96

Summary .............................................................................. 98

11 Using Data Manipulation Functions 99

Understanding Functions ....................................................... 99

Using Functions .................................................................. 100

Summary ............................................................................ 107

12 Summarizing Data 109

Using Aggregate Functions ................................................... 109

Aggregates on Distinct Values .............................................. 117

Combining Aggregate Functions ............................................ 118

Summary ............................................................................ 119


vi Sams Teach Yourself Oracle PL/SQL in 10 Minutes

13 Grouping Data 121

Understanding Data Grouping............................................... 121

Creating Groups .................................................................. 122

Filtering Groups ................................................................... 123

Grouping and Sorting ........................................................... 126

SELECT Clause Ordering....................................................... 129

Summary ............................................................................ 129

14 Working with Subqueries 131

Understanding Subqueries ................................................... 131

Filtering by Subquery ........................................................... 131

Using Subqueries as Calculated Fields ................................. 136

Summary ............................................................................ 140

15 Joining Tables 141

Understanding Joins ............................................................ 141

Creating a Join .................................................................... 144

Summary ............................................................................ 152

16 Creating Advanced Joins 153

Using Table Aliases ............................................................. 153

Using Different Join Types.................................................... 154

Using Joins with Aggregate Functions ................................... 160

Using Joins and Join Conditions ........................................... 161

Summary ............................................................................ 162


Contents vii

17 Combining Queries 163

Understanding Combined Queries......................................... 163

Creating Combined Queries.................................................. 164

Summary ............................................................................ 169

18 Inserting Data 171


Understanding Data Insertion ............................................... 171

Inserting Complete Rows ..................................................... 172

Inserting Retrieved Data ...................................................... 176

Summary ............................................................................ 178

19 Updating and Deleting Data 179

Updating Data ..................................................................... 179

Deleting Data ...................................................................... 181

Guidelines for Updating and Deleting Data ............................ 183

Summary ............................................................................ 184

20 Creating and Manipulating Tables 185

Creating Tables ................................................................... 185

Updating Tables .................................................................. 190

Deleting Tables ................................................................... 193

Renaming Tables ................................................................ 193

Summary ............................................................................ 194


viii Sams Teach Yourself Oracle PL/SQL in 10 Minutes

21 Using Views 195

Understanding Views ........................................................... 195

Using Views ........................................................................ 197

Summary ............................................................................ 204

22 Working with Stored Procedures 205

Understanding Stored Procedures ........................................ 205

Why Use Stored Procedures ................................................. 206

Using Stored Procedures ..................................................... 207

Summary ............................................................................ 214

23 Using Cursors 215

Understanding Cursors ........................................................ 215

Working with Cursors ........................................................... 216

Summary ............................................................................ 222

24 Using Triggers 223

Understanding Triggers ........................................................ 223

Creating Triggers ................................................................. 224

Dropping Triggers ................................................................ 225

Using Triggers ..................................................................... 225

Summary ............................................................................ 232

25 Managing Transaction Processing 233

Understanding Transaction Processing.................................. 233

Controlling Transactions ...................................................... 235

Summary ............................................................................ 238


Contents ix

26 Managing Security 239

Understanding Access Control .............................................. 239

Managing Users .................................................................. 240

Summary ............................................................................ 244

A The Example Tables 245

Understanding the Sample Tables ........................................ 245

B Oracle PL/SQL Datatypes 251

String Datatypes ................................................................. 252

Numeric Datatypes .............................................................. 253

C Oracle PL/SQL Reserved Words and Keywords 255

Index 259
About the Author
Ben Forta has three decades of experience in the computer industry
in product design and development, support, training, and marketing.
As Adobe Inc.s Senior Director of Education Initiatives, he spends a
considerable amount of time teaching, talking, and writing about Adobe
products, coding and application development, creativity, and digital
literacy and provides feedback to help shape the future direction of Adobe
products.
Ben is the author of more than 40 books, including the worlds best-
selling title on SQL, as well as titles on topics as diverse as Regular
Expressions, mobile development, and Adobe ColdFusion. More than
750,000 copies of his books are in print in English, and titles have
been translated into fifteen languages. Many of these titles are used as
textbooks in colleges and universities worldwide.
Education is Bens passion. Between writing, lecturing, and in-classroom
experience, Ben has dedicated his professional and personal lives to
teaching, inspiring, and sharing his love for technology and creativity. He
is immensely grateful to have had the opportunity to share with millions
worldwide.
Ben is also a successful entrepreneur with experience creating, building,
and selling start-ups. He is a sought-after public speaker, a writer, and a
blogger, and he presents on education and development topics worldwide.
Acknowledgments
Its been sixteen years since the publication of my first book on SQL,
Sams Teach Yourself SQL in 10 Minutes. That book was met with such
positive feedback that it has been updated three times, has spawned four
spin-off titles (the most recent being the book you are reading right now),
and has been translated more than a dozen times. In all of its various
flavors and iterations, this little book has helped hundreds of thousands
learn the basics of SQL. So, first and foremost, thanks to all of you
who have trusted me and this book over the years; your support is both
incredibly humbling and a source of great pride.
I am blessed with some very vocal and opinionated readers who regularly
share ideas, comments, suggestions, and occasionally criticism. These
books continue to improve directly in response to that feedback, so
thanks, and please keep it coming.
Thanks to the numerous schools and colleges the world over who have
made this series part of their curriculum. Seeing students use my writing
as part of their studies never ceases to thrill.
And finally, thanks to my partners at Pearson with whom Ive now
published more than 40 titles, and without whose support none would
have seen the light of day. In particular, thanks to Betsy Gratner for
shepherding this book through the process, Paula Lowell for her editing
help, and Mark Taber for once again patiently and encouragingly
supporting whatever I toss his way.
Ben Forta
We Want to Hear from You!
As the reader of this book, you are our most important critic and
commentator. We value your opinion and want to know what were doing
right, what we could do better, what areas youd like to see us publish in,
and any other words of wisdom youre willing to pass our way.
We welcome your comments. You can email or write to let us know what
you did or didnt like about this bookas well as what we can do to
make our books better.
Please note that we cannot help you with technical problems related to
the topic of this book.
When you write, please be sure to include this books title and author
as well as your name and email address. We will carefully review your
comments and share them with the author and editors who worked on the
book.
Email: feedback@samspublishing.com
Mail: Sams Publishing
ATTN: Reader Feedback
800 East 96th Street
Indianapolis, IN 46240 USA

Reader Services
Visit our website and register this book at informit.com/register for
convenient access to any updates, downloads, or errata that might be
available for this book.
Introduction

Oracle Database (or Oracle RDBMS) is so prevalent and well estab-


lished that most users simple refer to it as Oracle (ignoring the fact
that Oracle, the company, produces other software, and even hardware).
Oracle Database (Ill do what most do and just call it Oracle to simplify
things) has been around since the 1970s, making it one of the earliest
database management systems. Oracle is one of the most used database
management systems (DBMS) in the world. In fact, most surveys rank it
as #1 in database use and popularity worldwide, especially among cor-
porate users, and over the years it has proven itself to be a solid, reliable,
fast, and trusted solution to all sorts of data storage needs.
Thats the good news. The not-so-good news is that getting started with
Oracle can be tricky, especially when compared to some of the alternative
DBMSs. Oracles power, capabilities, security, and more are an important
part of why it is so trusted. But that makes installation, configuration,
and even the tooling a little more complex, too. On top of that, Oracles
implementation of the SQL language, called PL/SQL, tends to differ sub-
tly from other SQL implementations, and this can make using Oracle just
a bit trickier.

What Is This Book?


This book is based on my best-selling Sams Teach Yourself SQL in 10
Minutes. That book has become one of the most used SQL tutorials in
the world, with an emphasis on teaching what you really need to know
methodically, systematically, and simply. However, as popular and as suc-
cessful as that book is, it does have some limitations:
In covering all the major DBMSs, coverage of DBMS-specific
features and functionality had to be kept to a minimum.
To simplify the SQL taught, the lowest common denominator
had to be foundSQL statements that would (as much as possi-
ble) work with all major DBMSs. This requirement necessitated
that better DBMS-specific solutions not be covered.
2 Introduction

Although basic SQL tends to be rather portable between DBMSs,


more advanced SQL most definitely is not. As such, that book
could not cover advanced topics, such as triggers, cursors, stored
procedures, access control, transactions, and more, in any real
detail.

And that is where this book comes in. Sams Teach Yourself Oracle
PL/SQL in 10 Minutes builds on the proven tutorials and structure of Sams
Teach Yourself SQL in Ten Minutes, without getting bogged down with
anything but Oracle and PL/SQL. Starting with simple data retrieval and
working toward more complex topics, including the use of joins, sub-
queries, regular expressions, full text-based searches, stored procedures,
cursors, triggers, table constraints, and much more. Youll learn what you
need to know methodically, systematically, and simplyin highly focused
lessons designed to make you immediately and effortlessly productive.

Who Is This Book For?


This book is for you if
You are new to SQL.

You are just getting started with Oracle PL/SQL and want to hit
the ground running.
You want to quickly learn how to get the most out of Oracle and
PL/SQL.
You want to learn how to use Oracle in your own application
development.
You want to be productive quickly and easily using Oracle with-
out having to call someone for help.

It is worth noting that this book is not intended for all readers. If you are
an experienced SQL user, then you might find the content in this book to
be too elementary. However, if the preceding list describes you and your
needs relative to Oracle, youll find Sams Teach Yourself Oracle PL/SQL
in 10 Minutes to be the fastest and easiest way to get up to speed with
Oracle.
Conventions Used in This Book 3

Companion Website
This book has a companion website at forta.com/books/0672328666.
Visit the site to
Access table creation and population scripts for creating the
example tables used throughout this book
Visit the online support forum

Access online errata (if one might be required)

Find other books that might be of interest to you

Conventions Used in This Book


This book uses different typefaces to differentiate between code and regu-
lar English, and also to help you identify important concepts.
Text that you type and text that should appear on your screen appears in
monospace type. It looks like this to mimic the way text looks
on your screen.

Placeholders for variables and expressions appear in monospace italic


font. You should replace the placeholder with the specific value it repre-
sents.
This arrow () at the beginning of a line of code means that a single line
of code is too long to fit on the printed page. Continue typing all the char-
acters after the as if they were part of the preceding line.

NOTE
A Note presents interesting pieces of information related to the
surrounding discussion.

TIP
A Tip offers advice or teaches an easier way to do something.
4 Introduction

CAUTION
A Caution advises you about potential problems and helps you
steer clear of disaster.

New Term sidebars provide clear definitions of new, essential


terms.

Input
The Input icon identifies code that you can type in yourself. It usually
appears by a listing.

Output
The Output icon highlights the output produced by running Oracle
PL/SQL code. It usually appears after a listing.

Analysis
The Analysis icon alerts you to the authors line-by-line analysis of input
or output.
LESSON 4
Retrieving Data

In this lesson, youll learn how to use the SELECT statement to retrieve one
or more columns of data from a table.

The SELECT Statement


NOTE: Sample Tables Required
From this point on, all lessons use the sample database tables.
If you have yet to install these, please refer to Lesson 3,
Working with Oracle, before proceeding.

As explained in Lesson 1, Understanding SQL, SQL statements are


made up of plain English terms. These terms are called keywords, and
every SQL statement is made up of one or more keywords. The SQL state-
ment youll probably use most frequently is the SELECT statement. Its pur-
pose is to retrieve information from one or more tables.
To use SELECT to retrieve table data, you must, at a minimum, specify two
pieces of informationwhat you want to select, and from where you want
to select it.

Retrieving Individual Columns


Well start with a simple SQL SELECT statement, as follows:

Input
SELECT prod_name
FROM products;
34 LESSON 4: Retrieving Data

TIP: Type Then Execute


By now it should be obvious, but Ill remind you one last time.
Type the SQL code in the Oracle SQL Developer Worksheet
screen, and then click the Run Script button to execute it.
Results appear in a screen below the Worksheet. If you need
more room, you can drag and resize all the screens.

Analysis
The previous statement uses the SELECT statement to retrieve a single col-
umn called prod_name from the products table. The desired column name
is specified right after the SELECT keyword, and the FROM keyword speci-
fies the name of the table from which to retrieve the data. The following
shows the output from this statement:

Output
+----------------+
| prod_name |
+----------------+
| .5 ton anvil |
| 1 ton anvil |
| 2 ton anvil |
| Oil can |
| Fuses |
| Sling |
| TNT (1 stick) |
| TNT (5 sticks) |
| Bird seed |
| Carrots |
| Safe |
| Detonator |
| JetPack 1000 |
| JetPack 2000 |
+----------------+

NOTE: Unsorted Data


If you tried this query yourself, you might have discovered that the
data displayed in a different order than shown here. If this is the
case, dont worryit is working exactly as it is supposed to. If
Retrieving Individual Columns 35

query results are not explicitly sorted (well get to that in the next
lesson), data will be returned in no order of any significance. It
might be the order in which the data was added to the table, but
it might not. As long as your query returned the same number of
rows, then it is working.

A simple SELECT statement like the one just shown returns all the rows in a
table. Data is not filtered (so as to retrieve a subset of the results), nor is it
sorted. Well discuss these topics in the next few lessons.

NOTE: Terminating Statements


Multiple SQL statements must be separated by semicolons
(the ; character). Oracle (like most DBMSs) does not require that
a semicolon be specified after single statements. That said, most
SQL developers get in the habit of always terminating their SQL
statements with semicolons, even when they are not needed.

NOTE: SQL Statements and Case


Note that SQL statements are not case sensitive, so SELECT is
the same as select, which is the same as Select. Many SQL
developers find that using uppercase for all SQL keywords and
lowercase for column and table names makes code easier to
read and debug.
However, be aware that while the SQL language is not case
sensitive, identifiers (the names of databases, tables, and
columns) might be. As a best practice, pick a case convention,
and use it consistently.

TIP: Use of White Space


All extra white space within a SQL statement is ignored when that
statement is processed. You can specify SQL statements on one
long line or break them up over many lines. Most SQL developers
find that breaking up statements over multiple lines makes them
easier to read and debug.
36 LESSON 4: Retrieving Data

Retrieving Multiple Columns


To retrieve multiple columns from a table, you use the same SELECT state-
ment. The only difference is that you must specify multiple column names
after the SELECT keyword, and separate each column by a comma.

TIP: Take Care with Commas


When selecting multiple columns, be sure to specify a comma
between each column name, but not after the last column name.
Doing so generates an error.

The following SELECT statement retrieves three columns from the prod-
ucts table:

Input
SELECT prod_id, prod_name, prod_price
FROM products;

Analysis
Just as in the prior example, this statement uses the SELECT statement
to retrieve data from the products table. In this example, three column
names are specified, each separated by a comma. The output from this
statement is as follows:

Output
+---------+----------------+------------+
| prod_id | prod_name | prod_price |
+---------+----------------+------------+
| ANV01 | .5 ton anvil | 5.99 |
| ANV02 | 1 ton anvil | 9.99 |
| ANV03 | 2 ton anvil | 14.99 |
| OL1 | Oil can | 8.99 |
| FU1 | Fuses | 3.42 |
| SLING | Sling | 4.49 |
| TNT1 | TNT (1 stick) | 2.5 |
| TNT2 | TNT (5 sticks) | 10 |
| FB | Bird seed | 10 |
Retrieving All Columns 37

| FC | Carrots | 2.5 |
| SAFE | Safe | 50 |
| DTNTR | Detonator | 13 |
| JP1000 | JetPack 1000 | 35 |
| JP2000 | JetPack 2000 | 55 |
+---------+----------------+------------+

NOTE: Presentation of Data


SQL statements typically return raw, unformatted data. Data
formatting is a presentation issue, not a retrieval issue.
Therefore, presentation (for example, alignment and displaying
the price values as currency amounts with the currency symbol
and commas) is typically specified in the application that displays
the data. Actual raw retrieved data (without application-provided
formatting) is rarely displayed as is.

Retrieving All Columns


In addition to being able to specify desired columns (one or more, as
shown previously), you can also use SELECT statements to request all col-
umns without having to list them individually. This is done using the aster-
isk (*) wildcard character in lieu of actual column names, as follows:

Input
SELECT *
FROM products;

Analysis
When you specify a wildcard (*), all the columns in the table are returned.
The columns are in the order in which the columns appear in the table
definition. However, you cannot rely on this because changes to table
schemas (adding and removing columns, for example) could cause order-
ing changes.
38 LESSON 4: Retrieving Data

CAUTION: Using Wildcards


As a rule, you are better off not using the * wildcard unless
you really do need every column in the table. Even though use
of wildcards might save you the time and effort needed to list
the desired columns explicitly, retrieving unnecessary columns
usually slows down the performance of your retrieval and your
application.

TIP: Retrieving Unknown Columns


There is one big advantage to using wildcards. As you do not
explicitly specify column names (because the asterisk retrieves
every column), it is possible to retrieve columns whose names
are unknown.

Retrieving Distinct Rows


As you have seen, SELECT returns all matched rows. But what if you did
not want every occurrence of every value? For example, suppose you
wanted the vendor ID of all vendors with products in your products table:

Input
SELECT vend_id
FROM products;

Output
+---------+
| vend_id |
+---------+
| 1001 |
| 1001 |
| 1001 |
| 1002 |
| 1002 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
Retrieving Distinct Rows 39

| 1003 |
| 1003 |
| 1003 |
| 1005 |
| 1005 |
+---------+

The SELECT statement returned 14 rows (even though only 4 vendors are
in that list) because 14 products are listed in the products table. So how
could you retrieve a list of distinct values?
The solution is to use the DISTINCT keyword which, as its name implies,
instructs Oracle to only return distinct values:

Input
SELECT DISTINCT vend_id
FROM products;

Analysis
SELECT DISTINCT vend_id tells Oracle to only return distinct (unique)
vend_id rows, and so only 4 rows are returned, as shown in the follow-
ing output. If you use it, you must place the DISTINCT keyword directly in
front of the column names:

Output
+---------+
| vend_id |
+---------+
| 1001 |
| 1002 |
| 1003 |
| 1005 |
+---------+

CAUTION: Cant Be Partially DISTINCT


The DISTINCT keyword applies to all columns, not just the one
it precedes. If you were to specify SELECT DISTINCT vend_id,
prod_price, all rows would be retrieved unless both of the
specified columns were distinct.
40 LESSON 4: Retrieving Data

Using Fully Qualified Table Names


The SQL examples used thus far have referred to columns by just the col-
umn names. Referring to columns using fully qualified names (using both
the table and column names) is also possible. Look at this example:

Input
SELECT products.prod_name
FROM products;

This SQL statement is functionally identical to the first one used in this
lesson, but here a fully qualified column name is specified.
Table names, too, may be fully qualified, as shown here:

Input
SELECT products.prod_name
FROM crashcourse.products;

Once again, this statement is functionally identical to the one just used
(assuming, of course, that the products table is indeed in the crashcourse
database).
Situations exist where fully qualified names are required, as we will see in
later lessons. For now, it is worth noting this syntax so youll know what it
is if you run across it.

Using Comments
As you have seen, SQL statements are instructions that Oracle processes.
But what if you wanted to include text that you do not want processed and
executed? Why would you ever want to do this? Here are a few reasons:
The SQL statements weve been using here are all very short
and very simple. But, as your SQL statement grows (in length
and complexity), youll want to include descriptive comments
(for your own future reference or for whoever has to work on the
project next). You need to embed these comments in the
SQL scripts, but they are obviously not intended for Oracle
Using Comments 41

processing. (For an example of this, see the create.sql and


populate.sql files you used in Lesson 3.)

The same is true for headers at the top of a SQL file, perhaps
containing the programmer contact information and a descrip-
tion and notes. (You also see this use case in the create.sql and
populate.sql files.)

Another important use for comments is to temporarily stop SQL


code from being executed. If you were working with a long SQL
statement and wanted to test just part of it, you could comment
out some of the code so that Oracle saw it as comments and
ignored it.

Oracle supports two forms of comment syntax. Well start with inline
comments:

Input
SELECT prod_name -- this is a comment
FROM products;

Analysis
You may embed comments inline using -- (two hyphens). Anything after
the -- is considered comment text, making this a good option for describ-
ing columns in a CREATE TABLE statement, for example.
Here is another form of inline comment:

Input
-- This is a comment
SELECT prod_name
FROM products;

Analysis
A -- at the start of a line makes the entire line a comment. You can
see this format comment used in the accompanying create.sql and
populate.sql scripts.
42 LESSON 4: Retrieving Data

You can also create multi-line comments, and comments that stop and start
anywhere within the script:

Input
/* SELECT prod_name, vend_id
FROM products; */

SELECT prod_name
FROM products;

Analysis
/* starts a comment, and */ ends it. Anything between /* and */ is com-
ment text. This type of comment is often used to comment out code, as
shown in this example. Here, two SELECT statements are defined, but the
first wont execute because it has been commented out.

TIP: Oracle SQL Developer Color Coding


You might have noticed that Oracle SQL Developer color codes
your PL/SQL. SQL statements are usually displayed in blue,
identifiers (like table and column names) are in black, and so
on. Color coding makes it easier to read your code and to find
mistakes; if youve mistyped a PL/SQL statement, itll probably
appear in the wrong color. Oracle SQL Developer also color codes
any comments (inline or multi-line) and displays them in a light
gray. This makes it easy to locate comments and commented-out
code (and can also help you find code that you no longer want
commented out).

Summary
In this lesson, you learned how to use the SQL SELECT statement to
retrieve a single table column, multiple table columns, and all table col-
umns. You also learned about commenting and saw various ways that
you can use comments. In the next lesson, youll learn how to sort the
retrieved data.
This page intentionally left blank
Symbols fields, concatenating, 9596
table names, 153154
|| operator, 9394 alphabetical sort order, 4749
% (percent sign), 70 ALTER TABLE, 190193
; (semicolon), 35 ALTER USER, 244
_ (underscore), 7273 anchors, regular expressions
* (wildcard), 37 (PL/SQL), 8788
AND keyword, 58
AND operator, combining WHERE
A clause, 6162
Application Express, 25
ABS() function, 107 applications, filtering query results,
access control, 239240 52
access rights, setting, 242243 ASC keyword, query results sort
passwords, 244 order, 49
user accounts, 241 AS keyword, 9596
users, 240241 auto increment, 174
access rights, setting, 242243 AVG() function, 110111
Add_Month(), 103 DISTINCT argument, 117
advantages
of IN operator, 67
of SQL, 11 B
AFTER trigger, 225, 228
aggregate functions, 109110 basic character matching, regular
ALL argument, 117 expressions (PL/SQL), 7679
AVG(), 110111 basic syntax, stored procedures,
combining, 118119 208209
COUNT(), 112113 BEFORE triggers, 225, 228
defined, 109 best practices
DISTINCT argument, 117 joins, 161
distinct values, 117118 primary keys, 10
joins, 160161 BETWEEN keyword, 58
MAX(), 113114 BETWEEN operator (WHERE
MIN(), 114115 clause), 57
naming aliases, 119 breaking up, data, 8
overview, 109
SUM(), 115116 C
aliases
alternative uses, 96 calculated fields, 9192
concatenating fields, 9596 concatenating fields, 9294
creating, 153 column aliases, 9596
260 calculated fields

mathematical calculations, INSERT SELECT statements,


9698 177
overview, 9192 INSERT statement and, 173
subqueries, 136139 INSERT statement, omitting
views, 202203 columns, 175
calculated values, totaling, 116 multiple, sorting query results
calculations, testing, 98 by, 4546
cartesian product, 145 NULL value columns, 187189
case insensitive equality omitting, 175
comparisons, 55 padded spaces, RTrim() func-
case sensitivity, 71 tion, 9495
query result sort order, 49 primary keys, 910
SQL statements, 35 retrieving, 3337
character classes, matching, 8485 all columns, 3738
characters separating names in queries, 36
% (percent sign) wildcard, sorting data
7071 descending on multiple
_ (underscore) wildcard, 72 columns, 49
clauses, 44 multiple columns, 4547
GROUP BY clause, 122123, subquery result restrictions, 135
126128 updating multiple, 181
HAVING clause, 124125 values, deleting, 181
ORDER BY clause, 4546, 52, combined queries, 163
126128 creating, 164166
positioning, 53, 59 duplicate rows and, 167168
SELECT clause, ordering, 129 including/eliminating duplicate
WHERE clause. See WHERE rows, 167
clause overview, 163
client-based results formatting, 92 rules, 166
clients, 14 sorting results, 168169
client-server software, Oracle, 1315 combining
client tools, Oracle, 1516 aggregate functions, 118119
CLOSE statement, 217 WHERE clause, 61
closing cursors, 217218 AND operator, 6162
color coding, Oracle SQL Developer, order of evaluation, 6365
42 OR operator, 6263
column aliases comments, 4042
alternative uses, 96 COMMIT statement (transaction pro-
concatenating fields, 9596 cessing), 236237
columns, 78, 92. See also fields commits (transaction processing),
concepts, 78 defined, 235
derived columns, 96 complex joins, views, 198199
fully qualified names, 145
GROUP BY clause, 123
individual columns, 111
data insertion 261

concatenating, 93 D
fields, 9295
aliases, 9596 data
column aliases, 9596 breaking correctly (columns), 8
mathematical calculations, breaking up, 8
9698 cursor data, 220222
connecting Oracle SQL Developer to fetching, 218220
Oracle servers, 2526 deleting, 181182
constructs, programming constructs guidelines, 183
(stored procedures), 209210 filtering
controlling transactions, 235 IN operator, 6567
COMMIT, 236237 NOT operator, 6768
ROLLBACK, 236 WHERE clause, 5153
SAVEPOINT, 237238 inserting, 171
correlated subqueries, 138 complete rows, 172176
COS() function, 107 retrieved data, 176177
COUNT() function, 110113 retrieving
DISTINCT argument, 118 all columns, 3738
joins and, 160 distinct rows, 3839
COUNT* subquery, 136 individual columns, 3335
CREATE OR REPLACE TRIGGER, multiple columns, 3637
227 sorting, 4345
CREATE PROCEDURE, 208 by multiple columns, 4547
create.sql, 30 by nonselected columns, 45
CREATE TABLE, 185187 specifying sort direction,
DEFAULT keyword, 189190 4749
CREATE TRIGGER, 224 updating, 179
CREATE USER statement, 241 guidelines, 183
CREATE VIEW statement, 197 database management systems. See
cursor data, 220222 DBMS (Database Management
fetching, 218220 System)
cursors, 215 databases, 58. See also tables
closing, 217218 columns, 78
creating, 216217 concepts, 56
data, fetching, 218220 datatypes, 78
explicit cursors, 216 defined, 6
implementing, 216 primary keys, 910
implicit cursors, 216 rows, 89
opening, 217218 schemas, 7
overview, 215 database servers, 14
customers table, 247 data grouping, 121122
custom workspaces, creating, 2425 data insertion, 171
inserting
complete rows, 172176
retrieved data, 176177
262 data insertion

views, 204 E
WHERE clause, 202
date and time functions, 100, empty strings, 189
103107 equality operator (WHERE clause),
dates, 104 53, 243
DBMS (Database Management equijoins, 148
System), 1, 6 escaping, 72
interactive tools, 143 ETrim() function, 95
Oracle, 13 example tables, 2829
query sort order, 44 creating, 3031
decimal rounding, 52 obtaining table scripts, 2830
DECLARE statement, 219 execute, 28
cursors, creating, 216217 executing stored procedures, 209
dedicated Oracle instances, 2224 EXP() function, 107
default system instance, 22 explicit commits, 236
default values, tables, 189190 explicit cursors, 216
DEFAULT values, 190 Extract(), 103
DELETE FROM, 182
DELETE statement, 181182
guidelines, 183 F
WHERE clause, 182
DELETE triggers, 228230 FETCH, 218220
deleting fetching cursor data, 218220
column values, 181 fields, 92. See also columns
data, 181183 calculated fields, 9192
tables, 193 concatenating fields, 9296
user accounts, 241 mathematical calculations,
derived columns, 96 9698
DESC keyword, 4749 overview, 9192
query results sort order, 4749 performing mathematical
dictionary sort order (query results), calculations, 9698
49 subqueries, 136139
DISTINCT argument, AVG() func- views, 202203
tion, 117 concatenating, 9295
DISTINCT keyword, 39 aliases, 9596
distinct rows, retrieving, 3839 filter condition, 51
distinct values, aggregate filtering
functions, 117118 AND operator, 6162
DROP command, 213 application level, 52
dropping data
stored procedures, 213 IN operator, 6567
triggers, 225 NOT operator, 6768
DROP TABLE statement, 193 WHERE clause, 5153
DROP TRIGGER, 225 by date, 104
DROP USER statement, 241 groups, 123126
duplicate rows, including/
eliminating, 167
INSERT statement 263

IN operator, 6567 text manipulation functions,


multiple criteria, 61 100102
NOT operator, 6768 types of, 100
order of evaluation, 6365
OR operator, 6263
by subqueries, 131135
G
views, unwanted data, 201202 GRANT, 242243
filters greater than operator (WHERE
% (percent sign) wildcard, clause), 53
7071 greater than or equal to operator
_ (underscore) wildcard, 7273 (WHERE clause), 53
foreign keys, 142 GROUP BY clause, 122123,
ALTER TABLE, 192 126128
formatting grouping versus sorting, 126128
retrieved data with views, grouping data, 121122
199200 filtering groups, 123126
serverbased compared to GROUP BY clause, 122123
clientbased, 92 nested groups, 123
statements, 187 groups
subqueries, 134 creating, 122123
four-digit years, 104 filtering, 123126
FROM clause nested groups, 123
creating joins, 144 guidelines for updating/deleting
subqueries, 139 data, 183
FROM keyword, 34
fully qualified column names, 145
fully qualified table names, 40 H
functions, 99, 207
aggregate functions, 109110 HAVING clause, 124125
AVG() function, 110111 grouping data, 124
combining, 118119
COUNT() function, I
112113
distinct values, 117118 implicit commit, 236
joins, 160161 implicit cursors, 216
MAX() function, 113114 IN keyword, 67
MIN() function, 114115 inner joins, 148149
SUM() function, 115116 IN operator, 6567
date and time functions, 100, INSERT, 171175
103107 inserting data, 171
defined, 99 complete rows, 172176
numeric functions, 100, 107 retrieved data, 176177
RTrim( ), 9495 INSERT SELECT, 176177
system functions, 100 INSERT statement
text functions, 100 columns lists, 175
completing rows, 172174
264 INSERT statement

omitting columns, 175 ASC, query results sort order, 49


overview, 171 BETWEEN, 58
query data, 176177 DEFAULT, table values,
security privileges, 171 189190
VALUES, 175 DESC, 4749
INSERT trigger, 225228 DISTINCT, 39
installing Oracle, 1920 FROM, 34
instances IN, 67
dedicated Oracle instances, NOT, 67
creating, 22 OR, 63
default system instance, 22
matching multiple instances,
regular expressions, 8587
L
languages, SQL, 11
J Last_Day(), 103
left outer join, 160
joining multiple tables, 149151 Length(), 101
joins, 141 less than operator (WHERE clause),
aggregate functions, 160161 53
best practices, 161 less than or equal to operator
complex joins, views, 198199 (WHERE clause), 53
creating, 144145 LIKE operator, 69, 88
cross joins, 148 searching with
equijoins, 148 percent sign (%), 70
inner joins, 148149 underscore (_), 7273
left outer join, 160 LOOP statement, 222
natural joins, 157158 Lower() function, 101
outer joins, 158160 LTrim() function, 95, 101
performance, 150
reasons for using, 143
right outer join, 160
M
self joins, 154157 Mac users, Oracle, 17
versus subqueries, 157 matching character classes, regular
views, 198199 expressions (PL/SQL), 8485
WHERE clause, 145148 matching multiple instances, regular
expressions (PL/SQL), 8587
K matching one of several characters,
regular expressions (PL/SQL),
keys 8081
foreign keys, 142 matching ranges, regular expressions
ALTER TABLE, 192 (PL/SQL), 8283
primary keys, 910, 142 matching special characters, regular
keywords, 33, 255257 expressions (PL/SQL), 8384
AND, 62 mathematical calculations, perform-
AS, 9596 ing in fields, 9698
Oracle SQL Developer 265

mathematical operators, 98 O
MAX() function, 110, 113114
nonnumeric data, 114 omitting columns, 175
NULL values, 114 opening cursors, 217218
MIN() function, 110, 114115 OPEN statement, 217
DISTINCT argument, 118 operators
NULL values, 115 defined, 61
Months_Between(), 103 grouping related, 64
multi-event triggers, 231 HAVING clause, 124
multiple columns IN operator, 6567
retrieving, 3637 LIKE operator, 69
sorting data, 4547 mathematical operators, 98
multiple instances, matching regular NOT operator, 6768
expressions, 8587 || operator, 9394
multiple tables, joining, 149151 WHERE clause, 53
multiple worksheets, 28 checking against a single
value, 5456
N checking for nonmatches,
5657
names checking for no value,
fully qualified column 5859
names, 145 checking for range of
fully qualified table names, 40 values, 5758
natural joins, 157158 Oracle, 13
navigating tables, cursors, 215 clientserver software, 1315
nested groups, 123 client tools, 1516
Next_Day(), 103 installing, 1920
nonequality operator (WHERE Mac users, 17
clause), 53, 243 PL/SQL, 15
NOT NULL, 189 savepoints, 237
NOT operator, 6768 setting up
NULL, 59 installing software, 1920
NULL keyword, updating columns, obtaining software, 1819
181 required software, 1618
NULL values, 187189 Oracle Database Express Edition, 18
AVG() function, 110111 Oracle Express Edition, creating
compared to empty strings, 189 custom workspaces, 2425
COUNT() function, 113 Oracle servers connecting to Oracle
empty strings, 189 SQL Developer, 2526
table columns, 187189 Oracle SQL Developer, 19, 32
numeric datatypes, 253254 color coding, 42
numeric functions, 100, 107 connecting to Oracle servers,
2526
overview, 2728
266 ORDER BY clause

ORDER BY clause, 4546, 52, matching multiple


126128 instances, 8587
ascending/descending sort order, matching one of several
4749 characters, 8081
compared to GROUP BY clause, matching ranges, 8283
126128 matching special charac-
SELECT statement, 44 ters, 8384
sorting by multiple columns, OR matches, 7980
4546 populate.sql, 30
views, 197 populating tables, 3132
ordering portability, 99
SELECT clause, 129 INSERT statements and, 175
sequence number, 47 predicates (operators), 70
orderitems table, 248249 primary keys, 910, 142
order of evaluation, combining best practices, 10
(WHERE clause), 6365 concepts, 910
orders table, 248 Customer example table, 248
OR matches, regular expressions (PL/ importance, 9
SQL), 7980 OrderItems example table, 249
OR operator, combining (WHERE Orders example table, 248
clause), 6263 Products example table, 247,
outer joins, 158160 250
updating tables, 191192
Vendors example table, 247
P privileges, 242243
parentheses, WHERE clause, 65 Procedural Language/Structured
passwords, access control, 244 Query Language. See PL/SQL
percent sign (%), wildcard searches, processing
70 subqueries, 133
performance, 151 transactions. See transaction
joins, 150 processing
subqueries, 136 productnotes table, 249250
views, 197 products table, 247
performing mathematical calcula- programming constructs, stored pro-
tions, calculated fields, 9698 cedures, 209210
PI() function, 107
placeholders, 235238 Q
PL/SQL (Procedural Language/
Structured Query Language), 15 queries, 131
regular expressions, 76 calculated fields, 136139
anchors, 8788 concatenating fields, 9296
basic character matching, mathematical calculations,
7679 9698
matching character classes, overview, 9192
8485
RTrim() function 267

combined queries, 163 matching one of several


creating, 164166 characters, 8081
including/eliminating matching ranges, 8283
duplicate rows, 167 matching special charac-
sorting results, 168169 ters, 8384
combining, 133 OR matches, 7980
data formatting, 37 relational databases, sort order and,
defined, 131 44
filtering by, 131134 relational tables, 141143
INSERT statement and, 176177 removing views, 197
multiple WHERE clauses, 166 renaming tables, 193
overview, 131 REPEAT UNTIL statement, 222
sorting results, 4344 repetition metacharacters, 85
ascending/descending REPLACE PROCEDURE, 208
order, 4749 reserved words, 255
case sensitivity, 49 restrictions, views, 197
by multiple columns, 4546 result sets, 215
nonselected columns and, retrieved data
45 inserting, 176177
subqueries, 139 reformatting with views,
table aliases, 154 199201
views, 195 retrieving data
wildcards (*), 3738 all columns, 3738
quotes, WHERE clause, 57 distinct rows, 3839
individual columns, 3335
multiple columns, 3637
R reusable views, 199
records, compared to rows, 9 REVOKE statement, 243244
referential integrity, 143 RIGHT keyword (outer joins), 159
reformatting retrieved data with right outer join, 160
views, 199201 ROLLBACK command (transaction
REGEXP_INSTR(), 76 processing), 236
REGEXP_LIKE(), 76, 88 rollbacks (transaction processing)
REGEXP_REPLACE(), 76 COMMIT statement, 237
REGEXP_SUBSTR(), 76 defined, 235
regular expressions, 75 ROLLBACK command, 236
PL/SQL, 76 savepoints and, 237238
anchors, 8788 statements, 238
basic character matching, rows, 89
7679 cursors, 215
matching character classes, duplicate rows, 167
8485 inserting complete rows,
matching multiple 172176
instances, 8587 INSERT statement, 172174
retrieving distinct rows, 3839
RTrim() function, 9495, 99101
268 rules

rules individual columns, 3335


UNION, 166 multiple columns, 3637
views, 197 unknown, 38
run scripts versus run statements, 28 subqueries, 133134
run statements, 28 WHERE clause, 51
self joins, 154157
semicolons (;), 35
S sequence number, ordering by, 47
sample tables, 245 sequence (SELECT statement
customers table, 247 clauses), 129
orderitems table, 248249 server-based results formatting
orders table, 248 compared to client-based, 92
productnotes table, 249250 servers, 14
products table, 247 SET command, updating tables, 180
vendors table, 246 SIN() function, 107
savepoints, transaction software, obtaining for Oracle setup,
processing, 235238 1819
scalablity, 143 sort direction, specifying, 4749
scale, 143 sorting
schemas, 7 combined query results,
schemata, 7 168169
search criteria, 51 data, 4345
search patterns, 69 by multiple columns, 4547
security by nonselected columns, 45
access control, 239240 specifying sort direction,
deleting user accounts, 241 4749
passwords, 244 versus grouping, 126128
setting access rights, query results, 4344
242243 ascending/descending
user accounts, 241 order, 4749
users, 240241 case sensitivity, 49
UPDATE statement, 179, 182 by multiple columns, 4546
SELECT clause, ordering, 129 nonselected columns and,
SELECT statement, 33 45
AS keyword, 9596 SOUNDEX() function 101102
AVG() function, 111 spaces, removing (RTrim function),
combined queries, 163 9495
combining, 61 special characters, 69
concatenating fields, 94 matching with regular expres-
COUNT() function, 113 sions, 8384
IS NULL clause, 58 SQL, 1011, 15
ORDER BY clause, 44 advantages of, 11
retrieving deleting/updating data, 183
all columns, 3738 overview, 10
distinct rows, 38 PL/SQL, 15
tables 269

SQL statements, 30, 33 subqueries, 131


case sensitivity, 35 as calculated fields, 136139
comments, 4042 building queries, 139
terminating, 35 correlated subqueries, 138
white space, 35 filtering by, 131135
SQRT() function, 107 FROM clause, 139
statements maximum amount of, 135
ALTER TABLE, 190 SELECT statements, 133134
clauses, 44 self joins and, 155157
COMMIT, 237 UPDATE statement, 181
CREATE TABLE, 185186 WHERE clause, 135
CREATE VIEW, 197 SUM() function, 110, 115116
DELETE, 181183 multiple columns, 116
DROP TABLE, 193 NULL values, 116
formatting, 187 syntax, stored procedures, 208209
grouping related operators, 64 Sysdate(), 103
INSERT. See INSERT statement SYS logins, 240
rollbacks, 238 system functions, 100
defined, 235 SYSTEM login, 240
SELECT. See SELECT state-
ment
stored procedures
T
disadvantages of, 207 table aliases, 153154
overview, 205206 table rights, 243
usefulness of, 206 tables, 67
UPDATE, 179183 calculated fields
stored procedures, 205208 concatenating fields, 9296
basic syntax, 208209 mathematical calculations,
building intelligent stored proce- 9698
dures, 210213 overview, 9192
creating, 208 Cartesian Product, 145
disadvantages of, 207 concepts, 67
dropping, 213 creating, 144, 185187
executing, 209 CREATE TABLE, 186
overview, 205206 default values, 189190
programming constructs, NULL values, 187189
209210 overview, 185
reasons for using, 206207 customers table, 247
usefulness of, 206 default values, 189190
strings. See also text functions deleting, 193
datatypes, 252253 example tables, 2829
empty, compared to NULL creating, 3031
values, 189 obtaining table scripts,
wildcard searching and, 70 2830
populating, 3132
270 tables

functions of, 28, 245 transactions


inner joins, 148 controlling, 235
inserting data, 172174 COMMIT, 236237
from queries, 176177 ROLLBACK, 236
joining multiple tables, 149151 SAVEPOINT, 237238
multiple tables, 149151 defined, 235
naming, 7 triggers, 223224
NULL value, checking for, 58 AFTER trigger, 228
NULL value columns, 187189 BEFORE triggers, 228
orderitems table, 248 creating, 224225
orders table, 248 DELETE triggers, 228230
overview, 141 dropping, 225
performance considerations, 150 INSERT trigger, 225228
products table, 247 multi-event triggers, 231
relational tables, 141143 overview, 232
renaming, 193 UPDATE triggers, 230231
rows, 8 Trim() function, 95
sample tables, 245 trimming padded spaces, 9495
updating, 179181, 190191
deleting data, 181182
primary keys, 191192
U
usefulness of, 143 underscore (_), 7273
vendors table, 246 UNION
virtual. See views creating combined queries,
WHERE clause, 145, 148 164166
table scripts, obtaining, 2930 duplicate rows, 167
Tan() function, 107 rules for, 166
terminating SQL statements, 35 sorting combined query results,
testing calculations, 98 168169
text functions, 100101 versus WHERE clause, 168
list of common, 101103 unions, 163. See also combined
text manipulation functions, 100102 queries
To_Date(), 103 unknown, 59
tools, client tools (Oracle), 1516 unsorted data, query results, 34
trailing spaces, 72 UPDATE, 179181
transaction processing, 233235, guidelines, 183
237238 security privileges, 179, 182
COMMIT command, 237 subqueries, 181
explicit commits, 236 UPDATE triggers, 230231
managing, 235 updating
overview, 233234 data, 179
ROLLBACK command, 236 guidelines, 183
terminology, 235 table data, 179181
deleting data, 181182
writing stored procedures 271

tables, 190191 checking for range of values,


primary keys, 191192 5758
views, 198, 203204 combining, 61
Upper() function, 101 AND operator, 6162
user accounts, 241 order of evaluation, 6365
users, access control, 240241 OR operator, 6263
in queries, 163
data retrieval, 202
V DELETE statements, 182
values filtering data, 124
concatenation, 93 filtering groups, 125
trimming padded space, 95 joins, 145148
VALUES, 175 operators, 53
vendors table, 246 checking against a single
views, 195196 value, 5456
calculated fields, 202203 checking for nonmatches,
complex joins, 198199 5657
creating, 197 checking for no value,
data retrieval, 204 5859
filtering data, 201202 checking for range of val-
joins, simplifying, 198199 ues, 5758
ORDER BY clause, 197 parentheses, 65
overview, 195 quotes, 57
performance, 197 Soundex() function, 102
reasons for using, 196 subqueries, 134135
reformatting retrieved data, UPDATE statements, 179180
199201 versus UNION, 168
removing, 197 wildcards, 69
restrictions, 197 white space, SQL statements, 35
reusable, 199 wildcard (*), 37
rules, 197 wildcards, 3738, 69
updating, 198, 203204 natural joins, 158
usefulness of, 196 wildcard searches
virtual tables. See views LIKE operators, 69
percent sign (%), 70
tips for, 74
WXYZ trailing spaces, 72
underscore (_), 7273
websites, example table download working environments, 21
site, 29 creating
WHERE clause, 5153. See also custom workspaces, 2425
HAVING clause dedicated Oracle instances,
checking against single value, 54 2224
checking for nonmatches, 5657 worksheets, multiple worksheets, 28
checking for NULL value, 58 writing stored procedures, 207

Das könnte Ihnen auch gefallen