Vous êtes sur la page 1sur 9

Basics of Data Dictionary

this query selects the TEXT column of DBA_VIEWS SQL> set long 5000 SQL> select text from dba_views where view_name='DBA_VIEWS'; Heres the output: select u.name, o.name, v.textlength, v.text, t.typetextlength, t.typetext, t.oidtextlength, t.oidtext, t.typeowner, t.typename, decode(bitand(v.property, 134217728), 134217728, (select sv.name from superobj$ h, "_CURRENT_EDITION_OBJ" sv where h.subobj# = o.obj# and h.superobj# = sv.obj#), null), decode(bitand(v.property, 32), 32, 'Y', 'N'), decode(bitand(v.property, 16384), 16384, 'Y', 'N') from sys."_CURRENT_EDITION_OBJ" o, sys.view$ v, sys.user$ u, sys.typed_view$ t where o.obj# = v.obj# and o.obj# = t.obj#(+) and o.owner# = u.user# this query displays the definition of the V$CONTROL_FILE select view_definition from v$fixed_view_definition where view_name='V$CONTROLFILE';

select table_name ,comments from dictionary where table_name like '%MV%'; Very Important Views : V$DATABASE V$INSTANCE DBA/ALL/USER_USERS DBA/USER_TABLESPACES DBA_DATA_FILES DBA/USER_FREE_SPACE V$DATAFILE V$DATAFILE_HEADER DBA/ALL/USER_TABLES DBA/ALL/USER_INDEXES DBA/USER_SEGMENTS DBA/ALL/USER_PART_TABLES DBA/ALL/USER_PART_INDEXES DBA/ALL/USER_TAB_PARTITIONS DBA/ALL/USER_IND_PARTITIONS DBA/USER_EXTENTS V$CONTROLFILE V$LOG V$LOG_HISTORY V$ARCHIVED_LOG

Basics of Data Dictionary


select count(*) ,username from v$session group by username; OSUSER, SQL_ID, PROCESS, MACHINE, PORT, TERMINAL, and PROGRAM

If you want to view SQL statements that currently connected users are running, issue this query: select a.sid ,a.username ,b.sql_text from v$session a ,v$sqltext_with_newlines b where a.sql_id = b.sql_id order by a.username ,a.sid ,b.piece; If you want to view SQL statements that currently connected users are running, issue this query: select a.sid ,a.username ,b.sql_text from v$session a ,v$sqltext_with_newlines b where a.sql_id = b.sql_id order by a.username ,a.sid ,b.piece;

The following displays information such as when each account was created, default and temporary tablespaces, and status: set lines 132 col username form a15 col default_tablespace form a18 col temporary_tablespace form a20 col account_status form a16 -select username ,default_tablespace ,temporary_tablespace ,account_status ,created ,lock_date from dba_users order by 1;

You can query the USER_TABLES view to display tables owned by the currently connected user: select

Basics of Data Dictionary


a.table_name ,b.created ,b.last_ddl_time ,a.last_analyzed from user_tables a, user_objects b where a.table_name = b.object_name;

When youre troubleshooting, you can check columns like CREATED and LAST_DDL_TIME, which tell when the structure of a table was last modified. Use the following query to view this information: select a.table_name ,b.created ,b.last_ddl_time ,a.last_analyzed from dba_tables a ,dba_objects b where a.table_name = b.object_name and a.owner = upper('&owner');

The next query is useful when you want to view the space consumption of objects for a user: UNDEFINE owner COL summer FORM 999,999.999 SET LINES 132 TRIMSPOOL ON PAGES 100 SPO space.txt
CHAPTER 10 DATA DICTIONARY BASICS

225
SELECT segment_name ,partition_name ,tablespace_name ,segment_type ,SUM(bytes)/1024/1024 summer FROM dba_extents WHERE owner = UPPER('&&owner') GROUP BY segment_name,partition_name,tablespace_name,segment_type ORDER BY segment_name,partition_name; SPO OFF; When youre investigating performance or space issues, its useful to display each tables row count. Run the following SQL code as a DBA-privileged schema. Notice that this script contains SQL*Plus-specific commands such as UNDEFINE and SPOOL. The script prompts you each time for a username: UNDEFINE user SPOOL tabcount_&&user..sql SET LINESIZE 132 PAGESIZE 0 TRIMSPO OFF VERIFY OFF FEED OFF TERM OFF SELECT 'SELECT RPAD(' || '''' || table_name || '''' ||',30)' || ',' || ' COUNT(*) FROM &&user..' || table_name || ';' FROM dba_tables WHERE owner = UPPER('&&user') ORDER BY 1; SPO OFF; SET TERM ON @@tabcount_&&user..sql

If you want to generate statistics for a table, use the DBMS_STATS package. This example generates statistics for a user and a table:
SQL> exec dbms_stats.gather_table_stats(ownname=>'INV',tabname=>'F_SALES',-

Basics of Data Dictionary


cascade=>true,estimate_percent=>20,degree=>4);

You can generate statistics for all objects for a user with the following code:
SQL> exec dbms_stats.gather_schema_stats(ownname => 'INV',estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,degree => DBMS_STATS.AUTO_DEGREE,cascade => true); If you have partitioned tables and want to show row counts by partition, use the next few lines of SQL and PL/SQL code: UNDEFINE user SET SERVEROUT ON SIZE 1000000 VERIFY OFF SPO part_count_&&user..txt DECLARE counter NUMBER; sql_stmt VARCHAR2(1000); CURSOR c1 IS SELECT table_name, partition_name FROM dba_tab_partitions WHERE table_owner = UPPER('&&user'); BEGIN FOR r1 IN c1 LOOP sql_stmt := 'SELECT COUNT(*) FROM &&user..' || r1.table_name ||' PARTITION ( '||r1.partition_name ||' )'; EXECUTE IMMEDIATE sql_stmt INTO counter; DBMS_OUTPUT.PUT_LINE(RPAD(r1.table_name ||'('||r1.partition_name||')',30) ||' '||TO_CHAR(counter)); END LOOP; END; / SPO OFF select object_name ,object_type from user_objects where object_name=upper('&object_name'); You can query the DBA_CONSTRAINTS view to display constraint information for an owner and table name. The following script prompts you for two SQL*Plus ampersand variables (OWNER and TABLE_NAME); if you arent using SQL*Plus, then you may need to modify the script with the appropriate values before you run the script: select table_name ,(case constraint_type when 'P' then 'Primary Key' when 'R' then 'Foreign Key' when 'C' then 'Check' when 'U' then 'Unique' when 'O' then 'Read Only View' when 'V' then 'Check view' when 'H' then 'Hash expression' when 'F' then 'REF column' when 'S' then 'Supplemental logging' end) cons_type ,constraint_name cons_name ,search_condition check_cons ,status from dba_constraints where owner like upper('&owner') and table_name like upper('&table_name') order by cons_type;

Basics of Data Dictionary


The following script queries the DBA_CONSTRAINTS view to determine the parent primary-key constraints that are related to child foreign-key constraints. You need to provide as input to the script the owner of the table and the child table for which you wish to display primary-key constraints: select a.constraint_type cons_type ,a.table_name child_table ,a.constraint_name child_cons ,b.table_name parent_table ,b.constraint_name parent_cons ,b.constraint_type cons_type from dba_constraints a ,dba_constraints b where a.owner = upper('&owner') and a.table_name = upper('&table_name') and a.constraint_type = 'R' and a.r_owner = b.owner and a.r_constraint_name = b.constraint_name; The next script takes the primary-key record and looks to see if it has any child records that have a constraint type of R. When you run this script, youre prompted for the primary-key table owner and name: select b.table_name primary_key_table ,a.table_name fk_child_table ,a.constraint_name fk_child_table_constraint from dba_constraints a ,dba_constraints b where a.r_constraint_name = b.constraint_name and a.r_owner = b.owner and a.constraint_type = 'R' and b.owner = upper('&table_owner') and b.table_name = upper('&table_name');

Use this query to view which roles are granted to the currently connected user: select username ,granted_role from user_role_privs; The next query displays the roles that have been granted to a specific user (you're prompted for GRANTEE): select grantee ,granted_role from dba_role_privs where grantee = upper('&grantee') order by grantee; The USER_ROLE_PRIVS and DBA_ROLE_PRIVS views describe roles granted to users. To display roles granted to roles, query the ROLE_ROLE_PRIVS view: select role ,granted_role from role_role_privs; When you create a database, several predefined roles are created for you, including DBA and SELECT_CATALOG_ROLE. To view all the roles in your database (both predefined and user-created), select the ROLE column from DBA_ROLES: select role from dba_roles; Query the DBA_SYS_PRIVS view to display which system privileges have been granted to users. Listed

Basics of Data Dictionary


next is a simple script that prompts for the GRANTEE: select grantee ,privilege ,admin_option from dba_sys_privs where grantee = UPPER('&grantee') order by privilege; The following query displays system privileges granted either directly to the currently connected user or through any roles granted to the user: select privilege ,'DIRECT GRANT' from user_sys_privs union select privilege ,'ROLE GRANT' from role_sys_privs; DBA_ROLES DBA_ROLE_PRIVS DBA_SYS_PRIVS DBA_TAB_PRIVS DBA_COL_PRIVS ROLE_ROLE_PRIVS ROLE_SYS_PRIVS ROLE_TAB_PRIVS ALL_TAB_PRIVS ALL_TAB_PRIVS_MADE ALL_TAB_PRIVS_RECD ALL_COL_PRIVS ALL_COL_PRIVS_MADE ALL_COL_PRIVS_RECD USER_ROLE_PRIVS USER_SYS_PRIVS USER_TAB_PRIVS USER_TAB_PRIVS_MADE USER_TAB_PRIVS_RECD USER_COL_PRIVS USER_COL_PRIVS_MADE USER_COL_PRIVS_RECD The following query selects from the USER_TAB_PRIVS_RECD view to display the table privileges that have been granted to the currently connected user: select owner ,table_name ,grantor ,privilege from user_tab_privs_recd; To view privileges that the current user has granted to other users, select from the USER_TAB_PRIVS_MADE view: select grantee ,table_name ,grantor ,privilege from user_tab_privs_made; Run the following query to view table privileges that have been granted to your current user:

Basics of Data Dictionary


select grantee, table_name, privilege from user_tab_privs where grantee = sys_context('USERENV','CURRENT_USER') order by table_name, privilege; The query can alternatively prompt you for your current username. For example: select grantee, table_name, privilege from user_tab_privs where grantee = UPPER('&your_user_name') order by table_name, privilege; This next query selects from USER_TAB_PRIVS and ROLE_TAB_PRIVS to check for any object privileges that have been granted directly to the user or granted through a role that has been granted to the user: select grantee ,owner ,table_name ,grantor ,privilege from user_tab_privs union select role ,owner ,table_name ,'ROLE' ,privilege from role_tab_privs order by 2, 3; You can use the DBA_DEPENDENCIES view to display object dependencies. The following query prompts you for a username and an object name: select '+' || lpad(' ',level+2) || type || ' ' || owner || '.' || name dep_tree from dba_dependencies connect by prior owner = referenced_owner and prior name = referenced_name and prior type = referenced_type start with referenced_owner = upper('&object_owner') and referenced_name = upper('&object_name') and owner is not null; If you want to inspect every object in a schema, you can use SQL to generate SQL to create scripts that display all dependencies for a schemas objects. The next section of code does that. For formatting and output, it uses some constructs specific to SQL*Plus, such as setting the page sizes and line size and spooling the output: UNDEFINE owner SET LINESIZE 132 PAGESIZE 0 VERIFY OFF FEEDBACK OFF TIMING OFF SPO dep_dyn_&&owner..sql SELECT 'SPO dep_dyn_&&owner..txt' FROM DUAL; -SELECT 'PROMPT ' || '_____________________________'|| CHR(10) || 'PROMPT ' || object_type || ': ' || object_name || CHR(10) || 'SELECT ' || '''' || '+' || '''' || ' ' || '|| LPAD(' || '''' || ' ' || '''' || ',level+3)' || CHR(10) || ' || type || ' || '''' || ' ' || '''' || ' || owner || ' || '''' || '.' || '''' || ' || name' || CHR(10) || ' FROM dba_dependencies ' || CHR(10) || ' CONNECT BY PRIOR owner = referenced_owner AND prior name = referenced_name ' || CHR(10) || ' AND prior type = referenced_type ' || CHR(10) || ' START WITH referenced_owner = ' || '''' || UPPER('&&owner') || '''' || CHR(10) || ' AND referenced_name = ' || '''' || object_name || '''' || CHR(10) ||

Basics of Data Dictionary


' AND owner IS NOT NULL;' FROM dba_objects WHERE owner = UPPER('&&owner') AND object_type NOT IN ('INDEX','INDEX PARTITION','TABLE PARTITION'); -SELECT 'SPO OFF' FROM dual; SPO OFF SET VERIFY ON LINESIZE 80 FEEDBACK ON Listed next is a more complex example of comparing two schemas objects. The following script compares several data-dictionary views for differences in metadata: spo diff.txt prompt Default or temp tablespace in db1 NOT IN db2 select default_tablespace, temporary_tablespace from user_users&&conn1 minus select default_tablespace, temporary_tablespace from user_users&&conn2;
CHAPTER 10 DATA DICTIONARY BASICS

240
prompt Default or temp tablespace in db2 NOT IN db1 select default_tablespace, temporary_tablespace from user_users&&conn2 minus select default_tablespace, temporary_tablespace from user_users&&conn1; prompt Tablespace quotas in db1 NOT IN db2 select tablespace_name, max_bytes from user_ts_quotas&&conn1 minus select tablespace_name, max_bytes from user_ts_quotas&&conn2; prompt Tablespace quotas in db2 NOT IN db1 select tablespace_name, max_bytes from user_ts_quotas&&conn2 minus select tablespace_name, max_bytes from user_ts_quotas&&conn1; prompt Objects in db1 NOT IN db2 select object_name, object_type from user_objects&&conn1 minus select object_name, object_type from user_objects&&conn2 order by 2; prompt Objects in db2 NOT IN db1 select object_name, object_type from user_objects&&conn2 minus select object_name, object_type from user_objects&&conn1 order by 2; prompt Tables in db1 NOT IN db2 select table_name from user_tables&&conn1 minus select table_name from user_tables&&conn2; prompt Tables in db2 NOT IN db1 select table_name from user_tables&&conn2 minus select table_name from user_tables&&conn1;

Basics of Data Dictionary


prompt Indexes in db2 NOT IN db1 select table_name, index_name, index_type, uniqueness from user_indexes&&conn2 minus select table_name, index_name, index_type, uniqueness
CHAPTER 10 DATA DICTIONARY BASICS

241
from user_indexes&&conn1 order by 1, 2; prompt Table columns db1 NOT IN db2 select table_name, column_name from user_tab_columns&&conn1 minus select table_name, column_name from user_tab_columns&&conn2 order by 1,2; prompt Table columns in db2 NOT IN db1 select table_name, column_name from user_tab_columns&&conn2 minus select table_name, column_name from user_tab_columns&&conn1 order by 1,2; spo off;

Vous aimerez peut-être aussi