8-1
8-2
Objectives
After completing this lesson, you should be able to do the
following:
Add constraints
Create indexes
Create indexes by using the CREATE TABLE statement
Create function-based indexes
Drop columns and set columns as UNUSED
Perform FLASHBACK operations
Create and use external tables
8-3
Lesson Agenda
Creating indexes:
Using the CREATE TABLE statement
Creating function-based indexes
Removing an index
8-5
8-6
Adding a Column
8-7
Modifying a Column
You can change a columns data type, size, and default value.
8-8
Dropping a Column
Use the DROP COLUMN clause to drop columns that you no
longer need from the table:
ALTER TABLE
DROP COLUMN
dept80
job_id;
8-9
ALTER
SET
OR
ALTER
SET
TABLE <table_name>
UNUSED(<column_name> [ , <column_name>]);
TABLE <table_name>
UNUSED COLUMN <column_name> [ , <column_name>];
8-10
Lesson Agenda
Creating indexes:
Using the CREATE TABLE statement
Creating function-based indexes
Removing an index
8-12
Adding a Constraint
Add a FOREIGN KEY constraint to the EMP2 table indicating that
a manager must already exist as a valid employee in the
EMP2 table.
ALTER TABLE emp2
MODIFY employee_id PRIMARY KEY;
ALTER TABLE emp2
ADD CONSTRAINT emp_mgr_fk
FOREIGN KEY(manager_id)
REFERENCES emp2(employee_id);
8-13
ON DELETE Clause
8-14
Deferring Constraints
Constraints can have the following attributes:
DEFERRABLE or NOT DEFERRABLE
INITIALLY DEFERRED or INITIALLY IMMEDIATE
ALTER TABLE dept2
ADD CONSTRAINT dept2_id_pk
PRIMARY KEY (department_id)
DEFERRABLE INITIALLY DEFERRED
SET CONSTRAINTS dept2_id_pk IMMEDIATE
ALTER SESSION
SET CONSTRAINTS= IMMEDIATE
Changing a specific
constraint attribute
8-15
INITIALLY IMMEDIATE
8-16
Dropping a Constraint
8-17
Disabling Constraints
8-18
Enabling Constraints
ALTER TABLE
emp2
ENABLE CONSTRAINT emp_dt_fk;
8-19
Cascading Constraints
8-20
Cascading Constraints
Example:
ALTER TABLE emp2
DROP COLUMN employee_id CASCADE CONSTRAINTS;
8-21
8-22
Lesson Agenda
Creating indexes:
Using the CREATE TABLE statement
Creating function-based indexes
Removing an index
Overview of Indexes
Indexes are created:
Automatically
PRIMARY KEY creation
UNIQUE KEY creation
Manually
The CREATE INDEX statement
The CREATE TABLE statement
8-24
8-25
Function-Based Indexes
SELECT *
FROM
dept2
WHERE UPPER(department_name) = 'SALES';
8-26
Removing an Index
8-27
8-28
Lesson Agenda
Creating indexes:
Using the CREATE TABLE statement
Creating function-based indexes
Removing an index
SCN
8-30
Syntax:
FLASHBACK TABLE[schema.]table[,
[ schema.]table ]...
TO { TIMESTAMP | SCN } expr
[ { ENABLE | DISABLE } TRIGGERS ];
8-31
8-32
Lesson Agenda
Creating indexes:
Using the CREATE TABLE statement
Creating function-based indexes
Removing an index
Temporary Tables
When
session/transaction
completes
8-34
8-35
Lesson Agenda
Creating indexes:
Using the CREATE TABLE statement
Creating function-based indexes
Removing an index
External Tables
8-37
8-38
8-39
8-40
SELECT *
FROM oldemp
OLDEMP
emp.dat
8-41
8-42
Quiz
A FOREIGN KEY constraint enforces the following action: When
the data in the parent key is deleted, all the rows in the child
table that depend on the deleted parent key values are also
deleted.
1. True
2. False
8-43
Quiz
In all the cases, when you execute a DROP TABLE command,
the database renames the table and places it in a recycle
bin, from where it can later be recovered by using the
FLASHBACK TABLE statement.
1. True
2. False
8-44
Summary
In this lesson, you should have learned how to:
Add constraints
Create indexes
Create indexes by using the CREATE TABLE statement
Create function-based indexes
Drop columns and set columns as UNUSED
Perform FLASHBACK operations
Create and use external tables
8-45
Practice 2: Overview
This practice covers the following topics:
Altering tables
Adding columns
Dropping columns
Creating indexes
Creating external tables
8-46
8-47
Objectives
After completing this lesson, you should be able to do the
following:
Write a multiple-column subquery
Use scalar subqueries in SQL
Solve problems with correlated subqueries
Update and delete rows by using correlated subqueries
Use the EXISTS and NOT EXISTS operators
Use the WITH clause
8-48
Lesson Agenda
8-49
Multiple-Column Subqueries
Main query
WHERE (MANAGER_ID, DEPARTMENT_ID)
IN
Subquery
100
102
124
90
60
50
8-50
Column Comparisons
Multiple-column comparisons involving subqueries can be:
Nonpairwise comparisons
Pairwise comparisons
8-51
8-52
8-53
Lesson Agenda
8-54
8-55
8-56
Lesson Agenda
8-57
Correlated Subqueries
Correlated subqueries are used for row-by-row processing.
Each subquery is executed once for every row of the outer
query.
GET
candidate row from outer query
EXECUTE
inner query using candidate row value
USE
values from inner query to qualify or
disqualify candidate row
8-58
Correlated Subqueries
The subquery references a column from a table in the parent
query.
SELECT column1, column2, ...
Outer_table
FROM
table1
WHERE column1 operator
(SELECT column1, column2
FROM
table2
WHERE
expr1 =
Outer_table
.expr2);
8-59
8-61
Lesson Agenda
8-62
8-63
8-64
8-65
Correlated UPDATE
Use a correlated subquery to update rows in one table based
on rows from another table.
UPDATE table1 alias1
SET
column = (SELECT expression
FROM
table2 alias2
WHERE alias1.column =
alias2.column);
8-66
8-67
Correlated DELETE
Use a correlated subquery to delete rows in one table based on
rows from another table.
DELETE FROM table1 alias1
WHERE column operator
(SELECT expression
FROM
table2 alias2
WHERE alias1.column = alias2.column);
8-68
8-69
Lesson Agenda
8-70
WITH Clause
Using the WITH clause, you can use the same query block
in a SELECT statement when it occurs more than once
within a complex query.
The WITH clause retrieves the results of a query block and
stores it in the users temporary tablespace.
The WITH clause may improve performance.
8-71
8-72
WITH
dept_costs AS (
SELECT d.department_name, SUM(e.salary) AS dept_total
FROM
employees e JOIN departments d
ON
e.department_id = d.department_id
GROUP BY d.department_name),
avg_cost
AS (
SELECT SUM(dept_total)/COUNT(*) AS dept_avg
FROM
dept_costs)
SELECT *
FROM
dept_costs
WHERE dept_total >
(SELECT dept_avg
FROM avg_cost)
ORDER BY department_name;
8-73
8-74
FLIGHTS Table
3
8-75
Quiz
With a correlated subquery, the inner SELECT statement drives
the outer SELECT statement.
1. True
2. False
8-76
Summary
In this lesson, you should have learned that:
A multiple-column subquery returns more than one column
Multiple-column comparisons can be pairwise or
nonpairwise
A multiple-column subquery can also be used in the FROM
clause of a SELECT statement
8-77
Summary
8-78
Practice 6: Overview
This practice covers the following topics:
Creating multiple-column subqueries
Writing correlated subqueries
Using the EXISTS operator
Using scalar subqueries
Using the WITH clause
8-79