Basics of Data Dictionary
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
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
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',-
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;
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
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;
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;