Basics of Data Dictionary

11
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'; Here’s 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

Transcript of Basics of Data Dictionary

Page 1: Basics of Data Dictionary

Basics of Data Dictionary

this query selects the TEXT column of DBA_VIEWS

SQL> set long 5000SQL> select text from dba_views where view_name='DBA_VIEWS';Here’s 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" svwhere 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$ twhere o.obj# = v.obj#and o.obj# = t.obj#(+)and o.owner# = u.user#

this query displays the definition of the V$CONTROL_FILE

selectview_definitionfrom v$fixed_view_definitionwhere view_name='V$CONTROLFILE';

selecttable_name,commentsfrom dictionary where table_name like '%MV%';

Very Important Views :

V$DATABASEV$INSTANCEDBA/ALL/USER_USERSDBA/USER_TABLESPACESDBA_DATA_FILESDBA/USER_FREE_SPACEV$DATAFILEV$DATAFILE_HEADERDBA/ALL/USER_TABLESDBA/ALL/USER_INDEXESDBA/USER_SEGMENTSDBA/ALL/USER_PART_TABLESDBA/ALL/USER_PART_INDEXESDBA/ALL/USER_TAB_PARTITIONSDBA/ALL/USER_IND_PARTITIONSDBA/USER_EXTENTSV$CONTROLFILEV$LOGV$LOG_HISTORYV$ARCHIVED_LOG

Page 2: Basics of Data Dictionary

Basics of Data Dictionary

selectcount(*),usernamefrom v$sessiongroup 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:selecta.sid,a.username,b.sql_textfrom v$session a,v$sqltext_with_newlines bwhere a.sql_id = b.sql_idorder bya.username,a.sid,b.piece;

If you want to view SQL statements that currently connected users are running, issue this query:selecta.sid,a.username,b.sql_textfrom v$session a,v$sqltext_with_newlines bwhere a.sql_id = b.sql_idorder bya.username,a.sid,b.piece;

The following displays information such as when each account wascreated, default and temporary tablespaces, and status:set lines 132col username form a15col default_tablespace form a18col temporary_tablespace form a20col account_status form a16--selectusername,default_tablespace,temporary_tablespace,account_status,created,lock_datefrom dba_usersorder by 1;

You can query the USER_TABLES view to display tables owned by the currentlyconnected user:

Page 3: Basics of Data Dictionary

Basics of Data Dictionary

selecta.table_name,b.created,b.last_ddl_time,a.last_analyzedfrom user_tables a, user_objects bwhere a.table_name = b.object_name;

When you’re troubleshooting, you can check columns like CREATED and LAST_DDL_TIME, whichtell when the structure of a table was last modified. Use the following query to view this information:selecta.table_name,b.created,b.last_ddl_time,a.last_analyzedfrom dba_tables a,dba_objects bwhere a.table_name = b.object_nameand a.owner = upper('&owner');

The next query is useful when you want to view the space consumption ofobjects for a user:UNDEFINE ownerCOL summer FORM 999,999.999SET LINES 132 TRIMSPOOL ON PAGES 100SPO space.txtCHAPTER 10 ■ DATA DICTIONARY BASICS225SELECTsegment_name,partition_name,tablespace_name,segment_type,SUM(bytes)/1024/1024 summerFROM dba_extentsWHERE owner = UPPER('&&owner')GROUP BY segment_name,partition_name,tablespace_name,segment_typeORDER BY segment_name,partition_name;SPO OFF;

When you’re investigating performance or space issues, it’s useful to display each table’s row count. Runthe following SQL code as a DBA-privileged schema. Notice that this script contains SQL*Plus-specificcommands such as UNDEFINE and SPOOL. The script prompts you each time for a username:UNDEFINE userSPOOL tabcount_&&user..sqlSET LINESIZE 132 PAGESIZE 0 TRIMSPO OFF VERIFY OFF FEED OFF TERM OFFSELECT'SELECT RPAD(' || '''' || table_name || '''' ||',30)'|| ',' || ' COUNT(*) FROM &&user..' || table_name || ';'FROM dba_tablesWHERE owner = UPPER('&&user')ORDER BY 1;SPO OFF;SET TERM ON@@tabcount_&&user..sql

Page 4: Basics of Data Dictionary

Basics of Data Dictionary

If you want to generate statistics for a table, use the DBMS_STATS package. This example generatesstatistics for a user and a table:SQL> exec dbms_stats.gather_table_stats(ownname=>'INV',-tabname=>'F_SALES',-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 ofSQL and PL/SQL code:UNDEFINE userSET SERVEROUT ON SIZE 1000000 VERIFY OFFSPO part_count_&&user..txtDECLAREcounter NUMBER;sql_stmt VARCHAR2(1000);CURSOR c1 ISSELECT table_name, partition_nameFROM dba_tab_partitionsWHERE table_owner = UPPER('&&user');BEGINFOR r1 IN c1 LOOPsql_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

selectobject_name,object_typefrom user_objectswhere object_name=upper('&object_name');

You can query the DBA_CONSTRAINTS view to displayconstraint information for an owner and table name. The following script prompts you for two SQL*Plusampersand variables (OWNER and TABLE_NAME); if you aren’t using SQL*Plus, then you may need to modifythe script with the appropriate values before you run the script:selecttable_name,(case constraint_typewhen '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

Page 5: Basics of Data Dictionary

Basics of Data Dictionary

,constraint_name cons_name,search_condition check_cons,statusfrom dba_constraintswhere owner like upper('&owner')and table_name like upper('&table_name')order by cons_type;The following script queries the DBA_CONSTRAINTS view to determine the parent primary-keyconstraints that are related to child foreign-key constraints. You need to provide as input to the scriptthe owner of the table and the child table for which you wish to display primary-key constraints:selecta.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_typefrom dba_constraints a,dba_constraints bwhere a.owner = upper('&owner')and a.table_name = upper('&table_name')and a.constraint_type = 'R'and a.r_owner = b.ownerand a.r_constraint_name = b.constraint_name;

The next script takes the primary-key recordand looks to see if it has any child records that have a constraint type of R. When you run this script,you’re prompted for the primary-key table owner and name:selectb.table_name primary_key_table,a.table_name fk_child_table,a.constraint_name fk_child_table_constraintfrom dba_constraints a,dba_constraints bwhere a.r_constraint_name = b.constraint_nameand a.r_owner = b.ownerand 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:selectusername,granted_rolefrom user_role_privs;The next query displays the roles that have been granted to a specific user (you're prompted forGRANTEE):selectgrantee,granted_rolefrom dba_role_privswhere grantee = upper('&grantee')order by grantee;

The USER_ROLE_PRIVS and DBA_ROLE_PRIVS views describe roles granted to users. To display rolesgranted to roles, query the ROLE_ROLE_PRIVS view:selectrole,granted_rolefrom role_role_privs;

Page 6: Basics of Data Dictionary

Basics of Data Dictionary

When you create a database, several predefined roles are created for you, including DBA andSELECT_CATALOG_ROLE. To view all the roles in your database (both predefined and user-created), selectthe ROLE column from DBA_ROLES:selectrolefrom dba_roles;

Query the DBA_SYS_PRIVS view to display which system privileges have been granted to users. Listednext is a simple script that prompts for the GRANTEE:selectgrantee,privilege,admin_optionfrom dba_sys_privswhere grantee = UPPER('&grantee')order by privilege;

The following query displays system privileges granted either directly to thecurrently connected user or through any roles granted to the user:selectprivilege,'DIRECT GRANT'from user_sys_privsunionselectprivilege,'ROLE GRANT'from role_sys_privs;

DBA_ROLESDBA_ROLE_PRIVSDBA_SYS_PRIVSDBA_TAB_PRIVSDBA_COL_PRIVSROLE_ROLE_PRIVSROLE_SYS_PRIVSROLE_TAB_PRIVSALL_TAB_PRIVSALL_TAB_PRIVS_MADEALL_TAB_PRIVS_RECDALL_COL_PRIVSALL_COL_PRIVS_MADEALL_COL_PRIVS_RECDUSER_ROLE_PRIVSUSER_SYS_PRIVSUSER_TAB_PRIVSUSER_TAB_PRIVS_MADEUSER_TAB_PRIVS_RECDUSER_COL_PRIVSUSER_COL_PRIVS_MADEUSER_COL_PRIVS_RECD

The following query selects from the USER_TAB_PRIVS_RECD view to display the tableprivileges that have been granted to the currently connected user:selectowner,table_name,grantor,privilegefrom user_tab_privs_recd;

Page 7: Basics of Data Dictionary

Basics of Data Dictionary

To view privileges that the current user has granted to other users, select from theUSER_TAB_PRIVS_MADE view:selectgrantee,table_name,grantor,privilegefrom user_tab_privs_made;

Run the following query to view table privileges that have been granted to your current user:select grantee, table_name, privilegefrom user_tab_privswhere grantee = sys_context('USERENV','CURRENT_USER')order by table_name, privilege;

The querycan alternatively prompt you for your current username. For example:select grantee, table_name, privilegefrom user_tab_privswhere 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 privilegesthat have been granted directly to the user or granted through a role that has been granted to the user:selectgrantee,owner,table_name,grantor,privilegefrom user_tab_privsunionselectrole,owner,table_name,'ROLE',privilegefrom role_tab_privsorder by 2, 3;

You can use the DBA_DEPENDENCIES view to displayobject dependencies. The following query prompts you for a username and an object name:select '+' || lpad(' ',level+2) || type || ' ' || owner || '.' || name dep_treefrom dba_dependenciesconnect by prior owner = referenced_owner and prior name = referenced_nameand prior type = referenced_typestart with referenced_owner = upper('&object_owner')and referenced_name = upper('&object_name')and owner is not null;

If you want to inspect everyobject in a schema, you can use SQL to generate SQL to create scripts that display all dependencies for aschema’s objects. The next section of code does that. For formatting and output, it uses some constructsspecific to SQL*Plus, such as setting the page sizes and line size and spooling the output:UNDEFINE ownerSET LINESIZE 132 PAGESIZE 0 VERIFY OFF FEEDBACK OFF TIMING OFFSPO dep_dyn_&&owner..sql

Page 8: Basics of Data Dictionary

Basics of Data Dictionary

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) ||' AND owner IS NOT NULL;'FROM dba_objectsWHERE owner = UPPER('&&owner')AND object_type NOT IN ('INDEX','INDEX PARTITION','TABLE PARTITION');--SELECT 'SPO OFF' FROM dual;SPO OFFSET VERIFY ON LINESIZE 80 FEEDBACK ON

Listed next is a more complex example of comparing two schemas’ objects. The following scriptcompares several data-dictionary views for differences in metadata:spo diff.txtprompt Default or temp tablespace in db1 NOT IN db2select default_tablespace, temporary_tablespacefrom user_users&&conn1minusselect default_tablespace, temporary_tablespacefrom user_users&&conn2;CHAPTER 10 ■ DATA DICTIONARY BASICS240prompt Default or temp tablespace in db2 NOT IN db1select default_tablespace, temporary_tablespacefrom user_users&&conn2minusselect default_tablespace, temporary_tablespacefrom user_users&&conn1;prompt Tablespace quotas in db1 NOT IN db2select tablespace_name, max_bytesfrom user_ts_quotas&&conn1minusselect tablespace_name, max_bytesfrom user_ts_quotas&&conn2;prompt Tablespace quotas in db2 NOT IN db1select tablespace_name, max_bytesfrom user_ts_quotas&&conn2minusselect tablespace_name, max_bytesfrom user_ts_quotas&&conn1;prompt Objects in db1 NOT IN db2select object_name, object_typefrom user_objects&&conn1minusselect object_name, object_typefrom user_objects&&conn2 order by 2;prompt Objects in db2 NOT IN db1select object_name, object_typefrom user_objects&&conn2

Page 9: Basics of Data Dictionary

Basics of Data Dictionary

minusselect object_name, object_typefrom user_objects&&conn1 order by 2;prompt Tables in db1 NOT IN db2select table_namefrom user_tables&&conn1minusselect table_namefrom user_tables&&conn2;prompt Tables in db2 NOT IN db1select table_namefrom user_tables&&conn2minusselect table_namefrom user_tables&&conn1;prompt Indexes in db2 NOT IN db1select table_name, index_name, index_type, uniquenessfrom user_indexes&&conn2minusselect table_name, index_name, index_type, uniquenessCHAPTER 10 ■ DATA DICTIONARY BASICS241from user_indexes&&conn1 order by 1, 2;prompt Table columns db1 NOT IN db2select table_name, column_namefrom user_tab_columns&&conn1minusselect table_name, column_namefrom user_tab_columns&&conn2 order by 1,2;prompt Table columns in db2 NOT IN db1select table_name, column_namefrom user_tab_columns&&conn2minusselect table_name, column_namefrom user_tab_columns&&conn1 order by 1,2;spo off;