oracle cheat sheet

6
Table of Contents Getting a list of indexes Getting a cross-reference of tables and their indexes Determining the indexes on a table Getting the names of indexes for a table Determining the columns on which an index is based: Other useful metadata queries Creating a user Changing user’s password Sharing tables between two users Creating a public synonym for a table Granting DBA privileges to a user Creating a rollback segment: If database is hung shutting down or starting up Dumping and Restoring an Oracle Database Dump: Restore Oracle 9.2.0 Intermedia Problems: CTX_DDL must be declared Error during stored procedure definition “Insufficient privilege” error EXPLAIN PLAN and Autotrace Managing statistics for the cost-based optimizer After bringing up database, can’t connect via SQL*Plus. Oracle Cheat Sheet Getting a list of indexes The query SELECT INDEX_NAME FROM DBA_INDEXES will list the names of all non-system indexes. Also, from http://technet.oracle.com/docs/products/oracle8i/doc_index.htm System index views USER_INDEXES, ALL_INDEXES, and DBA_INDEXES indicate bitmap indexes by the word BITMAP appearing in the TYPE column. A bitmap index cannot be declared as UNIQUE. Must have system privileges to issue the query. Getting a cross-reference of tables and their indexes SELECT table_name, index_name FROM dba_indexes WHERE table_name = 'TABLENAME' Must have system privileges to issue the query. Determining the indexes on a table Getting the names of indexes for a table SELECT INDEX_NAME name FROM USER_INDEXES WHERE TABLE_NAME = 'table_name' AND GENERATED = 'N'; Determining the columns on which an index is based: Oracle Cheat Sheet http://software.clapper.org/cheat-sheets/oracle.html#if-database-is-hung-... 1 of 6 5/14/2015 9:20 PM

Upload: aneekshokeen

Post on 25-Sep-2015

9 views

Category:

Documents


5 download

DESCRIPTION

cheat sheet for oracle database.

TRANSCRIPT

  • Table of Contents

    Getting a list of indexes

    Getting a cross-reference of

    tables and their indexes

    Determining the indexes on a

    table

    Getting the names of indexes

    for a table

    Determining the columns on

    which an index is based:

    Other useful metadata queries

    Creating a user

    Changing users password

    Sharing tables between two users

    Creating a public synonym for a

    table

    Granting DBA privileges to a user

    Creating a rollback segment:

    If database is hung shutting down

    or starting up

    Dumping and Restoring an Oracle

    Database

    Dump:

    Restore

    Oracle 9.2.0 Intermedia

    Problems:

    CTX_DDL must be declared

    Error during stored procedure

    definition

    Insufficient privilege error

    EXPLAIN PLAN and Autotrace

    Managing statistics for the

    cost-based optimizer

    After bringing up database, cant

    connect via SQL*Plus.

    Oracle Cheat Sheet

    Getting a list of indexes

    The query

    SELECT INDEX_NAME FROM DBA_INDEXES

    will list the names of all non-system indexes. Also, from

    http://technet.oracle.com/docs/products/oracle8i/doc_index.htm

    System index views USER_INDEXES, ALL_INDEXES, and

    DBA_INDEXES indicate

    bitmap indexes by the word BITMAP appearing in the TYPE

    column. A bitmap

    index cannot be declared as UNIQUE.

    Must have system privileges to issue the query.

    Getting a cross-reference of tables and

    their indexes

    SELECT table_name, index_name FROM dba_indexes WHERE table_name = 'TABLENAME'

    Must have system privileges to issue the query.

    Determining the indexes on a table

    Getting the names of indexes for a table

    SELECT INDEX_NAME name FROM USER_INDEXESWHERE TABLE_NAME = 'table_name' AND GENERATED = 'N';

    Determining the columns on which an index is based:

    Oracle Cheat Sheet http://software.clapper.org/cheat-sheets/oracle.html#if-database-is-hung-...

    1 of 6 5/14/2015 9:20 PM

  • SELECT aic.index_name, aic.column_name, aic.column_position, aic.descend, aic.table_owner, CASE alc.constraint_type WHEN 'U' THEN 'UNIQUE' WHEN 'P' THEN 'PRIMARY KEY' ELSE '' END AS index_type FROM all_ind_columns aicLEFT JOIN all_constraints alc ON aic.index_name = alc.constraint_name AND aic.table_name = alc.table_name AND aic.table_owner = alc.owner WHERE aic.table_name = 'TEST2' -- table name --AND aic.table_owner = 'HR' -- table owner --AND aic.index_name = 'TEST2_FIELD5_IDX' -- index name ORDER BY column_position;

    Other useful metadata queries

    See http://www.alberton.info/oracle_meta_info.html

    Creating a user

    In SQL*Plus, connect as SYSTEM/MANAGER@dbname. Then, issue the followingSQL:

    CREATE USER user IDENTIFIED BY passwordDEFAULT TABLESPACE users TEMPORARY TABLESPACE tempQUOTA UNLIMITED ON users;

    For example:

    CREATE USER user1 IDENTIFIED BY mypasswordDEFAULT TABLESPACE users TEMPORARY TABLESPACE tempQUOTA UNLIMITED ON users;

    Next, grant appropriate privileges to the new user:

    GRANT connect, resource TO user1;

    In SQL*Plus, connect as user1/mypassword@dbname. Create tables, indexes,etc Theyll be owned by the new user.

    Changing users password

    ALTER USER user IDENTIFIED BY password

    Sharing tables between two users

    Oracle Cheat Sheet http://software.clapper.org/cheat-sheets/oracle.html#if-database-is-hung-...

    2 of 6 5/14/2015 9:20 PM

  • Create a second user, user2. (See above) Then, connect as user1, and

    issue this command:

    GRANT insert, update, delete, select on user1.table1 TO user2;

    Do this for all appropriate tables/indexes.

    Now, if user2 logs in, he can access user1.table1, but he mustrefer to it as user1.table1. You can create a public synonym forthe table to make this easier. See below.

    Creating a public synonym for a table

    Log in as the tables owner (user1, in this example), and issue this SQL:

    CREATE PUBLIC SYNONYM table1 FOR user1.table1

    Granting DBA privileges to a user

    In a word: Dont. Instead, grant the appropriate privileges on the

    appropriate tables to an unprivileged user.

    Creating a rollback segment:

    Connect as SYSTEM/MANAGER, then:

    CREATE ROLLBACK SEGMENT segname TABLESPACE USERS;

    Then, add the rollback segment name to the initXXX.ora file for theinstance.

    If database is hung shutting down or starting up

    As Oracle user:

    $ svrmgrlOracle8i Enterprise Edition Release 8.1.5.0.2 - ProductionWith the Partitioning and Java optionsPL/SQL Release 8.1.5.0.0 - Production

    SVRMGR> connect internalSVRMGR> shutdownSVRMGR> startup forceSVRMGR> ^D

    If that fails, try killing the Oracle processes. Then, since Oracle uses

    two of the three evil sisters, semaphores and shared memory, ipcrm the

    Oracle-owned one after the kill -9 and it should restart.

    Dumping and Restoring an Oracle Database

    Dump:

    Oracle Cheat Sheet http://software.clapper.org/cheat-sheets/oracle.html#if-database-is-hung-...

    3 of 6 5/14/2015 9:20 PM

  • sqlplus system/manager
  • Error during stored procedure definition

    If the above error occurs during definition of a stored procedure, then:

    sqlplus system/manager@instance

  • Generated by Jekyll.

    trace says to run the statement(s) and print the plan(s), but avoid

    displaying any results.

    on prints the plan(s) and the results.

    Managing statistics for the cost-based optimizer

    Creating statistics for the cost-based optimizer

    sqlplus system/manager@instance