Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Oracle Data Dictionary 1.

Lesson 1
A DBAs Introduction to the
Oracle Data Dictionary

© 2004 SkillBuilders, Inc.


SKILLBUILDERS

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.2

1.2

Lesson Objectives
¾ Answer the question, “what is the data dictionary?”
¾ Understand the types of data dictionary tables and
views available to the DBA
¾ Gain hands-on experience querying the data
dictionary
¾ Primarily DBA_ and “V$” views
¾ Prepare you to use the data dictionary views later in
the course
¾ Show you where to get more information

© 2004 SkillBuilders, Inc.

In this lesson you will learn what the Oracle data dictionary is and what types of
tables and views make up the dictionary. You will also do a hands-on workshop,
which will give you confidence you will need to query the data dictionary in the
subsequent lessons of this course and in practical situations at your workplace.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.3

1.3

Introduction
¾ All successful DBAs must have a solid command of
the data dictionary
¾ Must know how to query
¾ Answer to many questions are in the dictionary
11 SELECT
SELECT owner,
owner, table_name
table_name
22 FROM dba_tables
FROM dba_tables
3*
3* WHERE
WHERE tablespace_name='USERS'
tablespace_name='USERS'
system@CLASS2>
system@CLASS2> //
OWNER
OWNER TABLE_NAME
TABLE_NAME
------------------------------
------------------------------ -----------
-----------
SCOTT
SCOTT DEPT
DEPT
SCOTT
SCOTT EMP
EMP
© 2004 SkillBuilders, Inc.

So many of the questions you will be faced with as an administrator will be


answered by querying the data dictionary. For example, a simple question such as
“what tables are in the USERS tablespace?” is answered with the query:
SELECT owner, table_name
FROM dba_tables
WHERE tablespace_name='USERS'
Thus, it is imperative that we have a solid command of the data dictionary.
(If you are not sure what a tablespace is, don’t worry, you will learn all about
tablespaces later in this course. However, if you are not sure what a table is, before
you continue with this course attend an Introduction to Oracle class or read Part II
of Oracle9i A Beginner’s Guide by Michael Abbey. ISBN 0-07-219279-8.)

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.4

1.4

What is the Dictionary?...


¾ Set of tables and views containing
information about the database
¾ “Metadata”
¾ The information is stored inside the database
¾ The database contains information about itself!
¾ Base tables owned by user SYS
¾ e.g. SYS.TAB$ table
¾ Too cumbersome to use regularly

© 2004 SkillBuilders, Inc.

The Oracle Data Dictionary is a set of tables and views containing information about
the database. This information is stored inside the database, so the database
contains information about itself, often called “metadata.”
There are hundreds of data dictionary tables and views. The views are built on
underlying base tables such as SYS.TAB$. However, the underlying tables such as
SYS.TAB$ are difficult to use, in part because they are not documented and in part
because the column names are not intuitive. (For fun, execute the SQL*Plus
DESCRIBE command on SYS.TAB$. I think you will agree that the column names
are not intuitive.)
Rather than use the SYS tables, we will use the more convenient – and
documented – data dictionary views.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.5

1.5

...What is the Dictionary?


¾ User-friendly views are available
¾ e.g. USER_TABLES
¾ One row for every table in your schema
¾ More than 1200 data dictionary views in 9i
¾ Static views, e.g. USER_TABLES
¾ Dynamic views, e.g. V$PARAMETER
¾ Global dynamic views, e.g. GV$PARAMETER
¾ Undocumented views, e.g. X$KSPPI
¾ See Oracle9i Database Reference
Do the mini-
workshop
© 2004 SkillBuilders, Inc.

The static and dynamic views built on the SYS tables are user-friendly.
“USER_TABLES”, for example, contains one row for every table owned by you (i.e.
created in your schema).
In this lesson, you will learn about static views and dynamic views. Other types of
dictionary views are also available:
¾ Global dynamic views are used in Oracle Parallel Server environments (called
Real Applications Clusters in Oracle9i).
¾ The “X$” views are undocumented views, and are typically used by the
database itself or Oracle support.
Refer to the Oracle9i Database Reference for descriptions of all documented
dictionary views.
Optional Mini-Workshop
To become familiar with USER_TABLES, and the data dictionary in general,
execute the following two commands from a SQL*Plus session while connected
with your DBA userid:
desc user_tables
select table_name, num_rows from user_tables;

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.6

1.6

Static Views
¾ Static views
¾ Contain data about objects, privileges, users, etc.
¾ Contents change with DDL operations
¾ Three types
¾ USER_
¾ Displays only information about current user objects
¾ e.g. USER_TABLES
¾ ALL_
¾ Displays info about users objects and objects privileged to use
¾ e.g. ALL_TABLES
¾ DBA_ Restricted access
¾ Displays all objects, etc.
¾ e.g. DBA_TABLES

© 2004 SkillBuilders, Inc.

The static data dictionary views contain information about database objects such as
tables, indexes and procedures. They are called “static” because the contents of
the views change only when DDL operations are executed. For example, a row is
added to the DBA_TABLES view when any user successfully executes a CREATE
TABLE statement. (There are also dynamic views, which we will discuss later in
this lesson.)
There are three types of static views, USER, ALL and DBA. Every user can query
the USER level views, which contain information about the current user’s objects.
Every user can also query the ALL-level views, which contain information about the
users objects and objects in other schemas that the user has some privilege on.
Supplemental Information
The DBA-level views can only be accessed by users who have been granted the
SELECT_CATALOG_ROLE role, the SELECT ANY DICTIONARY privilege or
SELECT privilege on specific views. The SELECT ANY DICTIONARY privilege is,
by default, granted to the DBA role. You will study more about privileges and roles
later in this course in the lesson Managing Security.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.7

1.7

Common DBA Views


¾ DBA_TABLES ¾ DBA_SEGMENTS
¾ DBA_TAB_COLUMNS ¾ DBA_EXTENTS
¾ DBA_CONSTRAINTS ¾ DBA_FREE_SPACE
¾ DBA_CONS_COLUMNS ¾ DBA_VIEWS
¾ DBA_INDEXES ¾ DBA_SYNONYMS
¾ DBA_TABLESPACES ¾ DBA_SYS_PRIVS
¾ DBA_DATA_FILES ¾ DBA_TAB_PRIVS
¾ DBA_OBJECTS ¾ DBA_TRIGGERS

© 2004 SkillBuilders, Inc.

Here is a short-list of commonly used static data dictionary views. Do not try to
remember every detail about these views now; you will use these views in
subsequent lessons in this course. Here’s a brief description of some commonly
used dictionary views:
¾ DBA_TABLES contains one row for every table and includes information such
as owner, tablespace_name, number of rows (NUM_ROWS) and average row
length (AVG_ROW_LEN). NUM_ROWS and AVG_ROW_LEN are statistics
and must be created and maintained with DBMS_STATS (formerly ANALYZE).
¾ DBA_TAB_COLUMNS contains one row for every column of every table. It
contains useful information such as data type, length, precision and scale, and
average length.
¾ DBA_CONSTRAINTS contains one row for every constraint created in the
database. DBA_CONSTRAINTS contains details such as the type of (“P” for
primary key, “C” for check and not null constraints, etc.) and status of the
constraint.
¾ DBA_CONS_COLUMNS contains the column names the constraint is built on.
Notes for this slide continue on the next page…

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.8

Commonly used data dictionary views, continued from the previous page:
¾ DBA_INDEXES contains one row for every index.
¾ DBA_TABLESPACES contains one row for every tablespace.
¾ DBA_DATA_FILES contains one row for every data file.
¾ DBA_OBJECTS contains one row for every object that has been created,
including tables, views, indexes, procedures, functions, etc.
¾ DBA_SEGMENTS contains information relating to the initial, next, percent
increase, number of extents, and the number of blocks and bytes for the
segment.
¾ DBA_EXTENTS contains information relating to each extent within a segment,
specifically data file that contains the extent, the starting block number and
length of the extent in blocks and bytes.
¾ DBA_FREE_SPACE contains information relating to the free space within the
database, specifically, the data file, starting block number and size in blocks
and bytes of every area of contiguous free space within the database.
¾ DBA_VIEWS contains a row for every view created in the database and
includes the view text (definition).
¾ DBA_SYNONYMS contains a row for every synonym and contains the
synonym name and the owner and table the synonym points to.
¾ DBA_SYS_PRIVS contains a row for every system privilege granted (e.g.
GRANT CREATE TABLE) to a user or role.
¾ DBA_TAB_PRIVS contains a row for every grant on every table. It contains
grantor, grantee and the privilege granted, among other things.
¾ DBA_TRIGGERS contains one row for every trigger defined. It contains the
type, event, and body (code) among other things.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.9

1.9

Example: DBA_USERS
¾ Show all users created in the database
11 select
select username,
username, account_status
account_status
22 from dba_users
from dba_users
3*
3* order
order by
by 11
SQL>
SQL> //

USERNAME
USERNAME ACCOUNT_STATUS
ACCOUNT_STATUS
------------------------------
------------------------------ -------------------
-------------------
ADMIN1
ADMIN1 OPEN
OPEN
APPTUNE
APPTUNE OPEN
OPEN
DAVE
DAVE OPEN
OPEN
DBSNMP
DBSNMP OPEN
OPEN
GARY
GARY OPEN
OPEN
HR
HR EXPIRED
EXPIRED && LOCKED
LOCKED

© 2004 SkillBuilders, Inc.

The names, account status, creation date and other information about Oracle user
accounts on the database can be obtained by querying the DBA_USERS data
dictionary view.
The ACCOUNT_STATUS column indicates the following:
¾ OPEN – The username can be used, i.e. you can logon to the database with
the username if you know the password.
¾ EXPIRED – The password has expired, either manually with the ALTER USER
PASSWORD EXPIRE command, or automatically by exceeding the lifetime of
the password (see the PASSWORD_LIFE_TIME parameter on the CREATE
PROFILE command.) The user will be prompted for the existing password and
a new password upon login.
¾ LOCKED – The username has been locked either manually with the ALTER
USER ACCOUNT LOCK command or by exceeding the login attempt limit (see
the FAILED_LOGIN_ATTEMPTS parameter of the CREATE PROFILE
command).
Supplemental Notes
Additional details about user accounts can be accessed from different data
dictionary views. For example, DBA_TS_QUOTAS contains information about
tablespace quotas (i.e. the amount of disk space a user can use).

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.10

1.10

Example: DBA_SYS_PRIVS
¾ Determine system privileges you have been granted:
11 select
select privilege,
privilege, admin_option
admin_option
22 from
from dba_sys_privs
dba_sys_privs
3*
3* where
where grantee
grantee == 'DAVE'
'DAVE'
SQL>
SQL> //

PRIVILEGE
PRIVILEGE ADM
ADM
-----------------------
----------------------- ---
--- Use upper-case!
CREATE
CREATE TYPE
TYPE NO
NO
CREATE
CREATE VIEW
VIEW NO
NO
CREATE
CREATE TABLE
TABLE NO
NO
.. .. ..

© 2004 SkillBuilders, Inc.

This example illustrates how to determine what system-level privileges you have
been granted. Specifically, the query finds the system-level privileges held by the
user ‘DAVE’ by querying the DBA_SYS_PRIVS view and applying the filter WHERE
GRANTEE = ‘DAVE’. It also shows that user DAVE is not an administrator of the
system privilege, meaning that this user cannot grant this privilege to other users.
As shown in this example, always use upper-case character strings when searching
the data dictionary. All character strings in the dictionary are stored in upper case.
Supplemental Notes
Don’t worry, you will learn all about privileges later in this course in the lesson
Managing Security. For now, here is a brief primer on privileges:
¾ System-level privileges are privileges such as CREATE TABLE and CREATE
PROCEDURE; they are not tied to one specific object.
¾ Object-level privileges on the other hand, are tied to a specific object. For
example GRANT SELECT ON CUSTOMER. Query the DBA_TAB_PRIVS to
see object privileges granted on tables.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.11

1.11

Dynamic Performance
Views
¾ Oracle continually updates during life of instance
¾ Common dynamic views:

¾ V$PARAMETER ¾ V$TRANSACTION
¾ V$DATABASE ¾ V$LOGFILE
¾ V$INSTANCE ¾ V$LOG
¾ V$SESSION ¾ V$ARCHIVED_LOG
¾ V$LOCK

© 2004 SkillBuilders, Inc.

Dynamic performance views are updated at startup time and usually throughout the
life of the instance. The names of these views always starts with “V$” (There are
also like-named views whose names start with the characters “GV$”. These are
used for the global, Real Application Clusters version of the view).
The following is a brief description of some common dynamic views. We will use
these views in the upcoming lessons in this course:
¾ V$PARAMETER contains one row for every database initialization parameter
and includes parameter name, current value and indicates if the parameter can
be dynamically modified.
¾ V$DATABASE – Contains one row describing the database state including
database name, database ID (DBID), creation date, archivelog mode and
checkpoint information used in recovery.
¾ V$INSTANCE – Contains one row describing the current instance. Contains
instance name, host (server) name and status (open, mount, nomount).
¾ V$SESSION – Contains one row for every active session, including database
background processes.
Notes for this slide continue on the next page…

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.12

Common dynamic performance views, continued from the previous page:


¾ V$LOCK – Contains one row for every lock held and locks waited on.
¾ V$TRANSACTION – Contains a rows for every active transaction.
¾ V$LOGFILE – Contains the names, locations of the online redo logs and
groups to which they belong.
¾ V$LOG – Contains the size, archive status and other information about the
online redo log files.
¾ V$ARCHIVED_LOG – Complete list of archived logs. Contains log name and
many details.
Refer to the Oracle9i Database Reference for a description of all documented data
dictionary views. (Remember, http://technet.oracle.com has all the Oracle manuals
online; membership is free!)

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.13

1.13

Example: V$PARAMETER
¾ Use V$PARAMETER to find the current value
of initialization parameters
¾ Find the location of the Alert log for this
instance:
11 select
select value
value
22 from
from v$parameter
v$parameter
3*
3* where
where name
name == 'background_dump_dest'
'background_dump_dest'
SQL>
SQL> //

VALUE
VALUE
----------------------------------------
----------------------------------------
C:\Oracle\admin\dave\bdump
C:\Oracle\admin\dave\bdump

© 2004 SkillBuilders, Inc.

The V$PARAMETER view shows the current value for all database initialization
parameters. Since there can sometimes be confusion as to what parameter file the
instance was started with, this view is invaluable.
The example above shows us how to determine the location of the alert log for the
instance. The alert log is typically named “ALERTinstance_name.LOG" and is a
standard text file that can be viewed with any text editor. The alert log contains
messages about significant events and is often used in debugging.

Notes for this slide continue on the next page…

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.14

You will find, among other events, these events in the Alert log:
¾ Instance startup and shutdown
¾ Log switches
¾ Instance recovery messages
¾ Oracle errors, for example:
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: 'C:\ORACLE\ORADATA\DAVE\SYSTEM01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) The system cannot find the file specified.
This error would be an indication that the SYSTEM datafile had been moved or
corrupted, and possibly needs recovery.
We will study how to find and solve some common Oracle errors later in this course.
Supplemental Notes
Note that the SQL*Plus SHOW command can also be used to display the database
initialization parameters. For example:
SQL> show parameter background_dump
NAME TYPE VALUE
-------------------- -------- ------------------
background_dump_dest string C:\oracle\admin\class2

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.15

1.15

Miscellaneous Views
¾ SESSION_PRIVS SQL> select
SQL> select **
¾ Active system privileges 22 from
from session_privs
session_privs
¾ DICTIONARY
¾ Name and descriptions of all data PRIVILEGE
dictionary views available to my
PRIVILEGE
-----------------------
-----------------------
user
ALTER
ALTER SYSTEM
SYSTEM
¾ See next page…
AUDIT
AUDIT SYSTEM
SYSTEM
¾ DICT_COLUMNS
CREATE
CREATE SESSION
SESSION
¾ Column descriptions for
dictionary views available to my
user
¾ TABLE_PRIVILEGES
¾ Privileges for objects for which
the user is the grantor, grantee or
owner, or PUBLIC is the grantee

© 2004 SkillBuilders, Inc.

There are several useful views that do not fit neatly into the categories we have
discussed in this lesson. These include:
¾ SESSION_PRIVS – Contains system privileges active for the current user and
session.
¾ DICTIONARY (DICT) – Contains the name and a brief description (in most
cases) of all data dictionary views.
¾ DICT_COLUMNS – Contains the name and a brief description of each column
of every data dictionary view.
¾ TABLE_PRIVILEGES – Contains object privileges for the current user.
You will see an example of querying the DICTIONARY view on the next page.
Again, don’t concern yourself with trying to remember every detail on each of these
views. Later in this course you will be presented each view again when it will help
your understanding of a subject.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.16

1.16

DICTIONARY View...
¾ Display views that start with “DBA_TAB”
SQL>
SQL> select
select table_name,
table_name, comments
comments
22 from dictionary
from dictionary
33 where
where table_name
table_name like
like 'DBA_TAB%'
'DBA_TAB%'
44 //
Use upper-case!
TABLE_NAME
TABLE_NAME COMMENTS
COMMENTS
-------------------------
------------------------- ------------------------------
------------------------------
DBA_TABLES
DBA_TABLES Description
Description of
of all
all relational
relational
tables in the database
tables in the database
DBA_TABLESPACES
DBA_TABLESPACES Description
Description ofof all
all tablespaces
tablespaces
DBA_TAB_COLS
DBA_TAB_COLS Columns
Columns of
of user's
user's tables,
tables,
views
views and
and clusters
clusters

© 2004 SkillBuilders, Inc.

The DICTIONARY view contains the name of all dictionary views that are available
to you (based on privileges granted). It contains two columns, TABLE_NAME and
COMMENTS. The COMMENTS column usually contains a descriptive comment
about what the view contains. Unfortunately, sometimes the comment is missing or
uninformative.
Since there are so many data dictionary views, it is sometimes difficult to find the
view with the information that you need. So if you need information about tables,
you could use a query like the one shown above to list all dictionary views
containing “DBA_TAB”. Use abbreviations, i.e. “DBA_TAB” instead of
“DBA_TABLES”, because many data dictionary views use abbreviations in their
names.
Reminder
Note that we have coded DBA_TAB in upper case. This is because all object
names in the data dictionary are stored in upper case. If you a used lower case
string, e.g. ‘dba_tab’, no rows would be found.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.17

1.17

...DICTIONARY View
¾ Create useful script to find dictionary tables:
col
col comments
comments format
format a30
a30 word_wrapped
word_wrapped
col
col table_name
table_name format a25
format a25 word_wrapped
word_wrapped

select
select **
from
from dictionary
dictionary
where
where table_name
table_name like
like upper('&1')
upper('&1')
order by table_name;
order by table_name;

undefine
undefine 11

¾ Execute with
SQL>
SQL> @dict
@dict dba_%priv%
dba_%priv%

© 2004 SkillBuilders, Inc.

Since finding data dictionary views is a frequent task, I keep a script handy that
assists me. This script, shown above, searches the dictionary view for views that
contain the string I provide as an argument.
For example, say I was interested in finding all DBA-level views related to
privileges. I would enter:
SQL> @dict dba_%priv%

TABLE_NAME COMMENTS
------------------------- ------------------------------
DBA_SYS_PRIVS System privileges granted to
users and roles

DBA_TAB_PRIVS All grants on objects in the


database

This script (DICT.SQL) is available with your supplied scripts.

© 2004 SkillBuilders, Inc. V2.0


Oracle Data Dictionary 1.18

1.18

Data Dictionary Workshop


¾ Data Dictionary

© 2004 SkillBuilders, Inc.

Data Dictionary Workshop


1. Connect to SQL*Plus with your DBA user (or the SYSTEM user).
2. Display the username and account status of all users on your database.
3. Display the name and status of all tablespaces on your database.
4. For each data file on your database, display the tablespace name, file name,
number of bytes used and number of blocks used. If you are familiar with the
SQL*Plus BREAK and COMPUTE commands, display the sum of the BYTES
column - this is the size of your database (excluding logs, control files and
parameter files).
5. Use a V$ view to determine the name of the database you are connected to.
6. Use a V$ view to determine the name of the instance you are connected to.
7. What is the current value of the BACKGROUND_DUMP_DEST parameter?
8. Locate the alert log and view its contents.

© 2004 SkillBuilders, Inc. V2.0

You might also like