1. 程式人生 > >oracle將一個使用者下的所有表複製到以一個使用者下

oracle將一個使用者下的所有表複製到以一個使用者下

在測試過程中,一般為了讓測試環境與開發環境隔離,一般要球測試環境對應的資料庫也與開發環境進行隔離

這時候我的做法是:先建立一個用於測試環境的使用者,然後將開發環境中該使用者對應的表及資料匯出,再匯入到測試使用者下

具體實現步驟:

方式一:

建立測試使用者,並賦許可權(看需要是否需要建立表空間)--》》》然後可直接使用imp命令將開發環境使用者下的所有表及資料,包括約束,儲存過程,觸發器匯出為一個dmp檔案,然後使用exp命令將該檔案匯入到測試使用者下即可

--建立表空間
CREATE TABLESPACE "TBS_KFMH_DAT01" DATAFILE 
  '/+DG_DATA/kfmh_dat01' SIZE 204800000
  LOGGING ONLINE PERMANENT BLOCKSIZE 8192
  EXTENT MANAGEMENT LOCAL AUTOALLOCATE DEFAULT 
 NOCOMPRESS  SEGMENT SPACE MANAGEMENT AUTO
--/+DG_DATA/kfmh_dat002為oracle的資料檔案路徑
--TBS_KFMH_DAT01為表空間名稱


---建立使用者
create user kfmhDev identified by kfmhDev
  default tablespace TBS_KFMH_DAT01
  temporary tablespace TEMP
  profile DEFAULT
  password expire;
  
--賦許可權
-- Grant/Revoke object privileges 
grant execute on DBMS_JOB to kfmhDev;
grant execute on DBMS_RANDOM to kfmhDev;
-- Grant/Revoke role privileges 
grant connect to kfmhDev;
-- Grant/Revoke system privileges 
grant alter session to kfmhDev;
grant create cluster to kfmhDev;
grant create materialized view to kfmhDev;
grant create procedure to kfmhDev;
grant create sequence to kfmhDev;
grant create session to kfmhDev;
grant create synonym to kfmhDev;
grant create table to kfmhDev;
grant create trigger to kfmhDev;
grant create type to kfmhDev;
grant create view to kfmhDev;
grant execute any procedure to kfmhDev;
grant execute any type to kfmhDev;
grant force transaction to kfmhDev;
grant select any table to kfmhDev;
grant unlimited tablespace to kfmhDev;


---查看錶空間大小及使用情況
SELECT a.tablespace_name "表空間名", 
total "表空間大小", 
free "表空間剩餘大小", 
(total - free) "表空間使用大小", 
total / (1024 * 1024 * 1024) "表空間大小(G)", 
free / (1024 * 1024 * 1024) "表空間剩餘大小(G)", 
(total - free) / (1024 * 1024 * 1024) "表空間使用大小(G)", 
round((total - free) / total, 4) * 100 "使用率 %" 
FROM (SELECT tablespace_name, SUM(bytes) free 
FROM dba_free_space 
GROUP BY tablespace_name) a, 
(SELECT tablespace_name, SUM(bytes) total 
FROM dba_data_files 
GROUP BY tablespace_name) b 
WHERE a.tablespace_name = b.tablespace_name 




select * from user_tables;


select * from user_tables where num_rows!=0;


--查詢外來鍵列表
select * from user_constraints c where c.constraint_type = 'R';


--查詢所有索引
select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name


--查詢觸發器 
SELECT NAME FROM USER_SOURCE WHERE TYPE = 'TRIGGER' GROUP BY NAME




--匯出使用者下的所有表結構及資料
--進入到oracle所在伺服器(通常為linux)
--進入linux
--# su - oracle


--匯出某個使用者下的所有資料
--$ exp sys/oracle\"@kfmh\" file=kfmhTest.dmp owner=kfmhTest log=exp_kfmhTest.log

只匯出資料物件,不匯出資料 (rows=n )




--向某個使用者匯入資料
--$ imp sys/oracle\"@kfmh\" file=kfmhTest.dmp log=imp_kfmhDev.log fromuser=kfmhTest touser=kfmhDev commit=y ignore=y



方式二:

我給人的專案由於表眾多,且不同表之間的關聯性比較強,使用第一張方式匯出表資料的時候會報錯,此時我是按照分佈匯出的方式進行的:
步驟如下:

建立測試使用者,並賦許可權(看需要是否需要建立表空間)--》》》然後匯出表結構,匯出表資料--》》》在匯入到測試使用者時,是先將表結構和主鍵匯入,然後匯入資料,再匯入表儲存過程,觸發器,最後匯入外來鍵

傳送門:

3. 三種模式 
    (1)表方式,將指定表的資料匯出/匯入。 
    匯出:匯出一張或幾張表:$ exp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1,table2 
    匯出某張表的部分資料 
    $ exp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1 query=\“where col1=\‘…\’and col2 \<…\” 
    匯入:匯入一張或幾張表 
    $ imp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1, 


    table2 fromuser=dbuser touser=dbuser2 commit=y ignore=y 
    (2)使用者方式,將指定使用者的所有物件及資料匯出/匯入。 
    匯出:$ exp user/pwd file=/dir/xxx.dmp log=xxx.log owner=(xx, yy) 
    只匯出資料物件,不匯出資料 (rows=n ) 
    $ exp user/pwd file=/dir/xxx.dmp log=xxx.log owner=user rows=n 
    匯入:$ imp user/pwd file=/dir/xxx.dmp log=xxx.log fromuser=dbuser touser=dbuser2 
    commit=y ignore=y 
    (3)全庫方式,將資料庫中的所有物件匯出/匯入匯出: 
    $ exp user/pwd file=/dir/xxx.dmp log=xxx.log full=ycommit=y ignore=y 
    匯入:$ imp user/pwd file=/dir/xxx.dmp log=xxx.log fromuser=dbuser touser=dbuser2