Manage Schema Objects
Manage Schema Objects
Objectives
After completing this lesson, you should be able to do the following: Add constraints Create indexes Create indexes using the CREATE TABLE statement Creating function-based indexes Drop columns and set column UNUSED Perform FLASHBACK operations Create and use external tables
2-2
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-3
2-4
Adding a Column
You use the ADD clause to add columns.
2-5
Modifying a Column
You can change a columns data type, size, and default value.
A change to the default value affects only subsequent insertions to the table.
2-6
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; Table altered.
2-7
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 SET OR ALTER SET TABLE <table_name> UNUSED(<column_name>);
2-8
2-10
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; Table altered. ALTER TABLE emp2 ADD CONSTRAINT emp_mgr_fk FOREIGN KEY(manager_id) REFERENCES emp2(employee_id); Table altered.
2-11
ON DELETE CASCADE
2-12
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
Deferring constraint on creation
2-13
Dropping a Constraint
Remove the manager constraint from the EMP2 table.
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; Table altered.
2-14
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.
2-15
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; Table altered.
A UNIQUE index is automatically created if you enable a UNIQUE key or PRIMARY KEY constraint.
2-16
Cascading Constraints
The CASCADE CONSTRAINTS clause is used along with the DROP COLUMN clause. The CASCADE CONSTRAINTS clause drops all referential integrity constraints that refer to the primary and unique keys defined on the dropped columns. The CASCADE CONSTRAINTS clause also drops all multicolumn constraints defined on the dropped columns.
2-18
Cascading Constraints
Example:
ALTER TABLE emp2 DROP COLUMN employee_id CASCADE CONSTRAINTS; Table altered.
ALTER TABLE test1 DROP (pk, fk, col1) CASCADE CONSTRAINTS; Table altered.
2-19
Overview of Indexes
Manually
CREATE INDEX statement CREATE TABLE statement
2-20
2-21
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.
2-23
Removing an Index
Remove an index from the data dictionary by using the DROP INDEX command.
DROP INDEX index;
To drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege.
2-25
2-26
Syntax:
FLASHBACK TABLE[schema.]table[, [ schema.]table ]... TO { TIMESTAMP | SCN } expr [ { ENABLE | DISABLE } TRIGGERS ];
2-27
2-28
External Tables
2-29
2-31
CREATE TABLE <table_name> ( <col_name> <datatype>, ) ORGANIZATION EXTERNAL (TYPE <access_driver_type> DEFAULT DIRECTORY <directory_name> ACCESS PARAMETERS ( ) ) LOCATION ('<location_specifier>') ) REJECT LIMIT [0 | <number> | UNLIMITED];
2-33
2-35
OLDEMP
emp.dat
2-37
Summary
In this lesson, you should have learned how to: Add constraints Create indexes Create a primary key constraint using an index Create indexes using the CREATE TABLE statement Creating function-based indexes Drop columns and set column UNUSED Perform FLASHBACK operations Create and use external tables
2-38
Practice 2: Overview
This practice covers the following topics: Altering tables Adding columns Dropping columns Creating indexes Creating external tables
2-39