The Oracle data dictionary is one of the most important components of the Oracle7 Server.. The Oracle data dictionary is one of the most important components of the Oracle7 Server.. Tabl
Trang 1Oracle Data Dictionary
10
Trang 2Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć2
Trang 3The Oracle data dictionary is one of the most important components of the Oracle7 Server It consists of a set of tables and views that provide a read-only reference to the database.
At the end of this lesson, you should be able to
D Describe the data dictionary views a user may access
D Query data from the data dictionary
Trang 4Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć4
Trang 5The Oracle data dictionary is one of the most important components of the Oracle7
Server It is created when a database is created Whenever the database is in
operation, the data dictionary is updated and maintained by the Oracle7 Server Alldata dictionary tables are owned by the SYS user The base tables are rarely accesseddirectly because the information in them is not easy to understand Therefore, userstypically access data dictionary views because the information is presented in aformat that is easy for them to understand
Tables Within the Oracle7 Database
Tables Description
User tables Collection of tables created and maintained by the user,
such as S_EMP, that contain user information
Data dictionary Collection of tables created and maintained by the Oracle7
Server, such as USER_OBJECTS, that contain informationabout the database
Example Data Dictionary Contents
D Names of Oracle7 Server users
D Privileges granted to users
D Database object names (for example, tables, views, and indexes)
D Table constraints
D Auditing information, such as who has accessed or updated specified databaseobjects
Data Dictionary Uses
The data dictionary is a reference for all database users It is a valuable source ofinformation for end users, application designers, and DBAs The data dictionary isalso critical for the operation of the Oracle7 Server because the database relies on thedata dictionary to record and verify information about itself
Trang 6Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć6
Trang 7Querying the Data Dictionary
You can query the data dictionary by issuing a SQL SELECT statement Depending
on your privileges, you can query various views
View Classes
Data dictionary view names reflect their intended use There are four categories ofviews; each category has a distinct prefix
Prefix Description
USER_ Contains objects owned by the user For example, views with this prefix
allow the user to display information about tables created by the user andprivileges granted by the user
ALL_ Accesses objects to which the user has been granted access rights, in
addition to objects owned by the user
DBA_ Allows users with the DBA privilege to access any object in the
database
V$ Displays database server performance and locking Initially available
only to the DBA
Additional Views
Several data dictionary views do not use the prefixes listed above These includesynonyms for views with long names
View Name Description
DICTIONARY Lists all data dictionary tables, views, and synonyms.TABLE_PRIVILEGES Grants on objects for which the user is the grantor,
grantee, or owner
Trang 8Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć8
Trang 9Querying the Data Dictionary continued
Example
The DICTIONARY view lists all data dictionary views accessible to the user with abrief description of the object in a comment column You can also reference thesynonym for the view, DICT
-OBJECT_TYPE VARCHAR2(13)CREATED DATE
LAST_DDL_TIME DATE
TIMESTAMP VARCHAR2(75)STATUS VARCHAR2(7)
3 WHERE table_name = ’USER_OBJECTS’;
For more information, see
Oracle7 Server SQL Language Quick Reference, Release 7.3.
Trang 10Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć10
Trang 11Querying the Data Dictionary continued
Example
Display the types of objects that the user owns
SQL> SELECT DISTINCT object_type
2 FROM user_objects;
Example
You can search the data dictionary for a specific topic by querying the COMMENTScolumn in the DICTIONARY view Find all data dictionary views pertaining to thekeyword Grant
COLUMN table_name FORMAT A20
COLUMN comments FORMAT A30
e user is the grantor, grantee , owner,
or an enabled role or PUBLIC i
s the grantee
ALL_COL_PRIVS_MADE Grants on columns for which th
e user is owner or grantor
ALL_COL_PRIVS_RECD Grants on columns for which th
e user, PUBLIC or enabled role
is the grantee
20 rows selected
Trang 12Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć12
Trang 13Checking Constraints on a Table
After creating a table, you can confirm its existence by issuing a DESCRIBE
command The only constraint you can verify is the NOT NULL constraint To viewall constraints on your table, you can check the USER_CONSTRAINTS table.Example
Confirm the constraints on the S_EMP table
SQL> SELECT constraint_name, constraint_type,
2 search_condition, r_constraint_name
3 FROM user_constraints
4 WHERE table_name = ’S_EMP’;
CONSTRAINT_NAME C SEARCH_CONDITION R_CONSTRAINT_NA - - - - S_EMP_MANAGER_ID_FK R S_EMP_ID_PK
S_EMP_LAST_NAME_NN C LAST_NAME IS NOT NULL
S_EMP_USERID_NN C USERID IS NOT NULL
Trang 14Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć14
Trang 15Checking Constraints on a Table continued
View the names of the columns involved in constraints by querying the
USER_CONS_COLUMNS data dictionary view This view is especially useful forconstraints that use the system-assigned name
Example
Display the column constraint information in the data dictionary for the S_EMP table
SQL> SELECT constraint_name, column_name
Trang 16Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć16
Trang 17The data dictionary is a set of tables that the user can query through views The datadictionary holds all the data about the database Write a SELECT statement to displaythe contents of the data dictionary views
Frequently Accessed Data Dictionary Views
Trang 18Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć18
Trang 19Practice Overview
In this practice, you will access the data dictionary views to verify information aboutyour tables
Practice Contents
D Querying the data dictionary to view table information
D Viewing constraint information from the data dictionary
Trang 20Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć20
Trang 21Note: If you did not complete Practice 9, Exercise 2 and Exercise 3, you can invoke
scripts p9q2.sql and p9q3.sql to create DEPARTMENT table and
EMPLOYEE table respectively
3. Query the USER_OBJECTS data dictionary to see information about the tablesyou created in Practice 9, DEPARTMENT and EMPLOYEE tables
4. Create a script to execute a generic query to confirm the constraints for the tablesyou have created You can use a substitution parameter for the table name Save
the query as p10q4.sql Execute the script to confirm the constraints for the tables
you created in Practice 9, DEPARTMENT and EMPLOYEE tables
Trang 22Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 10Ć22