There are four major groups of data dictionary views. The easiest way to identify them is by the prefix: USER , ALL , DBA, or V$ . In general, the USER, ALL, and DBA views have pretty much the same information. USER views can be seen as a subset of ALL views, and ALL views as a subset of DBA views. V$ views contain dynamically changing information, such as system usage, and they are usually of interest only to DBAs. To be perfectly clear, the DBA views provide information about the entire database: all users, all tables. ALL views return information about all the objects that a particular user can access in all schemas in the entire database ”but they are still user-oriented. And USER views provide information on only the particular schema that each user owns. For example, if you requested a list of all the objects for a typical user ” SQL> SELECT OBJECT_NAME FROM USER_OBJECTS ”maybe only a couple or a dozen or two dozen objects would be returned. If the user were a developer, however, you might get hundreds or thousands. Make the same request for ALL_OBJECTS , and Oracle will return thousands of objects, reflecting all the data dictionary views, system tables and indexes, and so forth that a user automatically can access. Log on as a DBA, and even more objects will be returned if you make the same request for DBA_OBJECTS . Is this clear? Good. It's just a hierarchical way of looking at the data. Table 10.1 summarizes the different groups of data dictionary views. Table 10.1. The Four Major Groups of Data Dictionary Views
Although thousands of views are available, you will use just a small set of them most frequently. Let's cover these commonly used views first. What follows is a collection of the most frequently asked questions, along with answers that reveal the views in this category. You may want to consider starting your own list of such questions as the beginning of your own data dictionary cheat sheet:
Note A nickname for dictionary is dict ”for example: select * from DICT;
Note There is a nickname (actually, it's a public synonym) for USER tables. Just use TABS , as in SQL>SELECT * FROM TABS (Another nickname, TAB , returns only three columns of information about a table, unlike the 26 columns that TABS returns.)
What's in USER_USERS? There are eight columns of information:
Note Let's review what synonyms, or public synonyms, are. (We talked about these in Chapter 8, but the idea is worth repeating.) We can create alternate names for objects, either to hide the underlying name , especially for a table, or to make the name easier for users to handle. Such a name is called a synonym . It just makes life easier to use the nicknames. If you're a purist, go ahead and use the full, original names. The SELECT commands will work no matter which name you use! (As long as you have the security rights. That is, if you have rights to the synonym but not the real table name, you cannot do a SELECT using the real table name. You'll get the "Object xxx DOES NOT EXIST" error message.)
Tip Given that the data dictionary will have the data for well over 10,000 tables and views ”more likely close to 20,000 ”it is strongly advised that you qualify any SELECT * statement. Otherwise, you should have spooling turned on and be prepared to spend hours searching a large file to find what you need. Instead, use qualifiers or exact names to reduce the number of rows returned ”for example, SQL>SELECT * FROM ALL_TABLES WHERE TABLE_NAME LIKE " WAIVER%"; Just be careful. The table names are case-sensitive , so put them in UPPERCASE or you won't get anything back.
Note DBA tables can be seen only by users who have the "SELECT ANY TABLE" privilege.
In the preceding Q&A section, I have given you a quick list of the data dictionary views that you will use the most. As mentioned already, there are many, many more. Let's take a look at some of them. My approach to showing you more of the data dictionary is to follow what we have already done. We have:
The next steps will be to show you the the power of the data dictionary with respect to these tasks . When you need to see what you have done, or when a user complains about a security or space error, how can you track it down quickly? In other words, how do you find this information after the fact? But wait, we're not done! We're also going to take a look at:
1 TablesStart with the basic view, ALL_TABLES. Quite a bit of information is returned from the command SQL>DESC ALL_TABLES :
2 ConstraintsTo find the constraints for a table, use the command SQL>DESC_ALL_CONSTRAINTS :
To look at any constraints on a particular column in a table, use the ALL_CONS_COLUMNS view:
3 Primary KeysTo find the primary key for a table, use the command SQL>DESC ALL_CONSTRAINTS to get the ALL_CONSTRAINTS view, and look at Constraint_Type. Here's an example:
Notice that the type is P, for primary key . If this were a foreign key, which we have in our waiver tables, there would be something in the columns R_Owner and R_Constraint_Name (see the complete list of columns in the ALL_CONSTRAINTS view in the preceding section). Now how would you find all the columns involved with a primary key if the primary key were made up of several columns? There are two steps: (1) run the ALL_CONSTRAINTS view (you've already done this); (2) take the information for Constraint_Name and run the view ALL_CONS_COLUMNS, using Constraint_Name: Select COLUMN_NAME from USER_CONS_COLUMNS Where CONSTRAINT_NAME = 'SYS_C0021715'; This command will return any columns that are part of the primary key. 4 ProfilesTo show the user's profile, you have to use a DBA view ”DBA_USERS:
Notice that this view gives you the profile name only. To see what the profile actually is, use the DBA_PROFILES view. The command is: SQL> select resource_name, Limit from dba_profiles 2 where profile = 'WAIVER_DEV_PROFILE';
See how this view will show you all the limits for the particular profile? Note Remember that to see the DBA views, you must have the "SELECT ALL TABLES" privilege! 5 RolesTo find out what roles exist, use the DBA_ROLES view:
Then use the view DBA_ROLE_PRIVS to get some more details about a particular role:
Notice that this view tells you who granted the role, and whether it was granted with the powerful Admin_Option. Well and good, you may be thinking, but the roles we created have lots of other information about things like number of sessions and so forth. Where do we find that information? It's not easy. Unfortunately, no single view shows you every privilege for a role. But since you've gotten this far in the book, you're obviously stubborn enough to stick with me as I show you how to get the final details. First understand that there are really three kinds of grants:
Each of these grants has its own view, as you might have suspected. I told you this would be a little complicated! Let's go. For grants about tables and columns, use the ROLE_TAB_PRIVS view:
For grants about any system privileges, use the ROLE_SYS_PRIVS view:
And for information about grants that give roles to other roles, use the ROLE_ROLE_PRIVS view:
Finally, to find out which roles a particular user has, use the USER_ROLE_PRIVS view:
6 UsersLet's start with basic user information. The most basic data dictionary view for users is USER_USERS:
Although this data dictionary view doesn't give much information, you can use it to:
To see which, if any, system privileges a user may have, use the USER_SYS_ PRIVS view:
And to find all the tables, views, and so forth that the user owns, use the USER_CATALOG view:
For user information, Table 10.2 gives a good synopsis of the available data dictionary views. Table 10.2. USER Data Dictionary Views
7 LimitsThis one is also not simple. You have to look at various views to get all the limits that have been set for the user. Some of the more common data dictionary views to check the limits are SESSION_PRIVS and SESSION_ROLES. These views describe all the privileges and roles that are available to the current session. 8 TablespacesFinding the tablespaces is easy. Just use the USER_TABLESPACES view:
To find the quotas for a user's tablespaces, use the USER_TS_QUOTAS view:
Note Don't forget: Most of these views can be run as USER, ALL, or DBA views. 9 AuditingRemember the short paragraph on auditing in Chapter 8? Well, as you have probably guessed, there is a plain, standard audit trail that anyone can look at, and three very complete views are available. Each of these views can be run as USER or DBA views. To follow the audit trail, you must have the auditing feature turned on for your database. Normally auditing is done for three types of things, through three different views:
Anyone can look at the USER_AUDIT_TRAIL view. This view has 30 columns that include information about all three types of auditing. Confused? I suggest that you consult the Oracle documentation when you need to use the auditing feature. 10 V$ TablesThe last stop on the Data Dictionary Express is to give you some information on the tables that are used to show how the system is performing and to provide a sundry list of statistics. As I mentioned earlier, V$ views are normally used by DBAs. Here are the most common V$ views:
You get the idea. Along with information on users, tables, rights, roles, and so forth, Oracle keeps real-time information on system performance. Again I encourage you to search the Oracle documentation for more information. This short discussion on the V$ tables is meant simply to make you aware that they exist. There are many more tables in the data dictionary. Some, such as USER_SOURCE, list any functions, packages, or procedures that you may create. Others show details on space usage, extents, and so forth. Don't be afraid to take a look at the "DICT" view and see what's out there. My goal in covering the basics of the data dictionary was to make you aware of this very powerful reference tool. In the Q&A section of this chapter I tried to start you on your way to your own data dictionary "cheat sheet" by showing you some of the most common commands. You can use the Q&A section as a starting point and add to it as you need to. You will find that your use of the data dictionary will change depending on what you're doing ”working with user accounts, creating roles, programming, and so forth. Just remember that many of the commands come with a USER_ , ALL_ , or DBA_ prefix, and you'll be in good shape. If you try one that doesn't seem to work, then run a DESCRIBE operation on it, and if that doesn't work, look for it in the data dictionary by using the following command: SQL>SELECT * FROM DICT WHERE TABLE_NAME LIKE '% what you're looking for% '; Good hunting! One Last Thought Now that you have worked a bit with the data dictionary, don't you think it makes sense to develop naming standards ? For example, in our waiver database, our tables have plain English names ”AUTHORS, QUESTIONS, and so forth. However, when you start developing systems with hundreds or thousands of tables, you will quickly run out of simple names. Hence I suggest that you develop a seven-character naming schema that will fit all the applications you develop. The first letter would stand for the general area ”F for finance, E for education, P for payroll, and so forth. Then the remaining characters would be used to refine the name. What does this gain for you? You immediately have a shortcut for the data dictionary. If you wanted to find all the tables associated with education, you could run your data dictionary query to find all tables that begin with E . Here's an example of a fairly standard naming convention:
Here's how it would work. Let's take finance as an example because that's an easy area to understand. The database object FAQBUDG translates into:
If you wanted all the objects that had to do with the Accounts Payable area, you could query the data dictionary by looking for tables beginning with FA% . See how easy it becomes to navigate through the data dictionary when you have a solid naming convention? It will take time to develop, but it is well worth it in the long run. OK, One More I don't want to leave you with the idea that the data dictionary contains magic wands that will easily answer all your questions. Actually, quite the opposite . In many cases you will have to work to get all the answers you need. As I have discussed, it is somewhat difficult to get all the information you need for something as common as roles. And one real shortcoming is that Oracle has no way to provide you with your original statements creating the roles. So let me leave you with a question: What is Oracle Designer, and what does repository mean? (Hint: Oracle Designer, one of the Oracle development tools, is a general-purpose modeling tool used to design and document databases. Although there is work to be done to maintain the information, the payback is having complete and accurate reference materials ”more complete and organized than in the data dictionary. When you decide to create a database or are assigned to a project that must create a database, I strongly advise that you use either Oracle Designer or another such tool.) |