Columns in the ALL_SYNONYMS View Column Contents OWNER The owner of the synonym.. Columns in the ALL_TAB_PRIVS View Column Contents GRANTOR The username of the person who granted the pri
Trang 2Page 192
Table 6-9 Columns in the ALL_TRIGGER_COLS View (continued)
TABLE_OWNER The owner of the table on which the trigger is defined Usually this is the same as the trigger
owner, but it doesn't have to be.
TABLE_NAME The name of the table on which the trigger is defined.
COLUMN_NAME The name of a column in the table that is used within the trigger, either within the trigger's
specification, or within the PL/SQL block executed by the trigger.
COLUMN_LIST Either YES or NO, depending on whether or not the firing of the trigger is dependent on this
column being updated This is only applicable to UPDATE triggers A value of YES indicates that the column forms part of the column list following the keyword UPDATE in the CREATE TRIGGER statement.
COLUMN_USAGE
This tells you how the column is used within the trigger It will contain some combination of the following strings, separated by one space:NEW: The new version of the column is referenced
OLD: The old version of the column is referenced
IN: The column is referenced, but not updated
OUT: The column's value is set, but never read
IN OUT: The column's value is both referenced and updated.
For example, a value of NEW IN OUT tells you that the trigger both sets the value of the column (OUT) and reads the value of te column (IN) It also tells you that it is the new version (NEW) of the column that is used.
The ALL_SYNONYMS view
The ALL_SYNONYMS view returns one row for each synonym you own and one for each public synonym Table 6-10
describes the columns in this view.
Table 6-10 Columns in the ALL_SYNONYMS View
Column Contents
OWNER The owner of the synonym The owner name will be PUBLIC for public synonyms, which
are accessibe by all detabase users.
SYNONYM_NAME The name of the synonym.
TABLE_OWNER The name of the table's owner.
TABLE_NAME The table name to which the synonym points.
DB_LINK If the synonym points to a table in another database instance, this is the database link name
that points to that instance.
The ALL_TAB_PRIVS view
The ALL_TAB_PRIVS view shows privileges granted to others on objects you own It also shows privileges granted to you on
Trang 4Page 193
Table 611 Columns in the ALL_TAB_PRIVS View
Column Contents
GRANTOR The username of the person who granted the privilege.
GRANTEE The username or rolename to which access was granted.
TABLE_SCHEMA The owner of the object.
TABLE_NAME The object's name.
PRIVILEGE The privilege that was granted on the object (e.g., INSERT or DELETE).
GRANTABLE This will be either YES or NO, depending on whether or not the privilege was granted with the
administrative option.
The ALL_COL_PRIVS view
This view only comes into play when you have granted UPDATE privileges to a user, and when you have limited those update privileges
to a specific set of columns This view returns that list of updateable columns, and is described in Table 612.
Table 612 Columns in the ALL_COL_PRIVS_MADE View
Column Contents
GRANTOR The username of the person who granted the privilege.
GRANTEE The username or rolename to which access was granted.
TABLE_SCHEMA The owner of the object.
TABLE_NAME The object's name.
COLUMN_NAME The column name on which the privilege was granted.
PRIVILEGE The privilege that was granted on the object (e.g., INSERT or DELETE).
GRANTABLE This will be either YES or NO, depending on whether or not the privilege was granted with the
administrative option.
Tables
When it comes to looking at a table and its column definitions, there are two data dictionary views you need to be concerned with These views are:
ALL_TABLES
ALL_TAB_COLUMNS
The ALL_TABLES view contains one row for each table You can use this view to get a quick list of tables you own or to which you have been granted some type of access ALL_TABLES has a one-to-many relationship to ALL_TAB_COLUMNS, which contains one record for each column in a table ALL_TAB_COLUMNS is the source for information on column names, datatypes, default values, and
so forth.
Trang 5
Listing Tables You Own
To get a quick list of tables you own, it's easier to use the USER_TABLES view than ALL_TABLES Remember, USER_TABLES shows you only the tables you own To see a list of your tables, simply select the table_name column
SQL> SELECT table_name FROM user_tables;
TABLE_NAME
-
DATA_TYPE_TEST
DT_TEST
EMPLOYEE
PROJECT
PROJECT_HOURS
5 rows selected
To see tables owned by other users, you need to query the ALL_TABLES view Just be sure to qualify your query by specifying the owner's username in the WHERE clause Here's an example that lists all tables owned by the user
SYSTEM:
SELECT table_name
FROM all_tables
WHERE owner = SYSTEM;
You can make things easy on yourself by writing a script to list tables The following listing shows one approach you can take It prompts you for a username, then lists all tables owned by that user The table listing is in alphabetical order
DESCRIPTION
List tables owned by the current user, or owned by a specified user
The user is prompted for an owner name If an owner is not specified,
i.e the user just presses ENTER, then tables are listed for
the current user
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET DEFINE ON
SET HEADING OFF
Ask the user for the owner
ACCEPT username CHAR PROMPT List tables for user:
PROMPT
Set up the title to print the owner
TTITLE LEFT TABLES OWNED BY: owner SKIP 1
COLUMN owner NOPRINT NEW_VALUE owner
List the tables
SELECT DECODE(&&username,NULL,USER,UPPER(&&username)) owner,
indent,
Trang 6
Page 195 table_name
FROM all_tables
WHERE owner = DECODE(&&username,NULL,USER,UPPER(&&username))
ORDER BY table_name;
Clean up: undefine vars, clear columns, change settings back to defaults UNDEFINE username
UNDEFINE owner
COLUMN owner CLEAR
TTITLE OFF
SET HEADING ON
SET FEEDBACK ON
SET VERIFY ON
When you run this script, it will prompt you for a username Then it will list all the tables owned by that user Here's an example:
SQL> @list_tables
List tables for user: jeff
TABLES OWNED BY: jeff
EMPLOYEE
MY_TABLE
PROJECT
PROJECT_HOURS
As you can see, the resulting list shows all the tables owned by the user named JEFF The DECODE function in the
SELECT statement translates any username you enter to uppercase If you were to press ENTER at the username prompt, that same DECODE statement would translate your empty response to your username, so you would see your tables
Listing Column Definitions for a Table
To list the columns in a table, you will almost certainly want to write a script The script shown next offers one possible approach to doing this Some of the expressions, particularly the ones that call the DECODE function, get a bit
complicated I'll explain those after the listing
The LIST_COLUMNS.SQL script
DESCRIPTION
List all the columns for a table, together with
their datatypes, default values, and nullability
INPUTS
Param 1 A table name, optionally qualified
optionally qualified by an owner name
For example: "SYS.ALL_TAB_COLUMNS"
SET ECHO OFF
SET VERIFY OFF
Trang 7
SET FEEDBACK OFF
SET RECSEP OFF
SET HEADING ON
SET PAGESIZE 9999
SET LONG 1000
SET LINESIZE 80
CLEAR COLUMNS
CLEAR BREAKS
TTITLE OFF
BTITLE OFF
Dissect the input argument, and get the owner name and
table name into two seperate substitution variables
The owner name defaults to the current user
DEFINE s_owner_name =
DEFINE s_table_name =
COLUMN owner_name NOPRINT NEW_VALUE s_owner_name
COLUMN table_name NOPRINT NEW_VALUE s_table_name
SELECT
DECODE(INSTR(&&1,.),
0,USER, /*Default to current user.*
UPPER(SUBSTR(&&1,1,INSTR(&&1,.)-1))) owne
DECODE(INSTR(&&1,.),
0,UPPER(&&1), /*Only the table name was passed in.*/
UPPER(SUBSTR(&&1,INSTR(&&1,.)+1))) table_name
FROM dual;
Set up the page title, force just one page break by breaking on
the table name column
TTITLE LEFT -
COLUMN LISTING FOR s_owner_name s_table_name -
SKIP 2
BREAK ON table_name SKIP PAGE
Set up the formats and widths for the columns to be displayed
COLUMN column_id NOPRINT
COLUMN table_name NOPRINT
COLUMN column_name FORMAT A30 HEADING Column Name
COLUMN data_type FORMAT A17 HEADING Data Type
COLUMN not_null FORMAT A9 HEADING Nullable?
SELECT table_name,
column_id,
column_name,
DECODE (data_type,
VARCHAR2 , VARCHAR2 ( ¦¦ TO_CHAR(data_length) ¦¦ ),
NVARCHAR2,NVARCHAR2 ( ¦¦ TO_CHAR(data_length) ¦¦ ),
CHAR,CHAR ( ¦¦ TO_CHAR(data_length) ¦¦ ),
NCHAR,NCHAR ( ¦¦ TO_CHAR(data_length) ¦¦ ),
NUMBER,
DECODE (data_precision,
NULL, NUMBER,
NUMBER ( ¦¦ TO_CHAR(data_precision)
¦¦ , ¦¦ TO_CHAR(data_scale) ¦¦ )),
Trang 9
FLOAT,
DECODE (data_precision,
NULL, FLOAT,
FLOAT ( ¦¦ TO_CHAR(data_precision) ¦¦ )),
DATE,DATE,
LONG,LONG,
LONG RAW,LONG RAW,
RAW,RAW ( ¦ ¦ TO_CHAR(data_length) ¦¦ ),
MLSLABEL,MLSLABEL,
ROWID,ROWID,
CLOB,CLOB,
NCLOB,NCLOB,
BLOB,BLOB,
BFILE,BFILE,
data_type ¦¦ ???) data_type,
DECODE (nullable, N,NOT NULL) not_null
FROM all_tab_columns
WHERE table_name = &&s_table_name
AND owner = &&s_owner_name
ORDER BY column_id;
Now list any columns with default values defined
This is done as a separate select because SQL*Plus
versions 8.0.3 and 8.0.4 do not handle null LONG
columns properly
TTITLE SKIP 2 LEFT -
DEFAULT VALUES FOR s_owner_name s_table_name -
SKIP 2
COLUMN default_value HEADING Default Value FORMAT A40 WORD_WRAPPED
SELECT table_name,
column_name,
data_default default_value
FROM all_tab_columns
WHERE table_name = &&s_table_name
AND owner = &&s_owner_name
AND data_default IS NOT NULL
ORDER BY column_id;
Clean up by undefining variables, clearing break and
column definitions, and changing all settings back to
their defaults
UNDEFINE s_owner_name
UNDEFINE s_table_name
CLEAR BREAKS
CLEAR COLUMNS
TTITLE OFF
SET ECHO OFF
SET VERIFY ON
SET FEEDBACK ON
SET RECSEP WRAPPED
SET HEADING ON