0% found this document useful (0 votes)
2 views

5-oracle-developer-tables-indexes-essentials-m5-slides

The document outlines the management of tables in Oracle databases, including adding, renaming, and dropping tables and columns. It details the Oracle Data Dictionary views that provide metadata about database objects and explains how to gather and utilize database statistics. Additionally, it covers the implications of modifying table structures, such as constraints and dependencies.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

5-oracle-developer-tables-indexes-essentials-m5-slides

The document outlines the management of tables in Oracle databases, including adding, renaming, and dropping tables and columns. It details the Oracle Data Dictionary views that provide metadata about database objects and explains how to gather and utilize database statistics. Additionally, it covers the implications of modifying table structures, such as constraints and dependencies.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Managing Tables

David Berry
http://buildingbettersoftware.blogspot.com/
Databases Require Changes Over Time

Adding columns Changing data types Renaming objects


Module Contents

Oracle
Database Managing Managing
Data
Statistics Tables Columns
Dictionary
Series of views containing
Oracle Data metadata about all database
Dictionary objects in Oracle
Data Dictionary Course Content Focus

Graphical presentation Be aware of underlying


varies by tool views

Use for writing custom


Become familiar with
queries, customizing
feature set of tool you use
output format
Three Sets of Views

dba_* Information about all objects in the database

Information about objects owned by the


user_* current user

Information about all objects the current


all_* user has permission to
USER_OBJECTS View

Definition
Contains An entry for every database object (table, view,
index, stored procedure, etc…)
Oracle Documentation http://bit.ly/Ora12cAllObjects

Key columns
object_name Name of the table, view, index, etc…
object_type Indicated if this object is a table, view, etc…
created Timestamp of when this object was created
last_ddl_time Timestamp of when the definition of this object was
last changed
status Indicated if the object is valid or invalid
USER_TABLES View

Definition
Contains One row for each table
Oracle Documentation http://bit.ly/Ora12cAllTables

Key columns
table_name Name of the table
num_rows Number of rows in the table
blocks Number of blocks that have been used in the table
avg_row_len Average length in bytes of a row in the table
last_analyzed Date that stats were most recently gathered against
the table
USER_TAB_COLS View

Definition
Contains An entry for every column in a table or a view
Oracle Documentation http://bit.ly/Ora12cAllTabCols

Key columns
table_name Name of the table or view the column belongs to
column_name Name of the column
data_type Data type of this column
num_distinct Number of distinct values in this column
num_nulls Number of rows in this column with a null value
USER_CONSTRAINTS View

Definition
Contains An entry for each constraint (primary key, foreign
key, check constraint)
Oracle Documentation http://bit.ly/Ora12cAllConstraints

Key columns
table_name Name of the table the constraint is associated with
constraint_name Name of the constraint
constraint_type Code that indicates the type of the constraint
r_constriant_name For foreign keys, the name of the unique constraint
in the parent table
deferrable Indicates if the constraint can be deferred
USER_INDEXES View

Definition
Contains An entry for each index
Oracle Documentation http://bit.ly/Ora12cAllindexes

Key columns
table_name Name of the table the index is on
index_name Name of index
uniqueness Indicated if this is a unique index
distinct_keys Number of distinct keys in the index
last_analyed Date that stats were most recently gathered against
the index
USER_IND_COLUMNS View

Definition
Contains An entry for each column in an index
Oracle Documentation http://bit.ly/Ora12cAllIndColumns

Key columns
table_name Name of the table the index is on
index_name Name of index
column_name Name of the column
column_position Position of the column in the index
Data Dictionary Summary

Invest some time in learning what is available

Most commonly used views


• http://bit.ly/Ora12cDataDictionaryViews
Life of a SQL Statement

Parse Phase Query Optimization Execution Phase


Check SQL Syntax Read Blocks
Evaluate Statistics
Check Object Permissions Filter Rows
Create Execution Plan
Query Rewrite Process Sort Data
By the Oracle Optimizer to determine the
How Are most efficient plan for your SQL statement
Database
Statistics Used? To estimate how much processing is required
for each option
How Are Database Statistics Gathered?

• Scheduled by Oracle
Automatically • Oracle detects objects with significant changes

• Executed by user
Manually • Used after large insert/delete operation
• Useful in test environments
Gathering Statistics Manually

-- Update for one table and all indexes on table


EXEC DBMS_STATS.GATHER_TABLE_STATS(
ownname => ‘<<Table Owner Name>>’,
tabname => ‘<<Table Name>>’,
estimate_percent => <<percent>>);

-- Update for all objects in a schema


EXEC DBMS_STATS. GATHER_SCHEMA_STATS(
ownname => ‘<<Schema Owner Name>>’,
estimate_percent => <<percent>> );

http://bit.ly/Orac12cDbmsStats
Dropping a Table

-- Basic DROP TABLE syntax


DROP TABLE departments;

Dependent
Tables
Foreign Keys and Dropping a Table

-- Drop dependent tables first


DROP TABLE degrees;
DROP TABLE courses;

-- Now drop the table of interest


DROP TABLE departments;

 Problematic if the dependent tables contain more


dependencies

 We don’t want to drop a significant portion of the


database
Drop Table With Cascade Constraints

-- Drop table and foreign keys that reference this table


DROP TABLE departments CASCADE CONSTRAINTS;

 Dependent tables stay intact


 Only their foreign keys are dropped

 Possible to recreate the table, re-add foreign keys


Renaming a Table

-- Syntax to rename an object in Oracle


-- RENAME old_name TO new_name;

RENAME departments TO depts;

 Oracle will automatically transfer all constraints and


indexes

 Views, stored procedures, functions, synonyms will


become invalid
 Find invalid objects using all_objects view
 You need to manually fix these
Renaming a Table

-- Syntax to rename an object in Oracle


-- RENAME old_name TO new_name;

RENAME departments TO depts;

 Oracle will automatically transfer all constraints and


indexes

 Views, stored procedures, functions, synonyms will


become invalid
 Find invalid objects using all_objects view
 You need to manually fix these
Making a Table Read Only

Place a table in read only mode


ALTER TABLE table_name READ ONLY;

 No DML allowed (INSERT, UPDATE, DELETE)


 TRUNCATE not allowed
 Cannot add, modify, remove columns

Return a table to read write mode


ALTER TABLE table_name READ WRITE;
Adding Columns to Tables

Frequent need over the lifetime of a database

Required capability to respond to changing business needs


Adding Columns to a Table

ALTER TABLE students ADD


(
immunization_form_received VARCHAR2(1) DEFAULT 'N' NOT NULL,
immunization_form_date DATE NULL
);
Adding Columns With a Default Value

ALTER TABLE students ADD


(
immunization_form_received VARCHAR2(1) DEFAULT 'N' NOT NULL,
immunization_form_date DATE NULL
);

 Oracle substitutes the default value at query time for existing


rows

 Existing rows do not have to be updated


Adding Columns – Breaking Insert Statements

Insert statement without column names


INSERT INTO courses
VALUES (‘CS’, ‘401’, ‘Operating Systems’, 4.0’);

Solution: add the column as invisible


ALTER TABLE courses ADD
(
textbook_name VARCHAR2(50) INVISIBLE NULL
);
Drop a Column From a Table

Logical Delete Physical Delete


Physical Delete of Column

ALTER TABLE table_name


DROP COLUMN (column_name1, column_name2);
Physical Delete of Column

Views, procedures will be


Your responsibility to fix these
invalidated

Oracle will update each Can be a performance impact on a


block in table large table
Logical Delete of Column

ALTER TABLE table_name


SET UNUSED (column_name1, column_name2);

At some later time:


ALTER TABLE table_name
DROP UNUSED COLUMNS;
Logical Delete of Column

Logically deleted columns


Regard this as a one way trip
cannot be recovered

Provides ability to defer Can occur during off hours or a


physical update of blocks maintenance window
Dropping Columns and Key Constraints

 Attempt to drop column in a primary or foreign key will result in an


error

 To drop the column and the primary/foreign key use


ALTER TABLE table_name
SET UNUSED (column_name1, column_name2)
CASCADE CONSTRAINTS;

 Is dropping a column in a key constraint what you really want?


Changing a Column Data Type

Possible to do, but there are limitations


Changing a Column Data Type

Operation Examples Allowed?

Increasing size VARCHAR2(30)  VARCHAR2(50)


Allowed
of data type NUMBER(8,4)  NUMBER(12,6)

Decreasing size Allowed if no data


VARCHAR2(50)  VARCHAR2(25)
of VARCHAR truncated

Decrease precision, Allowed if column


NUMBER(8,4)  NUMBER(12,6)
scale of NUMBER empty

Change to different NUMBER(8,4)  VARCHAR2(10) Allowed if column


data type VARCHAR2(10)  DATE empty
Change Column Data Type Syntax

ALTER TABLE table_name MODIFY


(
column_name new_data_type
);
Module Summary

Oracle Data Dictionary Database Statistics

What is the data dictionary What are database statistics


Useful views How are they used
How are stats gathered

Managing Tables Managing Columns

Dropping tables Adding columns


Renaming tables Renaming columns
Changing column data types

You might also like