SlideShare a Scribd company logo
1 of 40
Copyright © 2007, Oracle. All rights reserved.
Managing Schema Objects
Copyright © 2007, Oracle. All rights reserved.
2 - 2
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
• Create and use external tables
Copyright © 2007, Oracle. All rights reserved.
2 - 3
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
Copyright © 2007, Oracle. All rights reserved.
2 - 4
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
Copyright © 2007, Oracle. All rights reserved.
2 - 5
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);
Copyright © 2007, Oracle. All rights reserved.
2 - 6
Adding a Column
• You use the ADD clause to add columns:
• The new column becomes the last column:
ALTER TABLE dept80
ADD (job_id VARCHAR2(9));
Copyright © 2007, Oracle. All rights reserved.
2 - 7
Modifying a Column
• You can change a column’s data type, size, and default
value.
• A change to the default value affects only subsequent
insertions to the table.
ALTER TABLE dept80
MODIFY (last_name VARCHAR2(30));
Copyright © 2007, Oracle. All rights reserved.
2 - 8
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;
Copyright © 2007, Oracle. All rights reserved.
2 - 9
ALTER TABLE <table_name>
SET UNUSED(<column_name>);
ALTER TABLE <table_name>
SET UNUSED COLUMN <column_name>;
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.
OR
ALTER TABLE <table_name>
DROP UNUSED COLUMNS;
Copyright © 2007, Oracle. All rights reserved.
2 - 11
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
Copyright © 2007, Oracle. All rights reserved.
2 - 12
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>);
Copyright © 2007, Oracle. All rights reserved.
2 - 13
ALTER TABLE emp2
modify employee_id Primary Key;
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
ADD CONSTRAINT emp_mgr_fk
FOREIGN KEY(manager_id)
REFERENCES emp2(employee_id);
Copyright © 2007, Oracle. All rights reserved.
2 - 14
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;
Copyright © 2007, Oracle. All rights reserved.
2 - 15
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
ALTER SESSION
SET CONSTRAINTS= IMMEDIATE
SET CONSTRAINTS dept2_id_pk IMMEDIATE
Deferring constraint on
creation
Changing all constraints for a
session
Changing a specific
constraint attribute
Copyright © 2007, Oracle. All rights reserved.
2 - 16
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 );
Copyright © 2007, Oracle. All rights reserved.
2 - 18
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 emp2
DROP CONSTRAINT emp_mgr_fk;
ALTER TABLE dept2
DROP PRIMARY KEY CASCADE;
Copyright © 2007, Oracle. All rights reserved.
2 - 19
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;
Copyright © 2007, Oracle. All rights reserved.
2 - 20
Enabling Constraints
• Activate an integrity constraint currently disabled in the table
definition by using the ENABLE clause.
• A UNIQUE index is automatically created if you enable a
UNIQUE key or a PRIMARY KEY constraint.
ALTER TABLE emp2
ENABLE CONSTRAINT emp_dt_fk;
Copyright © 2007, Oracle. All rights reserved.
2 - 22
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.
Copyright © 2007, Oracle. All rights reserved.
2 - 23
Cascading Constraints
Example:
ALTER TABLE emp2
DROP COLUMN employee_id CASCADE CONSTRAINTS;
ALTER TABLE test1
DROP (col1_pk, col2_fk, col1) CASCADE CONSTRAINTS;
Copyright © 2007, Oracle. All rights reserved.
2 - 24
Renaming Table Columns and Constraints
Use the RENAME COLUMN clause of the ALTER TABLE
statement to rename table columns.
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.
ALTER TABLE marketing RENAME CONSTRAINT mktg_pk
TO new_mktg_pk;
a
b
Copyright © 2007, Oracle. All rights reserved.
2 - 25
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
Copyright © 2007, Oracle. All rights reserved.
2 - 26
Overview of Indexes
Indexes are created:
• Automatically
– PRIMARY KEY creation
– UNIQUE KEY creation
• Manually
– The CREATE INDEX statement
– The CREATE TABLE statement
Copyright © 2007, Oracle. All rights reserved.
2 - 27
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';
Copyright © 2007, Oracle. All rights reserved.
2 - 29
CREATE INDEX upper_dept_name_idx
ON dept2(UPPER(department_name));
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.
SELECT *
FROM dept2
WHERE UPPER(department_name) = 'SALES';
Copyright © 2007, Oracle. All rights reserved.
2 - 30
Removing an Index
• Remove an index from the data dictionary by using the DROP
INDEX command:
• Remove the UPPER_DEPT_NAME_IDX index from the data
dictionary:
• To drop an index, you must be the owner of the index or
have the DROP ANY INDEX privilege.
DROP INDEX upper_dept_name_idx;
DROP INDEX index;
Copyright © 2007, Oracle. All rights reserved.
2 - 31
DROP TABLE … PURGE
DROP TABLE dept80 PURGE;
Copyright © 2007, Oracle. All rights reserved.
2 - 32
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
Copyright © 2007, Oracle. All rights reserved.
2 - 33
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
Copyright © 2007, Oracle. All rights reserved.
2 - 34
FLASHBACK TABLE Statement
• Repair tool for accidental table modifications
– Restores a table to an earlier point in time
– Benefits: Ease of use, availability, and fast execution
– Is performed in place
• Syntax:
FLASHBACK TABLE[schema.]table[,
[ schema.]table ]...
TO { TIMESTAMP | SCN } expr
[ { ENABLE | DISABLE } TRIGGERS ];
Copyright © 2007, Oracle. All rights reserved.
2 - 35
Using the FLASHBACK TABLE Statement
DROP TABLE emp2;
FLASHBACK TABLE emp2 TO BEFORE DROP;
…
SELECT original_name, operation, droptime FROM
recyclebin;
Copyright © 2007, Oracle. All rights reserved.
2 - 36
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
Copyright © 2007, Oracle. All rights reserved.
2 - 37
External Tables
Copyright © 2007, Oracle. All rights reserved.
2 - 38
Creating a Directory for the External Table
Create a DIRECTORY object that corresponds to the directory
on the file system where the external data source resides.
CREATE OR REPLACE DIRECTORY emp_dir
AS '/…/emp_dir';
GRANT READ ON DIRECTORY emp_dir TO hr;
Copyright © 2007, Oracle. All rights reserved.
2 - 40
Creating an External Table
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];
Copyright © 2007, Oracle. All rights reserved.
2 - 42
Creating an External Table by Using
ORACLE_LOADER
CREATE TABLE oldemp (
fname char(25), lname CHAR(25))
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY emp_dir
ACCESS PARAMETERS
(RECORDS DELIMITED BY NEWLINE
NOBADFILE
NOLOGFILE
FIELDS TERMINATED BY ','
(fname POSITION ( 1:20) CHAR,
lname POSITION (22:41) CHAR))
LOCATION ('emp.dat'))
PARALLEL 5
REJECT LIMIT 200;
Copyright © 2007, Oracle. All rights reserved.
2 - 44
Querying External Tables
SELECT *
FROM oldemp
emp.dat
OLDEMP
Copyright © 2007, Oracle. All rights reserved.
2 - 45
Creating an External Table by Using
ORACLE_DATAPUMP: Example
CREATE TABLE emp_ext
(employee_id, first_name, last_name)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_DATAPUMP
DEFAULT DIRECTORY emp_dir
LOCATION
('emp1.exp','emp2.exp')
)
PARALLEL
AS
SELECT employee_id, first_name, last_name
FROM employees;
Copyright © 2007, Oracle. All rights reserved.
2 - 46
Summary
In this lesson, you should have learned how to:
• 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
• Create and use external tables
Copyright © 2007, Oracle. All rights reserved.
2 - 47
Practice 2: Overview
This practice covers the following topics:
• Altering tables
• Adding columns
• Dropping columns
• Creating indexes
• Creating external tables

More Related Content

Similar to database management system lessonchapter

Less07 schema
Less07 schemaLess07 schema
Less07 schema
Imran Ali
 
Clase 13 integridad modificada
Clase 13 integridad   modificadaClase 13 integridad   modificada
Clase 13 integridad modificada
Titiushko Jazz
 
Clase 13 integridad modificada
Clase 13 integridad   modificadaClase 13 integridad   modificada
Clase 13 integridad modificada
Titiushko Jazz
 
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OSIDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
Cuneyt Goksu
 

Similar to database management system lessonchapter (20)

Cursores.ppt
Cursores.pptCursores.ppt
Cursores.ppt
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
 
Less07 schema
Less07 schemaLess07 schema
Less07 schema
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
SQL_SERVER_BASIC_1_Training.pptx
SQL_SERVER_BASIC_1_Training.pptxSQL_SERVER_BASIC_1_Training.pptx
SQL_SERVER_BASIC_1_Training.pptx
 
Database Objects
Database ObjectsDatabase Objects
Database Objects
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Clase 13 integridad modificada
Clase 13 integridad   modificadaClase 13 integridad   modificada
Clase 13 integridad modificada
 
Clase 13 integridad modificada
Clase 13 integridad   modificadaClase 13 integridad   modificada
Clase 13 integridad modificada
 
Less08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxLess08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptx
 
plsql les02
 plsql les02 plsql les02
plsql les02
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data Base
 
Les02
Les02Les02
Les02
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
 
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OSIDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 

More from MohammedNouh7 (11)

JavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.pptJavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.ppt
 
Chapter7 database management system.pptx
Chapter7 database management system.pptxChapter7 database management system.pptx
Chapter7 database management system.pptx
 
Introduction to database m Chapter 9.pptx
Introduction to database m Chapter 9.pptxIntroduction to database m Chapter 9.pptx
Introduction to database m Chapter 9.pptx
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Lecture2.pptx
Lecture2.pptxLecture2.pptx
Lecture2.pptx
 
Ch13.pptx
Ch13.pptxCh13.pptx
Ch13.pptx
 
Ch21.pptx
Ch21.pptxCh21.pptx
Ch21.pptx
 
41slideAdvancedDatabaseProgramming.ppt
41slideAdvancedDatabaseProgramming.ppt41slideAdvancedDatabaseProgramming.ppt
41slideAdvancedDatabaseProgramming.ppt
 
34slideDatabaseProgramming.ppt
34slideDatabaseProgramming.ppt34slideDatabaseProgramming.ppt
34slideDatabaseProgramming.ppt
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
10slide.ppt
10slide.ppt10slide.ppt
10slide.ppt
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 

database management system lessonchapter

  • 1. Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects
  • 2. Copyright © 2007, Oracle. All rights reserved. 2 - 2 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 • Create and use external tables
  • 3. Copyright © 2007, Oracle. All rights reserved. 2 - 3 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
  • 4. Copyright © 2007, Oracle. All rights reserved. 2 - 4 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
  • 5. Copyright © 2007, Oracle. All rights reserved. 2 - 5 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);
  • 6. Copyright © 2007, Oracle. All rights reserved. 2 - 6 Adding a Column • You use the ADD clause to add columns: • The new column becomes the last column: ALTER TABLE dept80 ADD (job_id VARCHAR2(9));
  • 7. Copyright © 2007, Oracle. All rights reserved. 2 - 7 Modifying a Column • You can change a column’s data type, size, and default value. • A change to the default value affects only subsequent insertions to the table. ALTER TABLE dept80 MODIFY (last_name VARCHAR2(30));
  • 8. Copyright © 2007, Oracle. All rights reserved. 2 - 8 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;
  • 9. Copyright © 2007, Oracle. All rights reserved. 2 - 9 ALTER TABLE <table_name> SET UNUSED(<column_name>); ALTER TABLE <table_name> SET UNUSED COLUMN <column_name>; 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. OR ALTER TABLE <table_name> DROP UNUSED COLUMNS;
  • 10. Copyright © 2007, Oracle. All rights reserved. 2 - 11 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
  • 11. Copyright © 2007, Oracle. All rights reserved. 2 - 12 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>);
  • 12. Copyright © 2007, Oracle. All rights reserved. 2 - 13 ALTER TABLE emp2 modify employee_id Primary Key; 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 ADD CONSTRAINT emp_mgr_fk FOREIGN KEY(manager_id) REFERENCES emp2(employee_id);
  • 13. Copyright © 2007, Oracle. All rights reserved. 2 - 14 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;
  • 14. Copyright © 2007, Oracle. All rights reserved. 2 - 15 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 ALTER SESSION SET CONSTRAINTS= IMMEDIATE SET CONSTRAINTS dept2_id_pk IMMEDIATE Deferring constraint on creation Changing all constraints for a session Changing a specific constraint attribute
  • 15. Copyright © 2007, Oracle. All rights reserved. 2 - 16 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 );
  • 16. Copyright © 2007, Oracle. All rights reserved. 2 - 18 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 emp2 DROP CONSTRAINT emp_mgr_fk; ALTER TABLE dept2 DROP PRIMARY KEY CASCADE;
  • 17. Copyright © 2007, Oracle. All rights reserved. 2 - 19 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;
  • 18. Copyright © 2007, Oracle. All rights reserved. 2 - 20 Enabling Constraints • Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause. • A UNIQUE index is automatically created if you enable a UNIQUE key or a PRIMARY KEY constraint. ALTER TABLE emp2 ENABLE CONSTRAINT emp_dt_fk;
  • 19. Copyright © 2007, Oracle. All rights reserved. 2 - 22 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.
  • 20. Copyright © 2007, Oracle. All rights reserved. 2 - 23 Cascading Constraints Example: ALTER TABLE emp2 DROP COLUMN employee_id CASCADE CONSTRAINTS; ALTER TABLE test1 DROP (col1_pk, col2_fk, col1) CASCADE CONSTRAINTS;
  • 21. Copyright © 2007, Oracle. All rights reserved. 2 - 24 Renaming Table Columns and Constraints Use the RENAME COLUMN clause of the ALTER TABLE statement to rename table columns. 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. ALTER TABLE marketing RENAME CONSTRAINT mktg_pk TO new_mktg_pk; a b
  • 22. Copyright © 2007, Oracle. All rights reserved. 2 - 25 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
  • 23. Copyright © 2007, Oracle. All rights reserved. 2 - 26 Overview of Indexes Indexes are created: • Automatically – PRIMARY KEY creation – UNIQUE KEY creation • Manually – The CREATE INDEX statement – The CREATE TABLE statement
  • 24. Copyright © 2007, Oracle. All rights reserved. 2 - 27 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';
  • 25. Copyright © 2007, Oracle. All rights reserved. 2 - 29 CREATE INDEX upper_dept_name_idx ON dept2(UPPER(department_name)); 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. SELECT * FROM dept2 WHERE UPPER(department_name) = 'SALES';
  • 26. Copyright © 2007, Oracle. All rights reserved. 2 - 30 Removing an Index • Remove an index from the data dictionary by using the DROP INDEX command: • Remove the UPPER_DEPT_NAME_IDX index from the data dictionary: • To drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege. DROP INDEX upper_dept_name_idx; DROP INDEX index;
  • 27. Copyright © 2007, Oracle. All rights reserved. 2 - 31 DROP TABLE … PURGE DROP TABLE dept80 PURGE;
  • 28. Copyright © 2007, Oracle. All rights reserved. 2 - 32 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
  • 29. Copyright © 2007, Oracle. All rights reserved. 2 - 33 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
  • 30. Copyright © 2007, Oracle. All rights reserved. 2 - 34 FLASHBACK TABLE Statement • Repair tool for accidental table modifications – Restores a table to an earlier point in time – Benefits: Ease of use, availability, and fast execution – Is performed in place • Syntax: FLASHBACK TABLE[schema.]table[, [ schema.]table ]... TO { TIMESTAMP | SCN } expr [ { ENABLE | DISABLE } TRIGGERS ];
  • 31. Copyright © 2007, Oracle. All rights reserved. 2 - 35 Using the FLASHBACK TABLE Statement DROP TABLE emp2; FLASHBACK TABLE emp2 TO BEFORE DROP; … SELECT original_name, operation, droptime FROM recyclebin;
  • 32. Copyright © 2007, Oracle. All rights reserved. 2 - 36 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
  • 33. Copyright © 2007, Oracle. All rights reserved. 2 - 37 External Tables
  • 34. Copyright © 2007, Oracle. All rights reserved. 2 - 38 Creating a Directory for the External Table Create a DIRECTORY object that corresponds to the directory on the file system where the external data source resides. CREATE OR REPLACE DIRECTORY emp_dir AS '/…/emp_dir'; GRANT READ ON DIRECTORY emp_dir TO hr;
  • 35. Copyright © 2007, Oracle. All rights reserved. 2 - 40 Creating an External Table 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];
  • 36. Copyright © 2007, Oracle. All rights reserved. 2 - 42 Creating an External Table by Using ORACLE_LOADER CREATE TABLE oldemp ( fname char(25), lname CHAR(25)) ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY emp_dir ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE NOBADFILE NOLOGFILE FIELDS TERMINATED BY ',' (fname POSITION ( 1:20) CHAR, lname POSITION (22:41) CHAR)) LOCATION ('emp.dat')) PARALLEL 5 REJECT LIMIT 200;
  • 37. Copyright © 2007, Oracle. All rights reserved. 2 - 44 Querying External Tables SELECT * FROM oldemp emp.dat OLDEMP
  • 38. Copyright © 2007, Oracle. All rights reserved. 2 - 45 Creating an External Table by Using ORACLE_DATAPUMP: Example CREATE TABLE emp_ext (employee_id, first_name, last_name) ORGANIZATION EXTERNAL ( TYPE ORACLE_DATAPUMP DEFAULT DIRECTORY emp_dir LOCATION ('emp1.exp','emp2.exp') ) PARALLEL AS SELECT employee_id, first_name, last_name FROM employees;
  • 39. Copyright © 2007, Oracle. All rights reserved. 2 - 46 Summary In this lesson, you should have learned how to: • 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 • Create and use external tables
  • 40. Copyright © 2007, Oracle. All rights reserved. 2 - 47 Practice 2: Overview This practice covers the following topics: • Altering tables • Adding columns • Dropping columns • Creating indexes • Creating external tables