0% found this document useful (0 votes)
6 views31 pages

Les02

This document provides an overview of managing schema objects in Oracle, including adding, modifying, and dropping columns using the ALTER TABLE statement, managing constraints, and creating indexes. It also covers function-based indexes, the SET UNUSED option for columns, and performing FLASHBACK operations to recover tables. Key operations such as enabling/disabling constraints and the syntax for various commands are detailed throughout the lesson.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views31 pages

Les02

This document provides an overview of managing schema objects in Oracle, including adding, modifying, and dropping columns using the ALTER TABLE statement, managing constraints, and creating indexes. It also covers function-based indexes, the SET UNUSED option for columns, and performing FLASHBACK operations to recover tables. Key operations such as enabling/disabling constraints and the syntax for various commands are detailed throughout the lesson.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

2

Managing Schema Objects

Copyright © 2009, Oracle. All rights reserved.


Objectives

After completing this lesson, you should be able to do the


following:
• Add constraints
• Create indexes
• Create indexes using the CREATE TABLE statement
• Create function-based indexes
• Drop columns and set columns as UNUSED
• Perform FLASHBACK operations

2-2 Copyright © 2009, Oracle. All rights reserved.


Lesson Agenda

• Using the ALTER TABLE statement to add, modify, and


drop a column
• Managing constraints
– Adding and dropping a constraint
– Deferring constraints
– Enabling and disabling a constraint
• Creating indexes
– Using the CREATE TABLE statement
– Creating function-based indexes
– Removing an index
• Performing flashback operations

2-3 Copyright © 2009, Oracle. All rights reserved.


ALTER TABLE Statement

Use the ALTER TABLE statement to:


• Add a new column
• Modify an existing column
• Define a default value for the new column
• Drop a column

2-4 Copyright © 2009, Oracle. All rights reserved.


ALTER TABLE Statement

Use the ALTER TABLE statement to add, modify, or drop


columns:
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]...);

ALTER TABLE table


MODIFY (column datatype [DEFAULT expr]
[, column datatype]...);

ALTER TABLE table


DROP (column);

2-5 Copyright © 2009, Oracle. All rights reserved.


Adding a Column

• You use the ADD clause to add columns:


ALTER TABLE dept80
ADD (job_id VARCHAR2(9));

• The new column becomes the last column:

2-6 Copyright © 2009, Oracle. All rights reserved.


Modifying a Column

• You can change a column’s data type, size, and default


value.
ALTER TABLE dept80
MODIFY (last_name VARCHAR2(30));

• A change to the default value affects only subsequent


insertions to the table.

2-7 Copyright © 2009, Oracle. All rights reserved.


Dropping a Column

Use the DROP COLUMN clause to drop columns you no longer


need from the table:

ALTER TABLE dept80


DROP COLUMN job_id;

2-8 Copyright © 2009, Oracle. All rights reserved.


SET UNUSED Option

• You use the SET UNUSED option to mark one or more


columns as unused.
• You use the DROP UNUSED COLUMNS option to remove the
columns that are marked as unused.

ALTER TABLE <table_name>


SET UNUSED(<column_name>);
OR
ALTER TABLE <table_name>
SET UNUSED COLUMN <column_name>;

ALTER TABLE <table_name>


DROP UNUSED COLUMNS;

2-9 Copyright © 2009, Oracle. All rights reserved.


Lesson Agenda

• Using the ALTER TABLE statement to add, modify, and


drop a column
• Managing constraints
– Adding and dropping a constraint
– Deferring constraints
– Enabling and disabling a constraint
• Creating indexes
– Using the CREATE TABLE statement
– Creating function-based indexes
– Removing an index
• Performing flashback operations

2 - 11 Copyright © 2009, Oracle. All rights reserved.


Adding a Constraint Syntax

Use the ALTER TABLE statement to:


• Add or drop a constraint, but not modify its structure
• Enable or disable constraints
• Add a NOT NULL constraint by using the MODIFY clause

ALTER TABLE <table_name>


ADD [CONSTRAINT <constraint_name>]
type (<column_name>);

2 - 12 Copyright © 2009, Oracle. All rights reserved.


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);

2 - 13 Copyright © 2009, Oracle. All rights reserved.


ON DELETE CASCADE

Delete child rows when a parent key is deleted:

ALTER TABLE Emp2 ADD CONSTRAINT emp_dt_fk


FOREIGN KEY (Department_id)
REFERENCES departments(department_id) ON DELETE CASCADE;

2 - 14 Copyright © 2009, Oracle. All rights reserved.


Deferring Constraints

Constraints can have the following attributes:


• DEFERRABLE or NOT DEFERRABLE
• INITIALLY DEFERRED or INITIALLY IMMEDIATE
Deferring constraint on
ALTER TABLE dept2 creation
ADD CONSTRAINT dept2_id_pk
PRIMARY KEY (department_id)
DEFERRABLE INITIALLY DEFERRED

Changing a specific
SET CONSTRAINTS dept2_id_pk IMMEDIATE constraint attribute

ALTER SESSION Changing all constraints for a


session
SET CONSTRAINTS= IMMEDIATE

2 - 15 Copyright © 2009, Oracle. All rights reserved.


Difference Between INITIALLY DEFERRED and
INITIALLY IMMEDIATE
INITIALLY DEFERRED Waits to check the constraint until
the transaction ends
INITIALLY IMMEDIATE Checks the constraint at the end of
the statement execution

CREATE TABLE emp_new_sal (salary NUMBER


CONSTRAINT sal_ck
CHECK (salary > 100)
DEFERRABLE INITIALLY IMMEDIATE,
bonus NUMBER
CONSTRAINT bonus_ck
CHECK (bonus > 0 )
DEFERRABLE INITIALLY DEFERRED );

2 - 16 Copyright © 2009, Oracle. All rights reserved.


Dropping a Constraint

• Remove the manager constraint from the EMP2 table:


ALTER TABLE emp2
DROP CONSTRAINT emp_mgr_fk;

• Remove the PRIMARY KEY constraint on the DEPT2 table


and drop the associated FOREIGN KEY constraint on the
EMP2.DEPARTMENT_ID column:

ALTER TABLE dept2


DROP PRIMARY KEY CASCADE;

2 - 18 Copyright © 2009, Oracle. All rights reserved.


Disabling Constraints

• Execute the DISABLE clause of the ALTER TABLE


statement to deactivate an integrity constraint.
• Apply the CASCADE option to disable dependent integrity
constraints.

ALTER TABLE emp2


DISABLE CONSTRAINT emp_dt_fk;

2 - 19 Copyright © 2009, Oracle. All rights reserved.


Enabling Constraints

• Activate an integrity constraint currently disabled in the


table definition by using the ENABLE clause.
ALTER TABLE emp2
ENABLE CONSTRAINT emp_dt_fk;

• A UNIQUE index is automatically created if you enable a


UNIQUE key or a PRIMARY KEY constraint.

2 - 20 Copyright © 2009, Oracle. All rights reserved.


Cascading Constraints

Example:

ALTER TABLE emp2


DROP COLUMN employee_id CASCADE CONSTRAINTS;

ALTER TABLE test1


DROP (col1_pk, col2_fk, col1) CASCADE CONSTRAINTS;

2 - 22 Copyright © 2009, Oracle. All rights reserved.


Renaming Table Columns and Constraints

Use the RENAME COLUMN clause of the ALTER TABLE


statement to rename table columns.
a
ALTER TABLE marketing RENAME COLUMN team_id
TO id;

Use the RENAME CONSTRAINT clause of the ALTER TABLE


statement to rename any existing constraint for a table. b
ALTER TABLE marketing RENAME CONSTRAINT mktg_pk
TO new_mktg_pk;

2 - 23 Copyright © 2009, Oracle. All rights reserved.


Lesson Agenda

• Using the ALTER TABLE statement to add, modify, and


drop a column
• Managing constraints
– Adding and dropping a constraint
– Deferring constraints
– Enabling and disabling a constraint
• Creating indexes
– Using the CREATE TABLE statement
– Creating function-based indexes
– Removing an index
• Performing flashback operations
• Creating and using external tables

2 - 24 Copyright © 2009, Oracle. All rights reserved.


Overview of Indexes

Indexes are created:


• Automatically
– PRIMARY KEY creation
– UNIQUE KEY creation
• Manually
– The CREATE INDEX statement
– The CREATE TABLE statement

2 - 25 Copyright © 2009, Oracle. All rights reserved.


CREATE INDEX with the CREATE TABLE Statement

CREATE TABLE NEW_EMP


(employee_id NUMBER(6)
PRIMARY KEY USING INDEX
(CREATE INDEX emp_id_idx
ON
NEW_EMP(employee_id)),
first_name VARCHAR2(20),
last_name VARCHAR2(25));

SELECT INDEX_NAME, TABLE_NAME


FROM USER_INDEXES
WHERE TABLE_NAME = 'NEW_EMP';

2 - 26 Copyright © 2009, Oracle. All rights reserved.


Function-Based Indexes

• A function-based index is based on expressions.


• The index expression is built from table columns,
constants, SQL functions, and user-defined functions.

CREATE INDEX upper_dept_name_idx


ON dept2(UPPER(department_name));

SELECT *
FROM dept2
WHERE UPPER(department_name) = 'SALES';

2 - 28 Copyright © 2009, Oracle. All rights reserved.


Removing an Index

• Remove an index from the data dictionary by using the


DROP INDEX command:
DROP INDEX index;

• Remove the UPPER_DEPT_NAME_IDX index from the data


dictionary:

DROP INDEX upper_dept_name_idx;

• To drop an index, you must be the owner of the index or


have the DROP ANY INDEX privilege.

2 - 29 Copyright © 2009, Oracle. All rights reserved.


DROP TABLE … PURGE

DROP TABLE dept80 PURGE;

2 - 30 Copyright © 2009, Oracle. All rights reserved.


Lesson Agenda

• Using the ALTER TABLE statement to add, modify, and


drop a column
• Managing constraints
– Adding and dropping a constraint
– Deferring constraints
– Enabling and disabling a constraint
• Creating indexes
– Using the CREATE TABLE statement
– Creating function-based indexes
– Removing an index
• Performing flashback operations

2 - 31 Copyright © 2009, Oracle. All rights reserved.


FLASHBACK TABLE Statement

• Enables you to recover tables to a specified point in time


with a single statement
• Restores table data along with associated indexes, and
constraints
• Enables you to revert the table and its contents to a certain
point in time or SCN

SCN

2 - 32 Copyright © 2009, Oracle. All rights reserved.


Using the FLASHBACK TABLE Statement

DROP TABLE emp2;

SELECT original_name, operation, droptime FROM


recyclebin;

FLASHBACK TABLE emp2 TO BEFORE DROP;

2 - 33 Copyright © 2009, Oracle. All rights reserved.


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

2 - 34 Copyright © 2009, Oracle. All rights reserved.


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

2 - 35 Copyright © 2009, Oracle. All rights reserved.

You might also like