Documentos de Académico
Documentos de Profesional
Documentos de Cultura
m
lco
ai
m
t
o e
h
@
Oracle Database 11g:
SQL uid
z
v
G
a
Fundamentalslm
I o dent
e
s
tu
n
S
a
(
is
h
Volume
IIA
StudenttGuide
R
R use
A
EG se to
Z
LA licen
E
ZU able
N
LE sfer
A
V ran
O
M on-t
L
SE s a n
N
A ha
D49996GC20
Edition 2.0
October 2009
D63148
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Salome Clement
Brian Pottle
Puja Singh
Disclaimer
Technical Contributors
and Reviewers
Anjulaponni Azhagulekshmi
Clair Bennett
Zarko Cesljas
Yanti Chang
Gerlinde Frenzen
Steve Friedberg
Joel Goodman
Nancy Greenberg
Pedro Neves
Surya Rekha
Helen Robertson
Lauran Serhal
Tulika Srivastava
ai
m
t
o e
h
@ uid
z
v
Editors
a nt G
o
lm ude
Aju Kumar
e
s
Arijit Ghosh
an is St
(
RA se th
R
Graphic Designer
GA to u
E
Z nse
Rajiv Chandrabhanu
A
EL e lice
U
NZ rabl
Publishers
E
AL nsfe
Pavithran Adka
V
O n-tra
VeenaM
Narasimhan
EL a no
S
AN has
Trademark Notice
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Contents
Introduction
Lesson Objectives I-2
Lesson Agenda I-3
Course Objectives I-4
Course Agenda I-5
Appendixes Used in the Course I-7
Lesson Agenda I-8
Oracle Database 11g: Focus Areas I-9
Oracle Database 11g I-10
Oracle Fusion Middleware I-12
Oracle Enterprise Manager Grid Control I-13
Oracle BI Publisher I-14
Lesson Agenda I-15
Relational and Object Relational Database Management Systems I-16
Data Storage on Different Media I-17
Relational Database Concept I-18
Definition of a Relational Database I-19
Data Models I-20
Entity Relationship Model I-21
Entity Relationship Modeling Conventions I-23
Relating Multiple Tables I-25
Relational Database Terminology I-27
Lesson Agenda I-29
Using SQL to Query Your Database I-30
SQL Statements I-31
Development Environments for SQL I-32
Lesson Agenda I-33
Human Resources (HR) Schema I-34
Tables Used in the Course I-35
Lesson Agenda I-36
Oracle Database 11g Documentation I-37
Additional Resources I-38
Summary I-39
Practice I: Overview I-40
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
iii
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Objectives 1-2
Lesson Agenda 1-3
Capabilities of SQL SELECT Statements 1-4
Basic SELECT Statement 1-5
Selecting All Columns 1-6
Selecting Specific Columns 1-7
Writing SQL Statements 1-8
Column Heading Defaults 1-9
Lesson Agenda 1-10
Arithmetic Expressions 1-11
Using Arithmetic Operators 1-12
Operator Precedence 1-13
Defining a Null Value 1-14
Null Values in Arithmetic Expressions 1-15
Lesson Agenda 1-16
Defining a Column Alias 1-17
Using Column Aliases 1-18
Lesson Agenda 1-19
Concatenation Operator 1-20
Literal Character Strings 1-21
Using Literal Character Strings 1-22
Alternative Quote (q) Operator 1-23
Duplicate Rows 1-24
Lesson Agenda 1-25
Displaying the Table Structure 1-26
Using the DESCRIBE Command 1-27
Quiz 1-28
Summary 1-29
Practice 1: Overview 1-30
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
2
iv
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
ce
EL e li2-36
Using the VERIFYUCommand
NZ rabl
Quiz 2-37
E
fe
AL 2-38
Summary
s
V
n
a
OPractice
2:
2-39
trOverview
M
n
L
o
SE s a n
N
A ha 3 Using Single-Row Functions to Customize Output
Objectives 3-2
Lesson Agenda 3-3
SQL Functions 3-4
Two Types of SQL Functions 3-5
Single-Row Functions 3-6
Lesson Agenda 3-8
Character Functions 3-9
Case-Conversion Functions 3-11
Using Case-Conversion Functions 3-12
Character-Manipulation Functions 3-13
Using the Character-Manipulation Functions 3-14
Lesson Agenda 3-15
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
4 Using Conversion Functions and Conditional Expressions
an is St
(
Objectives 4-2
RA se th
R
Lesson Agenda 4-3
GA to u
E
Z nse
Conversion Functions 4-4
A
Implicit Data Type Conversion
EL4-5e lice
U
l
Explicit Data Type Conversion
NZ rab4-7
E
Lesson Agenda
AL 4-10nsfe
V
Using O
the TO_CHAR
traFunction with Dates 4-11
M
n
L nofothe Date Format Model 4-12
EElements
S
a
AN hUsing
as the TO_CHAR Function with Dates 4-16
Using the TO_CHAR Function with Numbers 4-17
Using the TO_NUMBER and TO_DATE Functions 4-20
Using the TO_CHAR and TO_DATE Function with the RR Date Format 4-22
Lesson Agenda 4-23
Nesting Functions 4-24
Nesting Functions: Example 1 4-25
Nesting Functions: Example 2 4-26
Lesson Agenda 4-27
General Functions 4-28
NVL Function 4-29
Using the NVL Function 4-30
Using the NVL2 Function 4-31
vi
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
th
Using the MIN and MAX Functions 5-8 RA
e
R
s
u
Using the COUNT Function 5-9 GA
o
t
E
Z 5-10nse
Using the DISTINCT Keyword
A
L lice5-11
Group Functions U
andENull Values
Z 5-12able
N
Lesson Agenda
E fer
LGroups
A
Creating
sof Data 5-13
V
n
a
r of Data: GROUP BY Clause Syntax 5-14
-tGroups
MOCreating
n
L
o
n the GROUP BY Clause 5-15
SE s aUsing
N
A ha Grouping by More Than One Column 5-17
Using the GROUP BY Clause on Multiple Columns 5-18
Illegal Queries Using Group Functions 5-19
Restricting Group Results 5-21
Restricting Group Results with the HAVING Clause 5-22
Using the HAVING Clause 5-23
Lesson Agenda 5-25
Nesting Group Functions 5-26
Quiz 5-27
Summary 5-28
Practice 5: Overview 5-29
vii
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
Retrieving Records with the ON Clause 6-16
@ uid
z
v
Creating Three-Way Joins with the ON Clause 6-17
a nt G
o
lm ude
Applying Additional Conditions to a Join 6-18
e
s
Lesson Agenda 6-19
an is St
(
Joining a Table to Itself 6-20
RA se th
R
u
Self-Joins Using the ON Clause 6-21 GA
o
t
E
Z nse
Lesson Agenda 6-22
A
Nonequijoins 6-23
EL e lice
U
ZNonequijoins
Retrieving Records N
with
bl 6-24
a
E
r
Lesson Agenda
AL 6-25nsfe
V
Returning
O Records
trawith No Direct Match Using OUTER Joins 6-26
M
n
L Versus
EINNER
no OUTER Joins 6-27
S
a
AN hLEFT
as OUTER JOIN 6-28
RIGHT OUTER JOIN 6-29
Creating Joins with the ON Clause 6-15
viii
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
Using the ALL Operator in Multiple-Row Subqueries
an 7-19is St
(
Using the EXISTS Operator 7-20
RA se th
R
A to u
Lesson Agenda 7-21
G
E
Null Values in a Subquery 7-22
Z nse
A
Quiz 7-24
EL e lice
U
Summary 7-25
NZ rabl
E
PracticeL7: Overview
A nsfe 7-26
V
O n-tra
M
o the Set Operators
EL 8 aUsing
n
S
8-2
AN has Objectives
Lesson Agenda 8-3
Set Operators 8-4
Set Operator Guidelines 8-5
Oracle Server and Set Operators 8-6
Lesson Agenda 8-7
Tables Used in This Lesson 8-8
Lesson Agenda 8-12
UNION Operator 8-13
Using the UNION Operator 8-14
UNION ALL Operator 8-16
Using the UNION ALL Operator 8-17
Lesson Agenda 8-18
ix
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
9
Manipulating Data
Objectives 9-2
Lesson Agenda 9-3
Data Manipulation Language 9-4
Adding a New Row to a Table 9-5
INSERT Statement Syntax 9-6
Inserting New Rows 9-7
Inserting Rows with Null Values 9-8
Inserting Special Values 9-9
Inserting Specific Date and Time Values 9-10
Creating a Script 9-11
Copying Rows from Another Table 9-12
Lesson Agenda 9-13
Changing Data in a Table 9-14
UPDATE Statement Syntax 9-15
Updating Rows in a Table 9-16
Updating Two Columns with a Subquery 9-17
Updating Rows Based on Another Table 9-18
Lesson Agenda 9-19
Removing a Row from a Table 9-20
DELETE Statement 9-21
Deleting Rows from a Table 9-22
Deleting Rows Based on Another Table 9-23
TRUNCATE Statement 9-24
Lesson Agenda 9-25
Database Transactions 9-26
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
FOR UPDATE Clause: Examples 9-44
a nt G
o
lm ude
Quiz 9-46
e
s
an is St
Summary 9-47
(
Practice 9: Overview 9-48
RA se th
R
GA to u
E
eManage Tables
Z and
10 Using DDL Statements to Create
s
A
n
Objectives 10-2 EL
ice
l
U
e
Lesson Agenda
NZ 10-3rabl
E
Database
fe10-4
AL Objects
s
V
n
ONamingn-Rules
tra 10-5
M
EL aLesson
no Agenda 10-6
S
AN has CREATE TABLE Statement 10-7
Referencing Another Users Tables 10-8
DEFAULT Option 10-9
Creating Tables 10-10
Lesson Agenda 10-11
Data Types 10-12
Datetime Data Types 10-14
Lesson Agenda 10-15
Including Constraints 10-16
Constraint Guidelines 10-17
Defining Constraints 10-18
NOT NULL Constraint 10-20
UNIQUE Constraint 10-21
xi
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
11 Creating Other Schema Objects
e
s
Objectives 11-2
an is St
(
Lesson Agenda 11-3
RA se th
R
Database Objects 11-4
GA to u
E
Z nse
What Is a View? 11-5
A
Advantages of Views 11-6EL
ice
l
U
e
Simple Views and Complex
bl 11-7
NZ rViews
a
E
Creating a View
AL 11-8nsfe
V
Retrieving
from
O Data
tra a View 11-11
M
n
L naoView 11-12
EModifying
S
s a a Complex View 11-13
AN hCreating
a
Rules for Performing DML Operations on a View 11-14
Using the WITH CHECK OPTION Clause 11-17
Denying DML Operations 11-18
Removing a View 11-20
Practice 11: Overview of Part 1 11-21
Lesson Agenda 11-22
Sequences 11-23
CREATE SEQUENCE Statement: Syntax 11-25
Creating a Sequence 11-26
NEXTVAL and CURRVAL Pseudocolumns 11-27
Using a Sequence 11-29
Caching Sequence Values 11-30
xii
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
Appendix A: Practice Solutions
v
a nt G
o
lm ude
e
s
Appendix B: Table Descriptions
an is St
(
RA se th
Appendix C: Using SQL Developer AR
u
G
o
t
Objectives C-2
E
Z nse
A
What Is Oracle SQL Developer?
L liceC-3
E
U
Specifications of
ZSQL aDeveloper
le C-4
b
N
r
SQL Developer
C-5
LE s1.5feInterface
A
V arDatabase
Creating
an Connection C-7
O
t
n Database Objects C-10
LM Browsing
o
E
n
S
Displaying the Table Structure C-11
AN has aBrowsing Files C-12
Creating a Schema Object C-13
Creating a New Table: Example C-14
Using the SQL Worksheet C-15
Executing SQL Statements C-18
Saving SQL Scripts C-19
Executing Saved Script Files: Method 1 C-20
Executing Saved Script Files: Method 2 C-21
Formatting the SQL Code C-22
Using Snippets C-23
Using Snippets: Example C-24
Debugging Procedures and Functions C-25
Database Reporting C-26
xiii
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
Using the CHANGE Command D-12
@ uid
z
v
SQL*Plus File Commands D-13
a nt G
o
Using the SAVE, START Commands D-14
lm ude
e
s
SERVEROUTPUT Command D-15
an is St
(
th
Using the SQL*Plus SPOOL Command D-16 RA
e
R
s
u
Using the AUTOTRACE Command D-17GA
o
t
E
Z nse
Summary D-18
A
EL e lice
U
Appendix E: Using JDeveloper
NZ rabl
E
Objectives E-2
AL nsfe
V
OracleO
JDeveloper
traE-3
M
n
L nNavigator
E-4
o
EDatabase
S
a
s Connection E-5
AN hCreating
a
Browsing Database Objects E-6
Executing SQL Statements E-7
Creating Program Units E-8
Compiling E-9
Running a Program Unit E-10
Dropping a Program Unit E-11
Structure Window E-12
Editor Window E-13
Application Navigator E-14
Deploying Java Stored Procedures E-15
xiv
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN hasAdditional Practices and Solutions
Index
xv
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Manipulating Data
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Objectives
After completing this lesson, you should be able to do the
following:
Describe each data manipulation language (DML)
statement
Insert rows into a table
Update rows in a table
Delete rows from a table
Control transactions
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Objective
E
e
AL nstofuse
In this lesson, youVlearn how
the data manipulation language (DML) statements to insert rows
a
r
O
t
into a table,
rows in a table, and delete existing rows from a table. You also learn
n
LMupdate
oexisting
n
howStoEcontrol
transactions
with
the COMMIT, SAVEPOINT, and ROLLBACK statements.
a
N
s
A ha
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
o e
h
@ uid
Read consistency
z
v
a nt G
o
FOR UPDATE clause in a SELECT statement
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Data Manipulation Language
E
fe is a core part of SQL. When you want to add, update, or delete
AL n(DML)
s
V
Data manipulation
language
O nyou
traexecute a DML statement. A collection of DML statements that form a
data in the
database,
M
L no
Eunit
logical
ofawork is called a transaction.
S
N
s
AConsider
haa banking database. When a bank customer transfers money from a savings account to a
checking account, the transaction might consist of three separate operations: decreasing the savings
account, increasing the checking account, and recording the transaction in the transaction journal.
The Oracle server must guarantee that all the three SQL statements are performed to maintain the
accounts in proper balance. When something prevents one of the statements in the transaction from
executing, the other statements of the transaction must be undone.
Note
Most of the DML statements in this lesson assume that no constraints on the table are violated.
Constraints are discussed later in this course.
In SQL Developer, click the Run Script icon or press [F5] to run the DML statements. The
feedback messages will be shown on the Script Output tabbed page.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
DEPARTMENTS
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Adding a New Row to E
aN
Table rab
fe the addition of a new department to the DEPARTMENTS table.
AL illustrates
s
The graphic in the
slide
V
n
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
INSERT Statement Syntax
E
feby issuing the INSERT statement.
ALto antable
s
You can add newV
rows
O n-tra
M
In the syntax:
EL a no Is the name of the table
S
table
ANcolumn
Is the name of the column in the table to populate
has
value
Is the corresponding value for the column
Note: This statement with the VALUES clause adds only one row at a time to a table.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Inserting New Rows EN
L sfer
A
Because you can V
insert a a
new
n row that contains values for each column, the column list is not
r
OINSERT
t
requiredLinM
the
clause.
However, if you do not use the column list, the values must be listed
n
o
E
according
S sto athendefault order of the columns in the table, and a value must be provided for each
N
Acolumn.
a
h
DESCRIBE departments
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
departments (department_id,
department_name)
(30, 'Purchasing');
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
NValues
Inserting Rows with Null
E
L sfer
A
V ran
-t
Method MO Description
n
L
o
SE s a n Omit the column from the column list.
Implicit
N
AExplicit
ha
Specify the NULL keyword in the VALUES list;
INSERT INTO
VALUES
departments
(100, 'Finance', NULL, NULL);
specify the empty string ('') in the VALUES list for character strings
and dates.
Be sure that you can use null values in the targeted column by verifying the Null status with the
DESCRIBE command.
The Oracle server automatically enforces all data types, data ranges, and data integrity constraints.
Any column that is not listed explicitly obtains a null value in the new row.
Common errors that can occur during user input are checked in the following order:
Mandatory value missing for a NOT NULL column
Duplicate value violating any unique or primary key constraint
Any value violating a CHECK constraint
Referential integrity maintained for foreign key constraint
Data type mismatches or values too wide to fit in column
Note: Use of the column list is recommended because it makes the INSERT statement more readable
and reliable, or less prone to mistakes.
Oracle Database 11g: SQL Fundamentals I 9 - 8
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Inserting Special Values
E
fe values in your table.
ALto enter
s
V
You can use functions
special
n
-trainformation for employee Popp in the EMPLOYEES table. It supplies the
MO orecords
n
The slideLexample
ntime in the HIRE_DATE column. It uses the SYSDATE function that returns the
SEdates and
current
a
N
Acurrenthdate
a and time of the database server. You may also use the CURRENT_DATE function to get
the current date in the session time zone. You can also use the USER function when inserting rows in
a table. The USER function records the current username.
Confirming Additions to the Table
SELECT employee_id, last_name, job_id, hire_date, commission_pct
FROM
employees
WHERE employee_id = 113;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
l
bValues
NZTime
a
Inserting Specific Date
and
E
r
L sfe
A
The DD-MON-RR
formataisngenerally
used to insert a date value. With the RR format, the system
V
r
O
t
providesLthe
Mcorrectoncentury automatically.
E
n
Smay also
a
You
supply the date value in the DD-MON-YYYY format. This is recommended because it
N
s
Aclearlyhspecifies
a
the century and does not depend on the internal RR format logic of specifying the
correct century.
If a date must be entered in a format other than the default format (for example, with another century
or a specific time), you must use the TO_DATE function.
The example in the slide records information for employee Raphealy in the EMPLOYEES table. It
sets the HIRE_DATE column to be February 3, 1999.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating a Script
(&department_id, '&department_name',&location);
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating a Script
E
fe
AL with
s
V
You can save commands
substitution
variables to a file and execute the commands in the file.
n
a information
r
O
t
The example
in
the
slide
records
for a department in the DEPARTMENTS table.
LM non
E
Run
afile and you are prompted for input for each of the ampersand (&) substitution
NSthe ascript
s
Avariables.
h After entering a value for the substitution variable, click the OK button. The values that
you input are then substituted into the statement. This enables you to run the same script file over and
over, but supply a different set of values each time you run it.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Copying Rows
from Another Table
mo udent
l
e
ans is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ Table
a
Copying Rows from Another
E
r
fe to add rows to a table where the values are derived from existing
AL statement
s
You can use the INSERT
V
n
a slide, for the INSERT INTO statement to work, you must have already
trthe
tables. InLthe
example
in
MO
n
Ethe sales_reps
no
created
table using the CREATE TABLE statement. CREATE TABLE is discussed
S
a
N
s
Ain the hlesson
a titled Using DDL Statements to Create and Manage Tables.
In place of the VALUES clause, you use a subquery.
Syntax
INSERT INTO table [ column (, column) ] subquery;
In the syntax:
table
Is the name of the table
column
Is the name of the column in the table to populate
subquery
Is the subquery that returns rows to the table
The number of columns and their data types in the column list of the INSERT clause must match the
number of values and their data types in the subquery. Zero or more rows are added depending on the
number of rows returned by the subquery. To create a copy of the rows of a table, use SELECT * in
the subquery:
INSERT INTO copy_emp
SELECT *
FROM
employees;
Oracle Database 11g: SQL Fundamentals I 9 - 12
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
o e
h
@ uid
Read consistency
z
v
a nt G
o
FOR UPDATE clause in a SELECT statement
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Changing Data in a Table
E
L sfe
Achanging
V
The slide illustrates
an the department number for employees in department 60 to
r
O
t
department
LM80. non
E
S
AN has a
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
table
column = value [, column = value, ...]
condition];
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
UPDATE Statement Syntax
E
L sfe
Aexisting
You can modify the
V
anvalues in a table by using the UPDATE statement.
r
O
t
In the syntax:
LM non
E
S s a Is the name of the table
ANtable
column
Is the name of the column in the table to populate
ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Values for all the rows in the table are modified if you omit
the WHERE clause:
ai
m
t
o e
h
@ uid
z
v
a nt G
o
e
elm taudcolumn
Specify SET column_name= NULL tons
update
S
a
(
s
i
value to NULL.
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Updating Rows in a Table
E
fethe values of a specific row or rows if the WHERE clause is
AL modifies
s
The UPDATE statement
V
n
Oexample
tinrathe slide shows the transfer of employee 113 (Popp) to department 50.
specified.
The
M
n
L no
SEomitsthe
IfNyou
a WHERE clause, values for all the rows in the table are modified. Examine the updated
Arows inhthe
a COPY_EMP table.
UPDATE
SET
copy_emp
department_id = 110;
For example, an employee who was an SA_REP has now changed his job to an IT_PROG.
Therefore, his JOB_ID needs to be updated and the commission field needs to be set to NULL.
UPDATE employees
SET job_id = 'IT_PROG', commission_pct = NULL
WHERE employee_id = 114;
Note: The COPY_EMP table has the same data as the EMPLOYEES table.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
employees
job_id = (SELECT job_id
FROM
employees
WHERE
employee_id = 205),
salary = (SELECT salary
FROM
employees
WHERE
employee_id = 205)
employee_id
= 113;
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
N
Updating Two Columns
with a rSubquery
E
fe in the SET clause of an UPDATE statement by writing multiple
AL columns
s
You can update multiple
V
n
O syntax
tisraas follows:
subqueries.
The
M
n
EL atable
no
UPDATE
S
ANSEThas column =
WHERE
(SELECT
column
FROM table
WHERE condition)
[ ,
column
[WHERE
(SELECT
column
FROM table
WHERE condition)]
condition ] ;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
copy_emp
department_id
WHERE
job_id
(SELECT department_id
FROM employees
WHERE employee_id = 100)
(SELECT job_id
FROM employees
WHERE employee_id = 200);
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
l
bTable
NZ
a
Updating Rows BasedEon
Another
r
feUPDATE statements to update values in a table. The example in the
AL ninsthe
You can use the subqueries
V
O
tra table based on the values from the EMPLOYEES table. It changes the
slide updates
the
COPY_EMP
M
n
EL number
no of all employees with employee 200s job ID to employee 100s current
department
S
a
N asnumber.
Adepartment
h
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
o e
h
@ uid
Read consistency
z
v
a nt G
o
FOR UPDATE clause in a SELECT statement
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Removing a Row fromEaN
Table rab
febeen removed from the DEPARTMENTS table (assuming no
AL nhas
s
The Contracting department
V
Othe DEPARTMENTS
tra
constraints
on
table are violated), as shown by the graphic in the slide.
M
n
L no
E
S
AN has a
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
DELETE Statement
You can remove existing rows from a table by using the
DELETE statement:
DELETE [FROM]
[WHERE
table
condition];
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
DELETE Statement Syntax
E
e
ffrom
AL rows
s
You can remove V
existing
a table by using the DELETE statement.
n
a
r
O
t
In the syntax:
LM non
E
S s a Is the name of the table
ANtable
condition
Identifies the rows to be deleted, and is composed of column names,
ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
All rows in the table are deleted if you omit the WHERE
clause:
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Deleting Rows from a E
Table
AL rowsnsbyfespecifying the WHERE clause in the DELETE statement. The first
You can delete specific
V
Oslidendeletes
tra the Accounting department from the DEPARTMENTS table. You can
exampleLinM
the
E the delete
nooperation by displaying the deleted rows using the SELECT statement.
confirm
S
a
AN SELECT
as *
h
FROM
departments
DELETE FROM
WHERE
copy_emp;
department_name = 'Finance';
However, if you omit the WHERE clause, all rows in the table are deleted. The second example in the
slide deletes all rows from the COPY_EMP table, because no WHERE clause was specified.
Example:
Remove rows identified in the WHERE clause.
DELETE FROM
DELETE FROM
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abTable
l
Deleting Rows Based E
onNAnother
r
fe rows from a table based on values from another table. The
AL ntosdelete
V
You can use the subqueries
O -tra all the employees in a department, where the department name contains
exampleLinM
the slidendeletes
E Public.
no
the S
string
a
N as
AThe
subquery
searches the DEPARTMENTS table to find the department number based on the
h
department name containing the string Public. The subquery then feeds the department number to
the main query, which deletes rows of data from the EMPLOYEES table based on this department
number.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
TRUNCATE Statement
Example:
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
TRUNCATE Statement EN
L sfer
A
A more efficient V
method a
ofn
emptying a table is by using the TRUNCATE statement.
r
O
t
You canLuse
the
TRUNCATE
statement to quickly remove all rows from a table or cluster. Removing
M on
E
n
rows
statement is faster than removing them with the DELETE statement for the
Swith stheaTRUNCATE
N
Afollowing
a reasons:
h
The TRUNCATE statement is a data definition language (DDL) statement and generates no
TRUNCATE TABLE copy_emp;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
o e
h
@ uid
Read consistency
z
v
a nt G
o
FOR UPDATE clause in a SELECT statement
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Database Transactions
A database transaction consists of one of the following:
DML statements that constitute one consistent change to
the data
One DDL statement
One data control language (DCL) statement
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Database Transactions
E
e
AL nsfconsistency
The Oracle serverVensuresadata
based on transactions. Transactions give you more
r
O
t
flexibility
and control
n when changing data, and they ensure data consistency in the event of user
LM
o
E
n
process
failure
or
system
failure.
S sa
N
ATransactions
ha consist of DML statements that constitute one consistent change to the data. For
example, a transfer of funds between two accounts should include the debit in one account and the
credit to another account of the same amount. Both actions should either fail or succeed together; the
credit should not be committed without the debit.
Transaction Types
Type
Description
Data manipulation
language (DML)
Data definition
language (DDL)
Data control language
(DCL)
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ and
a
Database Transaction:EStart
End
r
fe start and end?
ALtransaction
s
V
When does a database
n
O n-tra
M
A transaction
begins
EL occurs:
no when the first DML statement is encountered and ends when one of the
S
a
following
AN AhCOMMIT
as or ROLLBACK statement is issued.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Advantages of COMMIT
and ROLLBACK Statements
With COMMIT and ROLLBACK statements, you can:
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl Statements
NZROLLBACK
Advantages of COMMIT
and
a
E
r
fe statements, you have control over making changes to the data
ALROLLBACK
s
With the COMMIT
and
V
n
O n-tra
permanent.
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Time
COMMIT
Transaction
DELETE
SAVEPOINT A
INSERT
m
lco
ai
m
t
o e
h
@ uid
z
v
SAVEPOINT B
a nt G
o
lm ude
e
s
INSERT
n s St ROLLBACK
ROLLBACK
aROLLBACK
(
i
h
t
to SAVEPOINT BRAto SAVEPOINT
A
R use
A
o
EG seAll trights
Copyright Z
2009, Oracle.
reserved.
A cen
L
UE le li
Z
ab
Explicit Transaction Control
EN feStatements
r
L
Alogic ofntransactions
s
You can control the
by using the COMMIT, SAVEPOINT, and ROLLBACK
V
a
r
O
t
statements.
LM non
E
S sa
Description
ANStatement
a
h
COMMIT
COMMIT ends the current transaction by making all pending
UPDATE
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NaZMarker
a
Rolling Back ChangesEto
r
e
AL in nthesfcurrent
You can create a V
marker
transaction by using the SAVEPOINT statement, which
a
r
O
t
divides the
Mtransaction
n into smaller sections. You can then discard pending changes up to that marker
Lthe
o
E
n
by using
ROLLBACK
TO SAVEPOINT statement.
S sa
N
ANote that
haif you create a second savepoint with the same name as an earlier savepoint, the earlier
savepoint is deleted.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Implicit Transaction Processing
E
AL nsfe
V
tra Circumstances
Status MO
n
EL commit
no
Automatic
DDL statement or DCL statement issued
S
a
N
s
SQL Developer or SQL*Plus exited normally, without
A ha
explicitly issuing COMMIT or ROLLBACK commands
Automatic rollback
Note: In SQL*Plus, the AUTOCOMMIT command can be toggled ON or OFF. If set to ON, each
individual DML statement is committed as soon as it is executed. You cannot roll back the changes.
If set to OFF, the COMMIT statement can still be issued explicitly. Also, the COMMIT statement is
issued when a DDL statement is issued or when you exit SQL*Plus. The SET AUTOCOMMIT
ON/OFF command is skipped in SQL Developer. DML is committed on a normal exit from SQL
Developer only if you have the Autocommit preference enabled. To enable Autocommit, perform the
following:
In the Tools menu, select Preferences. In the Preferences dialog box, expand Database and select
Worksheet Parameters.
In the right pane, select the Autocommit in SQL Worksheet option. Click OK.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z aor
blROLLBACK
NCOMMIT
State of the Data Before
E
r
AL nsfethe transaction is temporary until the transaction is committed.
V
Every data change
made during
O datanbefore
tra COMMIT or ROLLBACK statements are issued can be described as
Mthe
The stateLof
E a no
S
follows:
s
AN Data
operations primarily affect the database buffer; therefore, the previous state
hamanipulation
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
State of the Data AfterECOMMIT
fe by using the COMMIT statement. Here is what happens after a
AL permanent
s
Make all pendingVchanges
n
a
COMMITLstatement:
MO on-tr
E changes
n are written to the database.
SData
a
N
s
A The
haprevious state of the data is no longer available with normal SQL queries.
All users can view the results of the transaction.
The locks on the affected rows are released; the rows are now available for other users to
perform new data changes.
All savepoints are erased.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Committing Data
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Committing Data
E
L sfe
Aslide,
In the example inV
the
na row is deleted from the EMPLOYEES table and a new row is inserted
atable.
r
O
t
into the DEPARTMENTS
The changes are saved by issuing the COMMIT statement.
LM non
E
NS as a
AExample:
h departments 290 and 300 in the DEPARTMENTS table and update a row in the EMPLOYEES
Remove
COMMIT;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
State of the Data AfterEROLLBACK
L sfe
Achanges
Discard all pending
V
anby using the ROLLBACK statement, which results in the following:
r
O
t
DataLchanges
M oaren undone.
E previous
n state of the data is restored.
SThe
a
N
s
A Locks
ha on the affected rows are released.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
id = 100;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
COMMIT;
o
lm ude
Commit complete.
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
l
NZ rabExample
State of the Data AfterEROLLBACK:
L sfe
While attemptingV
toAremove
ana record from the TEST table, you may accidentally empty the table.
r
O
t
However,
you can correct
n the mistake, reissue a proper statement, and make the data change
LM
o
E
n
permanent.
S
AN has a
SELECT * FROM
test WHERE
No rows selected.
id = 100;
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Statement-Level Rollback
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Statement-Level Rollback
E
fe
ALcan be
s
V
A part of a transaction
discarded
through an implicit rollback if a statement execution error is
n
a
r
O
t
detected.LIfM
a single n
DML statement fails during execution of a transaction, its effect is undone by a
o
E
n
statement-level
rollback,
but the changes made by the previous DML statements in the transaction
S sa
N
Aare nothdiscarded.
a
They can be committed or rolled back explicitly by the user.
The Oracle server issues an implicit commit before and after any DDL statement. So, even if your
DDL statement does not execute successfully, you cannot roll back the previous statement because
the server issued a commit.
Terminate your transactions explicitly by executing a COMMIT or ROLLBACK statement.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
o e
h
@ uid
Read consistency
z
v
a nt G
o
FOR UPDATE clause in a SELECT statement
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Read Consistency
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Read Consistency
E
fe in two ways:
ALthe database
s
V
Database users access
n
O n(SELECT
tra statement)
Read
operations
M
EL operations
no (INSERT, UPDATE, DELETE statements)
SWrite
a
N as
AYou
need
h read consistency so that the following occur:
The database reader and writer are ensured a consistent view of the data.
Readers do not view data that is in the process of being changed.
Writers are ensured that the changes to the database are done in a consistent manner.
Changes made by one writer do not disrupt or conflict with the changes being made by another
writer.
The purpose of read consistency is to ensure that each user sees data as it existed at the last commit,
before a DML operation started.
Note: The same user can log in to different sessions. Each session maintains read consistency in the
manner described above, even if they are the same users.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
User A
UPDATE employees
SET
salary = 7000
WHERE last_name = 'Grant';
Data
blocks
Undo
segments
Changed
and
unchanged
data
Before
change
(old data)
ai
m
t
Reado e
h
consistent
@ uid
z
v
image
a nt G
o
lm ude
e
s
User B
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Implementing Read Consistency
E
AL nsfeimplementation. It keeps a partial copy of the database in the undo
Read consistencyV
is an automatic
Oread-consistent
tra image is constructed from the committed data in the table and the old
segments.
The
M
n
o
ELis being
nchanged
dataSthat
and is not yet committed from the undo segment.
a
N
s
AWhen hanainsert, update, or delete operation is made on the database, the Oracle server takes a copy of
SELECT *
FROM userA.employees;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
o e
h
@ uid
Read consistency
z
v
a nt G
o
FOR UPDATE clause in a SELECT statement
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
l
ai
COMMIT.
m
t
o e
h
If the SELECT statement attempts to lock a row that
is
@ uid
z
v
G is
a thentrow
locked by another user, the database waits
until
o
lm ude
available, and then returns the resultsnsofetheSSELECT
t
a
(
s
i
statement.
th
RA
R use
A
o
EG seAll trights
Copyright Z
2009, Oracle.
reserved.
A cen
L
UE le li
Z
bStatement
FOR UPDATE Clause inEaNSELECT
a
r
L
e
A nstatement
sf
When you issue aVSELECT
against the database to query some records, no locks are placed
a
r
O
t
on the selected
nIn general, this is required because the number of records locked at any given
LM rows.
okept
n
timeSisE(by default)
to the absolute minimum: only those records that have been changed but not
a
N
s
Ayet committed
ha are locked. Even then, others will be able to read those records as they appeared
before the change (the before image of the data). There are times, however, when you may want to
lock a set of records even before you change them in your program. Oracle offers the FOR UPDATE
clause of the SELECT statement to perform this locking.
When you issue a SELECT...FOR UPDATE statement, the relational database management system
(RDBMS) automatically obtains exclusive row-level locks on all the rows identified by the SELECT
statement, thereby holding the records for your changes only. No one else will be able to change
any of these records until you perform a ROLLBACK or a COMMIT.
You can append the optional keyword NOWAIT to the FOR UPDATE clause to tell the Oracle server
not to wait if the table has been locked by another user. In this case, control will be returned
immediately to your program or to your SQL Developer environment so that you can perform other
work, or simply wait for a period of time before trying again. Without the NOWAIT clause, your
process will block until the table is available, when the locks are released by the other user through
the issue of a COMMIT or a ROLLBACK command.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
Rows from both the EMPLOYEES and DEPARTMENTShtables
o e
@ uid
z
are locked.
v
a nt G
o
Use FOR UPDATE OF column_name to e
qualify
lm uthe
decolumn
s
t
n from
S that specific
you intend to change, then only the(arows
s
i
RA se th
table are locked.
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
FOR UPDATE Clause: Examples
E
L sfe
Aslide,
In the example inV
the
locks rows in the EMPLOYEES table with JOB_ID set to
anthe statement
r
O
t
ST_CLERK
and
LOCATION_ID
set
to
1500,
and locks rows in the DEPARTMENTS table with
M on
L
E
n
departments
set as 1500.
S s ina LOCATION_ID
N
AYou can
hause the FOR UPDATE OF column_name to qualify the column that you intend to change.
The OF list of the FOR UPDATE clause does not restrict you to changing only those columns of the
selected rows. Locks are still placed on all rows; if you simply state FOR UPDATE in the query and
do not include one or more columns after the OF keyword, the database will lock all identified rows
across all the tables listed in the FROM clause.
The following statement locks only those rows in the EMPLOYEES table with ST_CLERK located in
LOCATION_ID 1500. No rows are locked in the DEPARTMENTS table:
SELECT e.employee_id, e.salary, e.commission_pct
FROM employees e JOIN departments d
USING (department_id)
WHERE job_id = 'ST_CLERK' AND location_id = 1500
FOR UPDATE OF e.salary
ORDER BY e.employee_id;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
In the following example, the database is instructed to wait for five seconds for the row to become
available, and then return control to you.
SELECT employee_id, salary, commission_pct, job_id
FROM employees
WHERE job_id = 'SA_REP'
FOR UPDATE WAIT 5
ORDER BY employee_id;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Quiz
The following statements produce the same results:
DELETE FROM copy_emp;
TRUNCATE TABLE copy_emp;
1. True
2. False
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Answer: 2
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Summary
In this lesson, you should have learned how to use the
following statements:
Function
Description
INSERT
UPDATE
DELETE
TRUNCATE
ai
m
t
SAVEPOINT
o e
Is used to roll back to the savepoint marker
h
@ uid
z
ROLLBACK
v
Discards all pending data changes
a nt G
o
lm
FOR UPDATE clause Locks rows identified by the SELECT
query ude
e
s
an is St
in SELECT
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Summary
E
e
AL nsflearned
In this lesson, youVshouldahave
how to manipulate data in the Oracle database by using the
r
O
t
INSERT,
UPDATE,
DELETE,
and
TRUNCATE
statements, as well as how to control data changes by
M on
L
E
n SAVEPOINT, and ROLLBACK statements. You also learned how to use the FOR
using
S the COMMIT,
a
N
s
AUPDATE
haclause of the SELECT statement to lock rows for your changes only.
COMMIT
Remember that the Oracle server guarantees a consistent view of data at all times.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Practice 9: Overview
This practice covers the following topics:
Inserting rows into the tables
Updating and deleting rows in the table
Controlling transactions
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Practice 9: Overview EN
L sfer
A
In this practice, you
V addrrows
n to the MY_EMPLOYEE table, update and delete data from the table, and
aYou
O
t
control your
transactions.
run a script to create the MY_EMPLOYEE table.
LM non
E
S
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, 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
Explain how constraints are created at the time of table
)
m
o
creation
ilc
a
Describe how schema objects work
otm
h ide
@
avz nt Gu
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Objectives
E
AL nsfeto the data definition language (DDL) statements. You learn the
In this lesson, youVare introduced
ra tables, alter them, and remove them. The data types available in DDL
tsimple
basics ofLhow
MOto create
n
E anda schema
no concepts are introduced. Constraints are discussed in this lesson. Exception
are S
shown
N athat
Amessages
h s are generated from violating constraints during DML operations are shown and
explained.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
ai
m
t
o e
h
@ uid
z
v
a nt G
o
Read-only tables
lm ude
e
s
DROP TABLE statement
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Database Objects
Object
Description
Table
View
Sequence
Index
Synonym
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Database Objects
E
L sfe
Acan
V
The Oracle Database
n multiple data structures. Each structure should be outlined in the
acontain
r
O
t
databaseLdesign
M soothat
n it can be created during the build stage of database development.
E Stores
n
STable:
data
a
s
AN View:
a
Subset
of data from one or more tables
h
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Naming Rules
Table names and column names must:
Begin with a letter
Be 130 characters long
Contain only AZ, az, 09, _, $, and #
Not duplicate the name of another object owned by the
same user
Not be an Oracle serverreserved word
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Naming Rules
E
L sfe
Atables
V
You name database
n columns according to the standard rules for naming any Oracle
aand
r
O
t
databaseLobject:
M on
E names
nand column names must begin with a letter and be 130 characters long.
STable
a
N
s
A Names
ha must contain only the characters AZ, az, 09, _ (underscore), $, and # (legal
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
ai
m
t
o e
h
@ uid
z
v
a nt G
o
Read-only tables
lm ude
e
s
DROP TABLE statement
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
You specify:
The table name
The column name, column data type, and column size
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
CREATE TABLE Statement
E
L sfe
You create tablesV
toAstoreadata
n by executing the SQL CREATE TABLE statement. This statement is
r
O
t
one of the
DDL statements
n that are a subset of the SQL statements used to create, modify, or remove
LM
o
E
n
Oracle
Database
structures.
These statements have an immediate effect on the database and they also
S sa
N
Arecordhinformation
a
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 (DBA) uses data control language (DCL) statements to
grant privileges to users.
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
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
USERA
USERB
@ uid
z
v
a nt G
SELECT *
SELECT *
o
lm ude
FROM userB.employees;
FROM userA.employees;
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ Tables
a
Referencing Another Users
E
r
fe structures of data or schema objects. A schema is owned by a
AL ofnlogical
s
V
A schema is a collection
trasame name as that user. Each user owns a single schema.
databaseLuser
MOand ohasn-the
ncan be created and manipulated with SQL and include tables, views, synonyms,
SE objects
a
Schema
N
s
Asequences,
ha 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, USERA 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, USERB must prefix the
table name with the schema name:
SELECT *
FROM
usera.employees;
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
DEFAULT Option
ai
m
t
o e
h
@ uid
CREATE TABLE hire_dates
z
v
a nt G
o
(id
NUMBER(8),
lm ude
e
hire_date DATE DEFAULT SYSDATE);
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
DEFAULT Option
E
fespecify that a column should be given a default value by using the
AL you
scan
When you defineV
a table, a
n
r prevents null values from entering the columns when a row is inserted
O This
-toption
DEFAULT
option.
M
n
L
o
nfor the column. The default value can be a literal, an expression, or a SQL function
without
SEa value
a
N
s
A(such as
aSYSDATE or USER), but the value cannot be the name of another column or a
h
pseudocolumn (such as NEXTVAL or CURRVAL). The default expression must match the data type of
the column.
Consider the following examples:
INSERT INTO hire_dates values(45, NULL);
The above statement will insert the null value rather than the default value.
INSERT INTO hire_dates(id) values(35);
The above statement will insert SYSDATE for the HIRE_DATE column.
Note: In SQL Developer, click the Run Script icon or press [F5] to run the DDL statements. The
feedback messages will be shown on the Script Output tabbed page.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating Tables
NUMBER(2),
VARCHAR2(14),
VARCHAR2(13),
DATE DEFAULT SYSDATE);
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating Tables
E
L sfe
Aslide
The example in the
creates
table with four columns: DEPTNO, DNAME, LOC, and
V
an the DEPT
r
O
t
CREATE_DATE.
The
CREATE_DATE
column
has a default value. If a value is not provided for an
M on
L
E
n the system date is automatically inserted.
INSERT
S statement,
a
N
s
ATo confirm
ha that the table was created, run the DESCRIBE command.
Because creating a table is a DDL statement, an automatic commit takes place when this statement is
executed.
Note: You can view the list of tables you own by querying the data dictionary. For example:
select table_name from user_tables
Using data dictionary views, you can also find information about other database objects such as
views, indexes, and so on. You will learn about data dictionaries in detail in the Oracle Database
11g: SQL Fundaments II course.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
ai
m
t
o e
h
@ uid
z
v
a nt G
o
Read-only tables
lm ude
e
s
DROP TABLE statement
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Data Types
Data Type
Description
NUMBER(p,s)
DATE
LONG
CLOB
ai
m
t
o e
h
BLOB
Binary data (up to 4 GB)
@ uid
z
v
BFILE
a4 GB) nt G
Binary data stored in an external file (up to
o
lmthe unique
de
e
u
ROWID
A base-64 number system representing
s
t
n sS
address of a row in its table (a
A e thi
R
AR to us
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
U le
Z
N
Data Types
E ferab
L
A nfor
s a table, you need to provide a data type for the column. There are
V
When you identify
a column
a
r
O
t
several data
nLMtypesnoavailable:
E
S sa
Description
ANDatahaType
VARCHAR2(size)
Variable-length character data (A maximum size must be
specified: minimum size is 1; maximum size is 4,000.)
Fixed-length character data of length size bytes (Default and
minimum size is 1; maximum size is 2,000.)
CHAR [(size)]
NUMBER [(p,s)]
DATE
LONG
CLOB
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
D escription
RA W( s i z e)
LO NG RA W
BL OB
BF ILE
RO WID
A base-64 num ber system representing the unique address of a row in its table
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.
Only one LONG column can be used per table.
No constraints can be defined on a LONG column.
You might want to use a CLOB column rather than a LONG column.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Description
TIMESTAMP
INTERVAL YEAR TO
MONTH
INTERVAL DAY TO
SECOND
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Datetime Data Types EN
L sfer
A
Data Type O V
anDescription
r
t
LM non
TIMESTAMP
Enables storage of time as a date with fractional seconds. It stores the
E
S sa
N
year, month, day, hour, minute, and the second value of the DATE data
A ha
type as well as the fractional seconds value
INTERVAL YEAR TO
MONTH
INTERVAL DAY TO
SECOND
Note: These datetime data types are available with Oracle9i and later releases. The datetime data
types are discussed in detail in the lesson titled Managing Data in Different Time Zones in the
Oracle Database 11g: SQL Fundamentals II course.
Also, for more information about the datetime data types, see the sections on TIMESTAMP
Datatype, INTERVAL YEAR TO MONTH Datatype, and INTERVAL DAY TO SECOND Datatype
in Oracle Database SQL Language Reference 11g, Release 1 (11.1).
Oracle Database 11g: SQL Fundamentals I 10 - 14
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
ai
m
t
o e
h
@ uid
z
v
a nt G
o
Read-only tables
lm ude
e
s
DROP TABLE statement
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Including Constraints
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Constraints
E
AL nsfe to prevent invalid data entry into tables.
The Oracle serverVuses constraints
O n-tra
M
You canLuse constraints
do the following:
E arules
noon thetodata
S
Enforce
in a table whenever a row is inserted, updated, or deleted from that
s
AN table.
a
h The constraint must be satisfied for the operation to succeed.
Prevent the deletion of a table if there are dependencies from other tables.
Provide rules for Oracle tools, such as Oracle Developer.
Data Integrity Constraints
C onstraint
D escription
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Constraint Guidelines
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Constraint GuidelinesEN
L sfer
A
Vstoredrainnthe data dictionary. Constraints are easy to reference if you give them a
All constraints are
O
-t names must follow the standard object-naming rules, except that the
meaningful
Mname.oConstraint
n
L
name
cannot a
be n
the same as another object owned by the same user. If you do not name your
SE
N
s
Aconstraint,
ha the Oracle server generates a name with the format SYS_Cn, where n is an integer so that
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Defining Constraints
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);
ai
m
t
o e
h
column,...
@ uid
z
v
[CONSTRAINT constraint_name] constraint_type
a nt G
o
(column, ...),
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Defining Constraints EN
L sfer
A
Vsyntaxrafor
The slide gives the
n defining constraints when creating a table. You can create constraints
O
t
at eitherL
the
level or table level. Constraints defined at the column level are included when
Mcolumn
E isadefined.
non Table-level constraints are defined at the end of the table definition and must
the S
column
N to the
Arefer
hascolumn or columns on which the constraint pertains in a set of parentheses. It is mainly
the syntax that differentiates the two; otherwise, functionally, a column-level constraint is the same
as a table-level constraint.
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 be used 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
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Defining Constraints
S
a
(
s
i
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Defining Constraints (continued)
E
fatethe same time as the table. Constraints can be added to a table after
ALcreated
s
V
Constraints are usually
n
ra
Oalsonbe-ttemporarily
its creation
and
disabled.
M
L no
E
Both
a in the slide create a primary key constraint on the EMPLOYEE_ID column of the
NSexamples
s
AEMPLOYEES
a
table.
h
1. The first example uses the column-level syntax to define the constraint.
2. The second example uses the table-level syntax to define the constraint.
More details about the primary key constraint are provided later in this lesson.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
Absence of NOT NULLsconstraint
n a nulls St
NOT NULL constraint
a
(Any row can contain
(
i
(Primary Key enforces
A
this
column.)th
NOT NULL value forR
NOT NULL constraint.)
R use
constraint
A
o
EG seAll trights
Copyright Z
2009, Oracle.
reserved.
A cen
L
UE le li
Z
b
NOT NULL Constraint EN
a
r
L
e
A ensures
sf that the column contains no null values. Columns without the NOT
The NOT NULL constraint
V
n
a
-tr null values by default. NOT NULL constraints must be defined at the
NULL constraint
contain
MO can
n
L
o
column
Innthe EMPLOYEES table, the EMPLOYEE_ID column inherits a NOT NULL constraint
SElevel.
a
N
s
Aas it ishdefined
a as a primary key. Otherwise, the LAST_NAME, EMAIL, HIRE_DATE, and JOB_ID
columns have the NOT NULL constraint enforced on them.
Note: Primary key constraint is discussed in detail later in this lesson.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
UNIQUE Constraint
EMPLOYEES
UNIQUE constraint
INSERT INTO
m
lco
Allowed
ma
t
o
Not allowed: already exists
@h ide
avz nt Gu
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
UNIQUE Constraint EN
L sfer
A
A UNIQUE key integrity
requires that every value in a column or a set of columns (key) be
V raconstraint
n
O
t
uniquethat
M is, nootwo
n rows of a table can have duplicate values in a specified column or a set of
LThe
E
n
columns.
column
(or set of columns) included in the definition of the UNIQUE key constraint is
S sa
N
Acalled hthea 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 the 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 the 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 reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
UNIQUE Constraint
Defined at either the table level or the column level:
CREATE TABLE employees(
employee_id
NUMBER(6),
last_name
VARCHAR2(25) NOT NULL,
email
VARCHAR2(25),
salary
NUMBER(8,2),
commission_pct
NUMBER(2,2),
hire_date
DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
UNIQUE Constraint (continued)
E
L sfe
Acan
UNIQUE constraints
be
V
andefined at the column level or table level. You define the constraint at
r
O
t
the tableLlevel
M when
nyou want to create a composite unique key. A composite key is defined when
o
E
n
there
is
not
a
single
S s a attribute that can uniquely identify a row. In that case, you can have a unique key
N
Athat ishcomposed
a
of two or more columns, the combined value of which is always unique and can
identify rows.
The example in the slide applies the UNIQUE constraint to the EMAIL column of the EMPLOYEES
table. The name of the constraint is EMP_EMAIL_UK.
Note: The Oracle server enforces the UNIQUE constraint by implicitly creating a unique index on the
unique key column or columns.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
PRIMARY KEY
DEPARTMENTS
Not allowed
(null value)
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
Not allowed
(
(50 already exists)
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
PRIMARY KEY Constraint
E
fe a primary key for the table. Only one primary key can be created
AL ncreates
s
A PRIMARY KEYVconstraint
-tra KEY constraint is a column or a set of columns that uniquely identifies
for each L
table.
MOTheoPRIMARY
n
E in aatable.
n This constraint enforces the uniqueness of the column or column combination
eachSrow
N
s
Aand ensures
ha that no column that is part of the primary key can contain a null value.
INSERT INTO
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 reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
DEPARTMENTS
PRIMARY
KEY
EMPLOYEES
FOREIGN
KEY
ai
m
t
o e
h
@ uid
z
v
Not
allowed
G
INSERT INTO
a doesnnot
t
o
(9
lm uexist)
de
e
s
t
an is SAllowed
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
FOREIGN KEY Constraint
E
fe integrity) constraint designates a column or a combination of
A(orL referential
s
The FOREIGN KEY
V
n
O -traand establishes a relationship with a primary key or a unique key in the
columnsLasM
a foreignnkey
E oraa different
no table.
same
table
S
AInNthe hexample
as 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.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
N(continued)
FOREIGN KEY Constraint
E
L sfer
A
FOREIGN KEY constraints
V rancan be defined at the column or table constraint level. A composite
O
-t by using the table-level definition.
foreign key
created
Mmust be
n
L
o
E ain nthe slide defines a FOREIGN KEY constraint on the DEPARTMENT_ID column of
Sexample
The
N
Athe EMPLOYEES
table, using table-level syntax. The name of the constraint is EMP_DEPT_FK.
has
The foreign key can also be defined at the column level, provided that the constraint is based on a
single column. The syntax differs in that the keywords FOREIGN KEY do not appear. For example:
CREATE TABLE employees
(...
department_id NUMBER(4) CONSTRAINT emp_deptid_fk
REFERENCES departments(department_id),
...
)
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
NKeywords
FOREIGN KEY Constraint:
E
L sfer
A
Vdefined
The foreign key is
ainn the child table and the table containing the referenced column is the
r
O
t
parent table.
key is defined using a combination of the following keywords:
MThe foreign
EL a KEY
nonis used to define the column in the child table at the table-constraint level.
S
FOREIGN
AN REFERENCES
identifies the table and the column in the parent table.
as
h
ON DELETE CASCADE indicates that when a row in the parent table is deleted, the dependent
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
CHECK Constraint
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
CHECK Constraint
E
fecondition that each row must satisfy. The condition can use the same
ALdefines
s
The CHECK constraint
a
V
n
ra
O
tconditions,
constructs
as
the
query
with the following exceptions:
M
n
EL a ntoothe CURRVAL, NEXTVAL, LEVEL, and ROWNUM pseudocolumns
SReferences
AN Calls
hasto SYSDATE, UID, USER, and USERENV functions
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
CREATE TABLE: Example
E
L sfe
Aslide
The example in the
shows
V
an the statement that is used to create the EMPLOYEES table in the HR
r
O
t
schema. LM
n
o
E
n
S
AN has a
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Violating Constraints
UPDATE employees
SET
department_id = 55
WHERE department_id = 110;
m
lco
ai
m
t
o e
h
@ uid
z
Department 55 does not exist.
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Violating Constraints EN
L sfer
A
V ranin place on columns, an error is returned if you try to violate the
When you have constraints
O
-t if you try to update a record with a value that is tied to an integrity
constraint
rule. For example,
M
n
L
o
n is returned.
constraint,
SE sanaerror
N
AIn the hexample
a in the slide, department 55 does not exist in the parent table, DEPARTMENTS, and so
you receive the parent key not found violation ORA-02291.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Violating Constraints
You cannot delete a row that contains a primary key that is
used as a foreign key in another table.
DELETE FROM departments
WHERE department_id = 60;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Violating Constraints E
(continued)
AL record
sfewith a value that is tied to an integrity constraint, an error is
If you attempt to V
delete a a
n
O n-tr
returned.LM
E ain ntheoslide tries to delete department 60 from the DEPARTMENTS table, but it results
S
The
example
AinNan error
hasbecause 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, you receive the child record found
violation ORA-02292.
The following statement works because there are no employees in department 70:
DELETE FROM departments
WHERE department_id = 70;
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
ai
m
t
o e
h
@ uid
z
v
a nt G
o
Read-only tables
lm ude
e
s
DROP TABLE statement
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Creating a Table Using
aNSubquery
E
L sfer
A
A second methodV
for creating
an a table is to apply the AS subquery clause, which both creates the
r
O
t
table andLinserts
M rows
n returned from the subquery.
o
E
n
S syntax:
a
Nthe
s
AIn
a
table
Is the name of the table
h
column
subquery
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. Note
that only the explicit NOT NULL constraint will be inherited. The PRIMARY KEY column will
not pass the NOT NULL feature to the new column. Any other constraint rules are not passed to
the new table. However, you can add constraints in the column definition.
Oracle Database 11g: SQL Fundamentals I 10 - 32
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl(continued)
Creating a Table Using
aNSubquery
E
L sfer
A
The example in the
creates
n a table named DEPT80, which contains details of all the employees
aNotice
r
O V slide
t
workingLinM
department
80.
that the data for the DEPT80 table comes from the EMPLOYEES
n
o
E
table.
S san
N
AYou can
haverify the existence of a database table and check the column definitions by using the
DESCRIBE dept80
DESCRIBE command.
However, 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 reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
ai
m
t
o e
h
@ uid
z
v
a nt G
o
Read-only tables
lm ude
e
s
DROP TABLE statement
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
ALTER TABLE Statement
E
feneed to change the table structure for any of the following reasons:
AL nmay
s
After you create aVtable, you
-tra
YouLomitted
MO aocolumn.
n
E column
n definition or its name needs to be changed.
SYour
a
N
s
A You
haneed to remove columns.
You want to put the table into the read-only mode
You can do this by using the ALTER TABLE statement.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Read-Only Tables
You can use the ALTER TABLE syntax to:
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Read-Only Tables
E
fe
AL11g, nyou
s
With Oracle Database
READ ONLY to place a table in the read-only mode.
V
a can specify
r
O
t
When the
table
is
in
the
READ-ONLY
mode,
you cannot issue any DML statements that affect the
M on
L
E
n ... FOR UPDATE statements. You can issue DDL statements as long as
table
Sor anys SELECT
a
N
Athey dohnot
a modify any data in the table. Operations on indexes associated with the table are allowed
when the table is in the READ ONLY mode.
ALTER TABLE employees READ WRITE;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
ai
m
t
o e
h
@ uid
z
v
a nt G
o
Read-only tables
lm ude
e
s
DROP TABLE statement
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Dropping a Table
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Dropping a Table
E
fe a table to the recycle bin or removes the table and all its data
AL nsmoves
The DROP TABLE
statement
V
-traUnless you specify the PURGE clause, the DROP TABLE statement does
from theLdatabase
MO oentirely.
n
E in space
n being released back to the tablespace for use by other objects, and the space
not S
result
a
N
s
Acontinues
hato count towards the users space quota. Dropping a table invalidates the dependent objects
and removes object privileges on the table.
When you drop a table, the database loses all the data in the table and all the indexes associated
with it.
Syntax
DROP TABLE table [PURGE]
In the syntax, table is the name of the table.
Guidelines
All the 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: Use the FLASHBACK TABLE statement to restore a dropped table from the recycle bin. This is
discussed in detail in the course titled Oracle Database 11g: SQL Fundamentals II.
Oracle Database 11g: SQL Fundamentals I 10 - 38
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Quiz
You can use constraints to do the following:
1. Enforce rules on the data in a table whenever a row is
inserted, updated, or deleted.
2. Prevent the deletion of a table.
3. Prevent the creation of a table.
4. Prevent the creation of data in a table.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Answers: 1, 2, 4
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Summary
In this lesson, you should have learned how to use the CREATE
TABLE statement to create a table and include constraints:
h ide
@
avz nt Gu
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Summary
E
e
AL nsflearned
In this lesson, youVshouldahave
how to do the following:
r
O
t
M on
CREATELTABLE
E
n TABLE statement to create a table and include constraints.
SUse the CREATE
a
N
s
A Create
ha a table based on another table by using a subquery.
DROP TABLE
Remove rows and a table structure.
When executed, this statement cannot be rolled back.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Practice 10: OverviewEN
L sfer
A
Create new tablesVby using
TABLE statement. Confirm that the new table was added to
antheto CREATE
r
O
t
the database.
You
also
learn
set
the
status
of a table as READ ONLY and then revert to
M on
L
E
READ/WRITE.
S san
N
ANote: hFora all the DDL and DML statements, click the Run Script icon (or press [F5]) to execute the
query in SQL Developer. This way you get to see the feedback messages on the Script Output tabbed
page. For SELECT queries, continue to click the Execute Statement icon or press [F9] to get the
formatted output on the Results tabbed page.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, 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
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Objectives
E
AL nsfeto the view, sequence, synonym, and index objects. You learn the
In this lesson, youVare introduced
tra views, sequences, and indexes.
basics ofLcreating
MO oandn-using
SE s a n
N
A ha
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Overview of views:
Creating, modifying, and retrieving data from a view
Data manipulation language (DML) operations on a view
Dropping a view
Overview of sequences:
Creating, using, and modifying a sequence
Cache sequence values
NEXTVAL and CURRVAL pseudocolumns
ai
m
t
o e
h
Creating, dropping indexes
@ uid
z
v
a nt G
o
Overview of synonyms
lm ude
e
s
Creating, dropping synonyms
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Overview of indexes
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Database Objects
Object
Description
Table
View
Sequence
Index
ai
m
t
Synonym
Gives alternative names to objects
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Database Objects
E
AL nsfinea database in addition to tables.
There are severalV
other objects
O n-tra
M
With views,
o present and hide data from the tables.
EL you
ncan
S
a
N applications
Many
require the use of unique numbers as primary key values. You can either build
s
Acode
a
h
into the application to handle this requirement or use a sequence to generate unique numbers.
If you want to improve the performance of data retrieval 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.
m
lco
What Is a View?
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
EMPLOYEES table
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
What Is a View?
E
feor combinations of data by creating views of tables. A view is a
AL subsets
s
V
You can present logical
n
a
O onn-atrtable
logical table
based
or another view. A view contains no data of its own, but is like a
M
L no
E
window
through
which
data
from
tables can be viewed or changed. The tables on which a view is
S sa
N
Abased hareacalled base tables. The view is stored as a SELECT statement in the data dictionary.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Advantages of Views
To restrict
data access
To make complex
queries easy
m
lco
ai
m
t
To present o
To provide
hof ide
different
views
@
data
u
G
avz data
the o
same
t
independence
m uden
l
e
ans is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Advantages of Views EN
L sfer
A
Vaccessratonthe data because it displays selected columns from the table.
Views restrict
O
-tto make simple queries to retrieve the results of complicated queries. For
Views
Mcan beoused
n
L
E aviews
n can be used to query information from multiple tables without the user knowing
Sexample,
N
s
A how
hato 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 the section on CREATE VIEW in Oracle Database SQL Language
Reference 11g, Release 1 (11.1).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Feature
Simple Views
Complex Views
Number of tables
One
One or more
Contain functions
No
Yes
No
Yes
Yes
Not always
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ Views
a
Simple Views and Complex
E
r
AL nsforfeviews: simple and complex. The basic difference is related to the
V
There are two classifications
O UPDATE,
tra and DELETE) operations.
DML (INSERT,
M
n
L no
SAEsimpleaview
is one that:
N
s
A h- aDerives data from only one table
- Contains no functions or groups of data
- Can perform DML operations through the view
A complex view is one that:
- Derives data from many tables
- Contains functions or groups of data
- Does not always allow DML operations through the view
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating a View
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating a View
E
fe a subquery in the CREATE VIEW statement.
ALby embedding
s
You can create a V
view
n
O n-tra
M
In the syntax:
EL a no Re-creates the view if it already exists
S
OR
REPLACE
N as
AFORCE
Creates the view regardless of whether or not the base tables exist
h
Creates the view only if the base tables exist (This is the default.)
Is the name of the view
Specifies names for the expressions selected by the views query
(The number of aliases must match the number of expressions selected by the
view.)
subquery
Is a complete SELECT statement (You can use aliases for the
columns in the SELECT list.)
WITH CHECK OPTION
Specifies that only those rows that are accessible to the view can
be inserted or updated
constraint
Is the name assigned to the CHECK OPTION constraint
WITH READ ONLY
Ensures that no DML operations can be performed on this view
NOFORCE
view
alias
Note: In SQL Developer, click the Run Script icon or press [F5] to run the data definition language
(DDL) statements. The feedback messages will be shown on the Script Output tabbed page.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating a View
ai
m
t
o e
DESCRIBE empvu80
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating a View (continued)
E
L sfe
Aslide
V
The example in the
creates
an a view that contains the employee number, last name, and salary for
r
O
t
each employee
80.
n
LM innodepartment
E
a the structure of the view by using the DESCRIBE command.
NScanadisplay
s
AYou
h
Guidelines
The subquery that defines a view can contain complex SELECT syntax, including joins, groups,
and subqueries.
If you do not specify a constraint name for the view created with the WITH CHECK OPTION,
the system assigns a default name in the SYS_Cn format.
You can use the OR REPLACE option to change the definition of the view without dropping and
re-creating it, or regranting the object privileges previously granted on it.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating a View
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating a View (continued)
E
L sfe
Acolumn
V
You can control the
annames by including column aliases in the subquery.
r
O
t
n creates a view containing the employee number (EMPLOYEE_ID) with the
The example
LM in ntheoslide
E
a name (LAST_NAME) with the alias NAME, and annual salary (SALARY) with the
NSID_NUMBER,
s
Aalias
a
alias ANN_SALARY
for every employee in department 50.
h
Alternatively, 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;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SELECT *
FROM
salvu50;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Retrieving Data from aEView
fe as you would from any table. You can display either the contents
AL na view
s
You can retrieve V
data from
a
O ornjust
trspecific
of the entire
view
rows and columns.
M
L no
E
S
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Modifying a View
m
lco
ai
m
t
Column aliases in the CREATE OR REPLACE VIEW clause
o e
h
@ uid
z
are listed in the same order as the columns in the
v
a nt G
o
subquery.
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Modifying a View
E
faeview can be created even if one exists with this name already, thus
AL option,
s
With the OR REPLACE
V
n
traof the view for its owner. This means that the view can be altered without
replacingLthe
old version
MO
n
E re-creating,
no and regranting object privileges.
dropping,
S
a
N as
ANote:
hWhen assigning column aliases in the CREATE OR REPLACE VIEW clause, remember that the
aliases are listed in the same order as the columns in the subquery.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating a Complex View
E
L sfe
Aslide
V
The example in the
creates
an a complex view of department names, minimum salaries, maximum
r
O
t
salaries,L
and
Mthe average
n salaries by department. Note that alternative names have been specified for
E Thisaisnaorequirement
the S
view.
if any column of the view is derived from a function or an expression.
N
s
AYou can
haview 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;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Group functions
A GROUP BY clause
The DISTINCT keyword
The pseudocolumn ROWNUM keyword
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl on a View
NZOperations
a
Rules for Performing DML
E
r
fe on data through a view if those operations follow certain
ALDMLnoperations
s
V
You can perform
O n-tra
rules.
M
ELcanaremove
no a row from a view unless it contains any of the following:
SYou
s functions
AN h- aGroup
- A GROUP BY clause
- The DISTINCT keyword
- The pseudocolumn ROWNUM keyword
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl on a View (continued)
NZOperations
a
Rules for Performing DML
E
r
L sfe
Athrough
V
You can modify data
n a view unless it contains any of the conditions mentioned in the
adefined
r
O
t
previousLslide
or
columns
by expressions (for example, SALARY * 12).
M on
E
S san
N
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ma
t
o
h ide
@
avz nt Gu
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl on a View (continued)
NZOperations
a
Rules for Performing DML
E
r
AL sfeunless it contains any of the items listed in the slide. You cannot
You can add dataV
throughaanview
-trview contains NOT NULL columns without default values in the base table.
add dataLtoM
aO
view ifnthe
o
nvalues must be present in the view. Remember that you are adding values directly to
All S
theErequired
a
N
s
Athe underlying
ha table through the view.
For more information, see the section on CREATE VIEW in Oracle Database SQL Language
Reference 11g, Release 1 (11.1).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
A ( e this
R
AR to us
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
U le
Z
b
N
Using the WITH CHECKEOPTIONrClause
a
L
e
A referential
sf integrity checks through views. You can also enforce constraints
V
It is possible to perform
n
a
-tr view can be used to protect data integrity, but the use is very limited.
at the database
The
MOlevel.
n
L
o
E CHECK
n OPTION clause specifies that INSERTs and UPDATEs performed through the
SWITH
The
a
N
s
Aview cannot
ha create rows that the view cannot select. Therefore, it enables integrity constraints and
data validation checks to be enforced on data being inserted or updated. 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;
causes:
Note: No rows are updated because, if the department number were to change to 10, the view would
no longer be able to see that employee. With the WITH CHECK OPTION clause, therefore, the view
can see only the employees in department 20 and does not allow the department number for those
employees to be changed through the view.
Oracle Database 11g: SQL Fundamentals I 11 - 17
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Denying DML Operations
E
fe
AnoLDMLnsoperations
You can ensure that
occur on your view by creating it with the WITH READ
V
a in the next slide
r
O
t
ONLY option.
The
example
modifies the EMPVU10 view to prevent any DML
LM non
E
operations
S sonathe view.
N
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
N(continued)
Denying DML Operations
E
L sfer
A
V ararow
Any attempt to remove
n from a view with a read-only constraint results in an error:
O
t
nFROM empvu10
LMDELETE
o
E
n
S
a
employee_number = 200;
AN has WHERE
Similarly, any attempt to insert a row or modify a row using the view with a read-only constraint
results in the same error.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Removing a View
E
fe to remove a view. The statement removes the view definition
AL statement
s
You use the DROP
VIEW
V
n
-tra dropping views has no effect on the tables on which the view was
from theLdatabase.
MO oHowever,
n
n views or other applications based on the deleted views become invalid. Only the
based.
SEAlternatively,
a
N
s
Acreatorhora 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 prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Practice 11: OverviewE
ofNPart 1rab
fe you with a variety of exercises in creating, using, and
ALpractice
s
V
Part 1 of this lessons
provides
n
O Complete
tra questions 16 at the end of this lesson.
removing
views.
M
n
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Overview of views:
Creating, modifying, and retrieving data from a view
DML operations on a view
Dropping a view
Overview of sequences:
Creating, using, and modifying a sequence
Cache sequence values
NEXTVAL and CURRVAL pseudocolumns
ai
m
t
o e
h
Creating, dropping indexes
@ uid
z
v
a nt G
o
Overview of synonyms
lm ude
e
s
Creating, dropping synonyms
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Overview of indexes
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Sequences
Object
Description
Table
View
Sequence
Index
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Sequences
E
fethat creates integer values. You can create sequences and then use
AL object
s
V
A sequence is a database
n
tra
them to L
generate
MO numbers.
n
E a no
S
AN has
Synonym
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Sequences
A sequence:
Can automatically generate unique numbers
Is a shareable object
Can be used to create a primary key value
Replaces application code
Speeds up the efficiency of accessing sequence values
when cached in memory
ai
m
t
o e
h
@ uid
2
4
6
8
10 vz
a nt G
o
1
3
5
7
9
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Sequences (continued)
E
fe object that can be shared by multiple users to generate
AL ndatabase
s
V
A sequence is a user-created
O n-tra
integers.LM
o
E define
nsequence
S
a
You
can
a
to generate unique values or to recycle and use the same numbers again.
s
AANtypical
a
h usage for sequences is to create a primary key value, which must be unique for each row. A
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 sequencegenerating routine.
Sequence numbers are stored and generated independent of tables. Therefore, the same sequence can
be used for multiple tables.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ Syntax
CREATE SEQUENCE Statement:
a
E
r
fe numbers by using the CREATE SEQUENCE statement.
AL sequential
s
Automatically generate
V
n
O n-tra
M
In the syntax:
EL a no
S
sequence
Is the name of the sequence generator
s
ANINCREMENT
a
BY n
Specifies the interval between sequence numbers, where
h
START WITH n
MAXVALUE n
NOMAXVALUE
MINVALUE n
NOMINVALUE
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating a Sequence
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating a Sequence (continued)
E
AL nsfe Specifies whether the sequence continues to generate
CYCLE | NOCYCLE
V
O n-tra
values after reaching its maximum or minimum value
M
L no
E
(NOCYCLE is the default option.)
S sa
N
A CACHE
Specifies how many values the Oracle server preallocates
ha n | NOCACHE
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
NEXTVAL and CURRVAL
Pseudocolumns
E
feit generates sequential numbers for use in your tables. Reference the
ALsequence,
s
V
After you create your
n
-trathe NEXTVAL and CURRVAL pseudocolumns.
sequenceLvalues
MO byousing
n
E a pseudocolumn
n
SNEXTVAL
The
is used to extract successive sequence numbers from a specified
N
s
Asequence.
a
h 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. However, 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 prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Using a Sequence
ai
m
t
o e
h
SELECT
dept_deptid_seq.CURRVAL
@ uid
z
v
a nt G
FROM
dual;
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using a Sequence
E
L sfe
Aslide
The example in the
inserts
department in the DEPARTMENTS table. It uses the
V
an atonew
r
O
t
DEPT_DEPTID_SEQ
sequence
generate
a new department number as follows.
LM non
E
You
athe current value of the sequence using the sequence_name.CURRVAL, as shown in the
NScanaview
s
Asecond
hexample in the slide. The output of the query is shown below:
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.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Caching Sequence Values
E
fe to provide faster access to those sequence values. The cache is
AL innmemory
s
V
You can cache sequences
ra refer to the sequence. Each request for the next sequence value is
Ofirst time
tyou
populated
the
M
n
ELfromathe
nocached sequence. After the last sequence value is used, the next request for the
retrieved
S
N apulls
Asequence
h s another cache of sequences into memory.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Modifying a Sequence
Change the increment value, maximum value, minimum value,
cycle option, or cache option:
ALTER SEQUENCE dept_deptid_seq
INCREMENT BY 20
MAXVALUE 999999
NOCACHE
NOCYCLE;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Modifying a SequenceEN
L sfer
A
If you reach the MAXVALUE
V ranlimit for your sequence, no additional values from the sequence are
O
t
-receive
allocatedLand
you
an error indicating that the sequence exceeds the MAXVALUE. To
M owill
n
E
n sequence, you can modify it by using the ALTER SEQUENCE statement.
continue
the
S tosuse
a
N
ASyntaxha
ALTER SEQUENCE
sequence
[INCREMENT BY n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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.
To remove a sequence, use the DROP statement:
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NaZSequence
a
Guidelines for Modifying
E
r
L sfe
Aowner
You must beV
the
nor have the ALTER privilege for the sequence to modify it. You must be
aDROP
r
O
t
the L
owner
or
have
the
ANY SEQUENCE privilege to remove it.
M on
E
nsequence numbers are affected by the ALTER SEQUENCE statement.
SOnly future
a
N
s
A The
haSTART WITH option cannot be changed using ALTER SEQUENCE. The sequence must be
DROP SEQUENCE dept_deptid_seq;
The error:
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Overview of views:
Creating, modifying, and retrieving data from a view
DML operations on a view
Dropping a view
Overview of sequences:
Creating, using, and modifying a sequence
Cache sequence values
NEXTVAL and CURRVAL pseudocolumns
ai
m
t
o e
h
Creating, dropping indexes
@ uid
z
v
a nt G
o
Overview of synonyms
lm ude
e
s
Creating, dropping synonyms
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Overview of indexes
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Indexes
Object
Description
Table
View
Sequence
Index
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Indexes
E
L sfe
Aobjects
V
Indexes are database
anthat you can create to improve the performance of some queries.
r
O
t
Indexes L
can
Malso beoncreated automatically by the server when you create a primary key or a unique
E
constraint.
S san
N
A ha
Synonym
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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 input/output (I/O) by using a rapid path
access method to locate data quickly
Is independent of the table that it indexes
Is used and maintained automatically by the Oracle server
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Indexes (continued) EN
L sfer
A
An Oracle serverV
index isaan
schema object that can speed up the retrieval of rows by using a pointer.
r
O
t
Indexes L
can
Mbe created
n explicitly or automatically. If you do not have an index on the column, a full
o
E
n
table
scan
occurs.
NS as a
AAn
index
h provides direct and fast access to rows in a table. Its purpose is to reduce the disk I/O by
using an indexed path to locate data quickly. An 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, the corresponding indexes are also dropped.
For more information, see the section on Schema Objects: Indexes in Oracle Database Concepts
11g, Release 1 (11.1).
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
How Are Indexes Created?
E
L sfe
Atypes
V
You can create two
aofnindexes.
r
O
t
Unique
nThe Oracle server automatically creates this index when you define a column in
LMtoindex:
o
E
n
a
table
have
a
PRIMARY KEY or a UNIQUE constraint. The name of the index is the name
S sa
N
A that
hais given to the constraint.
Nonunique index: This is an index that a user can create. For example, you can create the
FOREIGN KEY column index for a join in a query to improve the speed of retrieval.
Note: You can manually create a unique index, but it is recommended that you create a unique
constraint, which implicitly creates a unique index.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating an Index
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating an Index
E
L sfe
A
Create an index on
one oramore
V
n columns by issuing the CREATE INDEX statement.
r
O
t
In the syntax:
LM non
E
S s a Is the name of the index
AN index
table
Is the name of the table
ha
column
Is the name of the column in the table to be indexed
Specify UNIQUE to indicate that the value of the column (or columns) upon which the index is based
must be unique. Specify BITMAP to indicate that the index is to be created with a bitmap for each
distinct key, rather than indexing each row separately. Bitmap indexes store the rowids associated
with a key value as a bitmap.
For more information, see the section on CREATE INDEX in Oracle Database SQL Language
Reference 11g, Release 1 (11.1).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
The table is small or most queries are expected to retrieve more thanh2%
o e
to 4% of the rows in the table
@ uid
z
v
a nt G
o
The table is updated frequently
lm ude
e
s
n expression
St
The indexed columns are referenced as part (of
aan
s
i
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Index Creation Guidelines
E
ALBetternsfe
V
More Is Not Always
O n-tra
M
Having more
o on a table does not produce faster queries. Each DML operation that is
EL onaindexes
ntable
S
committed
a
with indexes means that the indexes must be updated. The more indexes that
N as
Ayou
have
h associated with a table, the more effort the Oracle server must make to update all the
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Removing an Index
m
lco
ai
tm
To drop an index, you must be the owner of the indexoor
h
e
have the DROP ANY INDEX privilege.
z@ uid
av nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Removing an Index EN
L sfer
A
V indexes.
You cannot modify
anTo change an index, you must drop it and then re-create it.
r
O
t
n
RemoveLanMindex o
definition
from the data dictionary by issuing the DROP INDEX statement. To drop
E
n
S you
amust be the owner of the index or have the DROP ANY INDEX privilege.
Nindex,
s
Aan
a
In the h
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 prohibited Copyright 2014, Oracle and/or its affiliates
Lesson Agenda
Overview of views:
Creating, modifying, and retrieving data from a view
DML operations on a view
Dropping a view
Overview of sequences:
Creating, using, and modifying a sequence
Cache sequence values
NEXTVAL and CURRVAL pseudocolumns
ai
m
t
o e
h
Creating, dropping indexes
@ uid
z
v
a nt G
o
Overview of synonyms
lm ude
e
s
Creating, dropping synonyms
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Overview of indexes
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Synonyms
Object
Description
Table
View
Sequence
Index
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Synonyms
E
e
fthat
AL objects
s
V
Synonyms are database
enable you to call a table by another name. You can create
n
a
r
O
t
synonyms
to give annalternative name to a table.
LM
E
no
S
a
AN has
Synonym
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NanZObject
a
Creating a Synonym for
E
r
AL nsfeby another user, you need to prefix the table name with the name of
To refer to a tableVthat is owned
ra
tfollowed
the user L
who
creatednit,
by a period. Creating a synonym eliminates the need to qualify the
MO
o
E
n
object
name
with
the
schema
and
provides you with an alternative name for a table, view, sequence,
S sa
N
Aprocedure,
ha or other objects. This method can be especially useful with lengthy object names, such as
views.
In the syntax:
PUBLIC
synonym
object
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 the section on CREATE SYNONYM in Oracle Database SQL Language
Reference 11g, Release 1 (11.1).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Drop a synonym:
DROP SYNONYM d_sum;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
NSynonyms
Creating and Removing
E
L sfer
A
V ran
Creating a Synonym
O
-t a synonym for the DEPT_SUM_VU view for quicker reference.
M ocreates
n
The slideLexample
E an
Sdatabase
N
The
s administrator can create a public synonym that is accessible to all users. The following
Aexample
hacreates
a public synonym named DEPT for Alices DEPARTMENTS table:
CREATE PUBLIC SYNONYM dept
FOR
alice.departments;
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;
For more information, see the section on DROP SYNONYM in Oracle Database SQL Language
Reference 11g, Release 1 (11.1).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Quiz
Indexes must be created manually and serve to speed up
access to rows in a table.
1. True
2. False
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Answer: 2
E
fe up query performance. However, not all indexes are created
AL ntosspeed
Note: Indexes areVdesigned
OOraclen-server
tra automatically creates an index when you define a column in a table to
manually.
The
M
L noKEY or a UNIQUE constraint.
have
aEPRIMARY
S
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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 speed of query retrieval
Use synonyms to provide alternative names for objects
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Summary
E
e
AL nsflearned
In this lesson, youVshouldahave
about database objects such as views, sequences, indexes, and
r
O
t
synonyms.
LM non
E
S
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Practice 11: OverviewE
ofNPart 2rab
fe you with a variety of exercises in creating and using a
ALpractice
s
V
Part 2 of this lessons
provides
n
O
traasynonym.
sequence,
an
index,
and
M
n
EL questions
no 710 at the end of this lesson.
S
a
Complete
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Appendix A
Practices and Solutions
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Table of Contents
Practices for Lesson I................................................................................................................ 3
Practice I-1: Introduction ................................................................................................ 4
Practice Solutions I-1: Introduction ................................................................................ 5
Practices for Lesson 1 ............................................................................................................. 11
Practice 1-1: Retrieving Data Using the SQL SELECT Statement .............................. 12
Practice Solutions 1-1: Retrieving Data Using the SQL SELECT Statement .............. 16
Practices for Lesson 2 ............................................................................................................. 19
Practice 2-1: Restricting and Sorting Data.................................................................... 20
Practice Solutions 2-1: Restricting and Sorting Data ................................................... 24
Practices for Lesson 3 ............................................................................................................. 27
Practice 3-1: Using Single-Row Functions to Customize Output ................................ 28
Practice Solutions 3-1: Using Single-Row Functions to Customize Output ................ 32
Practices for Lesson 4 ............................................................................................................. 35
Practice 4-1: Using Conversion Functions and Conditional Expressions .................... 36
Practice Solutions 4-1: Using Conversion Functions and Conditional Expressions .... 39
Practices for Lesson 5 ............................................................................................................. 41
Practice 5-1: Reporting Aggregated Data Using the Group Functions......................... 42
Practice Solutions 5-1: Reporting Aggregated Data Using the Group Functions ........ 45
Practices for Lesson 6 ............................................................................................................. 48
Practice 6-1: Displaying Data from Multiple Tables Using Joins ................................ 49
Practice Solutions 6-1: Displaying Data from Multiple Tables Using Joins ................ 52
Practices for Lesson 7 ............................................................................................................. 54
Practice 7-1: Using Subqueries to Solve Queries ......................................................... 55
Practice Solutions 7-1: Using Subqueries to Solve Queries ......................................... 57
Practices for Lesson 8 ............................................................................................................. 59
Practice 8-1: Using the Set Operators........................................................................... 60
Practice Solutions 8-1: Using the Set Operators........................................................... 62
Practices for Lesson 9 ............................................................................................................. 64
Practice 9-1: Manipulating Data ................................................................................... 65
Practice Solutions 9-1: Manipulating Data ................................................................... 69
Practices for Lesson 10 ........................................................................................................... 73
Practice 10-1: Using DDL Statements to Create and Manage Tables .......................... 74
Practice Solutions 10-1: Using DDL Statements to Create and Manage Tables.......... 76
Practices for Lesson 11 ........................................................................................................... 79
Practice 11-1: Creating Other Schema Objects ............................................................ 80
Practice Solutions 11-1: Creating Other Schema Objects ............................................ 82
Practices for Appendix F ........................................................................................................ 84
Practice F-1: Oracle Join Syntax................................................................................... 85
Practice Solutions F-1: Oracle Join Syntax .................................................................. 88
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
If you are asked to save any lab files, save them in this location.
In any practice, there may be exercises that are prefaced with the phrases If you have
time or If you want an extra challenge. Work on these exercises only if you have
completed all other exercises within the allocated time and would like a further challenge
to your skills.
ai
m
t
o e
h
@ uid
z
Note
v
a nt G
o
lm environment.
1) All written practices use Oracle SQL Developer as the development
de
e
u
s
t
n s Syou can also use
Although it is recommended that you use Oracle SQLaDeveloper,
(
SQL*Plus that is available in this course.
A e thi
R
AR from
uthes database may differ from the
2) For any query, the sequence of rows retrieved
G
o
t
screenshots shown.
ZE nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Perform the practices slowly and precisely. You can experiment with saving and running
command files. If you have any questions at any time, ask your instructor.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
This is the first of many practices in this course. The solutions (if you require them) can
be found at the end of this practice. Practices are intended to cover most of the topics that
are presented in the corresponding lesson.
Starting Oracle SQL Developer
1) Start Oracle SQL Developer using the SQL Developer desktop icon.
Creating a New Oracle SQL Developer Database Connection
2) To create a new database connection, in the Connections Navigator, right-click
Connections. Select New Connection from the menu. The New/Select Database
Connection dialog box appears.
3) Create a database connection using the following information:
a) Connection Name: myconnection
ai
m
t
c) Password: ora1
o e
h
@ uid
z
d) Hostname: localhost
v
a nt G
o
e) Port: 1521
lm ude
e
s
an is St
f) SID: ORCL
(
RA se th
Ensure that you select the Save Password check
box.
R
GA to u
E
e Developer Database Connection
Testing and Connecting Using A
theZ
OraclenSQL
s
EL e lice
4) Test the new connection.
U
NZ rabl to the database using this new connection.
5) If the statusL
isE
Success,econnect
A nsf
V
Browsing
the
O nTables
tra in the Connections Navigator
M
o
Navigator, view the objects available to you in the Tables node.
E6)L In athenConnections
S
N
Verify
that
the
following
tables are present:
A has
COUNTRIES
b) Username: ora1
DEPARTMENTS
EMPLOYEES
JOB_GRADES
JOB_HISTORY
JOBS
LOCATIONS
REGIONS
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1) Start Oracle SQL Developer using the SQL Developer desktop icon.
a) Double-click the SQL Developer desktop icon.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN hCreating
as a New Oracle SQL Developer Database Connection
2) To create a new database connection, in the Connections Navigator, right-click
Connections and select New Connection from the menu.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
G
3) Create a database connection using the following information: oa
t
n
lm ude
e
a) Connection Name: myconnection
s
n s St
a
(
b) Username: ora1
A e thi
R
AR to us
c) Password: ora1
G
E se
d) Hostname: localhost A Z
L licen
E
e) Port: 1521
ZU able
N
f) SID: ORCL
LE sfer
A
V ranthe Save Password check box.
EnsureO
that you select
M on-t
L
SE s a n
N
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
Testing and Connecting Using the Oracle SQL Developer Database Connection
h
@ uid
z
4) Test the new connection.
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
5) If the status is Success, connect to the database using this new connection.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
When you create a connection, a SQL Worksheet for that connection opens h
@ uid
z
automatically.
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Browsing the Tables in the Connections Navigator
6) In the Connections Navigator, view the objects available to you in the Tables node.
Verify that the following tables are present:
COUNTRIES
DEPARTMENTS
EMPLOYEES
JOB_GRADES
JOB_HISTORY
JOBS
LOCATIONS
REGIONS
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
7) Browse the structure of the EMPLOYEES table.
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
8) View the data of the DEPARTMENTS table.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
In this practice, you write simple SELECT queries. The queries cover most of the SELECT
clauses and operations that you learned in this lesson.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
True/False
2) The following SELECT statement executes successfully:
SELECT *
FROM
job_grades;
ai
m
t
o e
h
True/False
@ uid
z
v
a nt G
o
lm identify
3) There are four coding errors in the following statement. Can
you
dethem?
e
u
s
t
an is S
(
SELECT
employee_id, last_name
sal x 12 ANNUAL SALARY
RA se th
R
FROM
employees;
GA to u
E
Z nse
A
EL e lice
U
Part 2
Z abl
N
E
r you begin with the practices:
Note the following
before
L points
e
f
A
s
V ranlab files at the following location:
O
Save all-your
t
M /home/oracle/labs/sql1/labs
n
L
o
n
SE s aEnter
N
your SQL statements in a SQL Worksheet. To save a script in SQL
A ha
Developer, make sure that the required SQL worksheet is active and then from the
File menu, select Save As to save your SQL statement as a
lab_<lessonno>_<stepno>.sql script. When you are modifying an existing
script, make sure that you use Save As to save it with a different file name.
To run the query, click the Execute Statement icon in the SQL Worksheet.
Alternatively, you can press [F9]. For DML and DDL statements, use the Run
Script icon or press [F5].
After you have executed the query, make sure that you do not enter your next
query in the same worksheet. Open a new worksheet.
You have been hired as a SQL programmer for Acme Corporation. Your first task is to
create some reports based on data from the Human Resources tables.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
4) Your first task is to determine the structure of the DEPARTMENTS table and its
contents.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
5) Determine the structure of the EMPLOYEES table. an
St
(
s
i
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
The HR department wants a query to display the last name, job ID, hire date, and
employee ID for each employee, with the employee ID appearing first. Provide an
alias STARTDATE for the HIRE_DATE column. Save your SQL statement to a file
named lab_01_05.sql so that you can dispatch this file to the HR department.
6) Test your query in the lab_01_05.sql file to ensure that it runs correctly.
Note: After you have executed the query, make sure that you do not enter your next
query in the same worksheet. Open a new worksheet.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
7) The HR department wants a query to display all unique job IDs from the EMPLOYEES
table.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
tra
Part
3O
M
n
EIfLyouahave
notime, complete the following exercises:
S
AN h8)asThe HR department wants more descriptive column headings for its report on
employees. Copy the statement from lab_01_05.sql to a new SQL Worksheet.
Name the column headings Emp #, Employee, Job, and Hire Date, respectively.
Then run the query again.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
9) The HR department has requested a report of all employees and their job IDs. Display
the last name concatenated with the job ID (separated by a comma and space) and
name the column Employee and Title.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
If you want an extra challenge, complete the following exercise:
e
s
St a query to
antable,iscreate
(
10) To familiarize yourself with the data in the EMPLOYEES
th
RAcolumn
display all the data from that table. Separate
each
output by a comma. Name
e
R
s
the column title THE_OUTPUT.
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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;
ai
m
t
o e
h
@ uid
z
v
True/False
a nt G
o
lm ude
e
s
t
S
3) There are four coding errors in the following statement.
identify them?
anCan you
(
s
i
RA se th
SELECT
employee_id, last_name
R
A to u
sal x 12 ANNUAL SALARY
G
E
FROM
employees; Z
e
s
A
n
e
ELdoesenot
iccontain
l
U
The EMPLOYEES
table
a column called sal. The column is
Z abl
N
called SALARY
LE .sfer
A
V ran
O
The
operator is *, not x, as shown in line 2.
-t
M omultiplication
n
L
n ANNUAL SALARY alias cannot include spaces. The alias should read
SE sThe
a
N
A ha ANNUAL_SALARY or should be enclosed within double quotation marks.
A comma is missing after the LAST_NAME column.
Part 2
You have been hired as a SQL programmer for Acme Corporation. Your first task is to
create some reports based on data from the Human Resources tables.
4) Your first task is to determine the structure of the DEPARTMENTS table and its
contents.
a. To determine the DEPARTMENTS table structure:
DESCRIBE departments
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SELECT *
FROM
departments;
The HR department wants a query to display the last name, job ID, hire date, and
employee ID for each employee, with the employee ID appearing first. Provide an
alias STARTDATE for the HIRE_DATE column. Save your SQL statement to a file
named lab_01_05.sql so that you can dispatch this file to the HR department.
SELECT employee_id, last_name, job_id, hire_date StartDate
FROM
employees;
ai
m
t
o e
h
SELECT employee_id, last_name, job_id, hire_date StartDate
@ uid
z
v
FROM
employees;
a nt G
o
lmfromuthedeEMPLOYEES
e
7) The HR department wants a query to display all unique job
IDs
s
an is St
table.
(
RA se th
SELECT DISTINCT job_id
R
FROM
employees;
GA to u
E
Z nse
A
EL e lice
U
Part 3
NZ rabl
E
fethe following exercises:
If you have time,
AL complete
s
V
n
OHR department
tra wants more descriptive column headings for its report on
8) M
The
n
EL employees.
no Copy the statement from lab_01_05.sql to a new SQL Worksheet.
S
a
AN hasName the column headings Emp #, Employee, Job, and Hire Date, respectively.
Then run the query again.
6) Test your query in the lab_01_05.sql file to ensure that it runs correctly.
9) The HR department has requested a report of all employees and their job IDs. Display
the last name concatenated with the job ID (separated by a comma and space) and
name the column Employee and Title.
SELECT last_name||', '||job_id "Employee and Title"
FROM
employees;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
2) Open a new SQL Worksheet. Create a report that displays the last name and
department number for employee number 176. Run the query.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
m udeModify
lemployees.
e
3) The HR department needs to find high-salary and low-salary
s
St whose salary
aforn any iemployee
lab_02_01.sql to display the last name and salary
(
s
A SQLestatement
th as
is not in the range of $5,000 to $12,000. SaveR
your
R
s
lab_02_03.sql.
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
4) Create a report to display the last name, job ID, and hire date for employees with the
last names of Matos and Taylor. Order the query in ascending order by the hire date.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Modify lab_02_03.sql to display the last name and salary of employees who earn
between $5,000 and $12,000, and are in department 20 or 50. Label the columns
Employee and Monthly Salary, respectively. Save lab_02_03.sql as
lab_02_06.sql again. Run the statement in lab_02_06.sql.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
th and hire date for all
7) The HR department needs a report that displays
the lastename
RA
R
s
u
employees who were hired in 1994. GA
o
t
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
E8)L Create
naoreport to display the last name and job title of all employees who do not have
S
a
manager.
a
AN has
9) Create a report to display the last name, salary, and commission of all employees who
earn commissions. Sort data in descending order of salary and commissions.
Use the columns numeric position in the ORDER BY clause.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
11) The HR department wants to run reports based on a manager. Create a query that
prompts the user for a manager ID and generates the employee ID, last name, salary,
and department for that managers employees. The HR department wants the ability
to sort the report on a selected column. You can test the data with the following
values:
manager_id = 103, sorted by last_name:
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
manager_id = 201, sorted by salary:
GA to u
E
Z nse
A
EL e lice
U
l
NZsortedrabybemployee_id:
manager_id =E
124,
AL nsfe
V
O n-tra
M
EL a no
S
AN has
If you have time, complete the following exercises:
12) Display all employee last names in which the third letter of the name is a.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
13) Display the last names of all employees who have both an a and an e in their last
name.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
15) Modify lab_02_06.sql to display the last name, salary, and
lmcommission
de for all
e
u
s
t
employees whose commission is 20%. Save lab_02_06.sql
as
lab_02_15.sql
an is S
again. Rerun the statement in lab_02_15.sql.A (
R se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
last_name, salary
employees
salary > 12000;
2) Open a new SQL Worksheet. 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;
ai
m
t
o e
h
@ uid
z
v
a nt G
o
SELECT last_name, salary
lm ude
e
s
FROM
employees
an is St
(
WHERE
salary NOT BETWEEN 5000 AND 12000;
th
RA sedate
4) Create a report to display the last name, A
jobRID, anduhire
for employees with the
G
o
t
last names of Matos and Taylor. Order
query
e in ascending order by hire date.
ZE the
s
A
n
SELECT
last_name,
ELjob_id,
icehire_date
l
U
FROM
employees
e
NZ raINbl('Matos', 'Taylor')
WHERE
last_name
E
fe
ALhire_date;
ORDER BY
s
V
n
O n-last
traname and department ID of all employees in departments 20 or 50 in
5)LM
Display the
E ascending
no alphabetical order by name.
S
a
AN hasSELECT last_name, department_id
FROM
employees
WHERE
department_id IN (20, 50)
ORDER BY last_name ASC;
6) Modify lab_02_03.sql to list the last name and salary of employees who earn
between $5,000 and $12,000, and are in department 20 or 50. Label the columns
Employee and Monthly Salary, respectively. Save lab_02_03.sql as
lab_02_06.sql again. Run the statement in lab_02_06.sql.
SELECT
FROM
WHERE
AND
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, 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';
8) Create a report to display the last name and job title of all employees who do not have
a manager.
SELECT
FROM
WHERE
last_name, job_id
employees
manager_id IS NULL;
9) Create a report to display the last name, salary, and commission for all employees
who earn commissions. Sort data in descending order of salary and commissions. Use
the columns numeric position in the ORDER BY clause.
SELECT
FROM
WHERE
ORDER BY
ai
m
t
o e
h
@ uid
z
v
G
athe queries
t
o
10) Members of the HR department want to have more flexibility m
with
that
n
l name uanddesalary of
e
you are writing. They would like a report that displays theslast
t
S
anspecifies
employees who earn more than an amount that the user
after a prompt. (You
(
s
i
A modify
thit.) Save this query to a
can use the query created in practice exerciseR
1 and
e
R
s
file named lab_02_10.sql.
GA to u
E
Z nse
SELECT last_name, salary
A
FROM
employees EL
ice
l
U
e
WHERE
salaryZ> &sal_amt;
N rabl
E
fe for a value in a dialog box. Click OK.
AL when
Enter 12000
prompted
s
V
n
O n-tra
M
EL a no
S
AN has
11) The HR department wants to run reports based on a manager. Create a query that
prompts the user for a manager ID and generates the employee ID, last name, salary,
and department for that managers employees. The HR department wants the ability
to sort the report on a selected column. You can test the data with the following
values:
manager _id = 103, sorted by last_name
manager_id = 201, sorted by salary
manager_id = 124, sorted by employee_id
Oracle Database 11g: SQL Fundamentals I A - 25
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
last_name
employees
last_name LIKE '__a%';
13) Display the last names of all employees who have both an a and an e in their last
name.
SELECT
FROM
WHERE
AND
last_name
employees
last_name LIKE '%a%'
last_name LIKE '%e%';
WHERE
commission_pct = .20;
ai
m
t
o e
h
@ uid
If you want an extra challenge, complete the following exercises:
z
v
G
athat ofnatsales
o
14) Display the last name, job, and salary for all employees whose
job
is
e
d$3,500,
eltom$2,500,
u
representative or a stock clerk, and whose salary is not n
equal
or
s
t
S
a
(
s
$7,000.
i
RA se th
R
SELECT
last_name, job_id, salary
GA to u
FROM
employees
E
e
Z n'ST_CLERK')
WHERE
job_id IN ('SA_REP',
s
A
AND
salary NOT
ELIN e(2500,
ice 3500, 7000);
l
U
NZ rabtoldisplay the last name, salary, and commission for all
15) Modify lab_02_06.sql
E
L commission
fe
Awhose
employees
amount is 20%. Save lab_02_06.sql as
s
V
n
a
r
O
t
lab_02_15.sql
again.
Rerun
the statement in lab_02_15.sql.
M onL
n last_name "Employee", salary "Monthly Salary",
SE sSELECT
a
N
A ha
commission_pct
FROM
employees
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
This practice provides a variety of exercises using different functions that are available
for character, number, and date data types.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1) Write a query to display the system date. Label the column Date.
Note: If your database is remotely located in a different time zone, the output will be
the date for the operating system on which the database resides.
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. Save your SQL statement in a file named
lab_03_02.sql.
3) Run your query in the lab_03_02.sql file.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
ea column that subtracts the old salary
Z tonadd
s
4) Modify your query lab_03_02.sql
A
ELthe ecolumn
iceIncrease. Save the contents of the file as
l
from the new salary. Label
U
bl query.
N. Z
lab_03_04.sql
Run the
revised
a
E
r
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Rewrite the query so that the user is prompted to enter a letter that the last name starts
with. For example, if the user enters H (capitalized) when prompted for a letter,
then the output should show all employees whose last name starts with the letter H.
m
lco
ai
m
t
o e
h
@ uid
z
v
G
Modify the query such that the case of the entered letter does noto
affect
a thenoutput.
t
The entered letter must be capitalized before being processed
by the SELECT
lm
de query.
e
u
s
t
an is S
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
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 as
MONTHS_WORKED. Order your results by the number of months employed. Round the
number of months up to the closest whole number.
Note: Because this query depends on the date when it was executed, the values in the
MONTHS_WORKED column will differ for you.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
fe the first eight characters of the employees last names
AL thatnsdisplays
8) Create V
a query
raamounts of their salaries with asterisks. Each asterisk signifies a
tthe
andO
indicates
M
n
EL thousand
nodollars. Sort the data in descending order of salary. Label the column
S
a
AN hasEMPLOYEES_AND_THEIR_SALARIES.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1) Write a query to display the system date. Label the column Date.
Note: If your database is remotely located in a different time zone, the output will be
the date for the operating system on which the database resides.
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. Save your SQL statement in a file named
lab_03_02.sql.
SELECT
ai
m
t
o e
3) Run your query in the file lab_03_02.sql.
h
@ uid
z
SELECT employee_id, last_name, salary,
v
a nt G
ROUND(salary * 1.155, 0) "New Salary"mo
l
de
e
FROM
employees;
u
s
t
S
(an hsubtracts
s
i
4) Modify your query lab_03_02.sql to add a A
column that
the old salary
t
R
e
s the contents of the file as
from the new salary. Label the column Increase.
AR to uSave
G
lab_03_04.sql. Run the revised
e
ZEquery.
s
A
n
SELECT employee_id,
EL last_name,
ice 0)salary,
l
U
ROUND(salary
*
1.155,
Salary",
e
Z ab*l 1.155, 0) "New
N
ROUND(salary
salary
"Increase"
LE sfer
FROM VAemployees;
an
r
O
t
5)LM
Write a o
query
n that displays the last name (with the first letter in uppercase and all the
E
n
other
letters
in lowercase) and the length of the last name for all employees whose
S sa
N
A ha name starts with the letters J, A, or M. Give each column an appropriate label.
Sort the results by the employees last names.
FROM
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 ;
Rewrite the query so that the user is prompted to enter a letter that starts the last
name. For example, if the user enters H (capitalized) when prompted for a letter, then
the output should show all employees whose last name starts with the letter H.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SELECT
INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM
employees
WHERE
last_name LIKE '&start_letter%'
ORDER BY last_name;
Modify the query such that the case of the entered letter does not affect the output. The
entered letter must be capitalized before being processed by the SELECT query.
SELECT INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM
employees
WHERE
last_name LIKE UPPER('&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.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
e in the
lm uthedvalues
Note: Because this query depends on the date when it was e
executed,
s
t
MONTHS_WORKED column will differ for you.
an is S
(
RA se th
SELECT last_name, ROUND(MONTHS_BETWEEN(
R
SYSDATE, hire_date)) MONTHS_WORKED
GA to u
E
FROM
employees
Z nse
A
ORDER BY months_worked;
EL e lice
U
Z athe
bl following exercises:
Ncomplete
If you have the time,
E
r
fe the last name and salary for all employees. Format the
AL to ndisplay
s
7) Create V
a query
a
O to ben-15trcharacters
salary
long, left-padded with the $ symbol. Label the column
M
L SALARY.
o
E
S san
N
A ha SELECT last_name,
LPAD(salary, 15, '$') SALARY
FROM
employees;
8) Create a query that displays the first eight characters of the employees last names
and indicates the amounts of their salaries with asterisks. Each asterisk signifies a
thousand dollars. Sort the data in descending order of salary. Label the column
EMPLOYEES_AND_THEIR_SALARIES.
SELECT rpad(last_name, 8)||' '||
rpad(' ', salary/1000+1, '*')
EMPLOYEES_AND_THEIR_SALARIES
FROM employees
ORDER BY salary DESC;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o
2) Display each employees last name, hire date, and salary review date, which h
is the de
z@theGdates
ui
first Monday after six months of service. Label the column REVIEW. Format
v
a
o 2000.
nt
to appear in the format similar to Monday, the Thirty-First of
mJuly,
e
l
d
e
s Stu
n
a
A ( e this
R
AR to us
G
ZE nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN h3)asDisplay 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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
4) Create a query that displays the employees last names and commission amounts. If
an employee does not earn commission, show No Commission. Label the column
COMM.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
If you have time, complete the following exercises:
o
lm ude
egrade
s
5) Using the DECODE function, write a query that displaysnthe
oftall employees
S
a
(
s
i
based on the value of the column JOB_ID, usingAthe following
th data:
R
e
Job
Grade
AR to us
G
AD_PRES
A
ZE nse
ST_MAN
B A
ECL e lice
IT_PROG
U
SA_REP
NZ raED bl
ST_CLERK LE
fe 0
s
None V
ofAthe above
n
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Rewrite the statement in the preceding exercise by using the CASE syntax.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
FROM
2) Display each employees last name, hire date, and salary review date, which is the
first Monday after six months of service. Label the column REVIEW. Format the
dates to appear in the format similar to Monday, the Thirty-First of July, 2000.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lmthe employee
3) Display the last name, hire date, and day of the week on which
de started.
e
u
s
t
Label the column DAY. Order the results by the day(of
starting with
S
anthe week,
s
i
Monday.
RA se th
R
A to u
SELECT last_name, hire_date,
G
E
TO_CHAR(hire_date,
eDAY
Z 'DAY')
s
n
FROM
employees LA
E e lic-e 1, 'd');
ORDER BY TO_CHAR(hire_date
U
Z abl
N
E
4) Create a query
L that sdisplays
er the employees last names and commission amounts. If
f
A
V does
an employee
annot earn commission, show No Commission. Label the column
r
O
t
COMM. n
LM
o
E
n
S
a
AN hasSELECT last_name,
NVL(TO_CHAR(commission_pct), 'No Commission') COMM
SELECT last_name, hire_date,
TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6),'MONDAY'),
'fmDay, "the" Ddspth "of" Month, YYYY') REVIEW
FROM
employees;
FROM
employees;
5) Using the DECODE function, write a query that displays the grade of all employees
based on the value of the JOB_ID column, using the following data:
Job
Grade
AD_PRES
ST_MAN
IT_PROG
SA_REP
ST_CLERK
None of the above
A
B
C
D
E
0
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
'E',
'D',
'C',
'B',
'A',
6) Rewrite the statement in the preceding exercise by using the CASE syntax.
SELECT job_id, CASE
WHEN
WHEN
WHEN
WHEN
WHEN
ELSE
FROM employees;
job_id
'ST_CLERK' THEN
'SA_REP'
THEN
'IT_PROG' THEN
'ST_MAN'
THEN
'AD_PRES' THEN
'0' END GRADE
'E'
'D'
'C'
'B'
'A'
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
At the end of this practice, you should be familiar with using group functions and
selecting groups of data.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
The HR department needs the following reports:
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. Save your SQL statement as lab_05_04.sql. Run the query.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lmmaximum,
de sum, and
e
5) Modify the query in lab_05_04.sql to display the minimum,
u
s
t
S
an as lab_05_05.sql
(
average salary for each job type. Save lab_05_04.sql
again.
s
i
A e th
Run the statement in lab_05_05.sql. RR
A to us
G
ZE nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Write a query to display the number of people with the same job.
m
lco
ai
m
t
otitle. e
Generalize the query so that the user in the HR department is prompted for a h
job
@ uid
Save the script to a file named lab_05_06.sql. Run the query. Enter z
IT_PROG
v
a nt G
when prompted.
o
lm ude
e
s
an is St
(
RA se th
R
7) Determine the number of managers without
uthem. Label the column Number
GA listing
o
t
E
of Managers.
Z nse
A
Hint: Use the MANAGER_ID
column
EL e lictoedetermine the number of managers.
U
NZ rabl
E
AL nsfe
V
Othe difference
8) M
Find
tra between the highest and lowest salaries. Label the column
n
EL DIFFERENCE
no .
S
a
AN has
If you have time, complete the following exercises:
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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
11) Create a matrix query to display the job, the salary for that job based on department
number, and the total salary for that job, for departments 20, 50, 80, and 90, giving
each column an appropriate heading.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
The HR department needs the following reports:
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. Save your SQL statement as lab_05_04.sql. Run the
query.
ai
m
t
o e
h
SELECT ROUND(MAX(salary),0) "Maximum",
@ uid
z
v
ROUND(MIN(salary),0) "Minimum",
a nt G
o
ROUND(SUM(salary),0) "Sum",
lm ude
e
ROUND(AVG(salary),0) "Average"
s
an is St
FROM
employees;
(
RA thesminimum,
e th maximum, sum,
5) Modify the query in lab_05_04.sqlA
toR
display u
to
and average salary for each job type.
Save lab_05_04.sql
as lab_05_05.sql
EG
e
Z
s
A
n
again. Run the statement inLlab_05_05.sql.
E e lice
U
SELECT job_id,ZROUND(MAX(salary),0)
"Maximum",
bl
N ROUND(MIN(salary),0)
a
E
"Minimum",
r
L sROUND(SUM(salary),0)
e
f
A
V ran ROUND(AVG(salary),0) "Sum",
O
"Average"
-t
M
n
L
FROM
employees
o
n BY job_id;
SE sGROUP
a
N
A ha
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_05_06.sql. Run the query. Enter IT_PROG
when prompted and click OK.
SELECT job_id, COUNT(*)
FROM
employees
WHERE job_id = '&job_title'
GROUP BY job_id;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
7) Determine the number of managers without listing them. Label the column Number
of Managers.
Hint: Use the MANAGER_ID column to determine the number of managers.
SELECT COUNT(DISTINCT manager_id) "Number of Managers"
FROM
employees;
8) Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.
SELECT
FROM
ai
m
t
o e
h
@ uid
z
SELECT
manager_id, MIN(salary)
v
a nt G
o
FROM
employees
lm ude
e
WHERE
manager_id IS NOT NULL
s
an is St
GROUP BY manager_id
(
HAVING
MIN(salary) > 6000
RA se th
R
ORDER BY MIN(salary) DESC;
GA to u
E
se exercises:
If you want an extra challenge,Lcomplete
A Z cethenfollowing
li
UE
10) Create a query thatZ
will
display
the total number of employees and, of that total, the
e
l
abin 1995, 1996, 1997, and 1998. Create appropriate
number of employees
EN fhired
r
L
e
A ns
columnVheadings.
O nCOUNT(*)
tra total,
M
SELECT
EL a no SUM(DECODE(TO_CHAR(hire_date,
S
AN has'YYYY'),1995,1,0))"1995",
SUM(DECODE(TO_CHAR(hire_date,
'YYYY'),1996,1,0))"1996",
SUM(DECODE(TO_CHAR(hire_date,
'YYYY'),1997,1,0))"1997",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1998,1,0))"1998"
FROM
employees;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
11) Create a matrix query to display the job, the salary for that job based on the
department number, and the total salary for that job, for departments 20, 50, 80, and
90, giving each column an appropriate heading.
SELECT
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",
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
This practice is intended to give you experience in extracting data from more than one
table using the SQL:1999compliant joins.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
n s St
a
(
A e thi
R
AR to us
G
ZE nse
A
ELa report
iofceemployees in Toronto. Display the last name,
3) The HR department needs
l
U
e
Z and
l department name for all employees who work in
job, department N
number,
bthe
a
E
r
Toronto. AL
fe
s
V
n
O n-tra
M
EL a no
S
AN has
4) Create a report to display employees last name and employee number along with
their managers last name and manager number. Label the columns Employee, Emp#,
Manager, and Mgr#, respectively. Save your SQL statement as lab_06_04.sql.
Run the query.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
6) Create a report for the HR department that displays employee last names,
G
a department
t
o
n
numbers, and all the employees who work in the same department
e
lm asuadgiven
escript
s
t
employee. Give each column an appropriate label. Saventhe
to
a
file
named
S
a
(
s
i
lab_06_06.sql.
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
l
NZneedsratobfind
E
9) The HR department
the names and hire dates of all the employees who
L sfe
A
were hired
before
their
managers,
along with their managers names and hire dates.
V ran
O
t
Save the script
- to a file named lab_06_09.sql.
n
LM
o
E
S san
N
A ha
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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 the employees.
SELECT last_name, department_id, department_name
FROM
employees
JOIN
departments
USING (department_id);
ai
m
t
o
"Mgr#"
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, 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.
ai
m
t
o e
h
SELECT e.last_name, e.job_id, d.department_name,
@ uid
z
v
e.salary, j.grade_level
a nt G
o
FROM
employees e JOIN departments d
lm ude
e
s
ON
(e.department_id = d.department_id)
an is St
(
JOIN
job_grades j
th
RA ANDsej.highest_sal);
ON
(e.salary BETWEEN j.lowest_sal
R
A to u
G
If you want an extra challenge, complete
the following
E
Z nse exercises:
A
8) The HR department wants
names of all employees who were hired
ELto determine
licethethename
after Davies. CreateZaU
query tole
display
and hire date of any employee hired
b
N
a
after employee
Davies.
LE sfer
A
V e.last_name,
SELECT
e.hire_date
an
r
O
t
FROM
employees
e
JOIN
davies
M
n
L ON no(davies.last_nameemployees
E
=
'Davies')
S
a davies.hire_date < e.hire_date;
AN hasWHERE
DESC JOB_GRADES
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_06_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;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a ofnallt G
2) Create a report that displays the employee number, last name,m
andosalary e
l
d of
e
employees who earn more than the average salary. Sort the
results intu
order
s
n
S
(a his
ascending salary.
A
R se t
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
3) Write a query that displays the employee number and last name of all employees who
work in a department with any employee whose last name contains the letter u.
Save your SQL statement as lab_07_03.sql. Run your query.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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.
Modify the query so that the user is prompted for a location ID. Save this to a file
named lab_07_04.sql.
5) Create a report for HR that displays the last name and salary of every employee who
reports to King.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
n s St
a
(
i name, and job ID for
6) Create a report for HR that displays the department
last
h
A number,
t
R
R use
every employee in the Executive department.
A
EG se to
Z
LA licen
E
ZU able
N
LE sfer
A
7) Create V
a report a
that
n displays a list of all employees whose salary is more than the
r
O
t
salary
of
any
employee
from department 60.
LM non
E
S s ahave the time, complete the following exercise:
AN hIfayou
8) Modify the query in lab_07_03.sql to display the employee number, last name,
and salary of all employees who earn more than the average salary, and who work in
a department with any employee whose last name contains a u. Save
lab_07_03.sql as lab_07_08.sql again. Run the statement in lab_07_08.sql.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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 order of
ascending salary.
ai
m
t
o e
h
@ uid
SELECT employee_id, last_name, salary
z
v
a nt G
FROM
employees
o
lm ude
WHERE salary > (SELECT AVG(salary)
e
s
n s St
FROM
employees)
a
(
ORDER BY salary;
A e thi
R
R and
Anumber
uslast name of all employees who
3) Write a query that displays the employee
G
o
t
work in a department with any employee
e last name contains a u. Save your
ZE nwhose
s
A
SQL statement as lab_07_03.sql
e your query.
EL e lic. Run
U
Z alast_name
SELECT employee_id,
bl
N
E
r
FROM
L sfe
Aemployees
WHEREV department_id
IN (SELECT department_id
an
r
O
t
FROM
employees
LM non
WHERE last_name like '%u%');
E
S
AN h4)asTheaHR department needs a report that displays the last name, department number,
and job ID of all employees whose department location ID is 1700.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Modify the query so that the user is prompted for a location ID. Save this to a file
named lab_07_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);
5) Create a report for HR that displays the last name and salary of every employee who
reports to King.
SELECT last_name, salary
FROM
employees
WHERE manager_id = (SELECT employee_id
FROM
employees
WHERE last_name = 'King');
ai
m
t
oID for e
6) Create a report for HR that displays the department number, last name, and job
h
@ uid
z
every employee in the Executive department.
v
a nt G
o
SELECT department_id, last_name, job_id
lm ude
e
s
FROM
employees
an is St
(
WHERE department_id IN (SELECT department_id
RA se th
FROM
departments
R
u
WHERE
=
GA department_name
o
t
E
'Executive');
Z nse
A
7) Create a report that displays
of
ELa list
icalle employees whose salary is more than the
l
U
e
l
salary of any employee
60.
bdepartment
NZ from
a
E
r
L sfeFROM employees
Alast_name
SELECT
V
an> ANY (SELECT salary
WHERE
salary
r
O
t
FROM employees
LM non
E
S
WHERE department_id=60);
a
AN has
If you have the time, complete the following exercise:
8) Modify the query in lab_07_03.sql to display the employee number, last name,
and salary of all employees who earn more than the average salary and who work in a
department with any employee whose last name contains a u. Save
lab_07_03.sql to lab_07_08.sql again. Run the statement in lab_07_08.sql.
SELECT employee_id, last_name, salary
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
employees
WHERE last_name like '%u%')
AND
salary > (SELECT AVG(salary)
FROM
employees);
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1) The HR department needs a list of department IDs for departments that do not contain
the job ID ST_CLERK. Use the set operators to create this report.
ai
m
t
o e
h
@ uid
z
v
aDisplaynthet G
3) Produce a list of jobs for departments 10, 50, and 20, in that order.
o
m ude job ID
l
and department ID by using the set operators.
e
ans is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
fethe employee IDs and job IDs of those employees who
AL thatnslists
4) Create V
a report
O have
traajob title that is the same as their job title when they were initially
currently
M
n
EL hired
nbyothe company (that is, they changed jobs, but have now gone back to doing
S
a
AN hastheir original job).
5) The HR department needs a report with the following specifications:
Last name and department ID of all employees from the EMPLOYEES table,
regardless of whether or not they belong to a department
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1) The HR department needs a list of department IDs for departments that do not contain
the job ID ST_CLERK. Use the set operators to create this report.
SELECT
FROM
MINUS
SELECT
FROM
WHERE
department_id
departments
department_id
employees
job_id = 'ST_CLERK';
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
t
SDisplay
ainnthat order.
3) Produce a list of jobs for departments 10, 50, and 20,
job ID and
(
s
i
h
A
t
department ID using the set operators.
RR use
A
SELECT distinct job_id, department_id
EG se to
Z
FROM employees
L=A10 licen
WHERE department_id
E
UNION ALL
ZU able
N
SELECT DISTINCT
E job_id, department_id
AL nsfer
FROM V
employees
WHERE
= 50
ra
O department_id
t
M
n
UNION o
ALL
L
n DISTINCT job_id, department_id
SE sSELECT
a
N
A ha FROM employees
WHERE department_id = 20
4) Create a report that lists the employee IDs and job IDs 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;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, 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
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
In this practice, you add rows to the MY_EMPLOYEE table, update and delete data from the
table, and control your transactions. You run a script to create the MY_EMPLOYEE table.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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.
Note: For all the DML statements, use the Run Script icon (or press [F5]) to execute the
query. This way you get to see the feedback messages on the Script Output tabbed page.
For SELECT queries, continue to use the Execute Statement icon or press [F9] to get the
formatted output on the Results tabbed page.
Insert data into the MY_EMPLOYEE table.
1) Run the statement in the lab_09_01.sql script to build the MY_EMPLOYEE table
used in this practice.
2) Describe the structure of the MY_EMPLOYEE table to identify the column names.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
A rowtoofudata to the MY_EMPLOYEE table
Gfirst
3) Create an INSERT statement to addEthe
ZDo not nlistsethe columns in the INSERT clause. Do not
from the following sample data.
A
L lice
enter all rows yet. UE
Z able
N
LE sfer FIRST_NAME
ID
LAST_NAME
USERID
SALARY
A
V
n
a
MO on-tr
L
Ralph
rpatel
895
SE s 1a n Patel
N
A ha
2
Dancs
Betty
bdancs
860
Biri
Ben
bbiri
1100
Newman
Chad
cnewman
750
Ropeburn
Audrey
aropebur
1550
4) Populate the MY_EMPLOYEE table with the second row of the sample data from the
preceding list. This time, list the columns explicitly in the INSERT clause.
5) Confirm your addition to the table.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Write an INSERT statement in a dynamic reusable script file to load the remaining
rows into the MY_EMPLOYEE table. The script should prompt for all the columns (ID,
LAST_NAME, FIRST_NAME, USERID, and SALARY). Save this script to a
lab_09_06.sql file.
7) Populate the table with the next two rows of the sample data listed in step 3 by
running the INSERT statement in the script that you created.
8) Confirm your additions to the table.
m
lco
ai
m
t
o e
h
@ uid
9) Make the data additions permanent.
z
v
a nt G
o
lm ude
e
s
St
Update and delete data in the MY_EMPLOYEE table. (an
s
i
RA se th
10) Change the last name of employee 3 to Drexler.
R
uhave a salary less than $900.
GA who
o
11) Change the salary to $1,000 for allE
employees
t
A Z cense
12) Verify your changes to theL
table.
UE le li
Z
EN ferab
L
A ns
V
O n-tra
M
EL a no
S
AN has
13) Delete Betty Dancs from the MY_EMPLOYEE table.
14) Confirm your changes to the table.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@
21) Discard the most recent DELETE operation without discarding the earlier
INSERT uid
z
v
a nt G
operation.
o
lm ude
e
22) Confirm that the new row is still intact.
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
bl
NZ permanent.
a
23) Make the dataE
addition
r
fe
AL ncomplete
s
If you haveVthe time,
the following exercise:
a
r
O
t
nlab_09_06.sql script such that the USERID is generated automatically
LM
24)
Modify
the
o
E
n
S
aconcatenating the first letter of the first name and the first seven characters of the
AN hasby
last name. The generated USERID must be in lowercase. Therefore, the script should
20) Confirm that the table is empty.
not prompt for the USERID. Save this script to a file named lab_09_24.sql.
ID
LAST_NAME
FIRST_NAME
USERID
SALARY
Anthony
Mark
manthony
1230
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
3) Create an INSERT statement to add the first row of data to the MY_EMPLOYEE table
from the following sample data. Do not list the columns in the INSERT clause.
ai
m
t
o e
h
d
@
z
ID
LAST_NAME
FIRST_NAME
USERID
SALARYui
v
G
oa ent
m
l
e
ud 895
s
t
n
1
Patel
Ralph
rpatel
S
(a his
A
R se t
R
ubdancs
2
Dancs
BettyGA
860
o
t
E
e
Z
A cens
L
3
Biri
bbiri
1100
li
UE Ben
e
Z
l
EN ferab
L
ANewmanns
4
Chad
cnewman
750
V
a
r
O
t
LM nonE
S
5
Ropeburn
Audrey
aropebur
1550
AN has a
INSERT INTO my_employee
VALUES (1, 'Patel', 'Ralph', 'rpatel', 895);
4) Populate the MY_EMPLOYEE table with the second row of the sample data from the
preceding list. This time, list the columns explicitly in the INSERT clause.
INSERT INTO my_employee (id, last_name, first_name,
userid, salary)
VALUES (2, 'Dancs', 'Betty', 'bdancs', 860);
*
my_employee;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Write an INSERT statement in a dynamic reusable script file to load the remaining
rows into the MY_EMPLOYEE table. The script should prompt for all the columns (ID,
LAST_NAME, FIRST_NAME, USERID, and SALARY). Save this script to a file named
lab_09_06.sql.
INSERT INTO my_employee
VALUES (&p_id, '&p_last_name', '&p_first_name',
'&p_userid', &p_salary);
7) Populate the table with the next two rows of sample data listed in step 3 by running
the INSERT statement in the script that you created.
INSERT INTO my_employee
VALUES (&p_id, '&p_last_name', '&p_first_name',
'&p_userid', &p_salary);
ai
m
t
o e
h
9) Make the data additions permanent.
@ uid
z
v
a nt G
o
COMMIT;
lm ude
e
s
an is St
(
Update and delete data in the MY_EMPLOYEE table.
RA se th
R
A to u
GDrexler.
10) Change the last name of employeeE
3 to
Z nse
UPDATE my_employee LA
E= 'Drexler'
ice
l
SET
last_name
U
e
Z abl
WHERE
id =N3;
E
fer for all employees with a salary less than $900.
ALsalaryntos$1,000
11) ChangeVthe
O n-tra
M
EL UPDATE
no my_employee
SET
salary = 1000
S
a
AN hasWHERE salary < 900;
SELECT
*
FROM my_employee;
*
my_employee;
*
my_employee;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
*
my_employee;
ai
m
t
o e
19) Delete all the rows from the MY_EMPLOYEE table.
h
@ uid
z
v
DELETE
a nt G
o
FROM my_employee;
lm ude
e
s
an is St
20) Confirm that the table is empty.
(
RA se th
SELECT *
R
FROM
my_employee;
GA to u
E
e
Zoperation
s
A
n
21) Discard the most recent DELETE
without
discarding the earlier INSERT
EL e lice
operation.
U
Z abl
EN
ROLLBACK L
TO
step_17;
er
f
A
s
Vthat the
n row is still intact.
anew
r
O
22) Confirm
t
n
LM
o
E
n
SELECT
*
S
a my_employee;
AN hasFROM
SAVEPOINT step_17;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
ho ide
ID
LAST_NAME
FIRST_NAME
USERID @ SALARY
vz t Gu
a
o
6
Anthony
Mark
manthony
m uden 1230
l
e
ns .s St
26) Confirm that the new row was added with the correct
aUSERID
(
A e thi
R
SELECT *
AR to us
FROM my_employee
G
WHERE ID='6';
ZE nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
25) Run the lab_09_24.sql script to insert the following record:
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ID
Key Type
Primary key
NAME
Nulls/Unique
FK Table
FK Column
Data type
NUMBER
VARCHAR2
Length
25
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
2) Populate the DEPT table with data from the DEPARTMENTS
RA se thtable. Include only
R
columns that you need.
GA to u
E
etable instance chart. Save the statement
Zfollowing
3) Create the EMP table based on the
s
A
n
e then execute the statement in the script to
in a script called lab_10_03.sql
EL e li,cand
U
Z that
create the table. N
Confirm
l table is created.
bthe
a
E
r
L IDsfe
LAST_NAME
FIRST_NAME
DEPT_ID
ColumnA
Name
V
n
O Typen-tra
Key
M
EL Nulls/Unique
no
S
a
AN has FK Table
DEPT
FK Column
ID
Data type
NUMBER
VARCHAR2
VARCHAR2
NUMBER
Length
25
25
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
FIRST_NAME
LAST_NAME
SALARY
DEPT_ID
34
Grant
Marcie
5678
10
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
to
read/write
status. Now, try to insert the same
7) Revert the EMPLOYEES2 table
L the
e
c
E
i
l
U
row again. You should
messages:
e
Z getathe
lfollowing
b
N
LE sfer
A
V ran
O
-t
M
n
L
EMPLOYEES2
table.
8)
Drop
the
o
E an
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ID
Key Type
Primary key
NAME
Nulls/Unique
FK Table
FK Column
Data type
NUMBER
VARCHAR2
Length
25
m
lco
ai
m
t
o e
h
CREATE TABLE dept
@ uid
z
(id
NUMBER(7)CONSTRAINT department_id_pk PRIMARY
KEY,
v
a nt G
o
name VARCHAR2(25));
lm ude
e
s
n sissue
Stthe following
To confirm that the table was created and to view its(a
structure,
i
command:
RA se th
R
DESCRIBE dept
GA to u
E
Zfrom nthese
A
2) Populate the DEPT table with
data
DEPARTMENTS table. Include only those
L
e
c
E
i
columns that you need.
ZU able l
N
E deptfer
INSERT INTO
Ldepartment_id,
A
SELECT
department_name
V rans
O
FROM
departments;
t
LM nonE
S 3) Create the EMP table based on the following table instance chart. Save the statement
AN hasin aa script called lab_10_03.sql, and then execute the statement in the script to
create the table. Confirm that the table is created.
Column Name ID
LAST_NAME
FIRST_NAME
DEPT_ID
Key Type
Nulls/Unique
FK Table
DEPT
FK Column
ID
Data type
NUMBER
VARCHAR2
VARCHAR2
NUMBER
Length
25
25
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
To confirm that the table was created and to view its structure:
DESCRIBE emp
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.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
5) Alter the EMPLOYEES2 table status to read-only.
e
s
an is St
(
ALTER TABLE employees2 READ ONLY
RA se th
R
6) Try to insert the following row in the EMPLOYEES2
GA to utable.
E
e
Z nSALARY
FIRST_NAME
LAST_NAME
DEPT_ID
ID
s
A
L lice 5678
EMarcie
Grant
10
34
U
e
Z
l
EN ferab
L
A ns
V
a the Update operation not allowed on table error message.
Note,
O younwill
trget
M
o you will not be allowed to insert any row into the table because it is
EL Therefore,
n
S
a
AN hasassigned a read-only status.
INSERT INTO employees2
CREATE TABLE employees2 AS
SELECT employee_id id, first_name, last_name, salary,
department_id dept_id
FROM
employees;
7) Revert the EMPLOYEES2 table to the read/write status. Now try to insert the same row
again.
Now, because the table is assigned a READ WRITE status, you will be allowed to
insert a row into the table.
ALTER TABLE employees2 READ WRITE
INSERT INTO employees2
VALUES (34, 'Grant','Marcie',5678,10)
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Part 1
1) The staff in the HR department wants to hide some of the data in the EMPLOYEES
table. Create a view called EMPLOYEES_VU based on the employee numbers,
employee last names, and department numbers from the EMPLOYEES table. The
heading for the employee name should be EMPLOYEE.
2) Confirm that the view works. Display the contents of the EMPLOYEES_VU view.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm udtoedisplay all
edepartment
3) Using your EMPLOYEES_VU view, write a query for thenHR
s
St
a
(
s
employee names and department numbers.
i
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
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 have been 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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
G
9) Create a nonunique index on the NAME column in the DEPT table.oa
t
n
lm ude
e
s
10) Create a synonym for your EMPLOYEES table. Call it EMP
.
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Part 1
1) The staff in the HR department wants to hide some of the data in the EMPLOYEES
table. Create a view called EMPLOYEES_VU based on the employee numbers,
employee last names, and department numbers from the EMPLOYEES table. The
heading for the employee name should be EMPLOYEE.
CREATE OR REPLACE VIEW employees_vu AS
SELECT employee_id, last_name employee, department_id
FROM employees;
2) Confirm that the view works. Display the contents of the EMPLOYEES_VU view.
SELECT
FROM
*
employees_vu;
3) Using your EMPLOYEES_VU view, write a query for the HR department to display all
employee names and department numbers.
ai
m
t
o e
SELECT
employee, department_id
h
@ uid
FROM
employees_vu;
z
v
a nt G
o
e that
4) Department 50 needs access to its employee data. Create a view
DEPT50
lmnamed
dnumbers
e
u
s
t
contains the employee numbers, employee last names,
and
department
for
n sS
a
(
i
all employees in department 50. They have requested
h label the view columns
A that
tyou
R
e
EMPNO, EMPLOYEE, and DEPTNO. For security
purposes,
do
R us not allow an employee to
A
G thetoview.
be reassigned to another department
Ethrough
e
Z
s
A
n
CREATE VIEW dept50 AS
EL eempno,
ice last_name employee,
l
SELECT
employee_id
U
Z abl deptno
department_id
N
E
er
FROMAL employees
f
s
V radepartment_id
n
WHERE
= 50
O
t
nCHECK OPTION CONSTRAINT emp_dept_50;
LM WITH
o
E
n
S
a the structure and contents of the DEPT50 view.
AN h5)asDisplay
DESCRIBE dept50
SELECT
FROM
*
dept50;
dept50
deptno = 80
employee = 'Matos';
The error is because the DEPT50 view has been created with the WITH CHECK
OPTION constraint. This ensures that the DEPTNO column in the view is protected
from being changed.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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.
CREATE SEQUENCE dept_id_seq
START WITH 200
INCREMENT BY 10
MAXVALUE 1000;
8) To test your sequence, write a script to insert two rows in the DEPT table. Name your
script lab_11_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.
INSERT INTO dept
VALUES (dept_id_seq.nextval, 'Education');
ai
m
t
o e
h
INSERT INTO dept
@ uid
z
VALUES (dept_id_seq.nextval, 'Administration');
v
a nt G
o
lm ude
9) Create a nonunique index on the NAME column in the DEPTetable.
s
an is St
(
CREATE INDEX dept_name_idx ON dept (name);
RA se th
R
10) Create a synonym for your EMPLOYEES A
uit EMP.
G table. Call
o
t
E
Z nse
CREATE SYNONYM emp FOR EMPLOYEES;
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
This practice is intended to give you practical experience in extracting data from more
than one table using the Oracle join syntax.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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. Run the query.
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. Run the query.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
A to u
G
E
3) The HR department needs a report
of
employees
in Toronto. Display the last name,
e
Z
s
A
n
job, department number, and
ce name for all employees who work in
EL department
i
l
Toronto.
U
Z able
N
LE sfer
A
V ran
O
M on-t
L
n
SE4) sCreate
a
a report to display the employees last names and employee number along
N
A ha with their
managers last names and manager number. Label the columns Employee,
Emp#, Manager, and Mgr#, respectively. Save your SQL statement as
lab_f_04.sql.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Create a report for the HR department that displays employee last names, department
numbers, and all employees who work in the same department as a given employee.
Give each column an appropriate label. Save the script to a file named
lab_f_06.sql.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
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.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
9) The HR department
fe to find the names and hire dates for all employees who
AL nsneeds
V
were
managers, along with their managers names and hire dates.
Ohirednbefore
trtoa atheir
M
Save
the
script
file
named lab_f_09.sql.
L no
E
S
AN has a
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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. Run the query.
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. Run the query.
SELECT e.last_name, e.department_id, d.department_name
FROM
employees e, departments d
WHERE e.department_id = d.department_id;
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.
ai
m
t
o e
h
@ uid
z
SELECT e.last_name, e.job_id, e.department_id,
v
a nt G
o
d.department_name
lml ude
e
FROM
employees e, departments d , locations
s
WHERE e.department_id = d.department_id
an is St
(
AND
d.location_id = l.location_id
RA se th
R
AND
LOWER(l.city) = 'toronto';
GA to u
E
Z nlastsename and the employee number along
4) Create a report to display the employee
A
L licmanager
e
with the last name of theEemployees
and manager number. Label the
U
e
columns EmployeeZ, Emp#, b
Manager
, and Mgr#, respectively. Save your SQL
l
EN fera .
statement asLlab_f_04.sql
A ns
V
SELECT
"Employee", w.employee_id "EMP#",
O w.last_name
tra
M
n
m.last_name
"Manager", m.employee_id "Mgr#"
L no
E
w, employees m
S FROM
a employees
AN hasWHERE
w.manager_id = m.employee_id;
5) Modify lab_f_04.sql to display all employees including King, who has no
manager. Order the results by the employee number. Save the SQL statement as
lab_f_05.sql. Run the query in lab_f_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 (+);
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given
employee. Give each column an appropriate label. Save the script to a file named
lab_f_06.sql.
SELECT e1.department_id department, e1.last_name employee,
e2.last_name colleague
FROM
employees e1, employees e2
WHERE e1.department_id = e2.department_id
AND
e1.employee_id <> e2.employee_id
ORDER BY e1.department_id, e1.last_name, e2.last_name;
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.
DESC JOB_GRADES
ai
m
t
SELECT e.last_name, e.job_id, d.department_name,
o e
h
e.salary, j.grade_level
@ uid
FROM
employees e, departments d, job_grades j avz
G
t
o
n
WHERE e.department_id = d.department_id
lm ude
e
AND
e.salary BETWEEN j.lowest_sal AND j.highest_sal;
s
an is St
(
th
RA exercises:
If you want an extra challenge, complete the following
e
R
s
u
GA
o
8) The HR department wants to determine
the names
of all employees hired after
t
E
eand hire date of any employee hired after
Z the name
s
Davies. Create a query to display
A
n
EL e lice
Davies.
U
bl
NZ rae.hire_date
SELECT e.last_name,
E
L sfee , employees davies
FROM VA
employees
an
r
WHERE
davies.last_name
= 'Davies'
O
t
M
n
L
AND
davies.hire_date
< e.hire_date;
o
E an
S
AN h9)asThe 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_f_09.sql.
SELECT
FROM
WHERE
AND
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Table Descriptions
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Schema Description
Overall Description
The Oracle Database sample schemas portray a sample company that operates worldwide to fill
orders for several different products. The company has three divisions:
Human Resources: Tracks information about the employees and facilities
Order Entry: Tracks product inventories and sales through various channels
Sales History: Tracks business statistics to facilitate business decisions
Each of these divisions is represented by a schema. In this course, you have access to the objects in
all the schemas. However, the emphasis of the examples, demonstrations, and practices is on the
Human Resources (HR) schema.
All scripts necessary to create the sample schemas reside in the
$ORACLE_HOME/demo/schema/ folder.
Human Resources (HR)
This is the schema that is used in this course. In the Human Resource (HR) records, each employee
)
has an identification number, email address, job identification code, salary, and manager. Some com
il
employees earn commissions in addition to their salary.
a
oantm e
The company also tracks information about jobs within the organization. Each job has
h
id
@
z
u
identification code, job title, and a minimum and maximum salary range for v
the
job. Some
G
oa positions
employees have been with the company for a long time and have heldm
different
nt within the
e
l
d
e
company. When an employee resigns, the duration the employee
swas working
tu for, the job
n
S
a
identification number, and the department are recorded. A (
his
R the locations
The sample company is regionally diverse, so it tracks
se of its warehouses and
A
u
G
toand each department is identified either by
departments. Each employee is assigned to E
a department,
e
Z
s
a unique department number or a short
Each
n department is associated with one location, and
LAname.
e
c
E
i
each location has a full addressU
that includes
e l the street name, postal code, city, state or province, and
Z
l
b
N
the country code.
LE sfera
A
In places where the
warehouses are located, the company records details such as the
V departments
an and
r
O
t
country L
name,
currency
symbol,
currency
name, and the region where the country is located
M on
E
geographically.
S
an
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
HR
DEPARTMENTS
department_id
department_name
manager_id
location_id
JOB_HISTORY
employee_id
start_date
end_date
job_id
department_id
LOCATIONS
location_id
street_address
postal_code
city
state_province
country_id
EMPLOYEES
employee_id
first_name
last_name
email
phone_number
hire_date
job_id
salary
commission_pct
manager_id
department_id
COUNTRIES
country_id
country_name
region_id
ai
m
t
o e
job_id
h
job_title
@ uid
z
v
REGIONS
min_salary
G
a region_id
t
o
n
max_salary
lm uregion_name
de
e
s
t
an is S
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
JOBS
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
SELECT * FROM employees;
. . .
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, 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
Identify the menu items of Oracle SQL Developer
Create a database connection
Manage database objects
Use SQL Worksheet
)
m
o
Save and run SQL scripts
ilc
a
m
Create and save reports
hot e
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Objectives
E
fe
AL introduced
s
n
In this appendix,Vyou are
to the graphical tool called SQL Developer. You learn how
a
r
O
t
to use SQL
n for your database development tasks. You learn how to use SQL
LMDeveloper
o
E
n
S
Worksheet
to execute SQL statements and SQL scripts.
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
SQL Developer
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
What Is Oracle SQLLDeveloper?
EN ferab
A is na sfree graphical tool designed to improve your productivity and
V
Oracle SQL Developer
tra of everyday database tasks. With just a few clicks, you can easily
MO
simplifyLthe
development
n
Eand debug
nostored procedures, test SQL statements, and view optimizer plans.
S
create
a
N as
ASQL
hDeveloper, which is 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 by using standard Oracle database
authentication. When connected, you can perform operations on objects in the database.
The SQL Developer 1.2 release tightly integrates with Developer Migration Workbench that
provides users with a single point to browse database objects and data in third-party databases,
and to migrate from these databases to Oracle. You can also connect to schemas for selected
third-party (non-Oracle) databases, such as MySQL, Microsoft SQL Server, and Microsoft
Access, and view metadata and data in these databases.
Additionally, SQL Developer includes support for Oracle Application Express 3.0.1 (Oracle
APEX).
Oracle Database 11g: SQL Fundamentals I C - 3
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Specifications of SQL
Developer
E
e
AL 1.5nissfshipped
V
Oracle SQL Developer
along with Oracle Database 11g Release 2. SQL
a
r
O
t
Developer
n in Java leveraging the Oracle JDeveloper integrated development
LMis developed
o
E
n
environment
NS as a(IDE). Therefore, it is a cross-platform tool. The tool runs on Windows, Linux, and
AMac
operating
system (OS) X platforms.
h
The default connectivity to the database is through the Java Database Connectivity (JDBC) Thin
driver, and therefore, no Oracle Home is required. SQL Developer does not require an installer
and you need to simply unzip the downloaded file. With SQL Developer, users can connect to
Oracle Databases 9.2.0.1 and later, and all Oracle database editions including Express Edition.
Note
For Oracle Database versions before Oracle Database 11g Release 2, you will have to download
and install SQL Developer. SQL Developer 1.5 is freely downloadable from the following link:
http://www.oracle.com/technology/products/database/sql_developer/index.html.
For instructions on how to install SQL Developer, see the Web site at:
http://download.oracle.com/docs/cd/E12151_01/index.htm
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
SQL Developer 1.5 L
Interface
EN ferab
A 1.5 interface
V
The SQL Developer
ns contains three main navigation tabs, from left to right:
a
r
O
t
- By using this tab, you can browse database objects and users to which
Connections
LM nontab:
E
S youshave
a access.
AN hFiles
a tab: Identified by the Files folder icon, this tab enables you to access files from your
local machine without having to use the File > Open menu.
Reports tab: Identified by the Reports icon, this tab enables you to run predefined reports
or create and add your own reports.
General Navigation and Use
SQL Developer uses the left side for navigation to find and select objects, and the right side to
display information about selected objects. You can customize many aspects of the appearance
and behavior of SQL Developer by setting preferences.
Note: You need to define at least one connection to be able to connect to a database schema and
issue SQL queries or run procedures/functions.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
otm e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
l
ai
Language (XML) file.
m
t
o e
h
Each additional database connection created is zlisted
@ inuid
v
the Connections Navigator hierarchy.
oa nt G
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Creating a Database
Connection
E
AL nsfe object that specifies the necessary information for connecting
A connection isV
a SQLrDeveloper
O
tasaa specific user of that database. To use SQL Developer, you must have
M
to a specific
database
n
ELone adatabase
no connection, which may be existing, created, or imported.
S
at
least
N as
AYou
h create and test connections for multiple databases and for multiple schemas.
can
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
l
NZ rab(continued)
Creating a Database
Connection
E
fe
ALconnection,
s
V
n
To create a database
perform the following steps:
a
r
O
t
1. On
the Connections
n tabbed page, right-click Connections and select New Connection.
LM
o
E
n
2.S In the a
New/Select Database Connection window, enter the connection name. Enter the
AN husername
as and password of the schema that you want to connect to.
a) From the Role drop-down list, you can select either default or SYSDBA. (You choose
SYSDBA for the sys user or any user with database administrator privileges.)
b) You can select the connection type as:
Basic: In this type, enter host name and SID for the database you want to connect
to. Port is already set to 1521. You can also choose to enter the Service name
directly if you use a remote database connection.
TNS: You can select any one of the database aliases imported from the
tnsnames.ora file.
LDAP: You can look up database services in Oracle Internet Directory, which is a
component of Oracle Identity Management.
Advanced: You can define a custom Java Database Connectivity (JDBC) URL to
connect to the database.
c) Click Test to ensure that the connection has been set correctly.
d) Click Connect.
Oracle Database 11g: SQL Fundamentals I C - 8
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Browsing DatabaseLObjects
EN ferab
A nsconnection, you can use the Connections Navigator to browse
After you createVa database
tinraa database schema including Tables, Views, Indexes, Packages,
MOobjects
throughLmany
n
E aTriggers,
no and Types.
S
Procedures,
N as
ASQL
hDeveloper uses the left side for navigation to find and select objects, and the right side to
display information about the selected objects. You can customize many aspects of the
appearance of SQL Developer by setting preferences.
You can see the definition of the objects broken into tabs of information that is pulled out of the
data dictionary. For example, if you select a table in the Navigator, the details about columns,
constraints, grants, statistics, triggers, and so on are displayed on an easy-to-read tabbed page.
If you want to see the definition of the EMPLOYEES table as shown in the slide, perform the
following steps:
1. Expand the Connections node in the Connections Navigator.
2. Expand Tables.
3. Click EMPLOYEES. By default, the Columns tab is selected. It shows the column
description of the table. Using the Data tab, you can view the table data and also enter new
rows, update data, and commit these changes to the database.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Displaying the Table
Structure
E
L sfe
Ayou
V
In SQL Developer,
acann also display the structure of a table using the DESCRIBE command.
r
O
t
The result
is a display of column names and data types as well as an indication
n
LMof thenocommand
E
if aScolumn must contain data.
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Browsing Files
Use the File Navigator to explore the file system and open
system files.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Browsing DatabaseLObjects
EN ferab
A ns to browse and open system files.
You can use theV
File Navigator
a
-trnavigator,
MOtheofiles
ToLview
click the Files tab, or select View > Files.
n
E
n
S To view
a the contents of a file, double-click a file name to display its contents in the SQL
AN hworksheet
as area.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Creating a SchemaLObject
EN ferab
A the
nscreation of any schema object by executing a SQL statement in
SQL DeveloperV
supports
a
r
O
t
SQL Worksheet.
you can create objects using the context menus. When created,
nLM noAlternatively,
E
you
athe objects using an edit dialog box or one of the many context-sensitive menus.
NScanaedit
s
AAs
h objects are created or existing objects are edited, the DDL for those adjustments is
new
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 how to create 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 to create and edit
database objects have multiple tabs, each reflecting a logical grouping of properties for that type
of object.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
N
Creating a New Table:
Example
E
L sfer
A
V dialog
In the Create Table
anbox, if you do not select the Advanced check box, you can create a
r
O
t
table quickly
columns and some frequently used features.
n
LM bynospecifying
E
S select
athe Advanced check box, the Create Table dialog box changes to one with multiple
Nyou
s
AIfoptions,
a
h in which you can specify an extended set of features while you create the table.
The example in the slide shows how to create the DEPENDENTS 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 Create TABLE.
3. In the Create Table dialog box, select Advanced.
4. Specify the column information.
5. Click OK.
Although it is not required, you should also specify a primary key by using the Primary Key tab
in the dialog box. Sometimes, you may want to edit the table that you have created; to do so,
right-click the table in the Connections Navigator and select Edit.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
Select SQL
o e
h
Worksheet from the
@ uid
z
v
Tools menu.
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using the SQL Worksheet
E
fe
AtoLa database,
s
V
n
When you connect
a SQL Worksheet window for that connection automatically
a
r
O
t
opens. You
nthe SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus
LMcannuse
o
E
statements.
a SQL Worksheet supports SQL*Plus statements to a certain extent. SQL*Plus
NS as The
Astatements
h that are not supported by the SQL Worksheet are ignored and not passed to the
Or, click the Open
SQL Worksheet
icon.
database.
You can specify 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 one of the following:
Select Tools > SQL Worksheet.
Click the Open SQL Worksheet icon.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ (continued)
a
Using the SQL Worksheet
E
r
fe
AL shortcut
s
V
n
You may want to
use the
keys or icons to perform certain tasks such as executing a SQL
a
r
O
t
statement,
na script, and viewing the history of SQL statements that you have executed.
LMrunning
o
E
n
Scan use
You
athe SQL Worksheet toolbar that contains icons to perform the following tasks:
AN1. hExecute
as Statement: Executes the statement where the cursor is located in the Enter SQL
2.
3.
4.
5.
6.
7.
8.
9.
Statement box. You can use bind variables in the SQL statements, but not substitution
variables.
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.
Commit: Writes any changes to the database and ends the transaction
Rollback: Discards any changes to the database, without writing them to the database, and
ends the transaction
Cancel: Stops the execution of any statements currently being executed
SQL History: Displays a dialog box with information about SQL statements that you have
executed
Execute Explain Plan: Generates the execution plan, which you can see by clicking the
Explain tab
Autotrace: Generates trace information for the statement
Clear: Erases the statement or statements in the Enter SQL Statement box
Oracle Database 11g: SQL Fundamentals I C - 16
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Enter SQL
statements.
ot e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ (continued)
a
Using the SQL Worksheet
E
r
fe
AtoLa database,
s
V
n
When you connect
a SQL Worksheet window for that connection automatically
a
r
O
t
opens. You
nthe SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus
LMcannuse
o
E
statements.
a SQL and PL/SQL commands are supported as they are passed directly from the
NS as All
ASQL
hWorksheet to the Oracle database. SQL*Plus commands used in the SQL Developer have
to be interpreted by the SQL Worksheet before being passed to the database.
The SQL Worksheet currently supports a number of SQL*Plus commands. Commands not
supported by the SQL Worksheet are ignored and are not sent to the Oracle database. Through
the SQL Worksheet, you can execute SQL statements and some of the SQL*Plus commands.
You can display a SQL Worksheet by using any of the following options:
Select Tools > SQL Worksheet.
Click the Open SQL Worksheet icon.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
F9
F5
F5
F9
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Executing SQL Statements
E
fe
AL shows
s
n
The example in V
the slide
the difference in output for the same query when the [F9] key or
a
r
O
t
M onis used versus the output when [F5] or Run Script is used.
ExecuteLStatement
E
S san
N
A ha
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Identify a location,
enter a file name,
and click Save.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Saving SQL ScriptsLEN
er
f
A
s
V SQLrastatements
n
You can save your
from the SQL Worksheet into a text file. To save the
O
t
Mthe Enter
contentsLof
n SQL Statement box, perform the following steps:
o
E
n
1.S Click the
icon or use the File > Save menu item.
s aSaveSave
AN2. hInathe
dialog box, enter a file name and the location where you want the file saved.
3. Click Save.
After you save the contents to a file, the Enter SQL Statement window displays a tabbed page of
your file contents. You can have multiple files open at the same time. Each file displays as a
tabbed page.
Script Pathing
You can select a default path to look for scripts and to save scripts. Under Tools > Preferences >
Database > Worksheet Parameters, enter a value in the Select default path to look for scripts
field.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1.
2.
2
Select a connection from
the drop-down list.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
NFiles:
Executing Saved Script
E
L sfer Method 1
A
n the code in the SQL Worksheet area, perform the following
To open a scriptV
file and
adisplay
r
O
t
steps: LM
n
o
E
n
1.S In the a
files navigator, select (or navigate to) the script file that you want to open.
AN2. hDouble-click
as
to open. The code of the script file is displayed in the SQL Worksheet area.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
NFiles:
Executing Saved Script
E
L sfer Method 2
A
V script,
To run a saved SQL
anperform the following steps:
r
O
t
1. Use
the @ command,
followed by the location, and name of the file you want to run, in the
n
LM
o
E
n
S Enters SQL
a Statement window.
AN2. hClick
a the Run Script icon.
The results from running the file are displayed on the Script Output tabbed page. You can also
save the script output by clicking the Save icon on the Script Output tabbed page. The File Save
dialog box appears and you can identify a name and location for your file.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
After
lm ude
e
s
formatting
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Formatting the SQLLCode
EN ferab
A the
V
nsindentation, spacing, capitalization, and line separation of the SQL
You may want to
format
a
r
O
t
- has a feature for formatting SQL code.
code. SQL
n
LMDeveloper
o
E
n
Sformatsthe
a
To
SQL code, right-click in the statement area, and select Format SQL.
N
A ha
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 reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Using Snippets
Snippets are code fragments that may be just syntax or
examples.
When you place your cursor here,
it shows the Snippets window.
From the drop-down list, you can
select the functions category that
you want.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using Snippets
E
fe
AL nscode
V
You may want to
use certain
fragments when you use the SQL Worksheet or create or edit
a
r
O
t
a PL/SQL
nor procedure. SQL Developer has the feature called Snippets. Snippets are
LMfunction
o
E
n
code
a such as SQL functions, Optimizer hints, and miscellaneous PL/SQL
NS fragments
s
Aprogramming
a
techniques. You can drag snippets into the Editor window.
h
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Inserting a
snippet
m
lco
Editing the
snippet
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using Snippets: Example
E
L sfe
Ainto
V
n code in a SQL Worksheet or in a PL/SQL function or procedure,
To insert a Snippet
ayour
r
O
t
M from
drag theLsnippet
n the Snippets window to the desired place in your code. Then you can edit
o
E
n
the
soathat the SQL function is valid in the current context. To see a brief description of a
NSsyntax
s
ASQL
a
hfunction 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 as in the following:
SELECT CONCAT(first_name, last_name)
FROM employees;
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Nand
Debugging Procedures
E
r
L sfeFunctions
A
V youracann debug PL/SQL procedures and functions. Using the Debug menu
In SQL Developer,
O
-t the following debugging tasks:
M can operform
options,Lyou
n
E an
S Find Execution
Point goes to the next execution point.
N
A hResume
as continues execution.
Step Over bypasses the next method and goes to the next statement after the method.
Step Into goes to the first statement in the next method.
Step Out leaves the current method and goes to the next statement.
Step to End of Method goes to the last statement of the current method.
Pause halts execution, but does not exit, thus allowing you to resume execution.
Terminate halts and exits the execution. You cannot resume execution from this point;
instead, to start running or debugging from the beginning of the function or procedure,
click the Run or Debug icon on the Source tab toolbar.
Garbage Collection removes invalid objects from the cache in favor of more frequently
accessed and more valid objects.
These options are also available as icons on the debugging toolbar.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Database Reporting
SQL Developer provides a number of predefined reports about
the database and its objects.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Database ReportingLEN
er
f
A
s
n reports about the database and its objects. These reports can be
SQL DeveloperV
provides
amany
r
O
t
M the ofollowing
groupedLinto
categories:
n
E
n
S Abouta
Your Database reports
AN hDatabase
as Administration reports
Table reports
PL/SQL reports
Security reports
XML reports
Jobs reports
Streams reports
All Objects reports
Data Dictionary reports
User-Defined reports
To display reports, click the Reports tab at the left of the window. Individual reports are
displayed in tabbed panes at the right of the window; and for each report, you can select (using a
drop-down list) the database connection for which to display the report. For reports about
objects, the objects shown are only those visible to the database user associated with the selected
database connection, and the rows are usually ordered by Owner. You can also create your own
user-defined reports.
Oracle Database 11g: SQL Fundamentals I C - 26
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
n s St
Organize reports (inafolders.
A e thi
R
AR to us
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
ZU able
N
Creating a User-Defined
LE Report
er
f
A
s
V arerareports
n created by SQL Developer users. To create a user-defined
User-defined reports
O
t
report, perform
nfollowing steps:
LM nthe
o
E
1.S Right-click
User Defined Reports node under Reports, and select Add Report.
s aCreatetheReport
AN2. hInathe
dialog box, specify the report name and the SQL query to retrieve
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Shortcuts to
frequently used tools
1
2
m
lco
ai
m
t
o e
Links to popular
h
@ uid
z
search engines and
v
a nt G
o
discussion forums
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ rTools
a
Search Engines and
External
E
AL ofnsthefeSQL developers, SQL Developer has added quick links to
V
To enhance productivity
a
trand
MO engines
popularLsearch
discussion forums such as AskTom, Google, and so on. Also, you
n
o
E
n
have
a icons to some of the frequently used tools such as Notepad, Microsoft Word, and
NS shortcut
s
ADreamweaver,
a
available to you.
h
You can add external tools to the existing list or even delete shortcuts to tools that you do not
use frequently. To do so, perform the following steps:
1. From the Tools menu, select External Tools.
2. In the External Tools dialog box, select New to add new tools. Select Delete to remove any
tool from the list.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Setting Preferences
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Setting PreferencesLEN
er
f
A
s
V many
You can customize
anaspects of the SQL Developer interface and environment by modifying
r
O
t
SQL Developer
according to your preferences and needs. To modify SQL Developer
n
LM npreferences
o
E
preferences,
NS as aselect Tools, then Preferences.
AThe
h
preferences
are grouped into the following categories:
Environment
Accelerators (Keyboard shortcuts)
Code Editors
Database
Debugger
Documentation
Extensions
File Types
Migration
PL/SQL Compilers
PL/SQL Debugger
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Resetting the SQL L
Developer
EN ferLayout
A SQL nDeveloper,
s
V
While working with
if the Connections Navigator disappears or if you cannot
a
r
O
t
M window
dock the
n in its original place, perform the following steps to fix the problem:
LLog
o
E
n
1.S Exit from
a SQL Developer.
AN2. hOpen
as a terminal window and use the locate command to find the location of
windowinglayout.xml.
3. Go to the directory that has windowinglayout.xml and delete it.
4. Restart SQL Developer.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Summary
E
fe
AL graphical
s
n
SQL DeveloperV
is a free
tool to simplify database development tasks. Using SQL
a
r
O
t
Developer,
nbrowse, create, and edit database objects. You can use SQL Worksheet to
LMyouncan
o
E
run
and scripts. SQL Developer enables you to create and save your own special
a
NSSQLasstatements
Aset
ofhreports for repeated use.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Using SQL*Plus
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, 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 the output using SQL*Plus commands
Interact with script files
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Objectives
E
L sfe
A
You might want to
createaSELECT
statements that can be used again and again. This appendix also
V
n
r
O
t
covers the
use of SQL*Plus
commands to execute SQL statements. You learn how to format output
n
LM
o
E
n
using
SQL*Plus
commands,
edit SQL commands, and save scripts in SQL*Plus.
S sa
N
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SQL statements
Server
SQL*Plus
Query results
Buffer
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
SQL
e
s
scripts
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
SQL and SQL*Plus EN
L sfer
A
V language
SQL is a command
an used for communication with the Oracle server from any tool or
r
O
t
application.
n contains many extensions. When you enter a SQL statement, it is stored in a
LMOracle
oSQL
n
partS
ofEmemory
called
the SQL buffer and remains there until you enter a new SQL statement.
a
N
s
ASQL*Plus
ha 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
Oracle Database 11g: SQL Fundamentals I D - 3
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SQL*Plus
An environment
Oracle-proprietary
Keywords can be
abbreviated.
Commands do not
allow manipulation of
values in the database.
ai
m
t
o e
h
@ uid
z
v
a nt G
SQL
SQL
SQL*Plus lmo SQL*Plus
e
dbuffer
e
u
s
statements
buffer
commands
t
an is S
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
SQL and SQL*Plus (continued)
E
L sfe
Acompares
V
The following table
an SQL and SQL*Plus:
r
O
t
LM non
E
SQL
SQL*Plus
S sa
N
A Is ha language
for communicating with the
Recognizes SQL statements and sends them
a
Oracle server to access data
to the server
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
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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.
Execute saved files.
)
m
o
Load commands from the file to buffer to edit.
ilc
ma
t
o
h ide
@
avz nt Gu
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
SQL*Plus
E
e
AL nsinfwhich
V
SQL*Plus is an environment
you can:
a
r
O
t
Execute
to retrieve, modify, add, and remove data from the database
M SQLostatements
EL aperform
n ncalculations on, store, and print query results in the form of reports
SFormat,
AN Create
hasscript 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 reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Logging In to SQL*Plus
sqlplus [username[/password[@database]]]
m
lco
ai
m
t
o e
h
@ uid
z
v
a 2 nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Logging In to SQL*Plus
E
fe on which type of operating system you are running Oracle
AL ndepends
s
How you invoke V
SQL*Plus
O n-tra
Database.
M
o
EinLfroma anLinux
S
To
log
environment, perform the following steps:
s
AN1. Right-click
a
your Linux desktop and select terminal.
h
2. Enter the sqlplus command shown in the slide.
3. Enter the username, password, and database name.
In the syntax:
username Your database username
password Your database password (Your password is visible if you enter it here.)
@database The database connect string
Note: To ensure the integrity of your password, do not enter it at the operating system prompt.
Instead, enter only your username. Enter your password at the password prompt.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Displaying the Table Structure
E
festructure of a table using the DESCRIBE command. The result of
ALdisplay
s
In SQL*Plus, youVcan
the
n
Ois a display
traof column names and data types as well as an indication if a column must
the command
M
n
L no
Edata.
contain
S
a
AInNthe hsyntax:
as
tablename The name of any existing table, view, or synonym that is accessible to
the user
To describe the DEPARTMENTS table, use this command:
SQL> DESCRIBE DEPARTMENTS
Name
Null?
Type
----------------------- -------- --------------DEPARTMENT_ID
DEPARTMENT_NAME
MANAGER_ID
LOCATION_ID
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
DESCRIBE departments
Name
----------------------DEPARTMENT_ID
DEPARTMENT_NAME
MANAGER_ID
LOCATION_ID
Null?
Type
-------- -----------NOT NULL NUMBER(4)
NOT NULL VARCHAR2(30)
NUMBER(6)
NUMBER(4)
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ r(continued)
a
Displaying the Table Structure
E
L sfe
Aslide
The example in the
displays
V
an the information about the structure of the DEPARTMENTS table. In
r
O
t
the result:
n
L:M
o
E
n
Null?
Specifies
S s a whether a column must contain data (NOT NULL indicates that a column must
N
A contain
a data.)
h
Type: Displays the data type for a column
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
A[PPEND] text
C[HANGE] / old / new
C[HANGE] / text /
CL[EAR] BUFF[ER]
DEL
DEL n
DEL m n
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
SQL*Plus Editing Commands
E
feone line at a time and are not stored in the SQL buffer.
ALare entered
s
V
SQL*Plus commands
n
O n-tra
M
Comm
D escription
EL and
notext
S
a
A[PPEND]
A dds text to the end of the current line
AN C[HANGE]
/ old / new
Changes old text to new in the current line
has
C[HANGE] / text /
CL[EAR] BUFF[ER]
DEL
DEL n
DEL m n
D eletes
D eletes
D eletes
D eletes
D eletes
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 appears.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
I[NPUT]
I[NPUT] text
L[IST]
L[IST] n
L[IST] m n
R[UN]
n
n text
0 text
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ (continued)
a
SQL*Plus Editing Commands
E
r
AL nsfe
V
O n-tra Description
Command
M
I[NPUT]
Inserts an indefinite number of lines
EL a no
S
N
text
Inserts a line consisting of text
A I[NPUT]
has
L[IST]
Lists all lines in the SQL buffer
L[IST] n
L[IST] m n
R[UN]
n
n text
0 text
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 (-).
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
LIST
1 SELECT last_name
2* FROM
employees
1
1* SELECT last_name
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
LIST
lm ude
e
s
1 SELECT last_name, job_id
an is St
(
2* FROM
employees
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using LIST, n, and APPEND
E
feto display the contents of the SQL buffer. The asterisk (*) beside
ALcommand
s
Use the L[IST]
V
n
ra
O n-tindicates
lineL2M
in the buffer
that line 2 is the current line. Any edits that you made apply to the
o
E
n
current
line.
S sa
AN Change
ha the number of the current line by entering the number (n) of the line that you want to
A , job_id
1* SELECT last_name, job_id
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
LIST
1* SELECT * from employees
c/employees/departments
1* SELECT * from departments
m
lco
ai
m
t
o e
h
@ uid
LIST
z
v
a nt G
o
1* SELECT * from departments
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using the CHANGE Command
E
L sfe
Adisplay
Use L[IST]Vto
nthe contents of the buffer.
acommand
r
O
t
UseLthe
C[HANGE]
to alter the contents of the current line in the SQL buffer. In this
M on
E
n the employees table with the departments table. The new current line is
Scase, sreplace
a
N
A displayed.
a
h
Use the L[IST] command to verify the new contents of the buffer.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SAVE filename
GET filename
START filename
@ filename
EDIT filename
SPOOL filename
EXIT
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
SQL*Plus File Commands
E
fe the Oracle server. SQL*Plus commands control the environment,
AL nswith
V
SQL statements communicate
O
tramanage files. You can use the commands described in the following table:
format query
results,
and
M
n
EL a no
S
AN Command
Description
has
SAV[E] filename [.ext]
[REP[LACE]APP[END]]
@ filename
ED[IT]
ED[IT] [filename[.ext]]
Invokes the editor to edit the contents of a saved file
SPO[OL] [filename[.ext]| Stores query results in a file. OFF closes the spool file. OUT
OFF|OUT]
closes the spool file and sends the file results to the printer.
EXIT
Quits SQL*Plus
Oracle Database 11g: SQL Fundamentals I D - 13
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
START my_query
LAST_NAME
MANAGER_ID DEPARTMENT_ID
ai
m
t
------------------------- ---------- ------------- ho
e
d
i
@
King
90
avz90nt Gu
o
Kochhar
100
lm ude
e
...
s
an is St
(
107 rows selected.
h
A
R se t
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abCommands
l
N
Using the SAVE, START,
and EDIT
E
r
AL nsfe
SAVE
V
Ocommand
trato store the current contents of the buffer in a file. In this way, you can store
M
n
Use the SAVE
o
EL used
nscripts
S
a
frequently
for use in the future.
N as
ASTART
h
Use the START command to run a script in SQL*Plus. You can also, alternatively, use the symbol @
to run a script.
@my_query
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SERVEROUTPUT Command
avz nt Gu
o
lm ude
SET SERVEROUT[PUT] {ON | OFF} [SIZE {n | UNL[IMITED]}]
e
s
St
[FOR[MAT] {WRA[PPED] | WOR[D_WRAPPED]
an| TRU[NCATED]}]
(
s
i
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
SERVEROUTPUT Command
E
L sfe
Aprograms
V
Most of the PL/SQL
an perform input and output through SQL statements, to store data in
r
O
t
databaseLtables
M or oquery
n those tables. All other PL/SQL input/output is done through APIs that
E
n
interact
with
other
programs.
For example, the DBMS_OUTPUT package has procedures, such as
S sa
N
APUT_LINE
a . To see the result outside of PL/SQL requires another program, such as SQL*Plus, to
h
read and display the data passed to DBMS_OUTPUT.
SQL*Plus does not display DBMS_OUTPUT data unless you first issue the SQL*Plus command SET
SERVEROUTPUT ON as follows:
SET SERVEROUTPUT ON
Note
SIZE sets the number of bytes of the output that can be buffered within the Oracle Database
server. The default is UNLIMITED. n cannot be less than 2000 or greater than 1,000,000.
For additional information about SERVEROUTPUT, see Oracle Database PL/SQL User's Guide
and Reference 11g.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Description
file_name[.ext]
CRE[ATE]
REP[LACE]
ai
m
t
o e
h
@ uid
z
v
a nt G
Stops spooling
OFF
o
lm ude
e
s
n the file
Stops spooling and sends
to tyour computers
OUT
S
a
(
s
i
standard (default)A
printer th
R
R use
A
o
EG seAll trights
Copyright Z
2009, Oracle.
reserved.
A cen
L
UE le li
Z
b
Using the SQL*Plus SPOOL
a
EN Command
r
L
e
f results in a file or optionally sends the file to a printer. The
A storesnsquery
The SPOOL command
V
a
tr enhanced. You can now append to, or replace an existing file, where
-been
SPOOL command
MO ohas
n
L
n only use SPOOL to create (and replace) a file. REPLACE is the default.
previously
could
SE syou
a
N
ATo spool
haoutput generated by commands in a script without displaying the output on the screen, use
APP[END]
SET TERMOUT OFF. SET TERMOUT OFF does not affect output from commands that run
interactively.
You must use quotes around file names containing white space. To create a valid HTML file using
SPOOL APPEND commands, you must use PROMPT or a similar command to create the HTML
page header and footer. The SPOOL APPEND command does not parse HTML tags. SET
SQLPLUSCOMPAT[IBILITY] to 9.2 or earlier to disable the CREATE, APPEND and SAVE
parameters.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
SET AUTOTRACE ON
@ uid
z
-- The AUTOTRACE report includes both the optimizer
v
a nt G
o
-- execution path and the SQL statement execution
lm ude
e
s
-- statistics
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using the AUTOTRACEE
Command
L sfe
Aquery
EXPLAIN showsV
the
path by performing an EXPLAIN PLAN. STATISTICS
anexecution
r
O
t
displaysL
SQL
statement
statistics.
The
formatting of your AUTOTRACE report may vary depending
M on
E
on the
ofnthe server to which you are connected and the configuration of the server. The
S version
a
N
s
ADBMS_XPLAN
ha package provides an easy way to display the output of the EXPLAIN PLAN
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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 the output
Interact with script files
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Summary
E
fe that you can use to send SQL commands to the database
AL environment
s
V
SQL*Plus is an execution
n
O
traSQL commands. You can execute commands from the SQL prompt or
server and
to
edit
and
save
M
n
L no
from
aEscript a
file.
S
N
s
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Using JDeveloper
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Objectives
After completing this appendix, you should be able to do the
following:
List the key features of Oracle JDeveloper
Create a database connection in JDeveloper
Manage database objects in JDeveloper
Use JDeveloper to execute SQL Commands
Create and run PL/SQL Program Units
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Objectives
E
AL nsfe
V
a
In this appendix,
to JDeveloper. You learn how to use JDeveloper for your
O younare
trintroduced
M
database
ELdevelopment
no tasks.
S
a
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Oracle JDeveloper
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Oracle JDeveloper LEN
er
f
A
s
V is anraintegrated
n
Oracle JDeveloper
development environment (IDE) for developing and
O
t
deploying
and Web services. It supports every stage of the software
n
LMJavanapplications
o
E
development
NS as alife cycle (SDLC) from modeling to deploying. It has the features to use the latest
Aindustry
h standards for Java, XML, and SQL while developing an application.
Oracle JDeveloper 11g initiates a new approach to J2EE development with features that enable
visual and declarative development. This innovative approach makes J2EE development simple
and efficient.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Database Navigator
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Database NavigatorLEN
er
f
A
s
V ranyou can store the information necessary to connect to a database in an
Using Oracle JDeveloper,
O
-t A connection is stored as part of the IDE settings, and can be
M connection.
object called
n
L
o
n
SE sandaimported
exported
for easy sharing among groups of users. A connection serves several
N
Apurposes
a
h from browsing the database and building applications, all the way through to
deployment.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Creating Connection
1
2
ai
m
t
o e
Click Test Connection.
h
3
@ uid
z
v
a nt G
o
lm ude
e
s
n s St
a
(
AClickeOKthi
4
R
AR to us
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
ZU able
N
Creating Connection
LE sfer
A
A connection isV
an object
anthat specifies the necessary information for connecting to a specific
r
O
t
database
as a specific
n user of that database. You can create and test connections for multiple
LM
o
E
n
databases
a for multiple schemas.
NS asand
ATo
h a database connection, perform the following steps:
create
1.
2.
3.
4.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Browsing DatabaseLObjects
EN ferab
A nsconnection, you can use the Database Navigator to browse through
After you createVa database
Oin a database
tra schema including Tables, Views, Indexes, Packages, Procedures,
M
many objects
n
EL anda Types.
no
S
Triggers,
N as
AYou
h see the definition of the objects broken into tabs of information that is pulled out of the
can
data dictionary. For example, if you select a table in the Navigator, the details about columns,
constraints, grants, statistics, triggers, and so on are displayed on an easy-to-read Database
Navigator.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
3
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Executing SQL Statements
E
L sfe
Astatement,
V
To execute a SQL
an perform the following steps:
r
O
t
n SQL Worksheet icon.
1. E
Click
LMthenOpen
o
S s athe connection.
AN2. hSelect
a
3. Execute the SQL command by clicking:
1. The Execute statement button or by pressing F9. The output is as follows:
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
3
an is St
(
th
RA Skeleton
e
R
s
of the function
A to u
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
ZU able
N
Creating Program Units
LE sfer
A
V program
To create a PL/SQL
an unit, perform the following steps:
r
O
t
1. Select
>nDatabase Navigator. Select and expand a database connection. Right-click a
LM View
o
E
n
S folder
acorresponding to the object type (Procedures, Packages, Functions). Choose New
AN h[Procedures|Packages|Functions].
as
1
2. Enter a valid name for the function, package, or procedure and click OK.
3. A skeleton definition is created and opened in the Code Editor. You can then edit the
subprogram to suit your need.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Compiling
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
A e th
Compilation without
Rerrors
R
A to us
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
ZU able
N
Compiling
LE sfer
A
After editing theVskeleton
andefinition, you need to compile the program unit. Right-click the
r
O
t
M that
PL/SQLLobject
nyou need to compile in the Connection Navigator, and then select Compile.
o
E
n
S
Alternatively,
you can also press [Ctrl] + [Shift] + [F9] to compile.
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Running a ProgramLUnit
EN ferab
A unit,
V
ns right-click the object and click Run. The Run PL/SQL dialog box
To execute the program
a
r
O
t
- to change the NULL values with reasonable values that are passed into
M mayonneed
appears.LYou
E
n After you change the values, click OK. The output is displayed in the
Sprogram
the
unit.
a
N
s
AMessage-Log
ha window.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G 2
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Dropping a Program
Unit
E
L sfe
Aunit,
V
n
To drop a program
the object and select Drop. The Drop Confirmation dialog
aright-click
r
O
t
box appears;
nApply. The object is dropped from the database.
LM click
o
E
n
S
AN has a
1
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Structure Window
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Structure Window LEN
er
f
A
s
V roffers
The Structure window
an a structural view of the data in the document currently selected in
O
t
the active
nof those windows that participate in providing structure: the navigators, the
LMwindow
o
E
n
editors
and the Property Inspector.
a
NS aands viewers,
AClick
hView > Structure window to view the Structure window.
In the Structure window, you can view the document data in a variety of ways. The structures
available for display are based upon document type. For a Java file, you can view code structure,
UI structure, or UI model data. For an XML file, you can view XML structure, design structure,
or UI model data.
The Structure window is dynamic, tracking always the current selection of the active window
(unless you freeze the windows contents on a particular view), as is pertinent to the currently
active editor. When the current selection is a node in the navigator, the default editor is assumed.
To change the view on the structure for the current selection, click a different structure tab.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Editor Window
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Editor Window
E
AL nofsafeprogram unit opens it in the Editor window. You can view your
Double-clickingVthe name
ra editor window, you can open multiple views of the same file, or
-tsingle
MO
project L
files
all inoone
n
E opena multiple
n
Scan
you
views of different files.
N
s
AThe tabs
a
h at the top of the editor window are the document tabs. Clicking a document tab gives
that file focus, bringing it to the foreground of the window in the current editor.
The tabs at the bottom of the editor window for a given file are the editor tabs. Selecting an
editor tab opens the file in that editor.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Application Navigator
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Application Navigator
E
e
AL nsfgives
V
The Applications
- Navigator
you a logical view of your application and the data it
a
r
O
t
contains.
The Applications
- Navigator provides an infrastructure that the different extensions
n
LM
o
E
n
can
NSplugasin toa and use to organize their data and menus in a consistent, abstract manner. While
Athe
Applications
- Navigator can contain individual files (such as Java source files), it is
h
designed to consolidate complex data. Complex data types such as entity objects, UML
diagrams, EJB, or Web services appear in this navigator as single nodes. The raw files that make
up these abstract nodes appear in the Structure window.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
2
1
an is St 3
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
NProcedures
Deploying Java Stored
E
L sfer
A
V profile
Create a deployment
an for Java stored procedures, and then deploy the classes and,
r
O
t
optionally,
n static methods in JDeveloper using the settings in the profile.
LManynpublic
o
E
NS as toathe database uses the information provided in the Deployment Profile Wizard and
ADeploying
h Database utilities:
two Oracle
loadjava loads the Java class containing the stored procedures to an Oracle database.
publish generates the PL/SQL callspecific wrappers for the loaded public static
methods. Publishing enables the Java methods to be called as PL/SQL functions or
procedures.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
2
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z
l
Publishing Java to L
PL/SQL
EN ferab
A code
ns and illustrates how to publish the Java code in a PL/SQL
The slide showsV
the Java
a
r
O
t
procedure.
LM nonE
S
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Topic
Website
http://www.oracle.com/technology/products/jdev/index.
html
http://www.oracle.com/technology/obe/obe11jdev/11/in
dex.html
http://www.oracle.com/technology/documentation/jdev.
html
http://forums.oracle.com/forums/forum.jspa?forumID=8
3
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Summary
In this appendix, you should have learned to do the following:
List the key features of Oracle JDeveloper
Create a database connection in JDeveloper
Manage database objects in JDeveloper
Use JDeveloper to execute SQL Commands
Create and run PL/SQL Program Units
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Objectives
After completing this appendix, you should be able to do the
following:
Write SELECT statements to access data from more than
one table using equijoins and nonequijoins
Join a table to itself by using a self-join
View data that generally does not meet a join condition by
using outer joins
)
Generate a Cartesian product of all rows from two or more com
il
a
tables
tm
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Objectives
E
L sfe
Ahow
V
This lesson explains
atonobtain data from more than one table. A join is used to view information
r
O
t
from multiple
nTherefore, you can join tables together to view information from more than one
LM tables.
o
E
n
table.
NS as a
ANote:
hInformation about joins is found in the section on SQL Queries and Subqueries: Joins in
Oracle Database SQL Language Reference 11g, Release 1 (11.1).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
EMPLOYEES
DEPARTMENTS
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ Tables
a
Obtaining Data from Multiple
E
r
fefrom more than one table. In the example in the slide, the report
AtoL usendata
s
V
Sometimes you need
ra tables:
tseparate
displaysL
data
from two
MO
n
E a IDs
noexist in the EMPLOYEES table.
SEmployee
AN Department
as IDs exist in both the EMPLOYEES and DEPARTMENTS tables.
h
Department names exist in the DEPARTMENTS table.
To produce the report, you need to link the EMPLOYEES and DEPARTMENTS tables, and access data
from both of them.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Cartesian Products
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Cartesian Products EN
L sfer
A
V isrinvalid
When a join condition
an or omitted completely, the result is a Cartesian product, in which all
O
t
combinations
M of rows
on are displayed. In other words, all rows in the first table are joined to all rows in
EL table.
n
the S
second
a
s
AANCartesian
a
h product tends to generate a large number of rows and the result is rarely useful.
Therefore, you should always include a valid join condition unless you have a specific need to
combine all rows from all tables.
However, Cartesian products are useful for some tests when you need to generate a large number of
rows to simulate a reasonable amount of data.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
DEPARTMENTS (8 rows)
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Generating a Cartesian
Product
E
fewhen a join condition is omitted. The example in the slide displays
AisLgenerated
s
V
A Cartesian product
n
O
tra and the department name from the EMPLOYEES and DEPARTMENTS
the last name
of
the
employee
M
n
L no
Erespectively.
tables,
Because no join condition has been specified, all rows (20 rows) from the
S
a
N
s
AEMPLOYEES
ha table are joined with all rows (8 rows) in the DEPARTMENTS table, thereby generating
Cartesian product:
20 x 8 = 160 rows
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Equijoin
Nonequijoin
Outer join
Self-join
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Types of Joins
E
L sfe
A
V
To join tables, you
can use
anOracles join syntax.
r
O
t
n release, the join syntax was proprietary. The SQL:1999compliant join
Note: Before
LM thenOracle9i
o
E
a offer any performance benefits over the Oracle-proprietary join syntax.
not
NS does
s
Asyntax
a
Oraclehdoes not have an equivalent syntax to support the FULL OUTER JOIN of the SQL:1999
compliant join syntax.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
table1.column, table2.column
table1, table2
table1.column1 = table2.column2;
Prefix the column name with the table name when the
same column name appears in more than one table.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ Syntax
a
Joining Tables Using E
Oracle
r
e
ALthan nonesftable
V
When data from more
in the database is required, a join condition is used. Rows in one
a
r
O
t
table canLbe
Mjoinedoton rows in another table according to common values that exist in the
E
n
corresponding
(that is, usually primary and foreign key columns).
S s acolumns
N
ATo display
ha data from two or more related tables, write a simple join condition in the WHERE clause.
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.
Oracle Database 11g: SQL Fundamentals I F - 7
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl
NZ raNames
Qualifying AmbiguousEColumn
AL nsfe you need to qualify the names of the columns with the table name
When joining twoVor moreatables,
-tr the table prefixes, the DEPARTMENT_ID column in the SELECT list
to avoidL
ambiguity.
MO onWithout
n the DEPARTMENTS table or the EMPLOYEES table. Therefore, it is necessary
could
be fromaeither
SE
N
s
Ato addhthea 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 a table prefix improves performance,
because you tell the Oracle server exactly where to find the columns.
Qualifying column names with table names can be very time consuming, particularly if table names
are lengthy. Therefore, 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, thereby using less memory.
The table name is specified in full, followed by a space and then the table alias. For example, the
EMPLOYEES table can be given an alias of e, and the DEPARTMENTS table 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, that table alias must be
substituted for the table name throughout the SELECT statement.
Table aliases should be meaningful.
A table alias is valid only for the current SELECT statement.
Oracle Database 11g: SQL Fundamentals I F - 8
m
lco
Equijoins
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
EMPLOYEES
DEPARTMENTS
m
lco
ai
m
t
o e
h
@ uid
z
Foreign key
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Equijoins
E
fe
AL ndepartment
s
To determine an V
employees
you compare the value in the DEPARTMENT_ID
a table with thename,
r
O
t
column in
the
EMPLOYEES
DEPARTMENT_ID
values in the DEPARTMENTS table.
M on
L
E
nbetween the EMPLOYEES and DEPARTMENTS tables is an equijoin; that is, values
TheS
relationship
a
N
s
Ain the hDEPARTMENT_ID
column in both tables must be equal. Often, this type of join involves
a
Primary key
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
n s St
a
(
A e thi
R
AR to us
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
U le
Z
b
N
a
Retrieving Records with
E Equijoins
r
L
e
A nsf
In the example inV
the slide:
-traspecifies the column names to retrieve:
TheLSELECT
MO oclause
n
n last name, employee number, and department number, which are columns in the
SE- Employee
a
N
s
A haEMPLOYEES 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:
e.department_id = d.department_id
Because the DEPARTMENT_ID column is common to both tables, it must be prefixed with the table
alias to avoid ambiguity. Other columns that are not present in both the tables need not be qualified
by a table alias, but it is recommended for better performance.
Note: When you use the Execute Statement icon to run the query, SQL Developer suffixes a _1 to
differentiate between the two DEPARTMENT_IDs.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z ablExample
NEquijoins:
Retrieving Records with
E
L sfer
A
In the example inV
the slide,
nthe LOCATIONS table is joined to the DEPARTMENTS table by the
awhich
r
O
t
LOCATION_ID
column,
is the only column of the same name in both the tables. Table aliases
LM non
E
are S
used to qualify
the columns and avoid ambiguity.
a
N
s
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
bl the AND Operator
NZ Using
Additional Search Conditions
a
E
r
fehave criteria for your WHERE clause to restrict the rows under
ALyou nmay
s
In addition to theV
join,
a
Ofor onenor
trmore
consideration
tables in the join. The example in the slide limits the rows of output to
M
L no
E
those
with
a
department
ID
equal
to 20 or 50:
S sa
N
AFor example,
ha to display employee Matos department number and department name, you need an
additional condition in the WHERE clause.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
EMPLOYEES
DEPARTMENTS
LOCATIONS
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm a minimum
To join n tables together, you e
need
de of n1
u
s
t
join conditions. For example,
Sthree tables, a
(an tohijoin
s
minimum of two joinsAis required.
R se t
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Joining More than Two
Tables
E
L sfe
Aneed
V
Sometimes you may
njoin more than two tables. For example, to display the last name, the
atocity
r
O
t
department
name,
and
the
for each employee, you have to join the EMPLOYEES,
M on
L
E
DEPARTMENTS,
S s a nand LOCATIONS tables.
N
A SELECT
ha e.last_name, d.department_name, l.city
FROM
WHERE
AND
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Nonequijoins
EMPLOYEES
JOB_GRADES
m
lco
S
a
(
s
i
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Nonequijoins
E
AL nsfecontaining something other than an equality operator.
A nonequijoin is V
a join condition
O between
trathe EMPLOYEES table and the JOB_GRADES table is an example of a
M
n
The relationship
o
EL aThenSALARY
S
nonequijoin.
column in the EMPLOYEES table ranges between the values in the
N as
ALOWEST_SAL
and HIGHEST_SAL columns of the JOB_GRADES table. Therefore, each employee
h
can be graded based on the salary. The relationship is obtained using an operator other than the
equality operator (=).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
NNonequijoins
Retrieving Records with
E
L sfer
A
V slidercreates
The example in the
an a nonequijoin to evaluate an employees salary grade. The salary
O
t
must be L
between
M any
npair of the low and high salary ranges.
o
E
n
ItN
isS
important
ato note that all employees appear exactly once when this query is executed. No
s
Aemployee
a
h 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 the BETWEEN condition. The Oracle
server translates the BETWEEN condition to a pair of AND conditions. Therefore, using BETWEEN has
no performance benefits, but should be used only for logical simplicity.
Table aliases have been specified in the example in the slide for performance reasons, not because of
possible ambiguity.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
EMPLOYEES
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
There are no
employees
n s St in
a
(
i
department
h
A 190.
t
R
R use
A
o
EG seAll trights
Copyright Z
2009, Oracle.
reserved.
A cen
L
UE le li
Z
bMatch with Outer Joins
a
Returning Records with
ENNo Direct
r
L
e
f
A nscondition,
If a row does notV
satisfy aajoin
the row does not appear in the query result. For example, in
r
O
t
the equijoin
condition
of
the
EMPLOYEES
and DEPARTMENTS tables, department ID 190 does not
M on
L
E
n are no employees with that department ID recorded in the EMPLOYEES table.
appear
there
S because
a
N
s
ASimilarly,
ha there is an employee whose DEPARTMENT_ID is set to NULL, so this row will also not
appear in the query result of an equijoin. To return the department record that does not have any
employees, or to return the employee record that does not belong to any department, you can use the
outer join.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
You use an outer join to see rows that do not meet the join
condition.
The outer join operator is the plus sign (+).
SELECT table1.column, table2.column
FROM
table1, table2
WHERE table1.column(+) = table2.column;
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Outer Joins: Syntax EN
L sfer
A
Missing rows canVbe returned
an if an outer join operator is used in the join condition. The operator is a
r
O
t
plus signLenclosed
M owith
n parentheses (+), and is placed on the side of the join that is deficient in the
E
n
information.
This
operator
has the effect of creating one or more null rows, to which one or more
S sa
N
Arows from
ha the nondeficient table can be joined.
In the syntax:
table1.column =
table2.column (+)
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Using Outer Joins
E
L sfe
Aslide
V
The example in the
displays
an employee last names, department IDs, and department names. The
r
O
t
Contracting
n does not have any employees. The empty value is shown in the output.
LMdepartment
o
E
n
a
Restrictions
NS Join
s
AOuter
a
h outer join operator can appear only on one side of the expressionthe side in which the
The
information is missing. It returns those rows, from one table, that have no direct match in the
other table.
A condition involving an outer join cannot use the IN operator or be linked to another condition
by the OR operator.
Note: Oracles join syntax does not have an equivalent for the FULL OUTER JOIN of the
SQL:1999 compliant join syntax.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Outer Join: Another Example
E
feslide retrieves all rows in the EMPLOYEES table, even if there is no
AL innthe
s
The query in the V
example
O n-tra table.
match inLthe
MDEPARTMENTS
E a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
EMPLOYEES (WORKER)
EMPLOYEES (MANAGER)
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lmequal
dtoe
e
u
s
MANAGER_ID in the WORKER table
is
t
S
(an hitable.
s
EMPLOYEE_ID in the A
MANAGER
R se t
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Joining a Table to Itself
E
fe to itself. To find the name of each employees manager, you need
AtoL joinnastable
V
Sometimes you need
a to itself, or perform a self-join. For example, to find the name of
O n-trtable
to join the
EMPLOYEES
M
ELmanager,
no you need to:
Lorentzs
S
a
s
AN Find
in the EMPLOYEES table by looking at the LAST_NAME column
aLorentz
h
Find the manager number for Lorentz by looking at the MANAGER_ID column. Lorentzs
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Self-Join: Example
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
n s St
a
(
A e thi
R
AR to us
G
E seAll rights reserved.
Copyright Z
2009, Oracle.
A
L licen
E
U le
Z
b
N
a
Self-Join: Example E
r
L
e
f EMPLOYEES table to itself. To simulate two tables in the FROM
Aslide joins
sthe
The example in the
V
n
a
O twonaliases,
-tr namely worker and manager, for the same table, EMPLOYEES.
clause, there
Mare
L
o
nthe WHERE clause contains the join that means where a workers manager number
SEexample,
In
this
a
N
s
Amatches
hathe employee number for the manager.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Summary
In this appendix, you should have learned how to use joins to
display data from multiple tables by using Oracle-proprietary
syntax.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
NZ rabl
Summary
E
e
ftables.
AL njoin
s
V
There are multiple
ways to
O n-tra
M
Types ofLJoins o
E an
SEquijoins
N
A Nonequijoins
has
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 reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Practice F: Overview
This practice covers the following topics:
Joining tables by using an equijoin
Performing outer and self-joins
Adding conditions
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
eAll rights reserved.
s
Copyright Z
2009, Oracle.
A
n
EL e lice
U
Z abl
Practice F: Overview EN
L sfer
A
V rtoangive you practical experience in extracting data from more than one table
This practice is intended
O
-t
using theLOracle
syntax.
M join
n
o
SE s a n
N
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Appendix AP
Additional Practices and Solutions
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Table of Contents
Additional Practices ............................................................................................................ 3
Practice 1-1 ..................................................................................................................... 4
Practice Solutions 1-1 ................................................................................................... 12
Case Study ........................................................................................................................ 17
Practice 2-1 ................................................................................................................... 19
Practice Solutions 2-1 ................................................................................................... 27
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Additional Practices
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Practice 1-1
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
These exercises can be used for extra practice after you have discussed the following
topics: basic SQL SELECT statement, basic SQL Developer commands, and SQL
functions.
1) The HR department needs to find data for all the clerks who were hired after the year
1997.
2) The HR department needs a report of employees who earn commission. Show the last
name, job, salary, and commission of those employees. Sort the data by salary in
descending order.
m
lco
ai
m
t
o e
h
@ uid
z
v
G
a raises.
3) For budgeting purposes, the HR department needs a report on projected
t
o
n
m butuwho
ehaveThe
l
report should display those employees who have no commission,
a 10%
d
e
s
t
n
S
raise in salary (round off the salaries).
(a his
A
R se t
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
fewho have a last name starting with the letters J, K, L,
ALemployees
s
5) Show V
those
n
a
or O
M. n-tr
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Create a report that displays all employees, and indicate with the words Yes or No
whether they receive a commission. Use the DECODE expression in your query.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
l practice after you have discussed the following
These exercises can N
beZ
used for
bextra
a
E
r
L SELECT
topics: basic A
SQL
festatement, basic SQL Developer commands, SQL functions,
s
V
n
joins, and
ra
O group
tfunctions.
M
n
E7)L Create
naoreport that displays the department name, location ID, last name, job title,
S
a
salary of those employees who work in a specific location. Prompt the user for
AN hasand
the location. For example, if the user enters 1800, these are the results:
8) Find the number of employees who have a last name that ends with the letter n.
Create two possible solutions.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
9) Create a report that shows the name, location, and number of employees for each
department. Make sure that the report also includes departments without employees.
10) The HR department needs to find the job titles in departments 10 and 20. Create a
report to display the job IDs for those departments.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
11) Create a report that displays the jobs that are found in the Administration
lm udeand
e
s
t jobs. Show
Executive departments. Also display the number of employees
n sforSthese
a
(
i
the job with the highest number of employees first.
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O
trabe used for extra practice after you have discussed the following
These
exercises
can
M
n
L basic
Etopics:
noSQL SELECT statements, basic SQL Developer commands, SQL functions,
S
a
AN hjoins,
as group functions, and subqueries.
12) Show all the employees who were hired in the first half of the month (before the 16th
of the month).
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
13) Create a report that displays the following for all employees: last name, salary, and
salary expressed in terms of thousands of dollars.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
L havelmanagers
e with a salary higher than $15,000. Show
14) Show all the employees E
who
icmanager
U name,
the following data: Z
employee
name, manager salary, and salary grade
e
l
b
N
a
of the manager.
LE sfer
A
V ran
O
M on-t
L
SE s a n
N
A ha
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
15) Show the department number, name, number of employees, and average salary of all
the departments, together with the names, salaries, and jobs of the employees working
in each department.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
thsalary of the department
16) Create a report to display the department number
RAandslowest
e
R
with the highest average salary.
GA to u
E
Z nse
A
EL e lice
U
l
bdepartments
NZdisplays
a
17) Create a report
that
where no sales representatives work.
E
r
enumber, department
f
ALdepartment
s
IncludeVthe
name, manager ID, and the location in
n
a
r
O
t
the output. n
LM
o
E
n
S
AN has a
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
19) Create a report that displays the employee number, last name, salary, department
@ uid
z
v
number, and the average salary in their department for all employees.
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
20) Show all the employees who were hired on the day of the week on which the highest
number of employees were hired.
21) Create an anniversary overview based on the hire date of the employees. Sort the
anniversaries in ascending order.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
These exercises can be used for extra practice after you have discussed the following
topics: basic SQL SELECT statement, basic SQL Developer commands, and SQL
functions.
1) The HR department needs to find data for all of the clerks who were hired after the
year 1997.
SELECT *
FROM
employees
WHERE job_id = 'ST_CLERK'
AND hire_date > '31-DEC-1997';
2) The HR department needs a report of employees who earn commission. Show the last
name, job, salary, and commission of those employees. Sort the data by salary in
descending order.
SELECT last_name, job_id, salary, commission_pct
FROM
employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC;
ai
m
t
o e
h
@ uid
z
v
G
a raises.
t
3) For budgeting purposes, the HR department needs a report on projected
The
o
n
m ubutdwho
e have
l
report should display those employees who do not get a commission
a
e
s
t
n
S
10% raise in salary (round off the salaries).
(a his
A
t a 10% raise is '
R after
SELECT 'The salary of '||last_name||'
e
R
s
A "New
u
|| ROUND(salary*1.10)
salary"
G
o
t
E
FROM
employees
Z nse
A
WHERE commission_pct
IS
EL e lNULL;
ice
U
Z aband
l their duration of employment. Show the last names
4) Create a report of
Nemployees
E
r
of all employees
fe with the number of years and the number of completed
ALthey together
s
V
n
months
that
have
a been employed. Order the report by the duration of their
r
O
t
employment.
n The employee who has been employed the longest should appear at the
LM
o
E
n
top
of
the
S s a list.
N
A ha SELECT last_name,
TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date) / 12) YEARS,
TRUNC(MOD(MONTHS_BETWEEN(SYSDATE, hire_date), 12))
MONTHS
FROM employees
ORDER BY years DESC, MONTHS desc;
5) Show those employees who have a last name starting with the letters J, K, L, or
M.
SELECT last_name
FROM
employees
WHERE SUBSTR(last_name, 1,1) IN ('J', 'K', 'L', 'M');
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
6) Create a report that displays all employees, and indicate with the words Yes or No
whether they receive a commission. Use the DECODE expression in your query.
SELECT last_name, salary,
decode(commission_pct, NULL, 'No', 'Yes') commission
FROM
employees;
These exercises can be used for extra practice after you have discussed the following
topics: basic SQL SELECT statement, basic SQL Developer commands, SQL functions,
joins, and group functions.
7) Create a report that displays the department name, location ID, name, job title, and
salary of those employees who work in a specific location. Prompt the user for the
location.
a) Enter 1800 for location_id when prompted.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
n endsswith
8) Find the number of employees who have a last namea
that
Stthe letter n.
(
i
Create two possible solutions.
RA se th
R
SELECT COUNT(*)
GA to u
E
FROM
employees
Z nse
A
WHERE last_name LIKE
'%n';
EL e lice
--or
U
SELECT COUNT(*)
NZ rabl
E
L sfe
FROM
Aemployees
WHEREV SUBSTR(last_name,
-1) = 'n';
an
r
O
t
M
n that shows the name, location, and number of employees for each
9)L Createnaoreport
E
S
a
Make sure that the report also includes departments without employees.
AN hasdepartment.
SELECT d.department_id, d.department_name,
SELECT d.department_name, d.location_id, e.last_name,
e.job_id, e.salary
FROM
employees e, departments d
WHERE
e.department_id = d.department_id
AND
d.location_id = &location_id;
d.location_id,
COUNT(e.employee_id)
FROM
employees e RIGHT OUTER JOIN departments d
ON
e.department_id = d.department_id
GROUP BY d.department_id, d.department_name, d.location_id;
10) The HR department needs to find the job titles in departments 10 and 20. Create a
report to display the job IDs for those departments.
SELECT DISTINCT job_id
FROM
employees
WHERE department_id IN (10, 20);
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
11) Create a report that displays the jobs that are found in the Administration and
Executive departments. Also display the number of employees for these jobs. Show
the job with the highest number of employees first.
SELECT e.job_id, count(e.job_id) FREQUENCY
FROM
employees e JOIN departments d
ON e.department_id = d.department_id
WHERE
d.department_name IN ('Administration', 'Executive')
GROUP BY e.job_id
ORDER BY FREQUENCY DESC;
These exercises can be used for extra practice after you have discussed the following
topics: basic SQL SELECT statements, basic SQL Developer commands, SQL functions,
joins, group functions, and subqueries.
12) Show all employees who were hired in the first half of the month (before the 16th of
the month).
ai
m
t
o e
h
SELECT last_name, hire_date
@ uid
FROM
employees
z
v
a nt G
WHERE TO_CHAR(hire_date, 'DD') < 16;
o
lm ude
e
s
t salary, and
13) Create a report that displays the following for all employees:
name,
n last
S
a
(
s
i
salary expressed in terms of thousands of dollars.
RA se th
R
SELECT last_name, salary, TRUNC(salary,
GA to u -3)/1000 Thousands
FROM
employees;
E
Z nse
A
L managers
14) Show all employees whoEhave
ice with a salary higher than $15,000. Show the
l
U
e
following data: employee
bl manager name, manager salary, and salary grade of
NZ raname,
E
the manager.
AL nsfe
V
SELECT
m.last_name manager, m.salary,
O e.last_name,
tra
M
n
j.grade_level
EL FROM
noemployees e JOIN employees m
S
a
AN hasON
e.manager_id = m.employee_id
JOIN
job_grades j
ON
AND
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
SELECT
d.department_id, d.department_name,
count(e1.employee_id) employees,
NVL(TO_CHAR(AVG(e1.salary), '99999.99'), 'No average'
) avg_sal,
e2.last_name, e2.salary, e2.job_id
FROM
departments d RIGHT OUTER JOIN employees e1
ON
d.department_id = e1.department_id
RIGHT OUTER JOIN employees e2
ON
d.department_id = e2.department_id
GROUP BY d.department_id, d.department_name, e2.last_name,
e2.salary,
e2.job_id
ORDER BY d.department_id, employees;
ai
m
t
o e
16) Create a report to display the department number and lowest salary of the department
h
@ uid
with the highest average salary.
z
v
a nt G
o
SELECT department_id, MIN(salary)
lm ude
e
FROM
employees
s
an is St
GROUP BY department_id
(
HAVING AVG(salary) = (SELECT MAX(AVG(salary))
RA se th
R
FROM
A to u
GBYemployees
GROUP
E
e
Z nsdepartment_id);
A
17) Create a report that displays
ELtheedepartments
ice where no sales representatives work.
l
U
Include the department
bl department name, and location in the output.
NZ number,
a
E
r
fe
SELECT A
*L
s
V
n
FROM
O departments
tra
M
WHERE
department_id
NOT IN(SELECT department_id
n
L no
E
FROM employees
S sa
N
WHERE job_id = 'SA_REP'
A ha
AND department_id IS NOT NULL);
18) Create the following statistical reports for the HR department: Include the department
number, department name, and the number of employees working in each department
that:
a) Employs fewer than three employees:
SELECT d.department_id, d.department_name, COUNT(*)
FROM
departments d JOIN employees e
ON
d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
HAVING COUNT(*) < 3;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
19) Create a report that displays the employee number, last name, salary, v
department
z
a nt G
number, and the average salary in their department for all employees.
o
lm ude
e
s
SELECT e.employee_id, e.last_name, e.department_id,
an is St e.salary,
(
AVG(s.salary)
A e th
FROM
employees e JOIN employeesR
s
R
A to us
ON
e.department_id = s.department_id
G
E se e.department_id,
GROUP BY e.employee_id, Z
e.last_name,
A
e.salary;
L licen
E
U werelehired on the day of the week on which the highest
Zwho
20) Show all employees
b
N
ahired.
E fwere
r
number of employees
L
e
A ns
V
SELECT
TO_CHAR(hire_date, 'DAY') day
ra
O last_name,
t
M
n
FROM oemployees
L
n TO_CHAR(hire_date, 'Day') =
SE sWHERE
a
N
A ha
(SELECT TO_CHAR(hire_date, 'Day')
FROM
employees
GROUP BY TO_CHAR(hire_date, 'Day')
HAVING COUNT(*) = (SELECT MAX(COUNT(*))
FROM
employees
GROUP BY TO_CHAR(hire_date,
'Day')));
21) Create an anniversary overview based on the hire date of the employees. Sort the
anniversaries in ascending order.
SELECT last_name, TO_CHAR(hire_date, 'Month DD') BIRTHDAY
FROM
employees
ORDER BY TO_CHAR(hire_date, 'DDD');
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Case Study
In this case study, you build a set of database tables for a video application. After you
create the tables, you insert, update, and delete records in a video store database and
generate a report. The database contains only the essential tables.
The following is a diagram of the entities and attributes for the video application:
for
RESERVATION
#* res date
set up for
the subject
of
TITLE
#* id
* title
* description
o rating
o category
o release date
available as
m
lco
ai
m
t
o e
h
@ uid
z
v
G
a copyoa
responsible
t
n
lm ude
for
e
s
St
an TITLE_COPY
(
s
i
A e th#* id
MEMBER
R
* status
#* id
AR to us
G
* last name
ZE nse
the subject of
A
o first name
L lice
E
o address ZU responsible
le
b
o city EN
a
made against
oVphone
AL nsfer for
date
O * join
tra
RENTAL
M
n
L no
E
#* book date
created
S sa
N
o act ret date
A ha
for
o exp ret date
Note: If you want to build the tables, you can execute the commands in the
buildtab.sql script in SQL Developer. If you want to drop the tables, you can execute
the commands in the dropvid.sql script in SQL Developer. Then you can execute the
commands in the buildvid.sql script in SQL Developer to create and populate the
tables.
All the three SQL scripts are present in the /home/oracle/labs/sql1/labs
folder.
If you use the buildtab.sql script to build the tables, start with step 4.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
If you use the dropvid.sql script to remove the video tables, start with step 1.
If you use the buildvid.sql script to build and populate the tables, start with
step 6(b).
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Practice 2-1
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1) Create the tables based on the following table instance charts. Choose the appropriate
data types and be sure to add integrity constraints.
a) Table name: MEMBER
Column_
Name
MEMBER_
ID
Key
Type
Null/
Unique
Default
Value
PK
Data
Type
Length
NN,U
LAST_
NAME
FIRST_NAME
ADDRESS
CITY
PHONE
JOIN
_
DATE
NN
NN
NUMBER
VARCHAR2
VARCHAR2
VARCHAR2
VARCHAR2
VARCHAR2
System
Date
DATE
10
25
25
100
30
15
ai
m
t
o e
h
@ uid
z
v
a nt G
o
b) Table name: TITLE
lm ude
e
s
t
TITLE_ID TITLE
DESCRIPTION (RATING
RELEASE_
an is SCATEGORY
Column_
DATE
h
A
Name
t
R
e
PK
Key
AR to us
G
Type
ZENN nse
NN,U
NN A
Null/
EL e lice
Unique
U
G, PG, R,
DRAMA,
Check
NZ rabl
E
NC17,
NR
COMEDY,
L sfe
A
ACTION,
V ran
O
CHILD,
t
M onL
SCIFI,
E an
S
DOCUMEN
AN has
TARY
VARCHAR2 VARCHAR2
VARCHAR2
VARCHAR2
DATE
Data Type NUMBER
Length
10
60
400
20
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
FK Ref
Table
FK Ref
Col
Data
Type
Length
COPY_ID
TITLE_ID
PK
PK,FK
NN,U
NN,U
STATUS
NN
AVAILABLE,
DESTROYED,
RENTED,
RESERVED
TITLE
m
lco
TITLE_ID
ai
m
t
o e
h
@ uid
10
10
15 vz
a nt G
o
lm ude
e
s
an is St
(
d) Table name: RENTAL
RA se th
R
BOOK_
MEMBER_ GA
COPY_ o uACT_RET_ EXP_RET_ TITLE_
Column
t DATE
E
DATE
ID Z
ID e
DATE
ID
Name
s
A
n
PK
PK,FK2
Key
ELPK,FK1
ice PK,FK2
l
U
Type
e
Z
bl
System ra
System Date
DefaultLEN
e
f
A
s
Date
+
2 days
Value
V ran
MEMBER
TITLE_
TITLE_
O
FK
Ref
t
M Table
n
COPY
COPY
L
o
E an
S
MEMBER_I COPY_
TITLE_ID
Ref
AN has FK
D
ID
Col
Data
Type
Length
NUMBER
DATE
NUMBER
NUMBER
NUMBER
10
10
VARCHAR2
DATE
DATE
NUMBER
10
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
RES_
DATE
MEMBER_
ID
TITLE_
ID
PK
PK,FK1
PK,FK2
NN,U
NN,U
NN
MEMBER
TITLE
MEMBER_ID
TITLE_ID
NUMBER
NUMBER
10
10
DATE
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
2) Verify that the tables were created properly by checking in the Connections Navigator
in SQL Developer.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
a) Member number for the MEMBER table: Start with 101; do not allow caching of the
values. Name the sequence MEMBER_ID_SEQ.
b) Title number for the TITLE table: Start with 92; do not allow caching of the
values. Name the sequence TITLE_ID_SEQ.
c) Verify the existence of the sequences in the Connections Navigator in SQL
Developer.
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
E4)L Adda data
noto the tables. Create a script for each set of data to be added.
S
AN hasa) Add movie titles to the TITLE table. Write a script to enter the movie
information. Save the statements in a script named lab_apcs_4a.sql. Use the
sequences to uniquely identify each title. Enter the release dates in the DD-MONYYYY format. Remember that single quotation marks in a character field must be
specially handled. Verify your additions.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Title
Willie and
Christmas Too
Alien Again
The Glob
My Day Off
Description
All of Willies friends
make a Christmas list for
Santa, but Willie has yet
to add his own wish list.
Yet another installation of
science fiction history.
Can the heroine save the
planet from the alien life
form?
A meteor crashes near a
small American town and
unleashes carnivorous goo
in this classic.
With a little luck and a lot
of ingenuity, a teenager
skips school for a day in
New York.
A six-year-old has doubts
about Santa Claus, but she
discovers that miracles
really do exist.
After discovering a cache
of drugs, a young couple
find themselves pitted
against a vicious gang.
Rating
G
Category
CHILD
Release_date
05-OCT-1995
SCIFI
19-MAY-1995
NR
SCIFI
12-AUG-1995
PG
COMEDY
12-JUL-1995
ai
m
t
o e
h
PG
DRAMA
12-SEP-1995
Miracles on Ice
@ uid
z
v
a nt G
o
lm ude
e
s
St 01-JUN-1995
anACTION
(
s
Soda Gang
NR
i
RA se th
R
GA to u
E
Z nse
A
b) Add data to the MEMBER
Save
ELtable.
ice the insert statements in a script named
l
U
e
lab_apcs_4b.sql
bl commands in the script. Be sure to use the
NZ .rExecute
a
E
sequence
the
femember numbers.
ALto add
s
V
n
First_
O n-tra
M
Name o
Last_Name
Address
City
Phone
Join_Date
EL Carmen
n
S
a
Velasquez
283 King
Seattle
206-89908-MARAN has
Street
6666
1990
LaDoris
Ngao
5 Modrany
Bratislava
Midori
Nagayama
Mark
Quick-to-See
Audry
Ropeburn
68 Via
Sao Paolo
Centrale
6921 King
Lagos
Way
86 Chu Street Hong Kong
Molly
Urguhart
3035 Laurier
Quebec
586-3558882
254-8525764
63-559-7777
08-MAR1990
17-JUN1991
07-APR-1990
41-559-87
18-JAN1991
18-JAN1991
418-5429988
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Copy_Id
Status
Title
Copy_Id
Willie and
Christmas Too
Alien Again
AVAILABLE
AVAILABLE
RENTED
The Glob
AVAILABLE
The Glob
My Day Off
AVAILABLE
My Day Off
AVAILABLE
1
2
ai
m
t
o e
h
Miracles on Ice
1
AVAILABLE
Miracles on Ice
1
@ uid
z
v
a 1nt G
Soda Gang
1
AVAILABLE
Soda Ganglmo
de
e
u
s
t
an is S
(
d) Add the following rentals to the RENTAL table:
RA se th
R
u on the sequence number.
Note: The title number may be different
GA depending
o
t
E
Z nse
Title_ Id
Copy_
Member_Id
A
Id
Book_date
Exp_Ret_Date
EL e lice
U
92
1
3 days ago
1 day ago
bl
NZ ra101
E
AL 2 nsfe 101
93
1 day ago
1 day from now
V
a
r
O
t
LM
95 non 3
102
2 days ago
Today
E
S sa
N
A ha 97
1
106
4 days ago
2 days ago
3
RENTED
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
a) Add a new title. The movie is Interstellar Wars, which is rated PG and
@ uid
z
v
classified as a science fiction movie. The release date is 07-JUL-77.
The
G
a save the
t
o
n
description is Futuristic interstellar action movie. Can the
rebels
humans
lmtwo ucopies.
de
e
s
from the evil empire? Be sure to add a title copy record
for
t
an is S
(
h
ACarmen
b) Enter two reservations. One reservation isR
for
who wants to
tVelasquez,
e
RMark Quick-to-See,
s
rent Interstellar Wars. The other isA
for
who
wants to rent
u
G
o
t
Soda Gang.
ZE nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
6) Make changes to the data in the tables.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Title
Willie and Christmas Too
Alien Again
The Glob
My Day Off
Miracles on Ice
Soda Gang
Interstellar Wars
Price
25
35
35
35
30
35
29
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
ucontains update statements that
b) Create a script named lab_apcs_7b.sql
GA tthat
o
E
eto the preceding list. Run the commands
Z according
update each video with a price
s
A
n
in the script.
EL e lice
U
bl numbers available for this exercise.
Note: Have
the
NZTITLE_ID
a
E
r
fe each customers history of renting videos. Be sure to
AL thatnscontains
8) Create V
a report
O thencustomer
tra name, movie rented, dates of the rental, and duration of rentals.
include
M
EL Total
ntheonumber of rentals for all customers for the reporting period. Save the
S
a
AN hascommands that generate the report in a script file named lab_apcs_8.sql.
Note: Your results may be different.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1) Create the tables based on the following table instance charts. Choose the appropriate
data types and be sure to add integrity constraints.
a) Table name: MEMBER
CREATE TABLE member
(member_id
NUMBER(10)
CONSTRAINT member_member_id_pk PRIMARY KEY,
last_name
VARCHAR2(25)
CONSTRAINT member_last_name_nn NOT NULL,
first_name
VARCHAR2(25),
address
VARCHAR2(100),
city
VARCHAR2(30),
phone
VARCHAR2(15),
join_date
DATE DEFAULT SYSDATE
CONSTRAINT member_join_date_nn NOT NULL);
ai
m
t
o e
h
CREATE TABLE title
@ uid
z
(title_id
NUMBER(10)
v
a nt G
CONSTRAINT title_title_id_pk PRIMARY KEY,
o
lm ude
title
VARCHAR2(60)
e
s
n s St
CONSTRAINT title_title_nn NOT(a
NULL,
i
description
VARCHAR2(400) A
h
t
R
CONSTRAINT title_description_nn
R useNOT NULL,
A
rating
VARCHAR2(4)
EG se toCHECK
Z
CONSTRAINT title_rating_ck
A c'PG',
n 'R', 'NC17', 'NR')),
L('G',
e
(rating IN
E
i
U VARCHAR2(20)
category
el
Z
l
b
N
CHECK
E feraINtitle_category_ck
LCONSTRAINT
(category
('DRAMA',
'COMEDY',
'ACTION',
A
s
V
n
a
'CHILD',
'SCIFI',
'DOCUMENTARY')),
-tr
MO orelease_date
n
DATE);
L
E
n
S
AN hasc) aTable name: TITLE_COPY
b) Table name: TITLE
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
ce by checking in the Connections Navigator
ELcreated
iproperly
l
U
2) Verify that the tables
were
e
NZ rabl
in SQL Developer.
E
L sfe
AConnections
V
a) In the
an Navigator, expand Connections > myconnection > Tables.
r
O
t
n to uniquely identify each row in the MEMBER table and the TITLE
3)LM
Create sequences
o
E
n
S
a
AN hastable.
a) Member number for the MEMBER table: Start with 101; do not allow caching of the
CREATE TABLE reservation
(res_date
DATE,
member_id
NUMBER(10)
CONSTRAINT reservation_member_id REFERENCES
member(member_id),
title_id
NUMBER(10)
CONSTRAINT reservation_title_id REFERENCES
title(title_id),
CONSTRAINT reservation_resdate_mem_tit_pk PRIMARY KEY
(res_date, member_id, title_id));
b) Title number for the TITLE table: Start with 92; do not allow caching of the
values. Name the sequence TITLE_ID_SEQ.
CREATE SEQUENCE title_id_seq
START WITH 92
NOCACHE;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
b) Add data to the MEMBER table. Place the insert statements in a script named
lab_apcs_4b.sql. Execute the commands in the script. Be sure to use the
sequence to add the member numbers.
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
Afirst_name,
u last_name,
INSERT INTO member(member_id,
G
o
t
E
address, city,
e join_date)
Z phone,
s
A
n
VALUES (member_id_seq.NEXTVAL,
'LaDoris',
'Ngao',
ce
EL 'Bratislava',
i
l
'5 Modrany',
'586-355-8882',
U
Z able
TO_DATE('08-MAR-1990',
N
LE sfer
'DD-MM-YYYY'))
A
V ran
/
O
M on-t
L
n INTO member(member_id, first_name, last_name,
SE sINSERT
a
N
address, city, phone, join_date)
A ha VALUES (member_id_seq.NEXTVAL,
'Midori', 'Nagayama',
SET VERIFY OFF
INSERT INTO member(member_id, first_name, last_name,
address, city, phone, join_date)
VALUES (member_id_seq.NEXTVAL, 'Carmen', 'Velasquez',
'283 King Street', 'Seattle', '206-899-6666',
TO_DATE('08-MAR-1990',
'DD-MM-YYYY'))
/
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
Note: Have the TITLE_ID numbers available for this exercise.
h
z@ Guid
INSERT INTO title_copy(copy_id, title_id, status)av
VALUES (1, 92, 'AVAILABLE')
mo udent
l
e
/
St
ansstatus)
(
INSERT INTO title_copy(copy_id, title_id,
s
i
VALUES (1, 93, 'AVAILABLE')
RA se th
R
/
u
GA title_id,
o
t
INSERT INTO title_copy(copy_id,
status)
E
Z nse
VALUES (2, 93, 'RENTED')
A
EL e lice
/
U
Z abl
INSERT INTO N
title_copy(copy_id,
title_id, status)
E
r
VALUES (1,
94,
'AVAILABLE')
AL nsfe
/
V
O INTO
tratitle_copy(copy_id, title_id, status)
INSERT
M
n
o(1, 95, 'AVAILABLE')
EL VALUES
n
S
a
AN has/INSERT INTO title_copy(copy_id, title_id,status)
c) Add the following movie copies in the TITLE_COPY table:
VALUES
/
INSERT
VALUES
/
INSERT
VALUES
/
INSERT
VALUES
/
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Note: The title number may be different depending on the sequence number.
INSERT INTO rental(title_id, copy_id, member_id,
book_date, exp_ret_date, act_ret_date)
VALUES (92, 1, 101, sysdate-3, sysdate-1, sysdate-2)
/
INSERT INTO rental(title_id, copy_id, member_id,
book_date, exp_ret_date, act_ret_date)
VALUES (93, 2, 101, sysdate-1, sysdate-1, NULL)
/
INSERT INTO rental(title_id, copy_id, member_id,
book_date, exp_ret_date, act_ret_date)
VALUES (95, 3, 102, sysdate-2, sysdate, NULL)
/
INSERT INTO rental(title_id, copy_id, member_id,
book_date, exp_ret_date,act_ret_date)
VALUES (97, 1, 106, sysdate-4, sysdate-2, sysdate-2)
/
COMMIT
/
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm
de of each
5) Create a view named TITLE_AVAIL to show the movie titles,
the availability
e
u
s
t
n from
S view. Order the
copy, and its expected return date if rented. Query all
the
arows
(
s
i
results by title.
RA se th
R
u
Note: Your results may be different. GA
o
t
E
ZAS nse
CREATE VIEW title_avail
A
ELc.copy_id,
ice c.status, r.exp_ret_date
SELECT
t.title,
l
U
e
Z t aJOIN
FROM
title
bl title_copy c
N
E
r
ON
= c.title_id
L t.title_id
fe rental
AOUTER
s
V
FULL
JOIN
r
n
a
r
O
t
ON
c.copy_id
=
r.copy_id
LMANDnon c.title_id = r.title_id;
E
S
a
AN hasSELECT
*
FROM
title_avail
ORDER BY title, copy_id;
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@wants touid
z
v
b) Enter two reservations. One reservation is for Carmen Velasquez,
who
a wantsntot G
o
rent Interstellar Wars. The other is for Mark Quick-to-See,
who
m ude rent
l
e
Soda Gang.
ans is St
(
INSERT INTO reservation (res_date,
member_id,
th title_id)
RA
e
R
VALUES (SYSDATE, 101, 98)
s
GA to u
/
E
e member_id, title_id)
Z(res_date,
INSERT INTO reservation
s
A
n
VALUES (SYSDATE, 104,
EL 97)
ice
l
U
/
e
NZ rabl
E
7) Make a modification
AL nsftoeone of the tables.
V
tra
a) O
Run the
lab_apcs_7a.sql
script located in the
M
n
L /home/oracle/labs/sql1/labs
o
E
folder, to add a PRICE column to the TITLE
n
S s atable
N
to record the purchase price of the video. Verify your modifications.
A ha
ALTER TABLE title
ADD (price NUMBER(8,2));
DESCRIBE title
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
8) Create a report that contains each customers history of renting videos. Be sure to
include the customer name, movie rented, dates of the rental, and duration of rentals.
Total the number of rentals for all customers for the reporting period. Save the
commands that generate the report in a script file named lab_apcs_8.sql.
ai
m
t
o e
h
@ uid
SELECT m.first_name||' '||m.last_name MEMBER, t.title,
z
v
G
aDURATION
t
r.book_date, r.act_ret_date - r.book_date
o
n
lm ude
FROM member m
e
s
n s St
JOIN rental r
a
(
ON
r.member_id = m.member_id
A e thi
R
JOIN title t
AR to us
ON
r.title_id = t.title_id
G
ORDER BY member;
ZE nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Note: Your results may be different.
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Index
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
A
Alias 1-3, 1-5, 1-9, 1-10, 1-16, 1-17, 1-18, 1-19, 1-20, 1-21,
1-25, 1-29, 2-5, 2-6, 2-8, 2-23, 2-24, 2-38, 4-25, 5-14, 5-27,
6-7, 6-14, 6-16, 6-24, 6-36, 8-28, 10-33, C-8, F-8, F-10, F-11,
F-15, F-21, F-22
ALL Operator 7-19, 8-3, 8-6, 8-7, 8-12, 8-16, 8-17, 8-18, 8-21,
8-24, 8-27, 8-29, 8-30
ALTER TABLE Statement 10-35, 10-36
Alternative Quote (q) Operator 1-23
American National Standards Institute 4-4, 6-5, I-30
Ampersand Substitution 2-2, 2-29, 2-30, 2-33, 2-38, 2-39, 3-13
AND Operator 2-16, 2-21, F-12
ANY Operator 7-3, 7-9, 7-18, 7-21
ai
m
lco
E se t
Z
B
LA licen
E
BETWEEN Operator 2-10
ZU able
N
BI Publisher I-14ALE
er
f
s
V ran
O
C
M on-t
L
n 6-2, 6-3, 6-6, 6-8, 6-19, 6-22, 6-25, 6-31,
Cartesian
SE s aProduct
N
A h6-32,
a 6-33, 6-34, 6-35, 6-36, F-2, F-4, F-5, F-22
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
C
Constraints 1-14, 9-4, 9-8, 9-20, 9-24, 10-2, 10-3, 10-6, 10-11,
10-13, 10-15, 10-16, 10-17, 10-18, 10-19, 10-20, 10-21, 10-22, 10-25, 10-27,
10-29, 10-30, 10-31, 10-32, 10-34, 10-37, 10-39, 10-40, C-10, E-6, I-16,
I-39
Conversion Functions 3-7, 3-9, 3-11, 3-12, 3-28, 4-1, 4-2,
4-4, 4-9, 4-44, 8-25, I-5
COUNT Function 5-9
CREATE TABLE Statement 9-12, 10-3, 10-6, 10-7, 10-11, 10-15, 10-31,
10-32, 10-34, 10-37, 10-40, 10-41
Creating a Database Connection C-7, C-8, C-9
Cross Joins 6-5, 6-34, 6-36
CURRENT_DATE 3-24, 9-9
ai
m
t
o e
h
@ uid
z
D
v
a nt G
o
Data Types 1-11, 1-26, 1-27, 3-4, 3-33, 4-4, 4-29, 4-31, 4-44,lm
de
e
u
s
t
S
5-6, 5-8, 6-9, 6-11, 8-13, 8-19, 8-22, 9-8, 9-12, 10-2,
an 10-3,
(
s
i
th10-37, 10-40, C-11,
10-6, 10-11, 10-12, 10-13, 10-14, 10-15, 10-31,
RA 10-34,
e
R
s
GA to u
D-7, E-14, I-9, I-16
E
e3-4, 3-5,
s
A Z2-2,
n
Database 1-2, 1-4, 1-14, 1-15, L
1-29,
2-7,
e
E 3-25,
ic3-26,
l
U
3-10, 3-12, 3-16, 3-21,
3-24,
4-4, 4-9, 4-28, 5-15,
e
Z abl
N
LE6-6,s6-36,
5-18, 5-28,A
6-2,
er 7-8, 9-3, 9-4, 9-9, 9-13, 9-15,
f
V ra9-26,
n 9-27, 9-31, 9-33, 9-34, 9-39, 9-40, 9-41,
9-19,O
9-21, 9-25,
t
n9-44, 9-45, 9-47, 10-2, 10-3, 10-4, 10-5, 10-6, 10-7,
LM 9-43,
o
E
9-42,
n
S sa
AN h10-8,
a 10-10, 10-11, 10-14, 10-15, 10-17, 10-31, 10-33, 10-34, 10-36, 10-37,
CURRVAL 10-9, 10-27
10-38, 10-40, 10-41, B-2, C-2, C-3, C-4, C-5, C-6, C-7, C-8,
C-9, C-10, C-12, C-13, C-15, C-16, C-17, C-19, C-26, C-29, C-31,
D-3, D-4, D-5, D-6, D-15, D-17, D-18, E-2, E-4, E-5, E-6,
E-8, E-11, E-15, E-18, F-2, F-7, F-10, F-22, I-2, I-3, I-4,
I-8, I-9, I-10, I-11, I-12, I-13, I-14, I-15, I-16, I-17, I-18,
I-19, I-20, I-22, I-27, I-28, I-29, I-30, I-31, I-32, I-33, I-34,
I-36, I-37, I-38, I-39, I-40
Database Transactions 9-3, 9-13, 9-19, 9-25, 9-26, 9-27, 9-39,
9-42
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
D
Date 1-9, 1-11, 1-20, 1-21, 2-7, 2-8, 2-11, 2-12, 2-23, 2-24,
2-25, 2-28, 2-31, 3-2, 3-3, 3-4, 3-5, 3-7, 3-8, 3-15, 3-17,
3-18, 3-20, 3-21, 3-22, 3-23, 3-24, 3-25, 3-26, 3-27, 3-28, 3-29,
3-30, 3-32, 3-33, 4-2, 4-3, 4-5, 4-6, 4-7, 4-8, 4-9, 4-10,
4-11, 4-12, 4-13, 4-14, 4-15, 4-16, 4-20, 4-21, 4-22, 4-23, 4-25,
4-27, 4-29, 4-36, 4-43, 4-44, 4-45, 5-6, 5-8, 5-28, 8-8, 8-10,
9-2, 9-3, 9-4, 9-7, 9-9, 9-10, 9-13, 9-14, 9-15, 9-16, 9-17,
9-18, 9-19, 9-25, 9-29, 9-30, 9-35, 9-39, 9-40, 9-41, 9-42, 9-43,
9-44, 9-45, 9-47, 9-48, 10-9, 10-10, 10-12, 10-14, 10-16, 10-20, 10-22,
10-25, 10-26, 10-27, 10-28, 10-29, 10-33, 10-36, 10-38, 10-39, B-3, C-10,
D-17, E-14, I-4, I-9, I-16, I-17, I-18, I-22, I-31, I-34
Datetime Data Types 10-14
ai
m
lco
DBMS 9-43, D-15, D-17, I-2, I-17, I-18, I-25, I-27, I-39
otm e
h
@ uid
DECODE Function 4-37, 4-39, 4-40, 4-41, 4-42, 4-44, 5-6
z
v
a nt G
o
DEFAULT Option 10-3, 10-6, 10-9, 10-11, 10-15, 10-31, 10-34,
10-37
lm ude
e
s
DELETE Statement 9-3, 9-13, 9-19, 9-21, 9-22, 9-23, a
9-24,
9-25,
n s St
(
A e thi
9-39, 9-40, 9-42
R
AR1-25,
us 1-27, 9-8,
DESCRIBE Command 1-3, 1-10, 1-16, 1-19,
1-26,
G
o
t
ZE nse
10-10, 10-33, C-11, D-7
A
L lice
DISTINCT Keyword 1-3,Z1-10,
UE 1-16,
e 1-19, 1-24, 1-25, 5-3, 5-10,
l
b
N
ra
5-12, 5-25 LE
e
f
A
V rans
DUAL Table
3-17
O
-t 5-9, 8-6, 8-13, 8-16, 8-17, 8-30, I-27
MRowson1-24,
L
Duplicate
E
n
S
AEN has a
F
FOR UPDATE clause 9-3, 9-13, 9-19, 9-25, 9-39, 9-42, 9-43, 9-44,
9-45, 9-47
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
F
Format Model 3-28, 3-30, 4-11, 4-12, 4-14, 4-15, 4-16, 4-19, 4-20,
4-43
Functions 2-5, 2-7, 3-1, 3-2, 3-3, 3-4, 3-5, 3-6, 3-7,
3-8, 3-9, 3-10, 3-11, 3-12, 3-13, 3-14, 3-15, 3-16, 3-17, 3-18,
3-20, 3-24, 3-27, 3-28, 3-29, 3-30, 3-31, 3-32, 3-33, 4-1, 4-2,
4-3, 4-4, 4-7, 4-9, 4-10, 4-20, 4-21, 4-23, 4-24, 4-25, 4-26,
4-27, 4-28, 4-36, 4-44, 4-45, 5-1, 5-2, 5-3, 5-4, 5-5, 5-6,
5-7, 5-8, 5-11, 5-12, 5-13, 5-14, 5-15, 5-19, 5-20, 5-22, 5-25,
5-26, 5-27, 5-28, 5-29, 7-3, 7-9, 7-12, 7-16, 7-21, 7-25, 8-25,
9-9, 10-9, 10-27, C-5, C-6, C-23, C-24, C-25, E-8, E-15, I-4,
I-5
G
ai
m
t
GROUP BY Clause 5-2, 5-3, 5-12, 5-13, 5-14, 5-15, 5-16, 5-18,
o e
h
@ uid
z
5-19, 5-22, 5-23, 5-25, 5-26, 5-27, 5-28, 7-14
v
a nt G
o
Group Functions 3-5, 5-1, 5-2, 5-3, 5-4, 5-5, 5-6, 5-11,
lm ude
e
s
St
5-12, 5-13, 5-14, 5-15, 5-19, 5-20, 5-22, 5-25, 5-26,
an 5-27,is5-28,
(
th
5-29, 7-3, 7-9, 7-12, 7-16, 7-21, 7-25, I-5 RA
e
R
s
A to u7-21
Group Functions in a Subquery 7-3, 7-9,
EG7-12, 7-16,
Z nse
A
EL e lice
U
HAVING Clause 5-2, 5-3,
5-20,
bl 5-21, 5-22, 5-23, 5-24,
NZ5-12,
a
E
r
L 7-3,sf7-5,
e 7-9, 7-13, 7-16, 7-21, 7-25
5-25, 5-28,A
5-29,
V
n
O n-tra
I
M
o
EL Data
nType
S
Implicit
Conversion 4-4, 4-5, 4-6
a
s
ANIN Operator
a
h
2-11, 7-22, F-17, F-18
H
Index 10-4, 10-8, 10-10, 10-22, 10-23, 10-36, 10-38, C-4, C-10, E-6,
I-38
INSERT Statement 9-3, 9-6, 9-8, 9-12, 9-13, 9-19, 9-25, 9-39,
9-42, 10-10, 10-18
International Standards Organization I-31
INTERSECT Operator 8-3, 8-4, 8-5, 8-7, 8-12, 8-18, 8-19, 8-20,
8-21, 8-24, 8-27, 8-30, 8-31
INTERVAL YEAR TO MONTH 10-14
J
Java C-4, C-8, E-3, E-12, E-14, E-15, E-16, I-9, I-12, I-39
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
M
om
c
l
ai
MAX 5-3, 5-4, 5-5, 5-7, 5-8, 5-12, 5-21, 5-23, 5-25, 5-26,
m
t
o e
h
5-28, 7-18, 7-19, B-2, B-3, I-10, I-34
@ uid
z
v
MIN 1-8, 1-24, 1-30, 2-7, 2-20, 2-27, 3-13, 3-19, 3-21, 3-23,
a nt G
o
lm ude
e
3-33, 4-31, 4-35, 4-42, 5-3, 5-5, 5-7, 5-8, 5-12, 5-14,s5-25,
n s St
a7-17,
(
5-28, 6-7, 6-12, 6-37, 7-4, 7-6, 7-12, 7-13, 7-14,
i
h7-18,
A
R8-13, 8-14,
se8-16,
7-19, 7-24, 8-3, 8-4, 8-5, 8-6, 8-7, 8-12,
A
u
to 8-31, 9-9, 9-16,
EG
8-17, 8-18, 8-21, 8-22, 8-23, 8-24,
8-27, e
8-30,
Z
s
A c10-6,
n
9-20, 9-31, 9-38, 9-43, 10-3,
EL 10-5,
il e 10-7, 10-11, 10-14, 10-15,
U
l
b10-37,
10-17, 10-27, 10-31,
B-2, B-3, C-7, C-8, C-23, C-25,
NZ10-34,
a
E
r
L
e
AD-3, D-6,
sfD-9, D-17, F-7, F-8, F-9, F-13, F-23,
C-26, C-30,
V
n
a
O I-8,nI-10,
-tr I-12, I-13, I-15, I-16, I-20, I-27, I-28,
I-3,
MI-4,
L
o
n
SEI-29,
I-33, I-34, I-36, I-39, I-40
a
N
s
A MINUS
ha Operator 8-3, 8-5, 8-7, 8-12, 8-18, 8-21, 8-22, 8-23,
8-24, 8-27, 8-30, 8-31
MOD Function 3-19
N
Naming 10-3, 10-5, 10-6, 10-11, 10-15, 10-17, 10-31, 10-34, 10-37
NEXTVAL 10-9, 10-27
Nonequijoins 6-2, 6-3, 6-8, 6-19, 6-22, 6-23, 6-24, 6-25, 6-35,
6-36, F-2, F-14, F-15, F-22
NOT NULL Constraint 1-26, 10-18, 10-20, 10-21, 10-32
NOT Operator 2-3, 2-18, 2-19, 2-22, 2-26, 2-34, 2-38, 7-19
NULL Conditions 2-3, 2-14
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
N
Null Value 1-3, 1-10, 1-14, 1-15, 1-16, 1-19, 1-20, 1-25, 2-8,
2-14, 2-23, 2-24, 4-28, 4-29, 4-30, 4-31, 4-32, 4-40, 5-3, 5-6,
5-9, 5-10, 5-11, 5-12, 5-25, 7-3, 7-9, 7-15, 7-16, 7-21, 7-22,
7-23, 8-13, 8-19, 9-8, 10-9, 10-20, 10-23, E-10, I-28
Null Values 1-3, 1-10, 1-14, 1-15, 1-16, 1-19, 1-20, 1-25, 2-23,
2-24, 4-28, 4-30, 5-3, 5-6, 5-9, 5-10, 5-11, 5-12, 5-25, 7-3,
7-9, 7-15, 7-16, 7-21, 7-22, 7-23, 8-13, 8-19, 9-8, 10-9, 10-20,
E-10
NULLIF Function 4-32
Number Functions 3-3, 3-7, 3-8, 3-15, 3-16, 3-20, 3-27, 3-28,
4-3, 4-10, 4-23, 4-27, 4-36
NVL Function 4-29, 4-30, 4-33, 4-44, 5-11
ai
m
t
o e
h
@ uid
z
O
v
a nt G
o
Object Relational I-2, I-16, I-17
lm ude
e
s
OLTP I-11, I-16
an is St
(
th
ON clause 6-3, 6-5, 6-6, 6-8, 6-15, 6-16, 6-17, 6-18,
RA 6-19,
e
R
s
A to u
6-21, 6-22, 6-25, 6-27, 6-31, 8-17EG
Z nse
ON DELETE CASCADE 10-26LA
e
UE le lic
ON DELETE SET NULLZ
10-26
b
a
EN2-17,
r
L
OR Operator 2-3,
2-15,
2-19,
2-22, 2-26, 2-34, F-18
e
A nsf
V
Oracle Database
O n11g
tra3-24, 7-8, 10-10, 10-14, 10-36, 10-38, C-4,
M
L I-3, nI-4,
o I-8, I-9, I-10, I-11, I-14, I-15, I-29, I-32,
EI-2,
S
a
AN hI-33,
as I-36, I-37, I-38, I-39
NVL2 Function 4-31
4-20, 4-38, 5-22, 5-28, 6-7, 6-24, 7-13, 7-17, 7-25, 8-4, 8-6,
9-4, 9-8, 9-26, 9-32, 9-33, 9-38, 9-41, 9-43, 9-47, 10-5, 10-16,
10-17, 10-22, 10-23, D-3, D-13, F-8, F-15, I-2, I-16, I-39
Oracle SQL Developer C-2, C-3, C-4, I-2, I-32, I-40
ORDBMS I-2, I-39
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ai
m
t
o e
h
@ uid
z
v
Q
a nt G
o
lm ude
q operator 1-23
e
s
n s St
Query 1-4, 1-8, 1-17, 1-18, 1-21, 1-24, 2-2, 2-5, 2-23,(a
2-25,
i
h
A
t
R
e 6-6,
2-27, 2-31, 2-33, 3-2, 3-6, 3-26, 3-33, 4-21,
R 4-35,
s5-15,
A
u
G 6-30,to7-3, 7-4, 7-5,
6-7, 6-14, 6-16, 6-24, 6-26, 6-28,E
6-29,
e
Z
s
A
n
7-6, 7-7, 7-8, 7-9, 7-10, E
7-11,
7-12,
7-13,
L lice 7-14, 7-15, 7-16,
U
e 7-23, 7-24, 7-25, 7-26, 8-2,
Z 7-21,
l7-22,
7-17, 7-18, 7-19,N
7-20,
b
a
LE
er 8-20, 8-22, 8-25, 8-26, 8-28, 8-29,
f
8-5, 8-6, 8-13,
8-17,s8-19,
A
V ran
O
8-30,
9-12,
-t 9-17, 9-23, 9-33, 9-43, 9-44, 9-47, 10-3, 10-6,
M on9-15,
L
n 10-13, 10-15, 10-27, 10-31, 10-32, 10-33, 10-34, 10-37, 10-40,
10-11,
SE10-10,
a
N
s
A h10-41,
a C-18, C-27, D-3, D-5, D-13, D-14, D-15, D-16, D-17, F-7,
Projection 1-4
Pseudocolumns 10-27
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
R
Relational Database 9-43, I-2, I-3, I-4, I-8, I-15, I-16,
I-18, I-19, I-27, I-28, I-29, I-30, I-31, I-33, I-36, I-39
REPLACE 2-36, 3-9, 3-13, D-12, D-16
ROUND and TRUNC Functions 3-30, 3-32
ROUND Function 3-17, 3-18, 4-26
RPAD 3-9, 3-13
RR Date Format 3-22, 3-23, 4-22
Rules of Precedence 1-12, 1-13, 2-3, 2-19, 2-20, 2-21, 2-22,
2-26, 2-34
S
Schema 10-2, 10-5, 10-7, 10-8, 10-18, 10-28, 10-40, B-2, C-3, C-5,
C-7, C-8, C-10, C-13, E-5, E-6, I-2, I-3, I-4, I-6, I-8,
ai
m
t
I-15, I-29, I-33, I-34, I-36, I-40
o e
h
@ uid
z
SELECT Statement 1-1, 1-2, 1-3, 1-4, 1-5, 1-7, 1-10, 1-16,
v
a nt G
o
1-19, 1-21, 1-22, 1-25, 1-28, 1-29, 2-6, 2-9, 2-10, 2-12, 2-21,
lm ude
e
s
n 5-9,
St
2-23, 2-28, 2-32, 2-35, 2-38, 2-39, 3-2, 3-12, 4-2,
a4-44,
(
s
i
A 7-5,
th
5-15, 5-16, 5-17, 5-18, 5-19, 5-20, 6-2, 6-7,
7-8,
R7-2,
e
R
s
A 8-18,
u8-19, 8-20,
G8-12,
7-10, 7-11, 7-25, 7-26, 8-3, 8-6, 8-7,
o
t
E
e 9-19, 9-22, 9-25,
Z 9-3,ns9-13,
A
8-21, 8-22, 8-24, 8-25, 8-26,
8-27,
e
EL 9-43,
ic9-44,
l
U
9-33, 9-39, 9-40, 9-41,
9-42,
9-47, 10-32, D-2, F-2,
e
Z abl
N
E
F-7, F-8, I-4,
ALI-5 fer
ns
Selection 1-4,
E-12
O 2-4,
tra
M
n
L 6-35no
Ejoin
Self
S
a
s
ANSequences
2-24, 10-8
a
h
Set operators 8-1, 8-2, 8-3, 8-4, 8-5, 8-6, 8-7, 8-12,
8-18, 8-21, 8-24, 8-27, 8-31, I-5
SET VERIFY ON 2-36
Sorting 2-1, 2-3, 2-19, 2-22, 2-24, 2-25, 2-26, 2-34, 2-38,
2-39, I-5
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
S
SQL Developer 1-6, 1-8, 1-9, 1-14, 1-17, 1-26, 2-28, 2-29,
2-30, 2-31, 2-33, 2-35, 2-36, 6-16, 9-4, 9-21, 9-27, 9-31, 9-32,
9-43, 10-9, 10-41, C-1, C-2, C-3, C-4, C-5, C-6, C-7, C-9,
C-10, C-11, C-13, C-17, C-22, C-23, C-25, C-26, C-27, C-28, C-29,
C-30, C-31, F-10, I-2, I-7, I-9, I-32, I-37, I-40
subquery 7-3, 7-4, 7-5, 7-6, 7-7, 7-8, 7-9, 7-10, 7-11,
7-12, 7-13, 7-14, 7-15, 7-16, 7-17, 7-18, 7-19, 7-20, 7-21, 7-22,
7-23, 7-24, 7-25, 9-12, 9-15, 9-17, 9-23, 10-3, 10-6, 10-11, 10-13,
10-15, 10-31, 10-32, 10-33, 10-34, 10-37, 10-40
Substitution Variables 2-3, 2-19, 2-22, 2-26, 2-27, 2-28, 2-31,
2-32, 2-34, 2-36, 2-38, 2-39, 9-11, C-16
SUBSTR 3-9, 3-10, 3-13, 3-14, 3-32, 4-25
ai
m
t
o e
h
@ uid
z
v
a nt G
o
T
lm ude
e
s
TO_CHAR 4-2, 4-3, 4-7, 4-8, 4-9, 4-10, 4-11, 4-16, 4-17,
an is St
(
th 4-45,
4-18, 4-19, 4-22, 4-23, 4-25, 4-26, 4-27, 4-34,
4-44,
RA4-36,
e
R
s
GA to u
8-25
E
e 4-22,
Z 4-20,
s
A
n
TO_DATE 4-2, 4-3, 4-7, 4-8, 4-9,
4-10,
4-21,
EL 9-10
ice
l
U
4-23, 4-27, 4-36, 4-44,
4-45,
e
NZ rabl
E
TO_NUMBER 4-2,
fe4-8, 4-9, 4-10, 4-20, 4-21, 4-23,
AL4-3,n4-7,
s
V
4-27,O
4-36, 4-43,
tra4-44
M
n
EL a 9-2,
Transaction
no 9-3, 9-4, 9-13, 9-19, 9-25, 9-26, 9-27, 9-29,
S
AN h9-30,
as 9-31, 9-32, 9-33, 9-34, 9-38, 9-39, 9-41, 9-42, 9-48, 10-38,
Synonym 1-24, 1-26, 7-18, 10-4, 10-8, 10-38, D-7, I-23, I-24
SYSDATE Function 3-23, 3-24, 9-9
U
UNION ALL 8-3, 8-4, 8-5, 8-6, 8-7, 8-12, 8-16, 8-17, 8-18,
8-21, 8-24, 8-27, 8-29, 8-30
UNION Operator 8-13, 8-14, 8-15, 8-25, 8-26, 8-30, 8-31
UNIQUE Constraint 10-21, 10-22
Unique Identifier I-23, I-24
m
lco
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
U
UPDATE Statement 9-3, 9-13, 9-15, 9-16, 9-17, 9-18, 9-19, 9-25,
9-39, 9-42, 9-43, 10-36
USING Clause 6-3, 6-5, 6-8, 6-11, 6-13, 6-14, 6-17, 6-19, 6-22,
6-25, 6-31
Using Snippets C-23, C-24
V
VARIANCE 5-5, 5-8, 5-28
VERIFY Command 2-3, 2-19, 2-22, 2-26, 2-34, 2-36
Views 10-8, 10-10, 10-38, C-10, E-6, E-13
W
)
WHERE Clause 2-3, 2-4, 2-5, 2-6, 2-7, 2-8, 2-9, 2-11, 2-15,
m
o
ilc
2-19, 2-22, 2-26, 2-27, 2-28, 2-31, 2-32, 2-34, 2-37, 2-38, 2-39,
a
otm e
3-12, 5-9, 5-14, 5-15, 5-18, 5-20, 5-21, 5-22, 5-27, 5-28, 6-10,
h
@ uid
6-14, 6-15, 6-18, 6-36, 7-2, 7-5, 7-13, 7-14, 7-15, 7-23, 8-5, avz
G
t
o
n
9-15, 9-16, 9-22, F-4, F-7, F-10, F-12, F-17, F-21, F-22
elm de
s Stu
n
a
X
A ( e this
R
R I-14,uI-39
s
XML C-7, C-9, C-26, C-27, C-30, E-3, E-12,
AI-9,
G
o
t
ZE nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
m
lco
ai
m
t
o e
h
@ uid
z
v
a nt G
o
lm ude
e
s
an is St
(
RA se th
R
GA to u
E
Z nse
A
EL e lice
U
NZ rabl
E
AL nsfe
V
O n-tra
M
EL a no
S
AN has