Oracle Fundamentals
Oracle Fundamentals
Oracle Fundamentals
le
b
a
r
e
f
an
r
t
n
a
h
e
n
no
a
s
a
h
Oracle Database
10g: SQL
)
e
m
co GuidI
Fundamentals
o
aho dent Volume 2
y
Student Guide
@ Stu
8
4
s this
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
D17108GC30
Edition 3.0
January 2009
D57871
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Salome Clement
Chaitanya Koratamaddi
Nancy Greenberg
Disclaimer
Technical Contributors
and Reviewers
Wayne Abbott
Christian Bauwens
Claire Bennett
Perry Benson
Brian Boxx
Zarko Cesljas
Dairy Chan
Laszlo Czinkoczki
Joel Goodman
Matthew Gregory
Sushma Jagannath
Yash Jain
Angelika Krupp
Isabelle Marchand
Malika Marghadi
Valli Pataballa
Narayanan Radhakrishnan
Bryan Roberts
Helen Robertson
Lata Shivaprasad
John Soltani
James Spiller
Priya Vennapusa
a
h
e
n
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Trademark Notice
Editors
Arijit Ghosh
Raj Kumar
Graphic Designer
Rajiv Chandrabhanu
Publisher
Giri Venugopal
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Contents
Preface
I
Introduction
Lesson Objectives I-2
Goals of the Course I-3
Oracle10g I-4
Oracle Database 10g I-6
Oracle Application Server 10g I-7
Oracle Enterprise Manager 10g Grid Control I-8
Relational and Object Relational Database Management Systems I-9
Oracle Internet Platform I-10
System Development Life Cycle I-11
Data Storage on Different Media I-13
Relational Database Concept I-14
Definition of a Relational Database I-15
Data Models I-16
Entity Relationship Model I-17
Entity Relationship Modeling Conventions I-19
Relating Multiple Tables I-21
Relational Database Terminology I-23
Relational Database Properties I-25
Communicating with an RDBMS Using SQL I-26
Oracles Relational Database Management System I-27
SQL Statements I-28
Tables Used in the Course I-29
Summary I-30
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
iii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
a
h
e
n
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Restricting and Sorting Data
Objectives 2-2
Limiting Rows Using a Selection 2-3
Limiting the Rows That Are Selected 2-4
Using the WHERE Clause 2-5
Character Strings and Dates 2-6
Comparison Conditions 2-7
Using Comparison Conditions 2-8
Using the BETWEEN Condition 2-9
Using the IN Condition 2-10
Using the LIKE Condition 2-11
Using the NULL Conditions 2-13
Logical Conditions 2-14
iv
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
a
h
e
n
b
a
r
e
f
an
r
t
n
an
s
ha
)
3 Using Single-Row Functions to Customize Output
om uide
c
Objectives 3-2
oo nt G
h
a
SQL Functions 3-3
y tude
@
Two Types of SQL Functions
48 3-4his S
s
c
Single-Row Functions
a 3-5se t
h
e
Character Functions
n to3-7u
s
(
Case-Manipulation
va nse Functions 3-9
a
t
sUsing Case-Manipulation
Functions 3-10
ce
a
i
l
v
i
r
Character-Manipulation Functions 3-11
S
Using the Character-Manipulation Functions 3-12
Number Functions 3-13
Using the ROUND Function 3-14
Using the TRUNC Function 3-15
Using the MOD Function 3-16
Working with Dates 3-17
Arithmetic with Dates 3-20
Using Arithmetic Operators with Dates 3-21
Date Functions 3-22
Using Date Functions 3-23
Practice 3: Overview of Part 1 3-25
Conversion Functions 3-26
Implicit Data Type Conversion 3-27
Explicit Data Type Conversion 3-29
Using the TO_CHAR Function with Dates 3-32
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
an
r
t
n
an
s
ha
Using the CASE Expression 3-56
)
om uide
DECODE Function 3-57
c
oo nt G
Using the DECODE Function 3-58
h
a
y tude
Summary 3-60
@
48 his S
Practice 3: Overview of Part 2cs
3-61
a se t
h
e
u the Group Functions
n DatatoUsing
s
4 Reporting Aggregated
(
v4-2a nse
Objectives
a
t
e
s licFunctions?
a
What
Are Group
4-3
v
i
r
S
a Types of Group Functions 4-4
eh
n
S
b
a
r
e
f
vi
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
a
h
e
n
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
o
G
o5-14
t
h
n
a
Retrieving Records with the ON Clause
y tude
@
Self-Joins Using the ON Clause
5-15
48 his S
s
c
Applying AdditionalaConditions to
t a Join 5-17
e
h
s
e
Creating Three-Way
u with the ON Clause 5-18
n tJoins
s
o
(
Nonequijoins
va n5-19
se
a
t
e
icRecords with Nonequijoins 5-20
l
vasRetrieving
i
r
Outer Joins 5-21
S
INNER Versus OUTER Joins 5-22
vii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
an
r
t
n
n
a
s
7 Using the Set Operators
a
h
)
Objectives 7-2
e
m
d
o
i
c Gu
Set Operators 7-3
o
o
Tables Used in This Lesson 7-4
ah dent
y
@ Stu
UNION Operator 7-8
8
4
s this
c
Using the UNION Operator 7-9
ha use
e
UNION ALL Operator
7-11
n
(sALL Operator
to 7-12
Using the UNION
a
e
avOperator
ns7-13
t
e
s
INTERSECT
c
li
rivathe INTERSECT
Using
Operator 7-14
S
a
eh
n
S
b
a
r
e
f
Manipulating Data
Objectives 8-2
Data Manipulation Language 8-3
Adding a New Row to a Table 8-4
INSERT Statement Syntax 8-5
Inserting New Rows 8-6
viii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
an
r
t
n
n
a
s
Using a Subquery in an INSERT Statement 8-22
a
h
)
a
e
s or ROLLBACK 8-31
State of the Data
ehBeforeuCOMMIT
n
s
o
(
t
State of
athe Data
eAfter COMMIT 8-32
v
s
a
n
t
sCommitting
ceData 8-33
a
i
l
v
i
Sr State of the Data After ROLLBACK 8-34
TRUNCATE Statement 8-21
a
h
e
n
le
b
a
r
e
f
ix
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
10 Creating Other Schema Objects
y tude
@
Objectives 10-2
48 his S
s
c
t
Database Objects 10-3 a
e
h
s
e
What Is a View? s
10-4
n to u
(
e
Advantagesv
ofaViews s
10-5
a
n
t
e
Simple
asViews and
lic Complex Views 10-6
v
i
r
a SCreating a View 10-7
eh
n
S
b
a
r
e
f
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
a
h
e
n
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
t
a
h
View Information
11-15use
e
n to 11-16
s
(
Sequence
Information
va Information
se 11-18
a
n
Synonym
t
e
ic
vasAdding lComments
i
to a Table 11-19
r
S
Summary 11-20
Practice 11: Overview 11-21
Practice Solutions
xi
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Additional Practices
Additional Practices: Table Descriptions and Data
Additional Practices: Solutions
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
xii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Objectives
After completing this lesson, you should be able to do the
following:
Categorize the main database objects
Review the table structure
List the data types that are available for columns
Create a simple table
ble
a
r
Explain how constraints are created at the time of table sfe
an
r
creation
t
on
n
Describe how schema objects work
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Objectives
riva
S
Inathis lesson, you are introduced to the data definition language (DDL) statements. You are
h
e
n taught the basics of how to create simple tables, alter them, and remove them. The data types
available in DDL are shown, and schema concepts are introduced. Constraints are tied into this
lesson. Exception messages that are generated from violating constraints during DML are shown
and explained.
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-2
Database Objects
Object
Description
Table
View
Sequence
Index
Synonym
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
Database
rivObjects
S
AnaOracle Database can contain multiple data structures. Each structure should be outlined in
h
e
n
the database design so that it can be created during the build stage of database development.
Table: Stores data
View: Subset of data from one or more tables
Sequence: Generates numeric values
Index: Improves the performance of some queries
Synonym: Gives alternative names to objects
Oracle Table Structures
Tables can be created at any time, even while users are using the database.
You do not need to specify the size of a table. The size is ultimately defined by the amount
of space allocated to the database as a whole. It is important, however, to estimate how
much space a table will use over time.
Table structure can be modified online.
Note: More database objects are available but are not covered in this course.
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-3
Naming Rules
Table names and column names:
Must begin with a letter
Must be 130 characters long
Must contain only AZ, az, 09, _, $, and #
Must not duplicate the name of another object owned by
the same user
ble
a
r
Must not be an Oracle serverreserved word
sfe
a
r
t
on
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
NamingrRules
iva
S
a name database tables and columns according to the standard rules for naming any Oracle
You
h
e
n
Database object:
Table names and column names must begin with a letter and be 130 characters long.
Names must contain only the characters AZ, az, 09, _ (underscore), $, and # (legal
characters, but their use is discouraged).
Names must not duplicate the name of another object owned by the same Oracle server
user.
Names must not be an Oracle serverreserved word.
Naming Guidelines
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-4
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
CREATE iTABLE
li
r va Statement
S
You
a create tables to store data by executing the SQL CREATE TABLE statement. This
h
e
n
statement is one of the DDL statements, which are a subset of SQL statements used to create,
modify, or remove Oracle Database structures. These statements have an immediate effect on
the database, and they also record information in the data dictionary.
To create a table, a user must have the CREATE TABLE privilege and a storage area in which to
create objects. The database administrator uses data control language statements to grant
privileges to users (DCL statements are covered in a later lesson).
In the syntax:
schema
Is the same as the owners name
table
is the name of the table
DEFAULT expr
Specifies a default value if a value is omitted in the INSERT
statement
column
Is the name of the column
datatype
Is the columns data type and length
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-5
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
USERA
USERB
)
e
m
d
o
i
SELECT *
SELECT
c *Gu
o
o
FROM userB.employees;
FROM
nt
ah deuserA.employees;
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Users Tables
Referencing
riva Another
S
Aa
schema is a collection of objects. Schema objects are the logical structures that directly refer
h
e
n
to the data in a database. Schema objects include tables, views, synonyms, sequences, stored
procedures, indexes, clusters, and database links.
If a table does not belong to the user, the owners name must be prefixed to the table. For
example, if there are schemas named USERA and USERB, and both have an EMPLOYEES table,
then if USERA wants to access the EMPLOYEES table that belongs to USERB, he or she must
prefix the table name with the schema name:
SELECT *
FROM
userb.employees;
If USERB wants to access the EMPLOYEES table that is owned by USERA, he must prefix the
table name with the schema name:
SELECT *
FROM
usera.employees;
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-6
DEFAULT Option
on
n
a
CREATE TABLE hire_dates
s
a
(id
NUMBER(8),
) h e
m
hire_date DATE DEFAULTco
SYSDATE);
id
u
o
G
CREATE TABLE succeeded.
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
DEFAULTiv
li
r Option
S
a you define a table, you can specify that a column be given a default value by using the
When
h
e
n DEFAULT option. This option prevents null values from entering the columns if a row is
inserted without a value for the column. The default value can be a literal, an expression, or a
SQL function (such as SYSDATE or USER), but the value cannot be the name of another column
or a pseudocolumn (such as NEXTVAL or CURRVAL). The default expression must match the
data type of the column.
Note: CURRVAL and NEXTVAL are explained later in this lesson.
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-7
Creating Tables
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
CreatingriTables
va
S
The
a example in the slide creates the DEPT table, with four columns: DEPTNO, DNAME, LOC,
h
e
n
and CREATE_DATE. The CREATE_DATE column has a default value. If a value is not provided
for an INSERT statement, the system date is automatically inserted.
It further confirms the creation of the table by issuing the DESCRIBE command.
Because creating a table is a DDL statement, an automatic commit takes place when this
statement is executed.
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-8
Data Types
Data Type
Description
NUMBER(p,s)
DATE
LONG
CLOB
le
b
a
r
e
f
an
r
t
n
no
a
s
BLOB
Binary data (up to 4 GB)
a
h
)file (up eto4 GB)
m
BFILE
Binary data stored in an external
o
c Guid
o
o representing
t
ROWID
A base-64 number h
system
the unique
n
a
e
y
d
address of a@
row in its table
tu
8
S
4
s
cs thi
a
h use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Data Types
riva
S
a you identify a column for a table, you need to provide a data type for the column. There
When
h
e
n are several data types available:
Data Type
Description
VARCHAR2(size)
CHAR [(size)]
NUMBER [(p,s)]
DATE
LONG
CLOB
Unauthorized reproductionOracle
or distribution
2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Copyright
Fundamentals
I 9 Oracle
-9
D escription
RAW(size)
LONG RAW
BLOB
BFILE
ROWID
Guidelines
A LONG column is not copied when a table is created using a subquery.
A LONG column cannot be included in a GROUP BY or an ORDER BY clause.
ble
a
Only one LONG column can be used per table.
r
fe
s
No constraints can be defined on a LONG column.
n
ra
You might want to use a CLOB column rather than a LONG column. n-t
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
10
Description
TIMESTAMP
INTERVAL YEAR TO
MONTH
INTERVAL DAY TO
SECOND
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Types
Other Datetime
riva Data
S
aData Type
h
Description
e
n
TIMESTAMP
INTERVAL YEAR TO
MONTH
INTERVAL DAY TO
SECOND
Note: These datetime data types are available with Oracle9i and later releases. For detailed
information about the datetime data types, see the topics TIMESTAMP Datatype, INTERVAL
YEAR TO MONTH Datatype, and INTERVAL DAY TO SECOND Datatype in the Oracle
SQL Reference.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
11
an
r
t
n
TIMESTAMP[(fractional_seconds_precision)]
TIMESTAMP[(fractional_seconds_precision)]a
WITH TIME ZONE
has
le
b
a
r
e
f
no
e
m
d
o
i
c Gu
TIMESTAMP[(fractional_seconds_precision)]
o
o
ah dent
WITH LOCAL TIME ZONE
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
TIMESTAMP
li
rivaData Type
S
The
a TIMESTAMP data type is an extension of the DATE data type. It stores the year, month, and
h
e
day
of the DATE data type plus hour, minute, and second values. This data type is used for
n
Suppose that two rows are inserted in the NEW_EMPLOYEES table. The displayed output shows
the differences. (A DATE data type defaults to display the DD-MON-RR format.):
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
12
an
r
t
n
is the same as
TIMESTAMP '2003-04-15 11:00:00 -5:00'
le
b
a
r
e
f
no
a Standard Time.
That is, 8:00 a.m. Pacific Standard Time is the same as 11:00 a.m.sEastern
a
This can also be specified as follows:
)h
om uide
c
ooTypent G
h
TIMESTAMP WITH LOCAL TIME ZONE Data
a
y tude
@
TIMESTAMP WITH LOCAL TIME
ZONE
8 isisSanother variant of TIMESTAMP that
4
s
includes a time-zone displacement
h It differs from TIMESTAMP WITH TIME
c in eits tvalue.
a
h
ZONE in that data stored
us is normalized to the database time zone, and the
nein thetodatabase
s
(
time-zone displacement
a nissnot
e stored as part of the column data. When users retrieve the
v
a
t
data, it is returned
e users' local session time zone. The time-zone displacement is the
s linicthe
a
v
i
difference
Sr (in hours and minutes) between local time and UTC.
a
neh Unlike TIMESTAMP WITH TIME ZONE, you can specify columns of type TIMESTAMP
TIMESTAMP '2003-04-15 8:00:00 US/Pacific'
WITH LOCAL TIME ZONE as part of a primary or unique key, as in the following example:
CREATE TABLE time_example
(order_date TIMESTAMP WITH LOCAL TIME ZONE);
INSERT INTO time_example VALUES('15-JAN-04 09:34:28 AM');
SELECT *
FROM
time_example;
ORDER_DATE
---------------------------15-JAN-04 09.34.28.000000 AM
The TIMESTAMP WITH LOCAL TIME ZONE type is appropriate for two-tier
applications in which you want to display dates and times using the time zone of the client
system.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
13
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
INTERVAL
li MONTH Data Type
rivaYEAR TO
S
INTERVAL
YEAR TO MONTH stores a period of time using the YEAR and MONTH datetime
a
h
e
n fields.
Use INTERVAL YEAR TO MONTH to represent the difference between two datetime values,
where the only significant portions are the year and month. For example, you might use this
value to set a reminder for a date that is 120 months in the future, or check whether 6 months
have elapsed since a particular date.
In the syntax:
year_precision
Examples
ble
a
r
INTERVAL DAY TO SECOND stores a period of time in terms of days, hours, minutes,
feand
s
n
seconds.
a
r
t
Use INTERVAL DAY TO SECOND to represent the precise difference between
on two datetime
n
a that is 36 hours in the
values. For example, you might use this value to set a reminder for s
a time
a
future, or to record the time between the start and end of a race.
) hTo represent
long spans of time,
e
m
d
o
i
including multiple years, with high precision, you canuse
c a large
uvalue for the days portion.
o
G
o
t
In the syntax:
ah den
y
day_precision
@ SIstuthe number of digits in the DAY datetime
8
4
s this field. Accepted values are 0 to 9. The default
c
ha use
is 2.
e
n
s
o
fractional_seconds_precision
Is the number of digits in the fractional part of
(
t
a
e
v
s
the SECOND datetime field. Accepted values
ta icen
s
a
l
are 0 to 9. The default is 6.
riv
S
a
Examples
h
e
n
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
15
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
16
Including Constraints
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
le
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Constraints
riva
S
a Oracle server uses constraints to prevent invalid data entry into tables.
The
h
e
n
b
a
r
e
f
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
17
Constraint Guidelines
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Constraint
rivaGuidelines
S
aconstraints are stored in the data dictionary. Constraints are easy to reference if you give
All
h
e
n them a meaningful name. Constraint names must follow the standard object-naming rules. If you
do not name your constraint, the Oracle server generates a name with the format SYS_Cn,
where n is an integer so that the constraint name is unique.
Constraints can be defined at the time of table creation or after the table has been created.
For more information, see Constraints in Oracle Database SQL Reference.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
18
Defining Constraints
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);
le
Column-level constraint:
b
a
r
e
f
Table-level constraint:
a
r
t
on
an
s
column,...
ha
)
[CONSTRAINT constraint_name] constraint_type
om uide
c
(column, ...),
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
DefiningriConstraints
va
S
a slide gives the syntax for defining constraints when creating a table. You can create the
The
h
e
n
constraints at either the column level or table level. Constraints defined at the column level are
included when the column is defined. Table-level constraints are defined at the end of the table
definition and must refer to the column or columns on which the constraint pertains in a set of
parentheses.
NOT NULL constraints must be defined at the column level.
Constraints that apply to more than one column must be defined at the table level.
In the syntax:
schema
Is the same as the owners name
table
Is the name of the table
DEFAULT expr
Specifies a default value to use if a value is omitted in the
INSERT statement
column
Is the name of the column
datatype
Is the columns data type and length
column_constraint
Is an integrity constraint as part of the column definition
table_constraint
Is an integrity constraint as part of the table definition
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
19
Defining Constraints
Column-level constraint:
CREATE TABLE employees(
employee_id NUMBER(6)
CONSTRAINT emp_emp_id_pk PRIMARY KEY,
first_name
VARCHAR2(20),
...);
Table-level constraint:
CREATE TABLE employees(
employee_id NUMBER(6),
first_name
VARCHAR2(20),
a
...
s
a
job_id
VARCHAR2(10) NOT NULL,
) h e
m
CONSTRAINT emp_emp_id_pk
co Guid
o
PRIMARY KEY (EMPLOYEE_ID));
t
ho
le
b
a
r
e
f
an
r
t
n
no
n
a
e
y
d
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li (continued)
DefiningriConstraints
va
S
a
Constraints
are usually created at the same time as the table. Constraints can be added to a table
h
e
n
More details about the primary key constraint are provided later in this lesson.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
20
le
NOT NULL
constraint
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
NULL
li
vaConstraint
Sri
NOT
The
a NOT NULL constraint ensures that the column contains no null values. Columns without
h
e
Sn the NOT NULL constraint can contain null values by default. NOT NULL constraints must be
defined at the column level.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
21
UNIQUE Constraint
UNIQUE constraint
EMPLOYEES
le
INSERT INTO
Allowed
Not allowed: a
already exists
as
b
a
r
e
f
an
r
t
n
no
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
UNIQUE Constraint
li
riva
S
Aa
UNIQUE key integrity constraint requires that every value in a column or set of columns (key)
h
e
n
be uniquethat is, no two rows of a table can have duplicate values in a specified column or set
of columns. The column (or set of columns) included in the definition of the UNIQUE key
constraint is called the unique key. If the UNIQUE constraint comprises more than one column,
that group of columns is called a composite unique key.
UNIQUE constraints enable the input of nulls unless you also define NOT NULL constraints for
the same columns. In fact, any number of rows can include nulls for columns without NOT
NULL constraints because nulls are not considered equal to anything. A null in a column (or in
all columns of a composite UNIQUE key) always satisfies a UNIQUE constraint.
Note: Because of the search mechanism for UNIQUE constraints on more than one column, you
cannot have identical values in the non-null columns of a partially null composite UNIQUE key
constraint.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
22
UNIQUE Constraint
Defined at either the table level or the column level:
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
UNIQUE Constraint
li(continued)
riva
S
UNIQUE
constraints can be defined at the column level or table level. A composite unique key is
a
h
e
n
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
23
DEPARTMENTS
PRIMARY KEY
le
Not allowed
(null value)
b
a
r
e
f
INSERT INTO
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
Not allowed
tu
(50 already exists) 48@
S
s
cs thi
a
h use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
PRIMARYivKEY
li
r a Constraint
S
Aa
PRIMARY KEY constraint creates a primary key for the table. Only one primary key can be
h
e
n created for each table. The PRIMARY KEY constraint is a column or set of columns that
uniquely identifies each row in a table. This constraint enforces uniqueness of the column or
column combination and ensures that no column that is part of the primary key can contain a
null value.
Note: Because uniqueness is part of the primary key constraint definition, the Oracle server
enforces the uniqueness by implicitly creating a unique index on the primary key column or
columns.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
24
PRIMARY
KEY
EMPLOYEES
le
FOREIGN
KEY
b
a
r
e
f
an
r
t
n
no
a
s
a
a
h use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a Constraint
FOREIGNiv
li
r KEY
S
The
a FOREIGN KEY (or referential integrity) constraint designates a column or combination of
h
e
n
columns as a foreign key and establishes a relationship between a primary key or a unique key in
the same table or a different table.
In the example in the slide, DEPARTMENT_ID has been defined as the foreign key in the
EMPLOYEES table (dependent or child table); it references the DEPARTMENT_ID column of
the DEPARTMENTS table (the referenced or parent table).
Guidelines
A foreign key value must match an existing value in the parent table or be NULL.
Foreign keys are based on data values and are purely logical, rather than physical, pointers.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
25
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a Constraint
FOREIGNiv
(continued)
li
r KEY
S
FOREIGN
KEY constraints can be defined at the column or table constraint level. A composite
a
h
e
n
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
26
n
o
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a Constraint:
FOREIGNiv
Keywords
li
r KEY
S
a foreign key is defined in the child table, and the table containing the referenced column is
The
h
e
n
the parent table. The foreign key is defined using a combination of the following keywords:
FOREIGN KEY is used to define the column in the child table at the table-constraint level.
REFERENCES identifies the table and column in the parent table.
ON DELETE CASCADE indicates that when the row in the parent table is deleted, the
dependent rows in the child table are also deleted.
ON DELETE SET NULL converts foreign key values to null when the parent value is
removed.
The default behavior is called the restrict rule, which disallows the update or deletion of
referenced data.
Without the ON DELETE CASCADE or the ON DELETE SET NULL options, the row in the
parent table cannot be deleted if it is referenced in the child table.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
27
CHECK Constraint
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
CHECK Constraint
li
riva
S
The
a CHECK constraint defines a condition that each row must satisfy. The condition can use the
h
e
n same constructs as query conditions, with the following exceptions:
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
28
PRIMARY KEY
NOT NULL
NOT NULL
UNIQUE
le
NOT NULL
NOT NULL
b
a
r
e
f
an
r
t
n
no
a
CHECK
s (salary>0)
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent REFERENCES
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
CREATE iTABLE:
li
r va Example
S
The
a example shows the statement used to create the EMPLOYEES table in the HR schema.
h
e
n
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
29
Violating Constraints
UPDATE employees
SET
department_id = 55
WHERE department_id = 110;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
t
Department 55 does not exist. yah den
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
liError
IntegrityriConstraint
va
S
a you have constraints in place on columns, an error is returned to you if you try to violate
When
h
e
n the constraint rule.
For example, if you attempt to update a record with a value that is tied to an integrity constraint,
an error is returned.
In the example in the slide, department 55 does not exist in the parent table, DEPARTMENTS,
and so you receive the parent key violation ORA-02291.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
30
Violating Constraints
You cannot delete a row that contains a primary key that is
used as a foreign key in another table.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Error (continued)
IntegrityriConstraint
va
S
If a
you attempt to delete a record with a value that is tied to an integrity constraint, an error is
h
e
n
returned.
The example in the slide tries to delete department 60 from the DEPARTMENTS table, but it
results in an error because that department number is used as a foreign key in the EMPLOYEES
table. If the parent record that you attempt to delete has child records, then you receive the child
record found violation ORA-02292.
The following statement works because there are no employees in department 70:
DELETE FROM
WHERE
departments
department_id = 70;
1 row deleted.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
31
Creating a Table
by Using a Subquery
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Rows in Another Table
Creatingria
vaTable from
S
Aa
second method for creating a table is to apply the AS subquery clause, which both creates
h
e
n the table and inserts rows returned from the subquery.
In the syntax:
table
is the name of the table
column
is the name of the column, default value, and integrity constraint
subquery is the SELECT statement that defines the set of rows to be inserted into
the new table
Guidelines
The table is created with the specified column names, and the rows retrieved by the
SELECT statement are inserted into the table.
The column definition can contain only the column name and default value.
If column specifications are given, the number of columns must equal the number of
columns in the subquery SELECT list.
If no column specifications are given, the column names of the table are the same as the
column names in the subquery.
The column data type definitions and the NOT NULL constraint are passed to the new
table. The other constraint rules are not passed to the new table. However, you can add
constraints in the column definition.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
32
Creating a Table
by Using a Subquery
CREATE TABLE dept80
AS
SELECT employee_id, last_name,
salary*12 ANNSAL,
hire_date
FROM
employees
WHERE
department_id = 80;
CREATE TABLE Succeeded.
DESCRIBE dept80
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Rows in Another Table (continued)
Creatingria
vaTable from
S
The
a slide example creates a table named DEPT80, which contains details of all the employees
h
e
n working in department 80. Notice that the data for the DEPT80 table comes from the
EMPLOYEES table.
You can verify the existence of a database table and check column definitions by using the
DESCRIBE command.
Be sure to provide a column alias when selecting an expression. The expression SALARY*12 is
given the alias ANNSAL. Without the alias, the following error is generated:
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
33
le
b
a
r
e
f
an
r
t
n
ALTER
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
iva Statement
rTABLE
Syou create a table, you may need to change the table structure for any of the following
a
After
h
ne
reasons:
You omitted a column.
Your column definition needs to be changed.
You need to remove columns.
You can do this by using the ALTER TABLE statement. For information about the
ALTER TABLE statement, see the Oracle Database 10g SQL Fundamentals II course.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
34
Dropping a Table
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Dropping
rivaaTable li
S
The
a DROP TABLE statement removes the definition of an Oracle table. When you drop a table,
h
e
n the database loses all the data in the table and all the indexes associated with it.
Syntax
DROP TABLE table
In the syntax, table is the name of the table.
Guidelines
All data is deleted from the table.
Any views and synonyms remain but are invalid.
Any pending transactions are committed.
Only the creator of the table or a user with the DROP ANY TABLE privilege can remove a
table.
Note: The DROP TABLE statement, once executed, is irreversible. The Oracle server does not
question the action when you issue the DROP TABLE statement. If you own that table or have a
high-level privilege, then the table is immediately removed. As with all DDL statements, DROP
TABLE is committed automatically.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
35
Summary
In this lesson, you should have learned how to:
Use the CREATE TABLE statement to create a table and
include constraints
Categorize the main database objects
Review the table structure
List the data types that are available for columns
ble
a
r
Create a simple table
fe
s
n
tra
Explain how constraints are created at the time ofntable
no
creation
a
s
a
h
Describe how schema objects workm)
e
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Summary
riva
S
Inathis lesson, you should have learned how to do the following:
h
e
n CREATE TABLE
Use the CREATE TABLE statement to create a table and include constraints.
Create a table based on another table by using a subquery.
DROP TABLE
Remove rows and a table structure.
Once executed, this statement cannot be rolled back.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
36
Practice 9: Overview
This practice covers the following topics:
Creating new tables
Creating a new table by using the CREATE TABLE AS
syntax
Verifying that tables exist
Dropping tables
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Practiceri9:
vaOverview
S
Create
a new tables by using the CREATE TABLE statement. Confirm that the new table is added
h
e
n to the database. Create the syntax in the command file, and then execute the command file to
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
37
Practice 9
1. Create the DEPT table based on the following table instance chart. Place the
syntax in a script called lab_09_01.sql, and then execute the statement in the script to
create the table. Confirm that the table is created.
Column Name
ID
Key Type
Primary key
NAME
Nulls/Unique
FK Table
FK Column
Data type
NUMBER
VARCHAR2
Length
25
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
t table. Include only columns
n
ahDEPARTMENTS
2. Populate the DEPT table with data fromythe
e
ud
@
t
that you need.
8
S
s4 this
c
Nulls/Unique
FK Table
DEPT
FK Column
ID
Data type
NUMBER
VARCHAR2
VARCHAR2
NUMBER
Length
25
25
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
38
Practice 9 (continued)
4. Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table. Include
only the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and DEPARTMENT_ID
columns. Name the columns in your new table ID, FIRST_NAME, LAST_NAME, SALARY,
and DEPT_ID, respectively.
5. Drop the EMP table.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 9 -Oracle
39
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Objectives
After completing this lesson, you should be able to do the
following:
Create simple and complex views
Retrieve data from views
Create, maintain, and use sequences
Create and maintain indexes
Create private and public synonyms
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Objectives
riva
S
Inathis lesson, you are introduced to the view, sequence, synonym, and index objects. You are
h
e
n taught the basics of creating and using views, sequences, and indexes.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-2
Database Objects
Object
Description
Table
View
Sequence
Index
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
Database
rivObjects
S
a are several other objects in a database in addition to tables. In this lesson, you learn about
There
h
e
n
Synonym
With views, you can present and hide data from tables.
Many applications require the use of unique numbers as primary key values. You can either
build code into the application to handle this requirement or use a sequence to generate unique
numbers.
If you want to improve the performance of some queries, you should consider creating an index.
You can also use indexes to enforce uniqueness on a column or a collection of columns.
You can provide alternative names for objects by using synonyms.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-3
What Is a View?
EMPLOYEES table
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
What Is raivView?
S
a can present logical subsets or combinations of data by creating views of tables. A view is a
You
h
e
n logical table based on a table or another view. A view contains no data of its own but is like a
window through which data from tables can be viewed or changed. The tables on which a view
is based are called base tables. The view is stored as a SELECT statement in the data dictionary.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-4
Advantages of Views
To restrict
data access
To make complex
queries easy
le
b
a
r
e
f
an
r
t
n
To provide
data
independence
no
Toapresent
s
a
different
h views of
the
idesame data
)
m
o
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Advantages
riva of Views
S
a Views restrict access to the data because the view can display selected columns from the
h
e
n
table.
Views can be used to make simple queries to retrieve the results of complicated queries. For
example, views can be used to query information from multiple tables without the user
knowing how to write a join statement.
Views provide data independence for ad hoc users and application programs. One view can
be used to retrieve data from several tables.
Views provide groups of users access to data according to their particular criteria.
For more information, see CREATE VIEW in the Oracle SQL Reference.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-5
Feature
Simple Views
Complex Views
Number of tables
One
One or more
Contain functions
No
Yes
No
Yes
Yes
Not always
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Simple Views
Views
riva and Complex
S
a are two classifications for views: simple and complex. The basic difference is related to
There
h
e
n the DML (INSERT, UPDATE, and DELETE) operations.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-6
Creating a View
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Creatingria
vaView li
S
You
a can create a view by embedding a subquery in the CREATE VIEW statement.
h
e
n
In the syntax:
OR REPLACE
FORCE
NOFORCE
view
alias
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-7
Creating a View
empvu80
employee_id, last_name, salary
employees
department_id = 80;
succeeded.
le
b
a
r
e
Describe the structure of the view by using the DESCRIBE
sf
n
a
command:
tr
n
no
a
DESCRIBE empvu80
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Creatingria
vaView (continued)
S
a example in the slide creates a view that contains the employee number, last name, and
The
h
e
n salary for each employee in department 80.
You can display the structure of the view by using the DESCRIBE command.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-8
Creating a View
le
b
a
r
e
f
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Creatingria
vaView (continued)
S
a can control the column names by including column aliases in the subquery.
You
h
e
n The example in the slide creates a view containing the employee number (EMPLOYEE_ID) with
the alias ID_NUMBER, name (LAST_NAME) with the alias NAME, and annual salary (SALARY)
with the alias ANN_SALARY for every employee in department 50.
As an alternative, you can use an alias after the CREATE statement and before the SELECT
subquery. The number of aliases listed must match the number of expressions selected in the
subquery.
CREATE OR REPLACE VIEW
salvu50 (ID_NUMBER, NAME, ANN_SALARY)
AS SELECT employee_id, last_name, salary*12
FROM
employees
WHERE
department_id = 50;
CREATE VIEW succeeded.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 10Oracle
-9
SELECT *
FROM
salvu50;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li a View
Retrieving
rivaData from
S
a can retrieve data from a view as you would from any table. You can display either the
You
h
e
n contents of the entire view or just specific rows and columns.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 10
Modifying a View
le
b
a
r
e
f
an
r
t
n
e
m
subquery.
uid
co
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Modifying
rivaaView li
S
With
a the OR REPLACE option, a view can be created even if one exists with this name already,
h
e
n thus replacing the old version of the view for its owner. This means that the view can be altered
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 11
on
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Creatingria
vaComplexli View
S
a example in the slide creates a complex view of department names, minimum salaries,
The
h
e
n maximum salaries, and average salaries by department. Note that alternative names have been
specified for the view. This is a requirement if any column of the view is derived from a
function or an expression.
You can view the structure of the view by using the DESCRIBE command. Display the contents
of the view by issuing a SELECT statement.
SELECT
FROM
*
dept_sum_vu;
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 12
Group functions
A GROUP BY clause
The DISTINCT keyword
The pseudocolumn ROWNUM keyword
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Performing
on a View
rivaDML Operations
S
a can perform DML operations on data through a view if those operations follow certain
You
h
e
n rules.
You can remove a row from a view unless it contains any of the following:
Group functions
A GROUP BY clause
The DISTINCT keyword
The pseudocolumn ROWNUM keyword
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 13
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Performing
on a View (continued)
rivaDML Operations
S
a can modify data through a view unless it contains any of the conditions mentioned in the
You
h
e
n previous slide or columns defined by expressions (for example, SALARY * 12).
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 14
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Performing
on a View (continued)
rivaDML Operations
S
a can add data through a view unless it contains any of the items listed in the slide. You
You
h
e
n cannot add data to a view if the view contains NOT NULL columns without default values in the
base table. All required values must be present in the view. Remember that you are adding
values directly to the underlying table through the view.
For more information, see CREATE VIEW in the Oracle SQL Reference.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 15
le
b
a
r
e
f
an
r
t
n
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a CHECK
Using the
li OPTION Clause
rivWITH
S
It a
is possible to perform referential integrity checks through views. You can also enforce
h
e
n constraints at the database level. The view can be used to protect data integrity, but the use is
very limited.
The WITH CHECK OPTION clause specifies that INSERTs and UPDATEs performed through
the view cannot create rows that the view cannot select, and therefore it enables integrity
constraints and data validation checks to be enforced on data being inserted or updated.
Note: The WITH CHECK OPTION clause relates to inserts and updates on the view only. It has
no effect on deletes.
If there is an attempt to perform DML operations on rows that the view has not selected, an
error is displayed, along with the constraint name if that has been specified.
UPDATE empvu20
SET
department_id = 10
WHERE employee_id = 201;
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 16
le
b
a
r
e
f
s
n
a
r
-t10, the view
Note: No rows are updated because if the department number were to change
to
n
o
n
would no longer be able to see that employee. With the WITH CHECKa OPTION
clause,
s
a
therefore, the view can see only employees in department 20 and
allow the department
h does not
)
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
riva
S
a
h
e
n
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 17
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
DenyingriDML
va Operations
S
You
a can ensure that no DML operations occur on your view by creating it with the WITH
h
e
n
READ
ONLY option. The example in the next slide modifies the EMPVU10 view to prevent any DML
operations on the view.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 18
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
DenyingriDML
(continued)
va Operations
S
a attempt to remove a row from a view with a read-only constraint results in an error:
Any
h
e
n
Any attempt to insert a row or modify a row using the view with a read-only constraint results in
an Oracle server error:
01733: virtual column not allowed here.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 19
Removing a View
You can remove a view without losing data because a view is
based on underlying tables in the database.
DROP VIEW view;
DROP VIEW empvu80;
DROP VIEW empvu80 succeeded.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Removing
rivaa View li
S
You
a use the DROP VIEW statement to remove a view. The statement removes the view
h
e
n definition from the database. Dropping views has no effect on the tables on which the view was
based. Views or other applications based on deleted views become invalid. Only the creator or a
user with the DROP ANY VIEW privilege can remove a view.
In the syntax:
view
is the name of the view
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 20
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li of Part 1
Practiceri10:
vaOverview
S
a 1 of this lessons practice provides you with a variety of exercises in creating, using, and
Part
h
e
n
removing views.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 21
Sequences
Object
Description
Table
View
Sequence
Index
Synonym
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Sequences
riva
S
Aa
sequence is a database object that creates integer values. You can create sequences and then
h
e
n use them to generate numbers.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 22
Sequences
A sequence:
Can automatically generate unique numbers
Is a sharable object
Can be used to create a primary key value
Replaces application code
Speeds up the efficiency of accessing sequence values
ble
a
r
when cached in memory
sfe
a
r
t
on
an
s
ha
)
o6 m u8 ide 10
2
4
c
o
G 9
t
1
3 ho 5
7
n
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Sequences
riva(continued)
S
Aa
sequence is a user-created database object that can be shared by multiple users to generate
h
e
n integers.
You can define a sequence to generate unique values or to recycle and use the same numbers
again.
A typical usage for sequences is to create a primary key value, which must be unique for each
row. The sequence is generated and incremented (or decremented) by an internal Oracle routine.
This can be a time-saving object because it can reduce the amount of application code needed to
write a sequence-generating routine.
Sequence numbers are stored and generated independently of tables. Therefore, the same
sequence can be used for multiple tables.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 23
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Creatingria
vaSequence
S
Automatically
generate sequential numbers by using the CREATE SEQUENCE statement.
a
h
e
n
In the syntax:
sequence
INCREMENT BY n
START WITH n
MAXVALUE n
NOMAXVALUE
MINVALUE n
NOMINVALUE
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 24
Creating a Sequence
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li (continued)
Creatingria
vaSequence
S
CYCLE
| NOCYCLE
Specifies whether the sequence continues to generate
a
h
e
n
values after reaching its maximum or minimum value
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 25
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a CURRVAL
NEXTVALiv
li Pseudocolumns
r and
S
a you create your sequence, it generates sequential numbers for use in your tables. Reference
After
h
e
n
The NEXTVAL pseudocolumn is used to extract successive sequence numbers from a specified
sequence. You must qualify NEXTVAL with the sequence name. When you reference
sequence.NEXTVAL, a new sequence number is generated and the current sequence number
is placed in CURRVAL.
The CURRVAL pseudocolumn is used to refer to a sequence number that the current user has just
generated. NEXTVAL must be used to generate a sequence number in the current users session
before CURRVAL can be referenced. You must qualify CURRVAL with the sequence name.
When you reference sequence.CURRVAL, the last value returned to that users process is
displayed.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 26
a
h
e
n
on
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 27
Using a Sequence
le
b
a
r
e
f
a
s
a
SELECT
dept_deptid_seq.CURRVAL
) h e
m
FROM
dual;
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Using arSequence
iva
S
The
a example in the slide inserts a new department in the DEPARTMENTS table. It uses the
h
e
n DEPT_DEPTID_SEQ sequence to generate a new department number as follows.
Suppose that you now want to hire employees to staff the new department. The INSERT
statement to be executed for all new employees can include the following code:
INSERT INTO employees (employee_id, department_id, ...)
VALUES (employees_seq.NEXTVAL, dept_deptid_seq .CURRVAL, ...);
Note: The preceding example assumes that a sequence called EMPLOYEE_SEQ has already
been created to generate new employee numbers.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 28
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
liValues
CachingriSequence
va
S
a can cache sequences in memory to provide faster access to those sequence values. The
You
h
e
n
cache is populated the first time you refer to the sequence. Each request for the next sequence
value is retrieved from the cached sequence. After the last sequence value is used, the next
request for the sequence pulls another cache of sequences into memory.
Gaps in the Sequence
Although sequence generators issue sequential numbers without gaps, this action occurs
independent of a commit or rollback. Therefore, if you roll back a statement containing a
sequence, the number is lost.
Another event that can cause gaps in the sequence is a system crash. If the sequence caches
values in memory, those values are lost if the system crashes.
Because sequences are not tied directly to tables, the same sequence can be used for multiple
tables. If you do so, each table can contain gaps in the sequential numbers.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 29
Modifying a Sequence
Change the increment value, maximum value, minimum value,
cycle option, or cache option:
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Modifying
rivaaSequence
S
If a
you reach the MAXVALUE limit for your sequence, no additional values from the sequence are
h
e
n allocated and you will receive an error indicating that the sequence exceeds the MAXVALUE. To
continue to use the sequence, you can modify it by using the ALTER SEQUENCE statement.
Syntax
ALTER SEQUENCE
sequence
[INCREMENT BY n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 30
You must be the owner or have the ALTER privilege for the
sequence.
Only future sequence numbers are affected.
The sequence must be dropped and
re-created to restart the sequence at a different number.
Some validation is performed.
le
b
a
r
To remove a sequence, use the DROP statement:
fe
an
r
t
n
o
an
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Guidelines
a Sequence
rivafor Modifying
S
a You must be the owner or have the ALTER privilege for the sequence to modify it. You
h
e
n
must be the owner or have the DROP ANY SEQUENCE privilege to remove it.
DROP SEQUENCE dept_deptid_seq;
DROP SEQUENCE dept_deptid_seq succeeded.s
Only future sequence numbers are affected by the ALTER SEQUENCE statement.
The START WITH option cannot be changed using ALTER SEQUENCE. The sequence
must be dropped and re-created to restart the sequence at a different number.
Some validation is performed. For example, a new MAXVALUE that is less than the current
sequence number cannot be imposed.
ALTER SEQUENCE dept_deptid_seq
INCREMENT BY 20
MAXVALUE 90
NOCACHE
NOCYCLE;
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 31
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 32
Indexes
Object
Description
Table
View
Sequence
Index
Synonym
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Indexesriva
S
a are database objects that you can create to improve the performance of some queries.
Indexes
h
e
n Indexes can also be created automatically by the server when you create a primary key or unique
constraint.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 33
Indexes
An index:
Is a schema object
Can be used by the Oracle server to speed up the retrieval
of rows by using a pointer
Can reduce disk I/O by using a rapid path access method
to locate data quickly
ble
a
r
Is independent of the table that it indexes
fe
s
n
ra
Is used and maintained automatically by the Oracle-tserver
on
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Indexesr(continued)
iva
S
AnaOracle server index is a schema object that can speed up the retrieval of rows by using a
h
e
n
pointer. Indexes can be created explicitly or automatically. If you do not have an index on the
column, then a full table scan occurs.
An index provides direct and fast access to rows in a table. Its purpose is to reduce the necessity
of disk I/O by using an indexed path to locate data quickly. The index is used and maintained
automatically by the Oracle server. After an index is created, no direct activity is required by the
user.
Indexes are logically and physically independent of the table that they index. This means that
they can be created or dropped at any time and have no effect on the base tables or other
indexes.
Note: When you drop a table, corresponding indexes are also dropped.
For more information, see Schema Objects: Indexes in Database Concepts.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 34
le
b
a
r
e
f
on
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
Types of
rivIndexes
S
a types of indexes can be created.
Two
h
e
n
Unique index: The Oracle server automatically creates this index when you define a column in
a table to have a PRIMARY KEY or a UNIQUE key constraint. The name of the index is the
name that is given to the constraint.
Nonunique index: This is an index that a user can create. For example, you can create a
FOREIGN KEY column index for a join in a query to improve retrieval speed.
Note: You can manually create a unique index, but it is recommended that you create a unique
constraint, which implicitly creates a unique index.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 35
Creating an Index
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Creatingrian
vaIndex li
S
Create
a an index on one or more columns by issuing the CREATE INDEX statement.
h
e
n
In the syntax:
index
table
column
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 36
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
The table is updated frequently
d
o
i
c Gu
o
o
The indexed columns are referenced
oftan expression
n
ah as part
e
y
ud
@
t
8
S
s4 this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
More Is rNot
ivaAlwaysliBetter
S
a more indexes on a table does not produce faster queries. Each DML operation that is
Having
h
e
n committed on a table with indexes means that the indexes must be updated. The more indexes
The table is small or most queries are expected to retrieve more than 2%
to 4% of the rows in the table
that you have associated with a table, the more effort the Oracle server must make to update all
the indexes after a DML operation.
When to Create an Index
Therefore, you should create indexes only if:
The column contains a wide range of values
The column contains a large number of null values
One or more columns are frequently used together in a WHERE clause or join condition
The table is large and most queries are expected to retrieve less than 2% to 4% of the rows
Remember that if you want to enforce uniqueness, you should define a unique constraint in the
table definition. A unique index is then created automatically.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 37
Removing an Index
le
b
a
r
e
f
an
r
t
n
n index or
To drop an index, you must be the owner of a
the
s
have the DROP ANY INDEX privilege.) ha
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Removing
rivaan Indexli
S
a cannot modify indexes. To change an index, you must drop it and then re-create it.
You
h
e
n Remove an index definition from the data dictionary by issuing the DROP INDEX statement. To
drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege.
In the syntax, index is the name of the index.
Note: If you drop a table, indexes and constraints are automatically dropped but views and
sequences remain.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 38
Synonyms
Object
Description
Table
View
Sequence
Index
Synonym
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Synonyms
riva
S
a
Synonyms
are database objects that enable you to call a table by another name. You can create
h
e
n synonyms to give an alternative name to a table.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 39
Synonyms
Simplify access to objects by creating a synonym (another
name for an object). With synonyms, you can:
Create an easier reference to a table that is owned by
another user
Shorten lengthy object names
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li for an Object
Creatingria
vaSynonym
S
Toarefer to a table that is owned by another user, you need to prefix the table name with the
h
e
n name of the user who created it, followed by a period. Creating a synonym eliminates the need
to qualify the object name with the schema and provides you with an alternative name for a
table, view, sequence, procedure, or other objects. This method can be especially useful with
lengthy object names, such as views.
In the syntax:
PUBLIC
Creates a synonym that is accessible to all users
synonym
Is the name of the synonym to be created
object
Identifies the object for which the synonym is created
Guidelines
The object cannot be contained in a package.
A private synonym name must be distinct from all other objects that are owned by the same
user.
For more information, see CREATE SYNONYM in the Oracle SQL Reference.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 40
Drop a synonym:
DROP SYNONYM d_sum;
DROP SYNONYM succeeded.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Creatingria
vaSynonym
S
The
a slide example creates a synonym for the DEPT_SUM_VU view for quicker reference.
h
e
n
The database administrator can create a public synonym that is accessible to all users. The
following example creates a public synonym named DEPT for Alices DEPARTMENTS table:
CREATE PUBLIC SYNONYM dept
FOR
alice.departments;
CREATE SYNONYM succeeded.
Removing a Synonym
To remove a synonym, use the DROP SYNONYM statement. Only the database administrator can
drop a public synonym.
DROP PUBLIC SYNONYM dept;
DROP SYNONYM succeeded.
For more information, see DROP SYNONYM in the Oracle SQL Reference.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 41
Summary
In this lesson, you should have learned how to:
Create, use, and remove views
Automatically generate sequence numbers by using a
sequence generator
Create indexes to improve query retrieval speed
Use synonyms to provide alternative names for objects
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Summary
riva
S
Inathis lesson, you should have learned about database objects such as views, sequences, indexes,
h
e
n and synonyms.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 42
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li of Part 2
Practiceri10:
vaOverview
S
a 2 of this lessons practice provides you with a variety of exercises in creating and using a
Part
h
e
n
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 43
Practice 10
Part 1
1. The staff in the HR department want to hide some of the data in the EMPLOYEES table.
They want a view called EMPLOYEES_VU based on the employee numbers, employee
names, and department numbers from the EMPLOYEES table. They want the heading
for the employee name to be EMPLOYEE.
2. Confirm that the view works. Display the contents of the EMPLOYEES_VU view.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
3. Using your EMPLOYEES_VU view, write a query for the HR department to display all
employee names and department numbers.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 44
Practice 10 (continued)
4. Department 50 needs access to its employee data. Create a view named DEPT50 that
contains the employee numbers, employee last names, and department numbers for all
employees in department 50. You are asked to label the view columns EMPNO,
EMPLOYEE, and DEPTNO. For security purposes, do not allow an employee to be
reassigned to another department through the view.
5. Display the structure and contents of the DEPT50 view.
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y totdepartment
de
6. Test your view. Attempt to reassign@
Matos
80.
u
8
S
s4 this
c
ha use
e
n
(s e to
a
av cens
t
s
li
riva
S
eha
Sn
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 45
Practice 10 (continued)
Part 2
7. You need a sequence that can be used with the primary key column of the DEPT table. The
sequence should start at 200 and have a maximum value of 1,000. Have your sequence
increment by 10. Name the sequence DEPT_ID_SEQ.
8. To test your sequence, write a script to insert two rows in the DEPT table. Name your
script lab_10_08.sql. Be sure to use the sequence that you created for the ID column.
Add two departments: Education and Administration. Confirm your additions. Run the
commands in your script.
9. Create a nonunique index on the NAME column in the DEPT table.
10. Create a synonym for your EMPLOYEES table. Call it EMP.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 10 Oracle
- 46
Managing Objects
with Data Dictionary Views
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Objectives
After completing this lesson, you should be able to do the
following:
Use the data dictionary views to research data on your
objects
Query various data dictionary views
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Objectives
riva
S
Inathis lesson, you are introduced to the data dictionary views. You learn that the dictionary
h
e
n views can be used to retrieve metadata and create reports about your schema objects.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-2
Data Dictionary
Oracle server
Tables containing
business data:
EMPLOYEES
DEPARTMENTS
LOCATIONS
JOB_HISTORY
...
Data dictionary
views:
ble
a
r
DICTIONARY
fe
s
n
a
USER_OBJECTS
r
t
USER_TABLES
on
n
a
USER_TAB_COLUMNS
s
a
h...
)
m
de
o
i
c
u
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Data Dictionary
riva
S
User
a tables are tables created by the user and contain business data, such as EMPLOYEES. There
h
e
n is another collection of tables and views in the Oracle Database known as the data dictionary.
This collection is created and maintained by the Oracle server and contains information about
the database. The data dictionary is structured in tables and views, just like other database data.
Not only is the data dictionary central to every Oracle Database, but it is also an important tool
for all users, from end users to application designers and database administrators.
You use SQL statements to access the data dictionary. Because the data dictionary is read-only,
you can issue only queries against its tables and views.
You can query the dictionary views that are based on the dictionary tables to find information
such as:
Definitions of all schema objects in the database (tables, views, indexes, synonyms,
sequences, procedures, functions, packages, triggers, and so on)
Default values for columns
Integrity constraint information
Names of Oracle users
Privileges and roles that each user has been granted
Other general database information
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-3
Oracle server
Consists of:
ble
a
r
fe
Base tables
s
n
a
User-accessible views n-tr
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Data Dictionary
riva Structure
S
a
Underlying
base tables store information about the associated database. Only the Oracle server
h
e
n should write to and read these tables. You rarely access them directly.
There are several views that summarize and display the information stored in the base tables of
the data dictionary. These views decode the base table data into useful information (such as user
or table names) using joins and WHERE clauses to simplify the information. Most users are given
access to the views rather than the base tables.
The Oracle user SYS owns all base tables and user-accessible views of the data dictionary. No
Oracle user should ever alter (UPDATE, DELETE, or INSERT) any rows or schema objects
contained in the SYS schema, because such activity can compromise data integrity.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-4
Purpose
USER
ALL
DBA
V$
Performance-related data
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Data Dictionary
(continued)
riva Structure
S
a data dictionary consists of sets of views. In many cases, a set consists of three views
The
h
e
n containing similar information and distinguished from each other by their prefixes. For example,
there is a view named USER_OBJECTS, another named ALL_OBJECTS, and a third named
DBA_OBJECTS.
These three views contain similar information about objects in the database, except that the
scope is different. USER_OBJECTS contains information about objects that you own or created.
ALL_OBJECTS contains information about all objects to which you have access.
DBA_OBJECTS contains information on all objects that are owned by all users. For views that
are prefixed with ALL or DBA, there is usually an additional column in the view named OWNER
to identify who owns the object.
There is also a set of views that is prefixed with v$. These views are dynamic in nature and hold
information about performance. Dynamic performance tables are not true tables, and they should
not be accessed by most users. However, database administrators can query and create views on
the tables and grant access to those views to other users. This course does not go into details
about these views.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-5
le
SELECT *
FROM
dictionary
WHERE table_name = 'USER_OBJECTS';
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
How to rUse
Views
ivathe Dictionary
S
Toafamiliarize yourself with the dictionary views, you can use the dictionary view named
h
e
n DICTIONARY. It contains the name and short description of each dictionary view to which you
have access.
You can write queries to search for information on a particular view name, or you can search the
COMMENTS column for a word or phrase. In the example shown, the DICTIONARY view is
described. It has two columns. The SELECT statement retrieves information about the dictionary
view named USER_OBJECTS. The USER_OBJECTS view contains information about all the
objects that you own.
You can write queries to search the COMMENTS column for a word or phrase. For example, the
following query returns the names of all views that you are permitted to access in which the
COMMENTS column contains the word columns:
SELECT table_name
FROM dictionary
WHERE LOWER(comments) LIKE '%columns';
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-6
Date created
Date of last modification
Status (valid or invalid)
le
b
a
r
e
f
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
USER_OBJECTS
li
riva View
S
You
a can query the USER_OBJECTS view to see the names and types of all the objects in your
h
e
n
Note: This is not a complete listing of the columns. For a complete listing, see
USER_OBJECTS in the Oracle Database Reference.
You can also query the ALL_OBJECTS view to see a listing of all objects to which you have
access.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-7
USER_OBJECTS View
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
USER_OBJECTS
li (continued)
riva View
S
a example shows the names, types, dates of creation, and status of all objects that are owned
The
h
e
n
by this user.
The OBJECT_TYPE column holds the values of either TABLE, VIEW, SEQUENCE, INDEX,
PROCEDURE, FUNCTION, PACKAGE, or TRIGGER.
The STATUS column holds a value of VALID, INVALID, or N/A. While tables are always
valid, the views, procedures, functions, packages, and triggers may be invalid.
The CAT View
For a simplified query and output, you can query the CAT view. This view contains only two
columns: TABLE_NAME and TABLE_TYPE. It provides the names of all your INDEX, TABLE,
CLUSTER, VIEW, SYNONYM, SEQUENCE, or UNDEFINED objects.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-8
Table Information
USER_TABLES:
DESCRIBE user_tables
le
SELECT table_name
FROM
user_tables;
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
t
h
n
a
e
y
ud
@
t
8
S
s4 this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
USER_TABLES
riva Viewli
S
You
a can use the USER_TABLES view to obtain the names of all of your tables. The
h
e
n
USER_TABLES view contains information about your tables. In addition to providing the table
name, it contains detailed information on the storage.
The TABS view is a synonym of the USER_TABLES view. You can query it to see a listing of
tables that you own:
SELECT table_name
FROM tabs;
Note: For a complete listing of the columns in the USER_TABLES view, see USER_TABLES
in the Oracle Database Reference.
You can also query the ALL_TABLES view to see a listing of all tables to which you have
access.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I 11Oracle
-9
Column Information
USER_TAB_COLUMNS:
DESCRIBE user_tab_columns
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
ColumnrInformation
iva
S
You
a can query the USER_TAB_COLUMNS view to find detailed information about the columns
h
e
n
in your tables. While the USER_TABLES view provides information on your table names and
storage, detailed column information is found in the USER_TAB_COLUMNS view.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 10
Column Information
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li (continued)
ColumnrInformation
iva
S
Byaquerying the USER_TAB_COLUMNS table, you can find details about your columns such as
h
e
n
the names, data types, data type lengths, null constraints, and default value for a column.
The example shown displays the columns, data types, data lengths, and null constraints for the
EMPLOYEES table. Note that this information is similar to the output from the DESCRIBE
command.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 11
Constraint Information
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Constraint
rivaInformation
S
a can find out the names of your constraints, the type of constraint, the table name to which
You
h
e
n
the constraint applies, the condition for check constraints, foreign key constraint information,
deletion rule for foreign key constraints, the status, and many other types of information about
your constraints.
Note: For a complete listing and description of the columns in the USER_CONSTRAINTS view,
see USER_CONSTRAINTS in the Oracle Database Reference.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 12
Constraint Information
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
USER_CONSTRAINTS:
li Example
riva
S
Inathe example shown, the USER_CONSTRAINTS view is queried to find the names, types,
h
e
n
check conditions, name of the unique constraint that the foreign key references, deletion rule for
a foreign key, and status for constraints on the EMPLOYEES table.
The CONSTRAINT_TYPE can be:
C (check constraint on a table)
P (primary key)
U (unique key)
R (referential integrity)
V (with check option, on a view)
O (with read-only, on a view)
The DELETE_RULE can be:
CASCADE: If the parent record is deleted, the child records are deleted too.
NO ACTION: A parent record can be deleted only if no child records exist.
The STATUS can be:
ENABLED: Constraint is active.
DISABLED: Constraint is made not active.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 13
Constraint Information
DESCRIBE user_cons_columns
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
QueryingivUSER_CONS_COLUMNS
li
r a
S
Toafind the names of the columns to which a constraint applies, query the
h
e
n
USER_CONS_COLUMNS dictionary view. This view tells you the name of the owner of a
constraint, the name of the constraint, the table that the constraint is on, the names of the
columns with the constraint, and the original position of column or attribute in the definition of
the object.
Note: A constraint may apply to more than one column.
You can also write a join between the USER_CONSTRAINTS and USER_CONS_COLUMNS to
create customized output from both tables.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 14
View Information
1
DESCRIBE user_views
le
b
a
r
e
f
an
r
t
n
o
an
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Views inrithe
va Data Dictionary
S
After
a your view is created, you can query the data dictionary view called USER_VIEWS to see
h
e
Sn the name of the view and the view definition. The text of the SELECT statement that constitutes
3
your view is stored in a LONG column. The LENGTH column is the number of characters in the
SELECT statement. By default, when you select from a LONG column, only the first 80
characters of the columns value are displayed. To see more than 80 characters in SQL*Plus, use
the command SET LONG:
SET LONG 1000
Sequence Information
DESCRIBE user_sequences
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
USER_SEQUENCES
View
li
riva
S
The
a USER_SEQUENCES view describes all sequences that are owned by you. When you create
h
e
n
the sequence, you specify criteria that are stored in the USER_SEQUENCES view. The columns
in this view are:
SEQUENCE_NAME: Name of the sequence
MIN_VALUE: Minimum value of the sequence
MAX_VALUE: Maximum value of the sequence
INCREMENT_BY: Value by which sequence is incremented
CYCLE_FLAG: Does sequence wrap around on reaching limit?
ORDER_FLAG: Are sequence numbers generated in order?
CACHE_SIZE: Number of sequence numbers to cache
LAST_NUMBER: Last sequence number written to disk. If a sequence uses caching, the
number written to disk is the last number placed in the sequence cache. This number is
likely to be greater than the last sequence number that was used.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 16
Sequence Information
le
b
a
r
e
f
an
r
t
n
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Confirming
rivaSequences
S
a creating your sequence, it is documented in the data dictionary. Because a sequence is a
After
h
e
n database object, you can identify it in the USER_OBJECTS data dictionary table.
You can also confirm the settings of the sequence by selecting from the USER_SEQUENCES
data dictionary view.
Viewing the Next Available Sequence Value Without Incrementing It
If the sequence was created with NOCACHE, it is possible to view the next available sequence
value without incrementing it by querying the USER_SEQUENCES table.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 17
Synonym Information
DESCRIBE user_synonyms
le
SELECT *
FROM
user_synonyms;
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
USER_SYNONYMS
li
riva View
S
The
a USER_SYNONYMS dictionary view describes private synonyms (synonyms that are owned
h
e
n
by you).
You can query this view to find your synonyms. You can query ALL_SYNONYMS to find out the
name of all of the synonyms that are available to you and the objects on which these synonyms
apply.
The columns in this view are:
SYNONYM_NAME: Name of the synonym
TABLE_OWNER: Owner of the object that is referenced by the synonym
TABLE_NAME: Name of the table or view that is referenced by the synonym
DB_LINK: Name of the database link reference (if any)
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 18
ALL_COL_COMMENTS
USER_COL_COMMENTS
ALL_TAB_COMMENTS
USER_TAB_COMMENTS
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
lito a Table
Adding rComments
iva
S
a can add a comment of up to 4,000 bytes about a column, table, view, or snapshot by using
You
h
e
n
the COMMENT statement. The comment is stored in the data dictionary and can be viewed in one
of the following data dictionary views in the COMMENTS column:
ALL_COL_COMMENTS
USER_COL_COMMENTS
ALL_TAB_COMMENTS
USER_TAB_COMMENTS
Syntax
COMMENT ON TABLE table | COLUMN table.column
IS 'text';
In the syntax:
table
Is the name of the table
column
Is the name of the column in a table
text
Is the text of the comment
You can drop a comment from the database by setting it to empty string (''):
COMMENT ON TABLE employees IS ' ';
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 19
Summary
In this lesson, you should have learned how to find information
about your objects by using the following dictionary views:
DICTIONARY
USER_OBJECTS
USER_TABLES
USER_TAB_COLUMNS
ble
a
r
USER_CONSTRAINTS
fe
s
n
a
r
t
USER_CONS_COLUMNS
on
n
a
USER_VIEWS
s
a
) h e
USER_SEQUENCES
m
co Guid
o
USER_TAB_SYNONYMS
o
t
ah den
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Summary
riva
S
Inathis lesson, you learned about some of the dictionary views that are available to you. You can
h
e
n use these dictionary views to find information about your tables, constraints, views, sequences,
and synonyms.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 20
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Practiceri11:
vaOverview
S
Inathis practice, you query the dictionary views to find information about the objects in your
h
e
n
schema.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 21
Practice 11
1. For a specified table, create a script that reports the column names, data types, and
lengths of the data types, as well as whether nulls are allowed. Prompt the user to enter
the table name. Give appropriate aliases to the DATA_PRECISION and
DATA_SCALE columns. Save this script in a file named lab_11_01.sql.
For example, if the user enters DEPARTMENTS, the following output results:
le
b
a
r
e
2. Create a script that reports the column name, constraint name, constraint type, search
sf
n
condition, and status for a specified table. You must join the USER_CONSTRAINTS
a
r
tthe
n
and USER_CONS_COLUMNS tables to obtain all this information. Prompt
user to
o
n
enter the table name. Save the script in a file named lab_11_02.sql.
a
s
a
) h eoutput
results:
m
For example, if the user enters DEPARTMENTS, the
following
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
(s e to
a
3. Add a comment
av ctoethe
nsDEPARTMENTS table. Then query the
t
s
li
USER_TAB_COMMENTS
view to verify that the comment is present.
riva
S
a
h
e
n
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 22
Practice 11 (continued)
5. You need to determine the names and definitions of all the views in your schema.
Create a report that retrieves the view information: the view name and text from the
USER_VIEWS data dictionary view.
Note: Another view already exists. The EMP_DETAILS_VIEW was created as part of
your schema. Also, if you completed practice 10, you see the DEPT50 view.
Note: To see more contents of a LONG column, use the command SET LONG n, where
n is the value of the number of characters of the LONG column that you want to see.
6. Find the names of your sequences. Write a query in a script to display the following
ble
a
r
information about your sequences: sequence name, maximum value, increment size,
fe
s
n
and last number. Name the script lab_11_06.sql. Run the statement in ryour
a script.
a
h
e
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
n
o
n
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Database prohibited.
10g: SQL Fundamentals
I 11 Oracle
- 23
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
________________
A
Practice Solutions
________________
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 1: Solutions
Part 1
Test your knowledge:
1. The following SELECT statement executes successfully:
SELECT last_name, job_id, salary AS Sal
FROM
employees;
True/False
2. The following SELECT statement executes successfully:
SELECT *
FROM
job_grades;
le
b
a
r
e
f
an
r
t
n
no
a
s
3. There are four coding errors in this statement. Can you identifyhthem?
a
)
e
m
SELECT
employee_id, last_name
d
o
i
c Gu
sal x 12 ANNUAL SALARY
o
o
FROM
employees;
ah dent
y
@ a column
tu called sal. The column is called
The EMPLOYEES table does not contain
8
S
4
s
SALARY.
cs thi
a
e x, as shown in line 2.
h isu*,snot
The multiplication operator
e
n
(s alias
tocannot include spaces. The alias should read
The ANNUAL a
SALARY
e
av ceornsshould be enclosed in double quotation marks.
ANNUAL_SALARY
t
s
li after the LAST_NAME column.
va is missing
Aricomma
S
a
True/False
eh
n
S
Part 2
Note the following location for the lab files:
\home\oracle\labs\SQL1\labs
If you are asked to save any lab files, save them at this location.
To start Oracle SQL Developer, double-click the SQL Developer desktop icon.
Before you begin with the practices, you need a database connection to be able to connect to the
database and issue SQL queries.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
a
h
e
n
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
The HR department wants a query to display the last name, job code, hire date, and employee
sf
n
a
number for each employee, with the employee number appearing first. Provide-an
tralias
n
STARTDATE for the HIRE_DATE column. Save your SQL statement to n
ao
file named
a
lab_01_07.sql so that you can dispatch this file to the HR department.
s
a
h
) StartDate
t
FROM
employees;
ha use
e
n
o
(s needseatquery
a
9. The HR department
to display all unique job codes from the EMPLOYEES
v
s
a
n
t
e
table.as
lic
v
i
r
S DISTINCT job_id
SELECT
a
h
e FROM employees;
Sn
Part 3
If you have time, complete the following exercises:
10. The HR department wants more descriptive column headings for its report on employees.
Copy the statement from lab_01_07.sql to the SQL Developer text box. Name the
column headings Emp #, Employee, Job, and Hire Date, respectively. Then run your
query again.
SELECT employee_id "Emp #", last_name "Employee",
job_id "Job", hire_date "Hire Date"
FROM
employees;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
an
r
t
n
le
b
a
r
e
f
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 2: Solutions
The HR department needs your assistance with creating some queries.
1. Because of budget issues, the HR department needs a report that displays the last name and
salary of employees earning more than $12,000. Place your SQL statement in a text file
named lab_02_01.sql. Run your query.
SELECT
FROM
WHERE
last_name, salary
employees
salary > 12000;
2. Create a report that displays the last name and department number for employee number 176.
SELECT
FROM
WHERE
last_name, department_id
employees
employee_id = 176;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
t
h
SELECT last_name, salary
n
a
e
y
FROM
employees
ud
@
t
8
S
WHERE
salary NOT BETWEENs5000
4 AND
is 12000;
c
h
t
a se
hthe
e
4. Create a report to display
lastuname, job ID, and start date for the employees whose last
n
s
o
(
t
names are Matos
and
Taylor.
va nse Order the query in ascending order by start date.
a
t
SELECT aslast_name,
ce job_id, hire_date
i
l
v
i
FROM
employees
Sr
aWHERE
last_name IN ('Matos', 'Taylor')
eh ORDER
n
S
BY hire_date;
5. Display the last name and department number of all employees in departments 20 or 50 in
ascending alphabetical order by name.
SELECT
FROM
WHERE
ORDER BY
last_name, department_id
employees
department_id IN (20, 50)
last_name ASC;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
7. The HR department needs a report that displays the last name and hire date for all employees
who were hired in 1994.
SELECT
FROM
WHERE
last_name, hire_date
employees
hire_date LIKE '%94';
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
SELECT
last_name, job_id
e
m
d
o
FROM
employees
i
c Gu
o
WHERE
manager_id IS NULL;
o
ah dent
y
@ Sfortuall employees who earn commissions. Sort
9. Display the last name, salary, and commission
8
4
s and commissions.
is
data in descending order of salary
c
h
t
e
ha uscommission_pct
e
SELECT
last_name,
salary,
n
(s e to
FROM
employees
a
av cens IS NOT NULL
WHERE
commission_pct
t
s
ORDERivBY
r a salaryli DESC, commission_pct DESC;
S
a
8. Create a report to display the last name and job title of all employees who do not have a
manager.
eh 10. Members of the HR department want to have more flexibility with the queries that you are
n
S
writing. They would like a report that displays the last name and salary of employees who
earn more than an amount that the user specifies after a prompt. (You can use the query
created in practice exercise 1 and modify it.) Save this query to a file named
lab_02_10.sql.
SELECT
FROM
WHERE
last_name, salary
employees
salary > &sal_amt;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
12. Display all employee last names in which the third letter of the
) nameeis a.
m
o
c Guid
SELECT
last_name
o
FROM
employees
ho ent
a
y
WHERE
last_name LIKE '__a%';
ud
@
t
8
S
s4 thwho
is have both an a and an e in their last names.
c
14. Display the last name, job, and salary for all employees whose job is either that of a sales
representative or a stock clerk, and whose salary is not equal to $2,500, $3,500, or $7,000.
SELECT
FROM
WHERE
AND
15. Modify lab_02_06.sql to display the last name, salary, and commission for all
employees whose commission amount is 20%. Resave lab_02_06.sql as
lab_02_15.sql. Rerun the statement in lab_02_15.sql.
SELECT
FROM
WHERE
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 3: Solutions
1. Write a query to display the current date. Label the column Date.
SELECT
FROM
sysdate "Date"
dual;
2. The HR department needs a report to display the employee number, last name, salary, and
salary increased by 15.5% (expressed as a whole number) for each employee. Label the
column New Salary. Place your SQL statement in a text file named lab_03_02.sql.
SELECT
FROM
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)subtractsethe old salary from the
m
4. Modify your lab_03_02.sql query to add a columnothat
c Gofutheidfile as
contents
new salary. Label the column Increase. Saveothe
o
lab_03_04.sql. Run the revised query.
ah dent
y
@salary,
tu
8
S
SELECT employee_id, last_name,
4
s
i Salary",
cs 0)th"New
ROUND(salary *a1.155,
e
ROUND(salary
eh* 1.155,
us 0) - salary "Increase"
n
s
o
FROM
employees;
(
t
va nse
a
t
cedisplays the last name (with the first letter uppercase and all other letters
a
5. Write
asquery that
i
l
v
i
r
a Slowercase) and the length of the last name for all employees whose name starts with the
FROM
eh
n
S
letters J, A, or M. Give each column an appropriate label. Sort the results by the employees
last names.
SELECT
INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM
employees
WHERE
last_name LIKE 'J%'
OR
last_name LIKE 'M%'
OR
last_name LIKE 'A%'
ORDER BY last_name ;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM
employees
WHERE
last_name LIKE '&start_letter%'
ORDER BY last_name;
6. The HR department wants to find the duration of employment for each employee. For each
employee, display the last name and calculate the number of months between today and the
date on which the employee was hired. Label the column MONTHS_WORKED. Order your
results by the number of months employed. Round the number of months up to the closest
whole number.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
SELECT last_name, ROUND(MONTHS_BETWEEN(
)
e
SYSDATE, hire_date)) MONTHS_WORKED com
d
i
u
o
FROM
employees
G
o
t
ORDER BY months_worked;
ah den
y
@ Stu
8
4
sfollowing
7. Create a report that produces the
isfor each employee:
c
h
t
<employee last name>
ha earns
se<salary> monthly but wants <3 times
e
u
n
salary>.
(s e to
a
v
ns Salaries.
tacolumn
e
s
Labelathe
Dream
c
li
iv
r
S
aSELECT last_name || ' earns '
Note: Your results will differ.
eh
n
S
FROM
|| TO_CHAR(salary, 'fm$99,999.00')
|| ' monthly but wants '
|| TO_CHAR(salary * 3, 'fm$99,999.00')
|| '.' "Dream Salaries"
employees;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
10. Display the last name, hire date, and day of the week on which the employee started. Label
the column DAY. Order the results by the day of the week, starting with Monday.
SELECT last_name, hire_date,
TO_CHAR(hire_date, 'DAY') DAY
FROM
employees
ORDER BY TO_CHAR(hire_date - 1, 'd');
le
b
a
r
e
f
an
r
t
n
no
a
If you want an extra challenge, complete the following exercises:
s
a
h
)
amounts. If an
e
m
11. Create a query that displays the employees last nameso
and
commission
d
i
c GuLabel the column COMM.
o
employee does not earn commission, show Noo
Commission.
t
h
n
a
e
y
d
SELECT last_name,
uCommission')
@
t
8
S
NVL(TO_CHAR(commission_pct),
'No
COMM
s4 this
c
FROM
employees;
ha use
e
n
o first eight characters of the employees last names and
(sdisplays
tthe
12. Create a queryathat
e
indicatestthe
avamounts
nofs their salaries with asterisks. Each asterisk signifies a thousand
e
s
c
dollars.
iva Sort thelidata in descending order of salary. Label the column
r
S
a EMPLOYEES_AND_THEIR_SALARIES.
eh SELECT
n
S
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Grade
AD_PRES
ST_MAN
IT_PROG
SA_REP
ST_CLERK
le
'E',
'D',
'C',
'B',
'A',
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
14. Rewrite the statement in the preceding
exercise
8 is Susing the CASE syntax.
4
s
c e th
SELECT job_id, CASE job_id
a
h
WHEN
'E'
us THEN
ne'ST_CLERK'
s
o
(
t
WHEN
'SA_REP'
THEN 'D'
e
va WHENns'IT_PROG'
THEN 'C'
a
t
e
s
c
WHEN
'ST_MAN'
'B'
liWHEN 'AD_PRES' THEN
va
i
r
THEN 'A'
aS
ELSE '0' END GRADE
eh FROM
n
S
b
a
r
e
f
employees;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 4: Solutions
Determine the validity of the following three statements. Circle either True or False.
1. Group functions work across many rows to produce one result per group.
True/False
2. Group functions include nulls in calculations.
True/False
3. The WHERE clause restricts rows before inclusion in a group calculation.
True/False
le
b
a
r
e
f
4. Find the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest
whole number. Place your SQL statement in a text file named lab_04_04.sql.
an
r
t
n
no
a
s
a
SELECT ROUND(MAX(salary),0) "Maximum",
h
)
ROUND(MIN(salary),0) "Minimum",
e
m
d
o
i
ROUND(SUM(salary),0) "Sum",
c Gu
o
o
ROUND(AVG(salary),0) "Average"
ah dent
FROM
employees;
y
@ Stu
8
4
s thtoisdisplay the minimum, maximum, sum, and
c
5. Modify the query in lab_04_04.sql
hatype.uResave
se lab_04_04.sql as lab_04_05.sql. Run the
average salary for each
job
e
n
(s e to
statement in lab_04_05.sql.
a
ns
tav icROUND(MAX(salary),0)
e
s
SELECT a
job_id,
l ROUND(MIN(salary),0) "Maximum",
v
i
"Minimum",
r
ROUND(SUM(salary),0) "Sum",
aS
eh
n
S
ROUND(AVG(salary),0) "Average"
FROM
employees
GROUP BY job_id;
6. Write a query to display the number of people with the same job.
SELECT job_id, COUNT(*)
FROM
employees
GROUP BY job_id;
Generalize the query so that the user in the HR department is prompted for a job title. Save
the script to a file named lab_04_06.sql.
SELECT job_id, COUNT(*)
FROM
employees
WHERE job_id = '&job_title'
GROUP BY job_id;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
8. Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.
SELECT
FROM
le
b
a
r
e
f
an
r
t
n
9. Create a report to display the manager number and the salary of the lowest-paid employee for
that manager. Exclude anyone whose manager is not known. Exclude any groups where the
minimum salary is $6,000 or less. Sort the output in descending order of salary.
no
a
s
a
SELECT
manager_id, MIN(salary)
h
)
e
m
FROM
employees
d
o
i
c Gu
WHERE
manager_id IS NOT NULL
o
o
t
GROUP BY manager_id
h
n
a
e
y
HAVING
MIN(salary) > 6000
@ Stud
ORDER BY MIN(salary) DESC; 48
s this
c
hacomplete
sethe following exercises:
e
If you want an extra challenge,
u
n
(s e to
a
10. Create a query
displays
av that
ns the total number of employees and, of that total, the number of
t
e
s
c
employees
riva hiredli in 1995, 1996, 1997, and 1998. Create appropriate column headings.
S
aSELECT COUNT(*) total,
eh
n
S
FROM
SUM(DECODE(TO_CHAR(hire_date,
SUM(DECODE(TO_CHAR(hire_date,
SUM(DECODE(TO_CHAR(hire_date,
SUM(DECODE(TO_CHAR(hire_date,
employees;
'YYYY'),1995,1,0))"1995",
'YYYY'),1996,1,0))"1996",
'YYYY'),1997,1,0))"1997",
'YYYY'),1998,1,0))"1998"
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
job_id "Job",
SUM(DECODE(department_id
SUM(DECODE(department_id
SUM(DECODE(department_id
SUM(DECODE(department_id
SUM(salary) "Total"
FROM
employees
GROUP BY job_id;
,
,
,
,
20,
50,
80,
90,
salary))
salary))
salary))
salary))
"Dept
"Dept
"Dept
"Dept
20",
50",
80",
90",
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 5: Solutions
1. Write a query for the HR department to produce the addresses of all the departments. Use
the LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state or
province, and country in the output. Use a NATURAL JOIN to produce the results.
SELECT location_id, street_address, city, state_province, country_name
FROM
locations
NATURAL JOIN countries;
2. The HR department needs a report of all employees. Write a query to display the last name,
department number, and department name for all employees.
SELECT last_name, department_id, department_name
FROM
employees
JOIN
departments
USING (department_id);
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)d.department_name
ha use
e
n
s thetolast name and employee number of employees along with their
4. Create a report to(display
a
v name nandsemanager number. Label the columns Employee, Emp#,
managers
last
a
t
s andliMgr#,
ce respectively. Place your SQL statement in a text file named
a
Manager,
v
i
r
Slab_05_04.sql.
a
eh
3. The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and department name for all employees who work in Toronto.
Sn
5. Modify lab_05_04.sql to display all employees, including King, who has no manager.
Order the results by the employee number. Place your SQL statement in a text file named
lab_05_05.sql. Run the query in lab_05_05.sql.
SELECT w.last_name "Employee", w.employee_id "EMP#",
m.last_name "Manager", m.employee_id "Mgr#"
FROM
employees w
LEFT
OUTER JOIN employees m
ON
(w.manager_id = m.employee_id);
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
7. The HR department needs a report on job grades and salaries. To familiarize yourself with
the JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create a
query that displays the name, job, department name, salary, and grade for all employees.
le
b
a
r
e
f
an
r
t
n
DESC JOB_GRADES
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
is
cs thethfollowing
eh FROM
n
S
ON
WHERE
9. The HR department needs to find the names and hire dates for all employees who were hired
before their managers, along with their managers names and hire dates. Save the script to a
file named lab_05_09.sql.
SELECT w.last_name, w.hire_date, m.last_name, m.hire_date
FROM
employees w JOIN employees m
ON
(w.manager_id = m.employee_id)
WHERE
w.hire_date < m.hire_date;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 6: Solutions
1. The HR department needs a query that prompts the user for an employee last name. The
query then displays the last name and hire date of any employee in the same department as
the employee whose name the user supplies (excluding that employee). For example, if the
user enters Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey).
UNDEFINE Enter_name
SELECT last_name, hire_date
FROM
employees
WHERE department_id = (SELECT department_id
FROM
employees
WHERE last_name = '&&Enter_name')
AND
last_name <> '&Enter_name';
le
b
a
r
e
f
an
r
t
n
2. Create a report that displays the employee number, last name, and salary of all employees
who earn more than the average salary. Sort the results in ascending order by salary.
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@number
tuand last name of all employees who work
8
S
3. Write a query that displays the employee
4
s
cs whose
hilast name contains a u. Place your SQL statement
t
in a department with any employee
a
e
eh us Run your query.
in a text file named lab_06_03.sql.
n
s
(
to
a
e
SELECT employee_id,
tav icenslast_name
s
FROM a
employees
l
riv department_id
WHERE
IN (SELECT department_id
S
a
FROM
employees
SELECT employee_id, last_name, salary
FROM
employees
WHERE salary > (SELECT AVG(salary)
FROM
employees)
ORDER BY salary;
eh
n
S
WHERE
4. The HR department needs a report that displays the last name, department number, and job
ID of all employees whose department location ID is 1700.
SELECT last_name, department_id, job_id
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
departments
WHERE location_id = 1700);
Modify the query so that the user is prompted for a location ID. Save this to a file named
lab_06_04.sql.
SELECT last_name, department_id, job_id
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
departments
WHERE location_id = &Enter_location);
Oracle Database 10g: SQL Fundamentals I A - 18
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
6. Create a report for the HR department that displays the department number, last name, and
job ID for every employee in the Executive department.
SELECT department_id, last_name, job_id
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
departments
WHERE department_name = 'Executive');
le
b
a
r
e
f
an
r
t
n
no
a
s
a
If you have time, complete the following exercise:
h
)
e
m
d
o
i
unumber, last name, and
c employee
7. Modify the query in lab_06_03.sql to displayothe
G
o
t
h averageensalary and who work in a department
salary of all employees who earn more thanathe
y
ud lab_06_03.sql to
with any employee whose last name contains
a u.tResave
@
8
S
lab_06_07.sql. Run the statement
s4 tinhlab_06_07.sql.
is
c
ha use salary
SELECT employee_id,
last_name,
e
n
FROM
employees
(s e to
a
WHERE department_id
department_id
av cens IN (SELECT
t
s
FROM
employees
li
va
i
r
WHERE
last_name like '%u%')
aANDS salary > (SELECT AVG(salary)
eh
n
S
FROM
employees);
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 7: Solutions
1. The HR department needs a list of department IDs for departments that do not contain the job
ID ST_CLERK. Use set operators to create this report.
SELECT
FROM
MINUS
SELECT
FROM
WHERE
department_id
departments
department_id
employees
job_id = 'ST_CLERK';
2. The HR department needs a list of countries that have no departments located in them.
Display the country ID and the name of the countries. Use set operators to create this report.
SELECT country_id, country_name
FROM
countries
MINUS
SELECT country_id, country_name
FROM
countries
NATURAL JOIN locations
NATURAL JOIN departments;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
3. Produce a list of jobs for departments 10, 50,
hand 20, inenthatt order. Display the job ID and
a
y
department ID using set operators. @
ud
t
8
S
s4 this
COLUMN dummy NOPRINT
c
eh WHERE
n
S
UNION
department_id = 50
4. Create a report that lists the employee ID and job ID of those employees who currently have
a job title that is the same as their job title when they were initially hired by the company
(that is, they changed jobs but have now gone back to doing their original job).
SELECT
employee_id,job_id
FROM
employees
INTERSECT
SELECT
employee_id,job_id
FROM
job_history;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Last name and department ID of all the employees from the EMPLOYEES table,
regardless of whether or not they belong to a department
Department ID and department name of all the departments from the DEPARTMENTS
table, regardless of whether or not they have employees working in them
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 8: Solutions
The HR department wants you to create SQL statements to insert, update, and delete employee
data. As a prototype, you use the MY_EMPLOYEE table, before giving the statements to the HR
department.
Insert data into the MY_EMPLOYEE table.
1. Run the statement in the lab_08_01.sql script to build the MY_EMPLOYEE table to be
used for the lab.
CREATE TABLE my_employee
(id NUMBER(4) CONSTRAINT my_employee_id_nn NOT NULL,
last_name VARCHAR2(25),
first_name VARCHAR2(25),
userid VARCHAR2(8),
salary NUMBER(9,2));
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
DESCRIBE my_employee
)
e
m
d
o
i
c to the
uMY_EMPLOYEE table from
data
o
G
3. Create an INSERT statement to add the first row
of
o
t
nINSERT clause.
ah indthe
e
the following sample data. Do not list they
columns
@ Stu
8
4
s FIRST_NAME
is
c
h
t
ID
LAST_NAME
USERID
SALARY
a se
h
e
n to u
s
(
1
Ralph
rpatel
895
va Patel
se
a
n
t
e
as lic
v
i
r
Dancs
Betty
bdancs
860
a S2
2. Describe the structure of the MY_EMPLOYEE table to identify the column names.
eh
n
S
Biri
Ben
bbiri
Newman
Chad
cnewman
Ropeburn
Audrey
aropebur
1100
750
1550
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
*
my_employee;
le
b
a
r
e
f
6. Write an INSERT statement in a dynamic reusable script file named loademp.sql to load
rows into the MY_EMPLOYEE table. Concatenate the first letter of the first name and the first
seven characters of the last name to produce the user ID. Save this script to a file named
lab_08_06.sql.
an
r
t
n
no
a
SET ECHO OFF
s
a
h
SET VERIFY OFF
)
e
m
INSERT INTO my_employee
d
o
i
c Gu
VALUES (&p_id, '&&p_last_name', '&&p_first_name',
o
o
h 1, 1)en||t
lower(substr('&p_first_name',
a
y
d
substr('&p_last_name', @
1, 7)), tu
&p_salary);
8
S
SET VERIFY ON
s4 this
c
SET ECHO ON
ha use
UNDEFINE p_first_name
e
n
UNDEFINE p_last_name
(s e to
a
av cens
t
s
7. Populate
table
li with the next two rows of sample data listed in step 3 by running the
riva the
S
INSERT
statement in the script that you created.
eha
Sn
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
my_employee
last_name = 'Drexler'
id = 3;
11. Change the salary to $1,000 for all employees with a salary less than $900.
UPDATE
SET
WHERE
my_employee
salary = 1000
salary < 900;
le
an
r
t
n
last_name, salary
my_employee;
b
a
r
e
f
an
s
a
h
)
a table.se
14. Confirm your changes tohthe
e
u
n
s
o
(
t
SELECT *
va nse
FROM
my_employee;
a
t
s lice
a
v
i
r
a15.SCommit all pending changes.
eh COMMIT;
n
S
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
*
my_employee;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
) the earlier
21. Discard the most recent DELETE operation without discarding
INSERT operation.
e
m
d
o
i
c Gu
ROLLBACK TO step_18;
o
o
ah dent
y
22. Confirm that the new row is still intact.
@ Stu
8
4
s this
c
SELECT *
a se
FROM
my_employee;eh
u
n
s
o
(
t
e
vaaddition
spermanent.
23. Make theta
data
n
e
vas lic
i
COMMIT;
r
aS
eh
n
S
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice 9: Solutions
1. Create the DEPT table based on the following table instance chart. Place the
syntax in a script called lab_09_01.sql, and then execute the statement in the script to
create the table. Confirm that the table is created.
CREATE TABLE dept
(id
NUMBER(7)CONSTRAINT department_id_pk PRIMARY KEY,
name VARCHAR2(25));
DESCRIBE dept
2. Populate the DEPT table with data from the DEPARTMENTS table. Include only columns that
you need.
INSERT INTO dept
SELECT department_id, department_name
FROM
departments;
le
b
a
r
e
f
an
r
t
n
no
a
3. Create the EMP table based on the following table instance chart.aPlace
s the syntax in
h
) inethe script to create the
a script called lab_09_03.sql, and then execute the statement
m
o
table. Confirm that the table is created.
c Guid
o
CREATE TABLE emp
aho dent
y
(id
NUMBER(7),
@ Stu
8
4
last_name
VARCHAR2(25),
s this
c
first_name
VARCHAR2(25),
ha use
e
dept_id
NUMBER(7)
n
s
to
CONSTRAINT (emp_dept_id_FK
REFERENCES dept (id)
a
e
v
s
);
ta icen
s
a
riv emp l
DESCRIBE
S
a
eh 4.
n
S
Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table. Include only
the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and DEPARTMENT_ID
columns. Name the columns in your new table ID, FIRST_NAME, LAST_NAME, SALARY,
and DEPT_ID, respectively.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
2. Confirm that the view works. Display the contents of the EMPLOYEES_VU view.
SELECT
FROM
*
employees_vu;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
SELECT
employee, department_id
)
e
m
FROM
employees_vu;
d
o
i
c Gu
o
o
t
h Create
n
adata.
e
4. Department 50 needs access to its employee
a
y
d view named DEPT50 that
u
@
t
contains the employee numbers, employee
last
names,
8 is S and department numbers for all
4
s
employees in department 50.They
c havethrequested that you label the view columns
a
h
EMPNO, EMPLOYEE, and
DEPTNO.
seFor security purposes, do not allow an employee to be
e
u
n
(s department
reassigned to another
to through the view.
a
e
s
avdept50
nAS
t
CREATE VIEW
e
s
c
li
SELECT
empno, last_name employee,
riva employee_id
S
department_id
deptno
a
3. Using your EMPLOYEES_VU view, write a query for the HR department to display all
employee names and department numbers.
eh
n
S
FROM
employees
WHERE
department_id = 50
WITH CHECK OPTION CONSTRAINT emp_dept_50;
*
dept50;
dept50
deptno = 80
employee = 'Matos';
The error is due to the fact that the view DEPT50 is created with CHECK OPTION
CONSTRAINT. This ensures that the deptno column in the view is protected from being
changed.
Oracle Database 10g: SQL Fundamentals I A - 27
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
s
n
a
r
-tyour script
8. To test your sequence, write a script to insert two rows in the DEPT table. Name
n
o
lab_10_08.sql. Be sure to use the sequence that you created for the n
ID column. Add two
a
s
departments: Education and Administration. Confirm your additions.
the commands in
ha Run
)
your script.
om uide
c
VALUES (dept_id_seq.nextval,
'Administration');
t
a se
h
e
n to u
s
(
9. Create a nonunique
index
va nseon the NAME column in the DEPT table.
a
t
s dept_name_idx
CREATE a
INDEX
ON dept (name);
ce
i
l
v
i
r
aS
eh
n
S
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
2. Create a script that reports the column name, constraint name, constraint type, search
condition, and status for a specified table. You must join the USER_CONSTRAINTS and
USER_CONS_COLUMNS tables to obtain all of this information. Prompt the user to enter the
table name. Save the script in a file named lab_11_02.sql.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@table. Then
tuquery the USER_TAB_COMMENTS view
3. Add a comment to the DEPARTMENTS
8
S
4
s
cs thi
to verify that the comment ispresent.
a
eh use
COMMENT ON TABLE sdepartments
IS
n
o
(
t
'Company department
information
including name, code, and location.';
va nse
a
t
s lice
a
SELECT
COMMENTS
v
i
Sr user_tab_comments
aFROM
SELECT ucc.column_name, uc.constraint_name, uc.constraint_type,
uc.search_condition, uc.status
FROM
user_constraints uc JOIN user_cons_columns ucc
ON
uc.table_name = ucc.table_name
AND
uc.constraint_name = ucc.constraint_name
AND
uc.table_name = UPPER('&tab_name');
eh WHERE
n
S
table_name = 'DEPARTMENTS';
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
view_name, text
user_views;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
SELECT
sequence_name, max_value, increment_by,
c Gulast_number
o
o
FROM
user_sequences;
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
(s e to
a
av cens
t
s
li
riva
S
eha
6. Find the names of your sequences. Write a query in a script to display the following
information about your sequences: sequence name, maximum value, increment size, and last
number. Name the script lab_11_06.sql. Run the statement in your script.
Sn
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Practice C: Solutions
1. Write a query for the HR department to produce the addresses of all the departments. Use the
LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state or
province, and country in the output.
SELECT location_id, street_address, city, state_province, country_name
FROM
locations, countries
WHERE locations.country_id = countries.country_id;
2. The HR department needs a report of all employees. Write a query to display the last name,
department number, and department name for all employees.
SELECT e.last_name, e.department_id, d.department_name
FROM
employees e, departments d
WHERE e.department_id = d.department_id;
le
b
a
r
e
f
s
n
a
r
-t job,
3. The HR department needs a report of employees in Toronto. Display the last n
name,
o
department number, and department name for all employees who work innToronto.
a
s
a
SELECT e.last_name, e.job_id, e.department_id, d.department_name
h
FROM
employees e, departments d , locations
l)
m
de
i
WHERE e.department_id = d.department_id co
u
o
G
t
AND
d.location_id = l.location_idho
n
a
AND
LOWER(l.city) = 'toronto';y
de
u
@
t
S
s48 tlast
isname
c
h
4. Create a report to display the
employee
and employee number along with the last
a
e
h
s the manager number. Label the columns Employee,
name of the employees
uand
nemanager
s
o
(
t
Emp#, Manager,
e respectively. Place your SQL statement in a text file named
va andnsMgr#,
a
lab_c_04.sql.
t
s lice
a
v
i
r w.last_name "Employee", w.employee_id "EMP#",
SELECT
a S m.last_name "Manager", m.employee_id "Mgr#"
eh FROM
n
S
WHERE
employees w, employees m
w.manager_id = m.employee_id;
5. Modify lab_c_04.sql to display all employees, including King, who has no manager.
Order the results by the employee number. Place your SQL statement in a text file named
lab_c_05.sql. Run the query in lab_c_05.sql.
SELECT w.last_name "Employee", w.employee_id "EMP#",
m.last_name "Manager", m.employee_id "Mgr#"
FROM
employees w, employees m
WHERE w.manager_id = m.employee_id (+);
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
7. The HR department needs a report on job grades and salaries. To familiarize yourself with
the JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create a
query that displays the name, job, department name, salary, and grade for all employees.
le
b
a
r
e
f
an
r
t
n
DESC JOB_GRADES
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
If you want an extra challenge, complete
the
exercises:
s thfollowing
is
c
hato determine
se the names of all employees hired after Davies. Create
e
8. The HR department n
wants
u
o hire date of any employee hired after employee Davies.
(sthe name
tand
a query to display
a
e
tav icense.hire_date
s
SELECT a
e.last_name,
l e , employees davies
riv employees
FROM
S
aWHERE davies.last_name = 'Davies'
SELECT e.last_name, e.job_id, d.department_name,
e.salary, j.grade_level
FROM
employees e, departments d, job_grades j
WHERE e.department_id = d.department_id
AND
e.salary BETWEEN j.lowest_sal AND j.highest_sal;
eh AND
n
S
9. The HR department needs to find the names and hire dates for all employees who were hired
before their managers, along with their managers names and hire dates. Label the columns
Employee, Emp Hired, Manager, and Mgr Hired, respectively. Save the script to a file
named lab_c_09.sql.
SELECT
FROM
WHERE
AND
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
B
Table Descriptions and Data
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
JOB_GRADES
GRADE_LEVEL
LOWEST_SAL
HIGHEST_SAL
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
COUNTRIES Table
DESCRIBE countries
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
DEPARTMENTS Table
DESCRIBE departments
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
EMPLOYEES Table
DESCRIBE employees
le
b
a
r
e
f
an
r
t
n
an
s
SELECT * FROM employees;
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
riva
S
eha
Sn
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
JOBS Table
DESCRIBE jobs
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
JOB_GRADES Table
DESCRIBE job_grades
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
JOB_HISTORY Table
DESCRIBE job_history
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
LOCATIONS Table
DESCRIBE locations
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
REGIONS Table
DESCRIBE regions
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Objectives
After completing this lesson, you should be able to do the
following:
Write SELECT statements to access data from more than
one table using equijoins and nonequijoins
Use outer joins to view data that generally does not meet a
join condition
ble
a
Join a table to itself by using a self-join
r
e
s
n
a
r
-t
on
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Objectives
li
riva
S
This
a lesson explains how to obtain data from more than one table. A join is used to view information
h
e
Sn from multiple tables. Therefore, you can join tables together to view information from more than one
table.
Note: Information on joins is found in SQL Queries and Subqueries: Joins in
Oracle SQL Reference.
EMPLOYEES
DEPARTMENTS
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
Data fromiv
Tables
li
r Multiple
S
Sometimes
you need to use data from more than one table. In the slide example, the report displays
a
h
e
Sn data from two separate tables:
Cartesian Products
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
Cartesianiv
li
r Products
S
When
a a join condition is invalid or omitted completely, the result is a Cartesian product, in which all
h
e
n combinations of rows are displayed. All rows in the first table are joined to all rows in the second
table.
A Cartesian product tends to generate a large number of rows, and the result is rarely useful. You
should always include a valid join condition unless you have a specific need to combine all rows
from all tables.
Cartesian products are useful for some tests when you need to generate a large number of rows to
simulate a reasonable amount of data.
DEPARTMENTS (8 rows)
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
Cartesian product:
)
e
m
d
o
i
20 x 8 = 160 rows
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
i
Cartesianiv
l(continued)
r Products
S
AhCartesian
product is generated if a join condition is omitted. The example in the slide displays
a
e
employee
last
name and department name from the EMPLOYEES and DEPARTMENTS tables.
n
Because no join condition has been specified, all rows (20 rows) from the EMPLOYEES table are
joined with all rows (8 rows) in the DEPARTMENTS table, thereby generating 160 rows in the output.
SELECT last_name, department_name dept_name
FROM
employees, departments;
Types of Joins
Oracle-proprietary joins
(8i and earlier releases)
SQL:1999compliant joins
Equijoin
Nonequijoin
Outer join
Self-join
Cross join
Natural join
Using clause
ble
a
r
Full (or two-sided) sfe
an
r
outer join
t
on
n
Arbitrary join
a condition
s
a
h join
for )outer
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Types of Joins
li
riva
S
Before
a the release of Oracle9i Database, the join syntax was proprietary.
h
e
Sn Note: The SQL:1999compliant join syntax does not offer any performance benefits over the Oracleproprietary join syntax that existed in prior releases. For detailed information about the SQL:1999
compliant join syntax, see Lesson 5.
table1.column, table2.column
table1, table2
table1.column1 = table2.column2;
Prefix the column name with the table name when the ns
a
r
t
same column name appears in more than one table.
n
le
b
a
r
e
f
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Defining Joins
li
riva
S
When
a data from more than one table in the database is required, a join condition is used. Rows in one
h
e
n table can be joined to rows in another table according to common values that exist in corresponding
In the syntax:
table1.column
table1.column1 =
table2.column2
Guidelines
When writing a SELECT statement that joins tables, precede the column name with the table
name for clarity and to enhance database access.
If the same column name appears in more than one table, the column name must be prefixed
with the table name.
To join n tables together, you need a minimum of n-1 join conditions. For example, to join four
tables, a minimum of three joins is required. This rule may not apply if your table has a
concatenated primary key, in which case more than one column is required to uniquely identify
each row.
Equijoins
EMPLOYEES
DEPARTMENTS
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
tu
Foreign key48@
Primary key
S
s
s thi
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Equijoinsiva
li
r
S
Toadetermine an employees department name, you compare the value in the DEPARTMENT_ID
h in the EMPLOYEES table with the DEPARTMENT_ID values in the DEPARTMENTS table.
e
column
n
S
The relationship between the EMPLOYEES and DEPARTMENTS tables is an equijointhat is, values
in the DEPARTMENT_ID column in both tables must be equal. Frequently, this type of join involves
primary and foreign key complements.
Note: Equijoins are also called simple joins or inner joins.
Retrieving Records
with Equijoins
SELECT employees.employee_id, employees.last_name,
employees.department_id, departments.department_id,
departments.location_id
FROM
employees, departments
WHERE employees.department_id = departments.department_id;
le
b
a
r
e
f
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
i Equijoins
RetrievingivRecords
lwith
r a
S
In
athe slide example:
h
e
an
r
t
n
- Employee last name, employee number, and department number, which are columns in the
EMPLOYEES table
- Department number, department name, and location ID, which are columns in the
DEPARTMENTS table
The FROM clause specifies the two tables that the database must access:
- EMPLOYEES table
- DEPARTMENTS table
The WHERE clause specifies how the tables are to be joined:
EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
Because the DEPARTMENT_ID column is common to both tables, it must be prefixed by the table
name to avoid ambiguity.
DEPARTMENTS
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
AdditionalivSearch
Conditions
li
r a
S
In a
addition to the join, you may have criteria for your WHERE clause to restrict the rows under
h
e
n consideration for one or more tables in the join. For example, to display employee Matoss
department number and department name, you need an additional condition in the WHERE clause.
SELECT last_name, employees.department_id,
department_name
FROM
employees, departments
WHERE employees.department_id = departments.department_id
AND
last_name = 'Matos';
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
10
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
QualifyingivAmbiguous
li Column Names
r a
S
You
a need to qualify the names of the columns in the WHERE clause with the table name to avoid
h
e
ambiguity.
Without the table prefixes, the DEPARTMENT_ID column could be from either the
n
DEPARTMENTS table or the EMPLOYEES table. It is necessary to add the table prefix to execute
your query.
If there are no common column names between the two tables, there is no need to qualify the
columns. However, using the table prefix improves performance, because you tell the Oracle server
exactly where to find the columns.
The requirement to qualify ambiguous column names is also applicable to columns that may be
ambiguous in other clauses, such as the SELECT clause or the ORDER BY clause.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
11
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Using Table
rivaAliasesli
S
Qualifying
column names with table names can be very time consuming, particularly if table names
a
h
e
Sn are lengthy. You can use table aliases instead of table names. Just as a column alias gives a column
another name, a table alias gives a table another name. Table aliases help to keep SQL code smaller,
therefore using less memory.
Notice how table aliases are identified in the FROM clause in the example. The table name is
specified in full, followed by a space and then the table alias. The EMPLOYEES table has been given
an alias of e, and the DEPARTMENTS table has an alias of d.
Guidelines
Table aliases can be up to 30 characters in length, but shorter aliases are better than longer ones.
If a table alias is used for a particular table name in the FROM clause, then that table alias must
be substituted for the table name throughout the SELECT statement.
Table aliases should be meaningful.
The table alias is valid for only the current SELECT statement.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
12
EMPLOYEES
DEPARTMENTS
LOCATIONS
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
u of n1
c a minimum
o
To join n tables together, youoneed
G
t
nthree tables, a
ah todjoin
join conditions. For example,
e
y
@two joins
tuis required.
minimum
of
8
S
4
s
cs thi
a
h use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
AdditionalivSearch
Conditions
li
r a
S
Sometimes
you may need to join more than two tables. For example, to display the last name, the
a
h
e
department
name, and the city for each employee, you have to join the EMPLOYEES,
n
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
13
Nonequijoins
EMPLOYEES
JOB_GRADES
le
b
a
r
e
f
an
r
t
n
ud
@
t
8
S
s4 this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Nonequijoins
li
riva
S
Ahnonequijoin
is a join condition containing something other than an equality operator.
a
e
n
The relationship between the EMPLOYEES table and the JOB_GRADES table is an example of a
nonequijoin. A relationship between the two tables is that the SALARY column in the EMPLOYEES
table must be between the values in the LOWEST_SALARY and HIGHEST_SALARY columns of the
JOB_GRADES table. The relationship is obtained using an operator other than equality (=).
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
14
Retrieving Records
with Nonequijoins
SELECT e.last_name, e.salary, j.grade_level
FROM
employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Nonequijoins
li
riva (continued)
S
The
aslide example creates a nonequijoin to evaluate an employees salary grade. The salary must be
h
e
n between any pair of the low and high salary ranges.
It is important to note that all employees appear exactly once when this query is executed. No
employee is repeated in the list. There are two reasons for this:
None of the rows in the job grade table contain grades that overlap. That is, the salary value for
an employee can lie only between the low salary and high salary values of one of the rows in the
salary grade table.
All of the employees salaries lie within the limits that are provided by the job grade table. That
is, no employee earns less than the lowest value contained in the LOWEST_SAL column or more
than the highest value contained in the HIGHEST_SAL column.
Note: Other conditions (such as <= and >=) can be used, but BETWEEN is the simplest. Remember
to specify the low value first and the high value last when using BETWEEN.
Table aliases have been specified in the slide example for performance reasons, not because of
possible ambiguity.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
15
Outer Joins
DEPARTMENTS
EMPLOYEES
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
uemployees in
c are no
o
G
There
o
t
n 190.
ahdepartment
e
y
d
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
ReturningivRecords
with
li No Direct Match with Outer Joins
r a
S
Ifhaarow does not satisfy a join condition, the row does not appear in the query result. For example, in
e
the
n equijoin condition of the EMPLOYEES and DEPARTMENTS tables, employee Grant does not
appear because there is no department ID recorded for her in the EMPLOYEES table. Instead of
seeing 20 employees in the result set, you see 19 records.
SELECT e.last_name, e.department_id, d.department_name
FROM
employees e, departments d
WHERE e.department_id = d.department_id;
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
16
You use an outer join to see rows that do not meet the join
condition.
The outer join operator is the plus sign (+).
le
b
a
r
e
f
an
r
t
n
no
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Using Outer
rivaJoins toli Return Records with No Direct Match
S
The
amissing rows can be returned if an outer join operator is used in the join condition. The operator
h
e
Sn is a plus sign enclosed in parentheses (+), and it is placed on the side of the join that is deficient in
information. This operator has the effect of creating one or more null rows, to which one or more
rows from the nondeficient table can be joined.
In the syntax:
table1.column =
is the condition that joins (or relates) the tables together
table2.column (+) is the outer join symbol, which can be placed on either side of the
WHERE clause condition, but not on both sides. (Place the outer join symbol following the name of
the column in the table without the matching rows.)
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
17
e.department_id(+) = d.department_id ;
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Using Outer
rivaJoins toli Return Records with No Direct Match (continued)
S
The
aslide example displays employee last names, department IDs, and department names. The
h
e
Sn Contracting department does not have any employees. The empty value is shown in the output.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
18
Self-Joins
EMPLOYEES (WORKER)
EMPLOYEES (MANAGER)
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
t
h
n
aWORKER
e
MANAGER_ID in the
table
is equal to
y
d
u
@
t
EMPLOYEE_ID
s48 thisinSthe MANAGER table.
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Joining a iTable
li
r va to Itself
S
Sometimes
you need to join a table to itself. To find the name of each employees manager, you need
a
h
e
to
join
the
EMPLOYEES
table to itself; this type of join is called a self-join.
n
For example, to find the name of Lorentzs manager, you need to do the following:
Find Lorentz in the EMPLOYEES table by looking at the LAST_NAME column.
Find the manager number for Lorentz by looking at the MANAGER_ID column.
Lorentzs manager number is 103.
Find the name of the manager who has EMPLOYEE_ID 103 by looking at the LAST_NAME
column. Hunolds employee number is 103, so Hunold is Lorentzs manager.
In this process, you look in the table twice. The first time you look in the table to find Lorentz in the
LAST_NAME column and the MANAGER_ID value of 103. The second time you look in the
EMPLOYEE_ID column to find 103 and the LAST_NAME column to find Hunold.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
19
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Joining a iTable
li (continued)
r va to Itself
S
The
aslide example joins the EMPLOYEES table to itself. To simulate two tables in the FROM clause,
h
e
there
are two aliases, namely w and m, for the same table, EMPLOYEES.
n
In this example, the WHERE clause contains the join that means where a workers manager number
matches the employee number for the manager.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
20
Summary
In this appendix, you should have learned how to use joins to
display data from multiple tables by using Oracle-proprietary
syntax for versions 8i and earlier.
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Summaryiva
li
r
S
There
a are multiple ways to join tables.
h
e
Sn Types of Joins
Cartesian products
Equijoins
Nonequijoins
Outer joins
Self-joins
Cartesian Products
A Cartesian product results in a display of all combinations of rows. This is done by omitting the
WHERE clause.
Table Aliases
Table aliases speed up database access.
Table aliases can help to keep SQL code smaller by conserving memory.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
21
Practice C: Overview
This practice covers writing queries to join tables using Oracle
syntax.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
Practice C:
li
rivOverview
S
This
a practice is designed to give you a variety of exercises that join tables using the Oracle syntax
h
e
Sn that was covered in this appendix.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
22
2. The HR department needs a report of all employees. Write a query to display the last name,
department number, and department name for all employees.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
23
4. Create a report to display the employee last name and employee number along with the
employees managers last name and manager number. Label the columns Employee, Emp#,
Manager, and Mgr#, respectively. Place your SQL statement in a text file named
lab_c_04.sql.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
24
le
b
a
r
e
f
an
r
t
n
6. Create a report for the HR department that displays employee last names,
a n department numbers,
s
a employee. Give each
and all the employees who work in the same department as ah
given
)
a
h
e
n
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
25
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
26
le
b
a
r
e
f
an
r
t
n
9. The HR department needs to find the name and hire date for all employees
o who were hired
n
before their managers, along with their managers name and hire date.
a Label the columns
s
a
Employee, Emp Hired, Manager, and Mgr Hired, respectively.
h Save the script to a file
)
named lab_c_09.sql.
om ide
a
h
e
n
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I C -Oracle
27
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Using SQL*Plus
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Objectives
After completing this appendix, you should be able to do the
following:
Log in to SQL*Plus
Edit SQL commands
Format output using SQL*Plus commands
Interact with script files
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Objectives
riva
S
You
a might want to create SELECT statements that can be used again and again. This appendix
h
e
n also covers the use of SQL*Plus commands to execute SQL statements. You learn how to format
output using SQL*Plus commands, edit SQL commands, and save scripts in SQL*Plus.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-2
SQL statements
Server
SQL*Plus
le
Query results
Buffer
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
u
SQL ooc
G
t
scripts
ah den
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
SQL and
rivSQL*Plus
S
a is a command language for communication with the Oracle9i Server from any tool or
SQL
h
e
n application. Oracle SQL contains many extensions. When you enter a SQL statement, it is stored
in a part of memory called the SQL buffer and remains there until you enter a new SQL
statement. SQL*Plus is an Oracle tool that recognizes and submits SQL statements to the
Oracle9i Server for execution. It contains its own command language.
Features of SQL
Can be used by a range of users, including those with little or no programming
experience
Is a nonprocedural language
Reduces the amount of time required for creating and maintaining systems
Is an English-like language
Features of SQL*Plus
Accepts ad hoc entry of statements
Accepts SQL input from files
Provides a line editor for modifying SQL statements
Controls environmental settings
Formats query results into basic reports
Accesses local and remote databases
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-3
SQL*Plus
An environment
Oracle-proprietary
Keywords can be
abbreviated
Commands do not allow
le
manipulation of values in erab
f
the database
ans
tr
n
no
a
s
a
) h e
m
co Guid SQL*Plus
SQL
SQL
SQL*Plus
o
t
statements
buffer
buffer
n
ahocommands
e
y
d
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li(continued)
SQL and
rivSQL*Plus
S
a following table compares SQL and SQL*Plus:
The
h
e
n
SQL
Is a language for communicating with the
Oracle server to access data
Is based on American National Standards
Institute (ANSI)standard SQL
Manipulates data and table definitions in the
database
Is entered into the SQL buffer on one or
more lines
Does not have a continuation character
Cannot be abbreviated
Uses a termination character to execute
commands immediately
Uses functions to perform some formatting
SQL*Plus
Recognizes SQL statements and sends them
to the server
Is the Oracle-proprietary interface for
executing SQL statements
Does not allow manipulation of values in the
database
Is entered one line at a time, not stored in the
SQL buffer
Uses a dash () as a continuation character if
the command is longer than one line
Can be abbreviated
Does not require termination characters;
executes commands immediately
Uses commands to format data
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-4
Overview of SQL*Plus
Log in to SQL*Plus
Describe the table structure
Edit your SQL statement
Execute SQL from SQL*Plus
Save SQL statements to files and append SQL statements
to files
ble
a
r
Execute saved files
fe
s
n
a
r
t
Load commands from file to buffer to edit
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
SQL*Plus
riva
S
a
SQL*Plus
is an environment in which you can do the following:
h
e
n Execute SQL statements to retrieve, modify, add, and remove data from the database
Format, perform calculations on, store, and print query results in the form of reports
Create script files to store SQL statements for repeated use in the future
SQL*Plus commands can be divided into the following main categories:
Category
Environment
Format
File manipulation
Execution
Edit
Interaction
Miscellaneous
Purpose
Affect the general behavior of SQL statements for the session
Format query results
Save, load, and run script files
Send SQL statements from the SQL buffer to the Oracle server
Modify SQL statements in the buffer
Create and pass variables to SQL statements, print variable values, and
print messages to the screen
Connect to the database, manipulate the SQL*Plus environment, and
display column definitions
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-5
Logging In to SQL*Plus
sqlplus [username[/password
[@database]]]
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
LoggingriIn
vato SQL*Plus
S
a you invoke SQL*Plus depends on which type of operating system or environment you are
How
h
e
n running.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-6
DESC[RIBE] tablename
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Displaying
rivaTable Structure
S
InaSQL*Plus, you can display the structure of a table using the DESCRIBE command. The result
h
e
n of the command is a display of column names and data types as well as an indication if a column
Null?
--------
Type
----------VARCHAR2(3)
NUMBER
NUMBER
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-7
Name
----------------------DEPARTMENT_ID
DEPARTMENT_NAME
MANAGER_ID
LOCATION_ID
Null?
Type
-------- -----------NOT NULL NUMBER(4)
NOT NULL VARCHAR2(30)
le
b
a
r
NUMBER(6)
fe
s
n
NUMBER(4)
ra
n
o
n
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Displaying
(continued)
rivaTable Structure
S
The
a example in the slide displays the information about the structure of the DEPARTMENTS
h
e
n table.
In the result:
Null?
Type
Description
Number value that has a maximum number of digits p, which
is the number of digits to the right of the decimal point s
Variable-length character value of maximum size s
Date and time value between January 1, 4712 B.C., and
December 31, 9999 A.D.
Fixed-length character value of size s
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-8
A[PPEND] text
C[HANGE] / old / new
C[HANGE] / text /
CL[EAR] BUFF[ER]
DEL
DEL n
DEL m n
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
SQL*Plus
Commands
rivEditing
S
a
SQL*Plus
commands are entered one line at a time and are not stored in the SQL buffer.
h
e
n
Command
A[PPEND] text
C[HANGE] / old / new
Description
Adds text to the end of the current line
Changes old text to new in the current line
C[HANGE] / text /
CL[EAR] BUFF[ER]
DEL
DEL n
DEL m n
Deletes
Deletes
Deletes
Deletes
Deletes
Guidelines
If you press [Enter] before completing a command, SQL*Plus prompts you with a line
number.
You terminate the SQL buffer either by entering one of the terminator characters
(semicolon or slash) or by pressing [Enter] twice. The SQL prompt then appears.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I DOracle
-9
I[NPUT]
I[NPUT] text
L[IST]
L[IST] n
L[IST] m n
R[UN]
n
n text
0 text
le
I[NPUT]
I[NPUT] text
L[IST]
L[IST] n
L[IST] m n
R[UN]
n
n text
0 text
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
SQL*Plus
Commands
(continued)
rivEditing
S
aCommand
h
Description
e
n
b
a
r
e
f
Note: You can enter only one SQL*Plus command for each SQL prompt. SQL*Plus commands
are not stored in the buffer. To continue a SQL*Plus command on the next line, end the first line
with a hyphen (-).
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Databaseprohibited.
10g: SQL Fundamentals
I D Oracle
- 10
SQL> LIST
1 SELECT last_name
2* FROM
employees
SQL> 1
le
b
a
r
e
f
1* SELECT last_name
SQL> A , job_id
an
r
t
n
no
a
s
a
h
)
SQL> L
e
m
d
o
i
c Gu
o
o
t
1 SELECT last_name, job_id
h
n
a
e
y
2* FROM
employees @
ud
t
8
S
s4 this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Using LIST,
riva n, andliAPPEND
S
a Use the L[IST] command to display the contents of the SQL buffer. The asterisk (*)
h
e
n
beside line 2 in the buffer indicates that line 2 is the current line. Any edits that you made
1* SELECT last_name, job_id
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Databaseprohibited.
10g: SQL Fundamentals
I D Oracle
- 11
SQL> L
1* SELECT * from employees
SQL> c/employees/departments
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
Using the
Command
li
rivCHANGE
S
a Use L[IST] to display the contents of the buffer.
h
e
n Use the C[HANGE] command to alter the contents of the current line in the SQL buffer. In
1* SELECT * from departments
this case, replace the employees table with the departments table. The new current line is
displayed.
Use the L[IST] command to verify the new contents of the buffer.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Databaseprohibited.
10g: SQL Fundamentals
I D Oracle
- 12
SAVE filename
GET filename
START filename
@ filename
EDIT filename
SPOOL filename
EXIT
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a Commands
li
SQL*Plus
rivFile
S
a statements communicate with the Oracle server. SQL*Plus commands control the
SQL
h
e
n environment, format query results, and manage files. You can use the commands described in
Description
@ filename
ED[IT]
ED[IT] [filename[.ext]]
SPO[OL]
[filename[.ext]|
OFF|OUT]
EXIT
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Databaseprohibited.
10g: SQL Fundamentals
I D Oracle
- 13
SQL>
1
2*
SQL>
L
SELECT last_name, manager_id, department_id
FROM
employees
SAVE my_query
le
b
a
r
e
f
an
LAST_NAME
MANAGER_ID DEPARTMENT_ID
r
t
------------------------- ---------- ------------on
n
a
King
90
s
a
Kochhar
100
90
) h e
m
...
co Guid
o
20 rows selected.
ho
nt
ya tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
SAVE
li
riva
S
Use
a the SAVE command to store the current contents of the buffer in a file. In this way, you can
h
e
n store frequently used scripts for use in the future.
START
Use the START command to run a script in SQL*Plus.
EDIT
Use the EDIT command to edit an existing script. This opens an editor with the script file in it.
When you have made the changes, quit the editor to return to the SQL*Plus command line.
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Databaseprohibited.
10g: SQL Fundamentals
I D Oracle
- 14
Summary
In this appendix, you should have learned how to use SQL*Plus
as an environment to do the following:
Execute SQL statements
Edit SQL statements
Format output
Interact with script files
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Summary
riva
S
a
SQL*Plus
is an execution environment that you can use to send SQL commands to the database
h
e
n server and to edit and save SQL commands. You can execute commands from the SQL prompt
Unauthorized reproduction
or distribution
Copyright 2009,
and/or its affiliates.
Oracle
Databaseprohibited.
10g: SQL Fundamentals
I D Oracle
- 15
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Objectives
After completing this appendix, you should be able to do the
following:
List the key features of Oracle SQL Developer
Install Oracle SQL Developer
Identify menu items of Oracle SQL Developer
Create a database connection
ble
a
r
Manage database objects
fe
s
n
a
r
t
Use SQL Worksheet
on
n
Execute SQL statements and SQL scripts s a
a
h
)
Create and save reports
m de
co Gui
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Objectives
riva
S
Inathis appendix, you are introduced to the graphical tool SQL Developer. You learn how to use
h
e
n SQL Developer for your database development tasks. You learn how to use SQL Worksheet to
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-2
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
What Is rOracle
iva SQLli Developer?
S
a SQL Developer is a free graphical tool designed to improve your productivity and
Oracle
h
e
n simplify the development of everyday database tasks. With just a few clicks, you can easily
create and debug stored procedures, test SQL statements, and view optimizer plans.
SQL Developer, the visual tool for database development, simplifies the following tasks:
Browsing and managing database objects
Executing SQL statements and scripts
Editing and debugging PL/SQL statements
Creating reports
You can connect to any target Oracle Database schema using standard Oracle Database
authentication. When connected, you can perform operations on objects in the database.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-3
Key Features
Developed in Java
Supports Windows, Linux, and Mac OS X platforms
Default connectivity by using the JDBC Thin driver
Does not require an installer
Connects to any Oracle Database version 9.2.0.1 and later
Bundled with JRE 1.5
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Developer
Key Features
riva of SQL
S
a SQL Developer is developed in Java, leveraging the Oracle JDeveloper IDE. The tool
ehOracle
Sn
runs on the Windows, Linux, and Mac OS X platforms. You can install SQL Developer on the
Database Server and connect remotely from your desktop, thus avoiding client server network
traffic.
Default connectivity to the database is through the JDBC Thin driver; so, no Oracle Home is
required. SQL Developer does not require an installer and you need to just unzip the
downloaded file.
With SQL Developer, users can connect to Oracle Database 9.2.0.1 and later versions, and all
Oracle Database editions including Express Edition. SQL Developer is bundled with JRE 1.5,
with an additional tools.jar to support Windows clients. Non-Windows clients need only
JDK 1.5.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-4
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a Developer
li
Installing
rivSQL
S
a SQL Developer does not require an installer. To install SQL Developer, you need an
Oracle
h
e
n
unzip tool.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-5
2
1
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a Developer
li
Menus for
rivSQL
S
a Developer has two main navigation tabs.
SQL
h
e
n Connections Navigator: By using this tab, you can browse database objects and users to
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-6
an
You can export connections to an XML file.
r
t
n
nolisted
Each additional database connection createda is
in
s
the connections navigator hierarchy. ha
le
b
a
r
e
f
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Creatingria
vaDatabaseli Connection
S
Aa
connection is a SQL Developer object that specifies the necessary information for connecting
h
e
n to a specific database as a specific user of that database. To use SQL Developer, you must have
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-7
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Connection (continued)
Creatingria
vaDatabase
S
Toacreate a database connection, perform the following steps:
h
e
n 1. Double-click <your_path>\sqldeveloper\sqldeveloper.exe.
2. On the Connections tabbed page, right-click Connections and select New Connection.
3. Enter the connection name, username, password, host name, port number, and SID for the
database you want to connect.
4. Click Test to make sure that the connection has been set correctly.
5. Click Connect.
Note: If you select the Save Password check box, the password is saved to an XML file. So,
after you close the SQL Developer connection and open it again, you will not be prompted for
the password.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-8
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li Objects
Browsing
rivDatabase
S
a you create a database connection, you can use the Database Navigator to browse through
After
h
e
n many objects in a database schema including Tables, Views, Indexes, Packages, Procedures,
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I EOracle
-9
a
s
a
) h e
m
co Guid
o
aho dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Creatingria
vaSchemaliObject
S
a Developer supports the creation of any schema object by executing a SQL statement in
SQL
h
e
n SQL Worksheet. Alternatively, you can create objects using the context menus. Once created,
you can edit the objects using an edit dialog or one of many context-sensitive menus.
As new objects are created or existing objects are edited, the DDL for those adjustments is
available for review. An Export DDL option is available if you want to create the full DDL for
one or more objects in the schema.
The slide shows creating a table using the context menu. To open a dialog box for creating a
new table, right-click Tables and select New Table. The dialog boxes for creating and editing
database objects have multiple tabs, each reflecting a logical grouping of properties for that type
of object.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
10
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Example
Creatingria
vaNew Table:
S
Inathe Create Table dialog box, if you do not select the Advanced check box, you can create a
h
e
n table quickly by specifying columns and some frequently used features.
If you select the Advanced check box, the Create Table dialog box changes to one with multiple
tabs, in which you can specify an extended set of features while creating the table.
The example in the slide shows creating the DEPENDENT table by selecting the Advanced
check box.
To create a new table, perform the following steps:
1. In the Connections Navigator, right-click Tables.
2. Select New Table.
3. In the Create Table dialog box, select Advanced.
4. Specify column information.
5. Click OK.
Although it is not required, you should also specify a primary key by selecting Primary Key in
the dialog box. Sometimes, you may want to edit the table that you have created. To edit a table,
right-click the table in the Connections Navigator and select Edit.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
11
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Using SQL
rivaWorksheet
S
a you connect to a database, a SQL Worksheet window for that connection is automatically
When
h
e
n opened. You can use SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus
statements. The SQL Worksheet supports SQL*Plus statements to a certain extent. SQL*Plus
statements that are not supported by the SQL Worksheet are ignored and not passed to the
database.
You can specify any actions that can be processed by the database connection associated with
the worksheet, such as:
Creating a table
Inserting data
Creating and editing a trigger
Selecting data from a table
Saving the selected data to a file
You can display a SQL worksheet by using any of the following two options:
Select Tools > SQL Worksheet.
Click the Open SQL Worksheet icon.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
12
1
3
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li (continued)
Using SQL
rivaWorksheet
S
a may want to use shortcut keys or icons to perform certain tasks such as executing a SQL
You
h
e
n statement, running a script, and viewing the history of SQL statements that you have executed.
You can use the SQL Worksheet toolbar that contains icons to perform the following tasks:
1. Execute Statement: Executes the statement at the cursor in the Enter SQL Statement box.
You can use bind variables in the SQL statements but not substitution variables.
2. Run Script: Executes all statements in the Enter SQL Statement box by using the Script
Runner. You can use substitution variables in the SQL statements but not bind variables.
3. Commit: Writes any changes to the database, and ends the transaction
4. Rollback: Discards any changes to the database, without writing them to the database, and
ends the transaction
5. Cancel: Stops the execution of any statements currently being executed
6. Execute Explain Plan: Generates the execution plan, which you can see by clicking the
Explain tab
7. Autotrace: Generates trace information for the statement, which you can see by clicking
the Autotrace tab
8. Clear: Erases the statement or statements in the Enter SQL Statement box
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
13
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a Statements
li
Executing
rivSQL
S
InaSQL Worksheet, you can use the Enter SQL Statement box to enter a single or multiple SQL
h
e
n statements. For a single statement, the semicolon at the end is optional.
When you type in the statement, the SQL keywords are automatically highlighted. To execute a
SQL statement, ensure that your cursor is within the statement and click the Execute Statement
icon. Alternatively, you can press the F9 key.
To execute multiple SQL statements and see the results, click the Run Script icon.
Alternatively, you can press the F5 key.
In the example in the slide, as there are multiple SQL statements, the first statement is
terminated with a semicolon. The cursor is in the first statement and, therefore, when the
statement is executed, results corresponding to the first statement are displayed in the Results
box.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
14
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Plan
Viewingrithe
vaExecution
S
a can execute a SQL script and view the execution plan. To execute a SQL script file,
You
h
e
n
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
15
Before
Formatting
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
After
c Gu
o
o
Formatting
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
Formatting
rivathe SQLli Code
S
a may want to beautify the indentation, spacing, capitalization, and line separation of the
You
h
e
n
SQL code. SQL Developer has the feature of formatting the SQL code.
To format the SQL code, right-click in the statement area, and select Format.
In the example in the slide, before formatting, the SQL code has the keywords not capitalized
and the statement not properly indented. After formatting, the SQL code is beautified with the
keywords capitalized and the statement properly indented.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
16
Using Snippets
Snippets are code fragments that may be just syntax or
examples.
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Using Snippets
riva
S
a may want to use certain code fragments when you are using the SQL Worksheet or creating
You
h
e
n or editing a PL/SQL function or procedure. SQL Developer has the feature called Snippets.
Snippets are code fragments, such as SQL functions, Optimizer hints, and miscellaneous
PL/SQL programming techniques. You can drag and drop snippets into the editor window.
To display Snippets, select View > Snippets.
The Snippets window is displayed on the right. You can use the drop-down list to select a group.
A Snippets button is placed in the right window margin, so that you can display the Snippets
window if it becomes hidden.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
17
Inserting a
snippet
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Using Snippets:
riva Example
S
Toainsert a snippet into your code in SQL Worksheet or in a PL/SQL function or procedure, drag
h
e
n the snippet from the Snippets window to the desired place in your code. Then, you can edit the
Editing the
snippet
syntax so that the SQL function is valid in the current context. To see a brief description of a
SQL function in a tool tip, place the cursor over the function name.
The example in the slide shows that CONCAT(char1, char2)is dragged from the Character
Functions group in the Snippets window. Then, the CONCAT function syntax is edited and the
rest of the statement is added such as in the following:
SELECT CONCAT(first_name, last_name)
FROM employees;
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
18
Using SQL*Plus
append
archive
attribute
break
change
clear
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Using SQL*Plus
riva
S
a Worksheet supports some SQL*Plus statements. SQL*Plus statements must be interpreted
SQL
h
e
n by the SQL Worksheet before being passed to the database; any SQL*Plus statements that are
not supported by the SQL Worksheet are ignored and not passed to the database. For example,
some of the SQL*Plus statements that are not supported by SQL Worksheet are listed in the
slide. For the complete list of SQL*Plus statements that are supported and not supported by SQL
Worksheet, refer to SQL Developer online Help.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
19
Database Reporting
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
a
li
Database
rivReporting
S
a Developer provides many reports about the database and its objects. These reports are
SQL
h
e
n
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
20
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li Report
Creatingria
vaUser-Defined
S
a
User-defined
reports are any reports that are created by SQL Developer users. To create a userh
e
n defined report, perform the following steps:
1. Right-click the User Defined Reports node under Reports, and select Add Report.
2. In the Create Report Dialog box, specify the report name and the SQL query to retrieve
information for the report. Then, click Apply.
In the example in the slide, the report name is specified as emp_sal. An optional description is
provided indicating that the report contains details of employees with salary >= 10000. The
complete SQL statement for retrieving the information to be displayed in the user-defined report
is specified in the SQL box. You can also include an optional tool tip to be displayed when the
cursor stays briefly over the report name in the Reports navigator display.
You can organize user-defined reports in folders, and you can create a hierarchy of folders and
subfolders. To create a folder for user-defined reports, right-click the User Defined Reports node
or any folder name under that node and select Add Folder.
Information about user-defined reports, including any folders for these reports, is stored in a file
named UserReports.xml under the directory for user-specific information.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
21
Summary
In this appendix, you should have learned how to use SQL
Developer to do the following:
Browse, create, and edit database objects
Execute SQL statements and scripts in SQL Worksheet
Create and save custom reports
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
ah dent
y
@ Stu
8
4
s this
c
ha use
e
n
Copyright 2009, Oracle. All rights reserved.
s
(
to
a
e
av cens
t
s
li
Summary
riva
S
a Developer is a free graphical tool to simplify database development tasks. Using SQL
SQL
h
e
n Developer, you can browse, create, and edit database objects. You can use SQL Worksheet to
run SQL statements and scripts. SQL Developer enables you to create and save your own special
set of reports for repeated use.
Unauthorized reproductionOracle
or distribution
Copyright 2009,
and/or its affiliates.
Databaseprohibited.
10g: SQL Fundamentals
I E -Oracle
22
Index
le
b
a
r
e
f
an
r
t
n
a
h
e
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li
va
Sri
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
A
Adding Data Through a View 10-15
ADD_MONTHS Function 3-22, 3-23, 3-46, 3-60
Advanced Features of the SELECT Statement 6-2
Alias 1-4, 1-8, 1-14, 1-15, 1-16, 1-17, 1-25, 1-37, 1-40, 2-7,
2-20, 2-21, 2-31, 3-46, 4-12, 5-8, 5-11, 5-12, 5-20, 5-29, 7-17,
7-21, 9-33, 10-7, 10-9, 10-11, 11-22, C-11, C-12, C-15, C-20, C-21
ALL Operator 6-16, 7-11, 7-12, 7-18, 7-22
ALL_COL_COMMENTS Dictionary View 11-19
ALL_OBJECTS Dictionary View 11-5, 11-7
ALL_TAB_COMMENTS Dictionary View 11-19
ALTER SEQUENCE Statement 10-30, 10-31
ALTER TABLE Statement 9-34, 10-27
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
ui-30, 3-26, 3-54,
ci-25, i-28,
o
G
American National Standards Institute (ANSI) o
i-12,
t
ah den
y
5-4, 8-27, D-4
@ Stu
8
4
Ampersand Substitution 2-2, 2-24,
2-25,
s th2-28,
is 2-31, 2-32, 3-11
c
Double-ampersand
ha (&&)
se2-23, 2-28
e
u
n
ANY Operator 6-15(s
to
a
e
av ce3-3,
ns 3-5, 3-15, 3-50
t
Arguments
Function
s
li
riva Expressions
Arithmetic
1-9, 1-13, 1-16, 2-4
S
a
eh
n
S
B
BETWEEN Range Condition 2-9
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
C
Calculations 1-9, 1-14, 1-38, 3-3, 3-14, 3-20, 3-25, 3-60, 3-61, 4-26, D-5
Cardinality i-20
Cartesian Product 5-2, 5-5, 5-26, 5-27, 5-28, 5-29, C-4, C-5, C-21
CASE Expression 3-51, 3-54, 3-55, 3-56, 3-57, 3-60
Case-manipulation Functions 3-7, 3-9, 3-10
CAT View 11-8
Character strings 1-16, 1-17, 1-18, 2-6, 2-15, 3-9, 3-11, 3-35, 3-38
Character-manipulation Functions 3-7, 3-11, 3-12
CHECK Constraint 8-7, 9-28, 10-21, 11-12, 11-13
COALESCE Function 3-52, 3-53, 4-5
Column Alias 1-8, 1-14, 1-15, 1-17, 1-37, 2-21, 3-46, 4-12, 5-11,
5-12, 9-33, 10-9, 10-11, C-11, C-12
COMMENT Statement 11-19
COMMENTS Column 11-6, 11-19
COMMIT Statement 8-29, 8-32, 8-33
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
Composite Unique Key 9-22, 9-23
ah dent
y
@ S
tu 3-46, 3-60-61, 8-41, C-7, E-18
CONCAT Function 1-16, 1-18, 1-42,8
3-11-12,
3-38,
4
s
cs thi
eh
n
S
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
D
Data from More Than One Table 5-2, 5-3, 5-5, C-2, C-3, C-7
Data Structures i-9, i-14, i-17, 9-3
Data Types i-6, i-9, 1-9, 1-35, 1-36, 3-3, 3-25, 3-26, 3-48,
3-50, 3-60, 3-61, 4-5, 4-7, 5-6, 5-8, 7-8, 7-13, 7-15, 8-7,
8-11, 9-2, 9-9-12, 9-14, 9-36, 11-10-12, D-7, D-8
Data Warehouse Applications i-9
Database I, 1-2, 1-3, 1-22-29, 1-34, 1-37, 1-39, 2-2, 2-6, 3-10, 3-17-20, 3-26,
4-13, 4-16, 5-29, 6-7, 8-3, 8-13, 8-18, 8-24, 8-25, 8-29, 8-31, 8-32,
8-37, 8-38, 8-39, 9-2-6, 9-13, 9-18, 9-33-37, 10-3, 10-16, 10-20, 10-22,
10-23, 10-33, 10-34, 10-39, 10-41, 10-42, 11-3-12, 11-17-19, B-3,
C-6, C-7, C-9, C-21, D-3-6, D-15, E
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
a se
eh9-12,
u9-14
n
Datetime Data Types9-11,
o
(
t
DBA_OBJECTS
va11-5 nse
a
t
s licei-21, i-23, i-26, i-27, i-30
a
DBMS
i-2, i-13, i-14,
v
i
Sr Statement 8-25, 8-29, 9-5
aDCL
eh
n
S
DDL Statement 8-11, 8-24, 8-25, 8-29, 8-36, 9-1, 9-5, 9-8, 9-35
DECODE Function 3-54, 3-56, 3-57, 3-58, 3-59, 3-60, 3-67
Default Date Display 2-6, 3-17
DEFAULT Option 9-7, 10-24, 10-25
Default Sort Order 2-21, 4-16
DELETE Statement 8-18, 8-19, 8-20, 8-21, 8-37, 8-39
DESC Keyword 2-21
DESCRIBE Command 1-35, 1-36, 8-7, 9-8, 9-33, 10-8, 10-12, 11-11, D-7
Dictionary Views i-3, 11-1, 11-2, 11-3, 11-6, 11-19, 11-20, 11-21
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
D
DISTINCT Keyword 1-20, 4-9, 7-11, 10-13, 10-14, 10-15, 10-27
DML Operations on Data Through a View 10-13
Double-ampersand 2-23, 2-28
DROP ANY INDEX Privilege 10-38
DROP ANY VIEW Privilege 10-20
DROP INDEX Statement 10-38
DROP SYNONYM Statement 10-41
DROP TABLE Statement 9-35
DROP VIEW Statement 10-20
DUAL Table 3-14, 3-19
le
b
a
r
e
f
an
r
t
n
Duplicate Rows i-23, 1-20, 4-8, 7-8, 7-11, 7-12, 7-18, 7-22
n
a
s
E
ha
)
Entity Relationship i-17, i-19, i-20
om uide
c
o C-14,t C-15,
G C-21
Equijoins 5-2, 5-9, 5-19, 5-20, 5-29, C-2, C-8, o
C-9,
h
n
a
y tude
ESCAPE Option 2-12
@
Execute SQL 1-2, 1-28, 1-37, D-2,
s48D-5,thD-15,
is SE-2, E-12, E-22
c
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
G
Generate Unique Numbers 10-3, 10-23
GROUP BY Clause 4-2, 4-11, 4-12, 4-13, 4-14, 4-16, 4-17, 4-20,
4-21, 4-24, 6-12, 10-13, 10-14, 10-15
GROUP BY Column 4-14, 4-16
Group Functions 3-4, 4-1-13, 4-17, 4-18, 4-20, 4-23-26, 6-10, 6-19, 10-12-15
Group Functions in a Subquery 6-10
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
)
e
m
d
o
i
c Gu
o
o
t
5-26, 5-27, 10-37, C-2, C-4-7, C-13, C-14,
C-17
n
ah C-16,
e
y
ud10-33, 10-34-38,
Index 1-27, 9-3, 9-6, 9-23, 9-24, 9-35,
10-2, 10-3,
@
t
8
S
4 11-8,
s11-7,
isE-5, E-9
10-42, 10-43, 10-46, 11-3,
c
h
t
ha 8-22,
se8-23, 8-41, 9-8, 9-19, 10-27, 10-28
e
INSERT Statement 8-5,
8-11,
u
n
(si-30, 8-7,
to8-21, 9-17, 9-19, 9-22, 9-30,
a
e
Integrity Constraint
av 10-16,
ns11-3
t
e
s
c
9-31,
9-32,
li
iva
r
S
aInternational Standards Organization (ISO) i-28
IF-THEN-ELSE Logic 3-54, 3-55, 3-57, 3-60
Implicit Data Type Conversion 3-26, 3-27, 3-28
IN Condition 2-7, 2-10, 5-2, 5-4, 5-13, 5-18, 5-19, 5-21, 5-22,
eh
n
S
J
Java i-4, i-7, i-10, i-27, E-4
Joining Tables 5-5, 5-22, 5-30, C-7
K
Keywords 1-4, 1-7, 1-31, 1-32, 5-6, 9-26, 9-27, D-4, E-14, E-16
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
L
LEFT OUTER Join 5-23
LIKE Condition 2-11, 2-12
Literal 1-17, 1-18, 1-19, 2-4, 2-11, 2-12, 3-11, 3-35, 3-51,
3-55, 7-20, 9-7
Logical Condition 2-14
Logical Subsets 10-4
M
MAX Function 4-6, 4-7
MIN Function 4-7
MINUS Operator 7-15, 7-16, 7-17, 7-22, 7-23
MOD Function 3-16
le
an
r
t
n
n
a
s
Modifier 3-41, 3-42
ha
)
MONTHS_BETWEEN Function 3-6
om uide
c
a se t
h
e
n to u
s
(
N
va nse
a
t
e 11-5
s 9-4,lic9-18,
Naming
1-33,
a
v
i
Sr Functions 3-45, 3-61
Nested
a
neh Nested SELECT 6-4, 6-20
b
a
r
e
f
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
O
Object Relational i-2, i-9, i-13, i-30
Object Relational Database Management System i-2, i-30
Object-oriented Programming i-9
OLTP i-9
ON clause 5-5, 5-13-18, 5-22, 7-12, 10-16, 10-17
ON DELETE CASCADE 9-27
ON DELETE SET NULL 9-27
Online Transaction Processing i-9
OR REPLACE Option 10-8, 10-11
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
c
ha use
e
n
(s e to
a
av cens
t
s
li 7-22, 9-10, 10-27, C-11
va 7-17, 7-21,
ri6-6,
P
PRIMARY KEY Constraint 9-20, 9-24
Projection 1-3
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
R
RDBMS i-2, i-14, i-21, i-23, i-26, i-27, i-30
Read Consistency 8-31, 8-37, 8-38
READ ONLY Option 10-18
Read-only Constraint 10-19
REFERENCES 1-23, 1-27, 1-34, 8-29, 9-25-29, 11-13, E-6, E-9
Referential Integrity Constraint 8-21
Relational Database i
Relational Database Management System i-2, i-14, i-27, i-30
Restrict the Rows 1-3, 2-2, 2-4, 4-19, C-10
Retrieve Data from a View 10-10
Return a Value 3-3, 3-6, 3-14, 3-22
RIGHT OUTER Join 5-24
ROLLBACK Statement 8-2, 8-25-27, 8-29, 8-31, 8-34, 8-36, 8-39
ROUND and TRUNC Functions 3-24
ROUND Function 3-14, 3-15
le
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
RR Date Format 3-43, 3-44
a
y tude
@
Rules of Precedence 1-10, 1-11, 2-18,
2-19
s48 this S
c
ha use
e
n
(s e to
S
a
v
aStatement
ns8-28
SAVEPOINT
t
e
s
c
riva1-22-27,li9-2, 9-5, 9-6, 9-19, 9-29, 9-36, 10-1, 10-34, 10-40,
Schema
S
a 11-2-4, 11-7, 11-21-23, B-3, E-3, E-7, E-9, E-10
h
e
n
b
a
r
e
f
SELECT Statement i-3, 1-1-6, 1-17, 1-18, 1-37, 1-39, 2-5, 2-8, 2-9, 2-11,
2-19, 2-20, 2-23, 2-27, 2-29, 2-31, 2-32, 3-2, 3-10, 4-8, 4-13-18,
5-2, 5-12, 6-2-9, 6-19, 6-20, 7-8, 7-13-20, 8-19, 8-31, 8-37, 8-38, 9-32,
10-4, 10-7, 10-12, 10-27, 11-6, 11-15, C-2, C-7, C-12, D-2
Selection 1-3, 2-3
Sequences 2-21, 9-6, 10-2, 10-3, 10-22, 10-23, 10-29, 10-38, 10-42,
10-43, 11-3, 11-16, 11-17, 11-20, 11-23
Set operators 7-1, 7-2, 7-3, 7-17, 7-18, 7-23, 7-24
SET VERIFY ON 2-30
Sets of Rows 4-3
SGA i-27
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
S
Single-row Functions 2-6, 3-1, 3-4, 3-5, 3-6, 3-45, 3-60, 4-3
Single-row Operator 6-4, 6-6, 6-8, 6-12, 6-14, 6-19
Single-row Subqueries 6-2, 6-6, 6-7, 6-8, 6-9
SOME Operator 6-15
Sorted i-3, 2-35, 7-8, 7-10, 7-11, 7-18, 7-21, 7-22
Sorting 2-1, 2-21, 2-31, 2-32
SQL Developer i-2, 1-2, 1-7, 1-8, 1-14, 1-20, 1-21, 1-22,
1-23, 1-24, 1-25, 1-27, 1-28, 1-32, 1-34, 1-35, 1-37, 1-38, 1-39,
1-42, 2-22-30, 8-25, 8-29, 8-30, E
Statement-level Rollback 8-36
Structured Query Language i-25, i-26, 1-2
Sub-SELECT 6-4
Subqueries in UPDATE statements 8-16
le
b
a
r
e
f
an
r
t
n
no
a
s
a
h
Subqueries to Delete Rows 8-20
)
e
m
d
o
i
Subquery 6-3-19, 8-11, 8-13, 8-15, 8-20, 8-22, 8-23,
u 9-33, 9-36,
c 9-10,G9-32,
o
o
t
10-7, 10-8, 10-9, 10-11, 10-27
ah den
y
@2-27,S2-30-32,
tu 8-10, E-13
Substitution Variables 2-22, 2-23, 2-26,
8
4
s
cs thi
eh
n
S
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
T
Table Alias 5-12, 5-20, 5-29, C-12, C-15, C-21
Table Prefixes 5-11, C-11, C-12
Three-way Join 5-18
TO_CHAR Function 3-32, 3-37, 3-38, 3-39, 3-40
TO_NUMBER or TO_DATE Functions 3-41
Transactions 8-2, 8-24, 8-25, 8-27, 8-36, 8-40, 9-35
TRUNC Function 3-15, 3-24
Tuple i-23
Types of Indexes 10-35
le
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
Unique Identifier i-19, i-20
@
S10-46
48 h10-43,
Unique Index 9-23, 9-24, 10-35,s10-37,
s
i
c
t
a s9-22
UNIQUE Key Integrity Constraint
e
h
e
u 8-16, 10-27
n to 8-15,
UPDATE Statement(s
8-13, 8-14,
va3-10 nse
a
UPPER Function
t
s lice
a
v
i
USER_CONS_COLUMNS
Dictionary View 11-14
r
aS
UNION ALL Operator 7-11, 7-12, 7-18, 7-22
UNION Clause 7-12
UNION Operator 7-8, 7-9, 7-10, 7-19, 7-20, 7-21, 7-22, 7-23
UNIQUE Constraint 9-22, 9-23, 10-33, 10-35, 10-37, 11-13
eh
n
S
b
a
r
e
f
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
V
VALUES Clause 8-5, 8-7, 8-11, 8-22, 10-27
VARIANCE 4-4, 4-7, 4-24
VERIFY Command 2-30
Views i-3, i-8, 1-27, 9-6, 9-35, 10-2, 10-3, 10-4, 10-5, 10-6,
10-13, 10-16, 10-20, 10-21, 10-38, 10-40, 10-42, 11-1, 11-2, 11-3, 11-4,
11-5, 11-6, 11-7, 11-8, 11-15, 11-19, 11-20, 11-21, 11-23, E-9
W
When to Create an Index 10-37
WHERE Clause 2-3, 2-4, 2-5, 2-6, 2-7, 2-8, 2-10, 2-14, 2-22,
2-23, 2-26, 2-27, 2-31, 2-32, 3-10, 4-8, 4-12, 4-13, 4-18, 4-19,
4-24, 4-26, 5-7, 5-8, 5-13, 5-17, 5-29, 6-2, 6-4, 6-11, 6-12,
6-13, 6-18, 7-17, 8-14, 8-19, 10-37, 11-4, C-4, C-7, C-9, C-10,
C-11, C-17, C-20, C-21
Wildcard Search 2-11
WITH CHECK OPTION 10-7, 10-8, 10-16, 10-17, 11-13
le
b
a
r
e
f
an
r
t
n
an
s
ha
)
om uide
c
oo nt G
h
a
y tude
@
s48 this S
X
c
e E-20, E-21
ha E-7,usE-8,
XML i-4, i-6, i-27, 1-24,e
1-25,
n
(s e to
a
av cens
t
s
li
riva
S
eha
Sn
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.