schema refresh steps on same database.pdf

8
SCHEMA REFRESH IN ORACLE USING EXP/IMP Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu SCHEMA REFRESH SCHEMA REFRESH is a regular job for DBA’s. So I decide to prepare an informative document for schema refresh. We can achieve schema refresh in two ways, they are 1) Refresh the database schema with Production database. 2) Refresh the database schema from Production database to some other databases like DEV/TEST/... REFRESH SCHEMA STEPS ON SAME DATABASE Find the roles & privileges that are assigned to the schema. Capture source database schema objects count. Export database schema using EXP/EXPDP utility. Drop the schema in the production database except DBLINK. Recreate the schema with default tablespace and allocate quota. Make required roles & privileges for the schema. Connect to SHAM and ensure default tablespace, roles and privileges. Import the dump file using IMP/IMPDP into the target database. Recompile invalid objects. Gather schema statistics after the refresh. PRE - REQ FOR SCHEMA REFRESH Refreshing sham schema on crms database. Source and destination database is same. Let’s start. SYS> select username, account_status, default_tablespace, temporary_tablespace FROM dba_users WHERE username=upper('&username'); Enter value for username: SHAM old 2: where username=upper('&username') new 2: where username=upper('SHAM') USERNAME ACCOUNT_STATUS DEFAULT_TABLESPACE TEMPORARY_TABLESPACE --------- ----------------- -------------------- --------------------- SHAM OPEN TBS1 TEMP SYS> select owner, sum(bytes)/1024/1024/1024 "SIZE in GB" FROM dba_segments WHERE owner=upper('&owner') GROUP BY owner; Enter value for owner: sham old 2: where owner=upper('&owner') group by owner new 2: where owner=upper('sham') group by owner OWNER SIZE in GB ------- ---------- SHAM 40.35 SYS> select distinct tablespace_name from dba_segments WHERE owner in ('SHAM'); TABLESPACE_NAME ---------------- TBS1 TBS2 TBS3 USERS

Upload: grthiyagu-oracle-dba

Post on 08-Nov-2015

53 views

Category:

Documents


1 download

TRANSCRIPT

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    SCHEMA REFRESH

    SCHEMA REFRESH is a regular job for DBAs. So I decide to prepare an informative document for

    schema refresh. We can achieve schema refresh in two ways, they are

    1) Refresh the database schema with Production database.

    2) Refresh the database schema from Production database to some other databases like DEV/TEST/...

    REFRESH SCHEMA STEPS ON SAME DATABASE

    Find the roles & privileges that are assigned to the schema.

    Capture source database schema objects count.

    Export database schema using EXP/EXPDP utility.

    Drop the schema in the production database except DBLINK.

    Recreate the schema with default tablespace and allocate quota.

    Make required roles & privileges for the schema.

    Connect to SHAM and ensure default tablespace, roles and privileges.

    Import the dump file using IMP/IMPDP into the target database.

    Recompile invalid objects.

    Gather schema statistics after the refresh.

    PRE - REQ FOR SCHEMA REFRESH Refreshing sham schema on crms database. Source and destination database is same. Lets start.

    SYS> select username, account_status, default_tablespace, temporary_tablespace

    FROM dba_users WHERE username=upper('&username');

    Enter value for username: SHAM

    old 2: where username=upper('&username')

    new 2: where username=upper('SHAM')

    USERNAME ACCOUNT_STATUS DEFAULT_TABLESPACE TEMPORARY_TABLESPACE

    --------- ----------------- -------------------- ---------------------

    SHAM OPEN TBS1 TEMP

    SYS> select owner, sum(bytes)/1024/1024/1024 "SIZE in GB"

    FROM dba_segments WHERE owner=upper('&owner') GROUP BY owner;

    Enter value for owner: sham

    old 2: where owner=upper('&owner') group by owner

    new 2: where owner=upper('sham') group by owner

    OWNER SIZE in GB

    ------- ----------

    SHAM 40.35

    SYS> select distinct tablespace_name from dba_segments WHERE owner in ('SHAM');

    TABLESPACE_NAME

    ----------------

    TBS1 TBS2 TBS3 USERS

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    FIND THE TABLESPACES AND SIZE OF THAT SCHEMA

    SYS> select tablespace_name, sum(bytes/1024/1024/1024) "Size in GB"

    from dba_segments where owner='SHAM' group by tablespace_name;

    TABLESPACE_NAME Size in GB

    ------------------------------ ----------

    TBS1 9.062

    TBS2 6.048

    TBS3 8. 82

    USERS 16. 42

    CHECK TABLESPACE QUOTAS FOR THAT SCHEMA

    SYS> select * from dba_ts_quotas where username='SHAM';

    ..

    ...

    [Trimmed]

    Quota is the amount of space allocated to a user in a tablespace. In dba_ts_quotas view, MAXBYTES

    column value of -1 indicates UNLIMITED, means that user can use as much space in that tablespace.

    SPOOL THE OUTPUT OF ROLES & PRIVILEGES ASSIGNED TO THE USER

    SYS> select * from dba_role_privs where grantee in('SHAM');

    GRANTEE GRANTED_ROLE ADM DEF

    ------------------------------ ------------------------------ --- ---

    SHAM RESOURCE NO YES

    SHAM CONNECT NO YES

    SYS> select * from dba_sys_privs where grantee in('SHAM');

    GRANTEE PRIVILEGE ADM

    ------------------------------ ---------------------------------------- ---

    SHAM CREATE SEQUENCE NO

    SHAM CREATE ANY SEQUENCE NO

    SHAM CREATE VIEW NO

    SHAM CREATE DATABASE LINK NO

    SHAM CREATE SYNONYM NO

    SHAM CREATE PROCEDURE NO

    SHAM CREATE TRIGGER NO

    SHAM CREATE ANY TABLE NO

    SHAM UNLIMITED TABLESPACE NO

    9 rows selected.

    SHAM> spool roles_privs.sql;

    SHAM> select 'Grant ' || role ||' to SHAM' "ROLES_PRIVILIGES" from session_roles;

    SHAM> select 'Grant ' || privilege ||' to SHAM' "ROLES_PRIVILIGES" from session_privs;

    SHAM> spool off;

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    CHECK DATABASE LINKS FOR THAT SCHEMA

    SYS> select owner, db_link, username from dba_db_links

    WHERE owner='SHAM';

    OWNER DB_LINK USERNAME

    ------ ---------- --------

    SHAM TESTLINK ROSE

    CHECK ALL OBJECTS IN THAT SCHEMA

    SYS> select owner, object_type, count(*) FROM dba_objects

    WHERE owner=upper('&owner')

    AND object_name not like 'BIN$%'

    GROUP BY object_type, owner

    ORDER BY object_type;

    Enter value for owner: sham

    OWNER OBJECT_TYPE COUNT(*)

    ------------------------------ ------------------- ----------

    SHAM FUNCTION 1

    SHAM INDEX 10

    SHAM PACKAGE 1

    SHAM PACKAGE BODY 1

    SHAM PROCEDURE 1

    SHAM SEQUENCE 2

    SHAM SYNONYM 1

    SHAM TABLE 12

    SHAM TABLE PARTITION 3

    SHAM TRIGGER 2

    SHAM VIEW 1

    SHAM DATABASE LINK 1

    12 rows selected.

    FIND TOTAL OBJECTS IN THAT SCHEMA

    SHAM> select object_name, object_type, subobject_name, status FROM user_objects;

    OBJECT_NAME OBJECT_TYPE SUBOBJECT_NAME STATUS

    ---------------------------- ---------------- --------------- -------

    GRADE TABLE VALID

    PROJECT TABLE VALID

    TRANSPORT TABLE VALID

    EMP_PROJ_INFO TABLE VALID

    EMP_PERS_INFO TABLE VALID

    EMP_INFO TABLE VALID

    PAYROLL TABLE VALID

    EMP TABLE VALID

    DEPT TABLE VALID

    SEQ_EMP_INFO1 SEQUENCE VALID

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    SEQ_EMP_INFO SEQUENCE VALID

    TRI_EMP TRIGGER VALID

    EMP_AUDIT TABLE VALID

    LOGTAB TABLE VALID

    USRLOG TRIGGER VALID

    VEMP_INFO VIEW VALID

    CONT_EMP TABLE VALID

    CONT_EMP TABLE PARTITION P1 VALID

    CONT_EMP TABLE PARTITION P2 VALID

    CONT_EMP TABLE PARTITION P3 VALID

    MULT_TAB FUNCTION VALID

    PKGSAL PACKAGE BODY VALID

    PROC1 PROCEDURE VALID

    PAYROLL_INFO SYNONYM VALID

    PAYROLL_SAL_IN4 INDEX VALID

    EMP_DPID_IN1 INDEX VALID

    EMP_PRJ_INFO_PRJID_IN3 INDEX VALID

    EMP_PRJ_INFO_DPID_IN2 INDEX VALID

    PKGSAL PACKAGE VALID

    PAYROLL_ACCNO_C11_UQ INDEX VALID

    EMP_PRJ_INFO_MAILID_C9_UQ INDEX VALID

    PROJ_PRJID_C4_PK INDEX VALID

    DEPT_DEPTID_C3_PK INDEX VALID

    EMP_EMPID_C1_PK INDEX VALID

    GRD_EMPGRD_C12_PK INDEX VALID

    TESTLINK DATABASE LINK VALID

    36 rows selected.

    SHAM> select constraint_name , constraint_type , table_name , r_constraint_name, r_owner

    FROM all_constraints WHERE owner='SHAM';

    CONSTRAINT_NAME CONST TABLE_NAME R_CONSTRAINT_NAME R_OWNER

    ------------------------------ ----- -------------------- -------------------- --------

    PAYROLL_EMPID_C7_FK R PAYROLL EMP_EMPID_C1_PK SHAM

    EMP_PRJ_INFO_EMPID_C5_FK R EMP_PROJ_INFO EMP_EMPID_C1_PK SHAM

    EMP_PERS_INFO_EID_C6_FK R EMP_PERS_INFO EMP_EMPID_C1_PK SHAM

    TRNSPRT_PRJID_C10_FK R TRANSPORT EMP_EMPID_C1_PK SHAM

    EMP_ELEVEL_C13_FK R EMP GRD_EMPGRD_C12_PK SHAM

    GRD_EMPGRD_C12_PK P GRADE

    PROJ_PRJID_C4_PK P PROJECT

    PAYROLL_ACCNO_C11_UQ U PAYROLL

    EMP_PRJ_INFO_MAILID_C9_UQ U EMP_PROJ_INFO

    EMP_EMPID_C1_PK P EMP

    DEPT_DEPTID_C3_PK P DEPT

    EMP_DEPTID_C2_NTNL C EMP

    EMP_PRJ_INFO_DEPTID_C8_NL C EMP_PROJ_INFO

    GRD_DESC_C14_NL C GRADE

    14 rows selected.

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    FIND INDEXES AND ITS ASSOCIATED TABLESPACES

    SHAM> select table_name, index_name, tablespace_name from user_indexes;

    TABLE_NAME INDEX_NAME TABLESPACE_NAME

    ------------------------------ ------------------------------ ------------------------------

    PROJECT PROJ_PRJID_C4_PK TBS1

    PAYROLL PAYROLL_ACCNO_C11_UQ TBS1

    PAYROLL PAYROLL_SAL_IN4 USERS

    EMP_PROJ_INFO EMP_PRJ_INFO_MAILID_C9_UQ TBS1

    EMP_PROJ_INFO EMP_PRJ_INFO_DPID_IN2 USERS

    EMP_PROJ_INFO EMP_PRJ_INFO_PRJID_IN3 USERS

    EMP EMP_EMPID_C1_PK TBS1

    EMP EMP_DPID_IN1 USERS

    DEPT DEPT_DEPTID_C3_PK TBS1

    GRADE GRD_EMPGRD_C12_PK TBS1

    10 rows selected.

    FIND THE ENCRYPTED PASSWORD FOR THE SCHEMA

    SYS> select password from sys.user$ where name='SHAM';

    PASSWORD

    -----------------

    8657483259C22FDE

    EXPORT THE SCHEMA

    Export the schema from the crms database with the name of the file as refresh_sham.dmp

    $ export ORACLE_SID=crms

    $ exp system/manager file=refresh_sham.dmp log=schema_refresh_sham.log owner=sham direct=y

    statistics=none

    ..

    ...

    [Trimmed]

    Export terminated successfully without warnings.

    STEPS TO DROP AND RECREATE THE SCHEMA

    Drop the sham schema from the crms database.

    Recreate the user with default tablespace and quotas on different tablespaces.

    Assign temporary tablespace to that user.

    Execute the spooled script (roles_privs.sql) which contains roles and privileges to the user.

    EX : SYS>@roles_privs.sql;

    Script grants the roles and privileges to the user obtained before dropping it.

    Connect to the user and verify allocated tablespaces, roles and privileges.

    ** Before drop all the objects in that Schema, connect the schema and Spool the output.

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    POINTS TO NOTE

    Suppose if you want to remove tables manually first you have to delete child tables otherwise

    oracle wont allow directly to drop parents table. Lets see.

    SHAM> drop table emp;

    drop table emp

    *

    ERROR at line 1:

    ORA-02449: unique/primary keys in table referenced by foreign keys

    While dropping tables manually we can see ORA-02449 error. Use the below query to find reference

    key constraints and drop the tables first.

    SHAM> Select constraint_name, constraint_type, table_name

    FROM all_constraints WHERE constraint_type='R'

    AND r_constraint_name in (select constraint_name from all_constraints where table_name='EMP');

    CONSTRAINT_NAME C TABLE_NAME

    ------------------------------ - ------------------------------

    EMP_PRJ_INFO_EMPID_C5_FK R EMP_PROJ_INFO

    PAYROLL_EMPID_C7_FK R PAYROLL

    TRNSPRT_PRJID_C10_FK R TRANSPORT

    EMP_PERS_INFO_EID_C6_FK R EMP_PERS_INFO

    SCRIPT TO FIND & DROP OBJECTS FROM THAT SCHEMA

    SHAM> spool drop_tables.sql;

    SHAM> select 'drop table '||table_name||' cascade constraints purge; 'from user_tables;

    SHAM> spool off;

    SHAM> spool drop_other_objects.sql;

    SHAM> select 'drop '||object_type||' '||object_name||';' from user_objects;

    SHAM> spool off;

    SHAM> DISC

    DROP THE SCHEMA

    SYS> drop user sham cascade; # note SCN 4607636

    User dropped.

    RECREATE THE USER WITH DEFAULT TABLESPACE , TEMPORARY TABLESPACE & QUOTAS

    SYS> create user sham identified by values '8657483259C22FDE'

    default tablespace tbs1 temporary tablespace temp

    quota unlimited on tbs1

    quota unlimited on tbs2

    quota unlimited on tbs3

    quota unlimited on users;

    User created.

    ** Sham user has default profile. If you have specific profile name for the user add it.

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    RUN SPOOLED SCRIPT TO GRANT ROLES & PRIVILEGES

    SYS>@roles_privs.sql;

    roles_privs.sql

    IMPORT DATA INTO CRMS DATABASE

    $ export ORACLE_SID=crms

    $ imp system/manager file=refresh_sham.dmp log=refresh_sham_imp.log fromuser=sham touser=sham

    Import: Release 11.2.0.1.0 - Production on Fri May 28 22:48:48 2015

    Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.

    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

    With the Partitioning, OLAP, Data Mining and Real Application Testing options

    Export file created by EXPORT:V11.02.00 via direct path

    import done in US7ASCII character set and AL16UTF16 NCHAR character set

    import server uses WE8MSWIN1252 character set (possible charset conversion)

    . importing SHAM's objects into SHAM

    . importing partition "CONT_EMP":"P1" 42202 rows imported

    . . importing partition "CONT_EMP":"P2" 44020 rows imported

    . . importing partition "CONT_EMP":"P3" 42015 rows imported

    . . importing table "DEPT" 12 rows imported

    . . importing table "EMP" 168022 rows imported

    . . importing table "EMP_AUDIT" 24 rows imported

    . . importing table "EMP_INFO" 168022 rows imported

    . . importing table "EMP_PERS_INFO" 168022 rows imported

    . . importing table "EMP_PROJ_INFO" 168022 rows imported

    . . importing table "GRADE" 12 rows imported

    . . importing table "LOGTAB" 2666 rows imported

    . . importing table "PAYROLL" 168022 rows imported

    . . importing table "PROJECT" 22 rows imported

    . . importing table "TRANSPORT" 148213 rows imported

    About to enable constraints...

    Import terminated successfully without warnings.

    BASIC CHECKS AFTER IMPORT THE SCHEMA

    SYS> select owner, object_type, count(object_type) from dba_objects

    WHERE owner ='SHAM' GROUP BY owner, object_type;

    ** Schema object count should be same before export and after import.

  • SCHEMA REFRESH IN ORACLE USING EXP/IMP

    Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu

    SYS> select username, password, default_tablespace, temporary_tablespace,

    profile from dba_users WHERE username ='SHAM';

    SYS> select owner,sum(bytes)/1024/1024/1024 "SIZE in GB" from dba_segments

    WHERE owner=upper('&owner') GROUP BY owner;

    SYS> select * from dba_role_privs where grantee in('SHAM');

    SYS> select * from dba_sys_privs where grantee in('SHAM');

    SYS> select * from dba_ts_quotas where username='SHAM';

    FIND THE INVALID OBJECTS FOR THE SCHEMA

    SYS> select owner, object_name, object_type, status from dba_objects

    WHERE status 'VALID' AND OWNER='SHAM';

    If you find any invalid objects execute following script.

    SYS> @?/rdbms/admin/utlrp.sql;

    Check the privileges, tablespace quotas, and dblinks for that the schema. This is essential.

    ARE ALL GRANTS IMPORTED ?

    NO. If the schema received grant privilege from others does NOT get export when you export the

    schema. So, it does NOT get import when you import the schema.

    1) Grants given by a user are exported with the user.

    SHAM> grant select on sham.emp to scott;

    SHAM> grant update on sham.emp to scott;

    2) Grants received by a user are NOT exported with the user.

    SCOTT> grant select on scott.salgrade to sham;

    SCOTT> grant update on scott.salgrade to sham;

    GATHER NEW STATISTICS FOR THE SCHEMA

    SYS> exec dbms_stats.gather_schema_stats(ownname =>'SHAM', method_opt=>'FOR ALL INDEXES FOR ALL

    INDEXED COLUMNS SIZE 1',estimate_percent => 15,granularity=>'ALL',cascade=>TRUE);

    PL/SQL procedure successfully completed.