Managing Objects
with Data Dictionary Views
Copyright © 2007, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do the
following:
• Use the data dictionary views to research data on your
objects
• Query various data dictionary views
3-2 Copyright © 2007, Oracle. All rights reserved.
Lesson Agenda
• Introduction to data dictionary
• Querying the dictionary views for the following:
– Table information
– Column information
– Constraint information
• Querying the dictionary views for the following:
– View information
– Sequence information
– Synonym information
– Index information
• Adding a comment to a table and querying the dictionary
views for comment information
3-3 Copyright © 2007, Oracle. All rights reserved.
Data Dictionary
Oracle server
Tables containing Data dictionary
business data: views:
EMPLOYEES DICTIONARY
DEPARTMENTS USER_OBJECTS
LOCATIONS USER_TABLES
JOB_HISTORY USER_TAB_COLUMNS
... ...
3-4 Copyright © 2007, Oracle. All rights reserved.
Data Dictionary Structure
Oracle server
Consists of:
– Base tables
– User-accessible views
3-5 Copyright © 2007, Oracle. All rights reserved.
Data Dictionary Structure
View naming convention:
View Prefix Purpose
USER User’s view (what is in your schema; what you
own)
ALL Expanded user’s view (what you can access)
DBA Database administrator’s view (what is in
everyone’s schemas)
V$ Performance-related data
3-6 Copyright © 2007, Oracle. All rights reserved.
How to Use the Dictionary Views
Start with DICTIONARY. It contains the names and descriptions
of the dictionary tables and views.
DESCRIBE DICTIONARY
SELECT *
FROM dictionary
WHERE table_name = 'USER_OBJECTS';
3-7 Copyright © 2007, Oracle. All rights reserved.
USER_OBJECTS and ALL_OBJECTS Views
USER_OBJECTS:
• Query USER_OBJECTS to see all the objects that you own
• Is a useful way to obtain a listing of all object names and
types in your schema, plus the following information:
– Date created
– Date of last modification
– Status (valid or invalid)
ALL_OBJECTS:
• Query ALL_OBJECTS to see all objects to which you have
access
3-8 Copyright © 2007, Oracle. All rights reserved.
USER_OBJECTS View
SELECT object_name, object_type, created, status
FROM user_objects
ORDER BY object_type;
3-9 Copyright © 2007, Oracle. All rights reserved.
Lesson Agenda
• Introduction to data dictionary
• Querying the dictionary views for the following:
– Table information
– Column information
– Constraint information
• Querying the dictionary views for the following:
– View information
– Sequence information
– Synonym information
– Index information
• Adding a comment to a table and querying the dictionary
views for comment information
3 - 10 Copyright © 2007, Oracle. All rights reserved.
Table Information
USER_TABLES:
DESCRIBE user_tables
…
SELECT table_name
FROM user_tables;
3 - 11 Copyright © 2007, Oracle. All rights reserved.
Column Information
USER_TAB_COLUMNS:
DESCRIBE user_tab_columns
3 - 12 Copyright © 2007, Oracle. All rights reserved.
Column Information
SELECT column_name, data_type, data_length,
data_precision, data_scale, nullable
FROM user_tab_columns
WHERE table_name = 'EMPLOYEES';
3 - 13 Copyright © 2007, Oracle. All rights reserved.
Constraint Information
• USER_CONSTRAINTS describes the constraint definitions on
your tables.
• USER_CONS_COLUMNS describes columns that are owned
by you and that are specified in constraints.
DESCRIBE user_constraints
3 - 14 Copyright © 2007, Oracle. All rights reserved.
USER_CONSTRAINTS: Example
SELECT constraint_name, constraint_type,
search_condition, r_constraint_name,
delete_rule, status
FROM user_constraints
WHERE table_name = 'EMPLOYEES';
3 - 15 Copyright © 2007, Oracle. All rights reserved.
Querying USER_CONS_COLUMNS
DESCRIBE user_cons_columns
SELECT constraint_name, column_name
FROM user_cons_columns
WHERE table_name = 'EMPLOYEES';
…
3 - 16 Copyright © 2007, Oracle. All rights reserved.
Lesson Agenda
• Introduction to data dictionary
• Querying the dictionary views for the following:
– Table information
– Column information
– Constraint information
• Querying the dictionary views for the following:
– View information
– Sequence information
– Synonym information
– Index information
• Adding a comment to a table and querying the dictionary
views for comment information
3 - 17 Copyright © 2007, Oracle. All rights reserved.
View Information
1 DESCRIBE user_views
2 SELECT DISTINCT view_name FROM user_views;
3 SELECT text FROM user_views
WHERE view_name = 'EMP_DETAILS_VIEW';
3 - 18 Copyright © 2007, Oracle. All rights reserved.
Sequence Information
DESCRIBE user_sequences
3 - 19 Copyright © 2007, Oracle. All rights reserved.
Confirming Sequences
• Verify your sequence values in the USER_SEQUENCES data
dictionary table.
SELECT sequence_name, min_value, max_value,
increment_by, last_number
FROM user_sequences;
• The LAST_NUMBER column displays the next available
sequence number if NOCACHE is specified.
3 - 20 Copyright © 2007, Oracle. All rights reserved.
Index Information
• USER_INDEXES provides information about your indexes.
• USER_IND_COLUMNS describes columns comprising your
indexes and columns of indexes on your tables.
DESCRIBE user_indexes
3 - 21 Copyright © 2007, Oracle. All rights reserved.
USER_INDEXES: Examples
a SELECT index_name, table_name,uniqueness
FROM user_indexes
WHERE table_name = 'EMPLOYEES';
b SELECT INDEX_NAME, TABLE_NAME
FROM USER_INDEXES
WHERE TABLE_NAME = 'EMP_LIB';
3 - 22 Copyright © 2007, Oracle. All rights reserved.
Querying USER_IND_COLUMNS
DESCRIBE user_ind_columns
SELECT INDEX_NAME, COLUMN_NAME,TABLE_NAME
FROM user_ind_columns
WHERE INDEX_NAME = 'LNAME_IDX';
3 - 23 Copyright © 2007, Oracle. All rights reserved.
Synonym Information
DESCRIBE user_synonyms
SELECT *
FROM user_synonyms;
3 - 24 Copyright © 2007, Oracle. All rights reserved.
Lesson Agenda
• Introduction to data dictionary
• Querying the dictionary views for the following:
– Table information
– Column information
– Constraint information
• Querying the dictionary views for the following:
– View information
– Sequence information
– Synonym information
– Index information
• Adding a comment to a table and querying the dictionary
views for comment information
3 - 25 Copyright © 2007, Oracle. All rights reserved.
Adding Comments to a Table
• You can add comments to a table or column by using the
COMMENT statement:
COMMENT ON TABLE employees
IS 'Employee Information';
COMMENT ON COLUMN employees.first_name
IS 'First name of the employee';
• Comments can be viewed through the data dictionary views:
– ALL_COL_COMMENTS
– USER_COL_COMMENTS
– ALL_TAB_COMMENTS
– USER_TAB_COMMENTS
3 - 26 Copyright © 2007, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to find information
about your objects through the following dictionary views:
• DICTIONARY
• USER_OBJECTS
• USER_TABLES
• USER_TAB_COLUMNS
• USER_CONSTRAINTS
• USER_CONS_COLUMNS
• USER_VIEWS
• USER_SEQUENCES
• USER_INDEXES
• USER_SYNONYMS
3 - 27 Copyright © 2007, Oracle. All rights reserved.