oracle復制表數據,復制表結構


1、不同用戶之間的表數據復制

2、同用戶表之間的數據復制

3、B.x中個別字段轉移到B.y的相同字段

4、只復制表結構 加入了一個永遠不可能成立的條件1=2,則此時表示的是只復制表結構,但是不復制表內容

5、完全復制表(包括創建表和復制表中的記錄) 

6、將多個表數據插入一個表中

7、創建用戶budget_zlgc,權限和budget相同,A、只復制所有表結構B、復制所有表所有信息)

 

1.不同用戶之間的表數據復制 
對於在一個數據庫上的兩個用戶A和B,假如需要把A下表old的數據復制到B下的new,請使用權限足夠的用戶登入sqlplus:
insert into B.new(select * from A.old);

如果需要加條件限制,比如復制當天的A.old數據
insert into B.new(select * from A.old where date=GMT); 
藍色斜線處為選擇條件

2.同用戶表之間的數據復制 
用戶B下有兩個表:B.x和B.y,如果需要從表x轉移數據到表y,使用用戶B登陸sqlpus即可:
insert into 目標表y select * from x where log_id>'3049' -- 復制數據 
注意:要示目標表y必須事先創建好
如insert into bs_log2 select * from bs_log where log_id>'3049'    


3.B.x中個別字段轉移到B.y的相同字段 
--如果兩個表結構一樣
insert into table_name_new select * from table_name_old 
如果兩個表結構不一樣:
insert into y(字段1,字段2) select 字段1,字段2 from x

4.只復制表結構 加入了一個永遠不可能成立的條件1=2,則此時表示的是只復制表結構,但是不復制表內容   
create table 用戶名.表名 as select * from 用戶名.表名 where 1=2
如create table zdsy.bs_log2 as select * from zdsy.bs_log where 1=2

5完全復制表(包括創建表和復制表中的記錄)
create table test as select * from bs_log  --bs_log是被復制表


將多個表數據插入一個表中
insert into 目標表test(字段1。。。字段n) (select 字段1.。。。。字段n) from 表 union all select 字段1.....字段n from 表

 

7、創建用戶budget_zlgc,權限和budget相同,A、只復制所有表結構

B、復制所有表所有信息)

創建用戶budget_zlgc,並導出budge用戶數據

exp userid="\"sys/sys as sysdba"\" file='/backup/expdb/oa0824.dmp' log='/backup/expdb/oaex0825.log' owner=budget ignore=Y buffer=256000000

 

Abudget用戶所有表,表結構和budget相同,(無數據)

imp userid="\"sys/sys as sysdba"\" file='/backup/expdb/oa0824.dmp' log='/backup/expdb/oa0825.log' fromuser=budget touser=budget_zlgc ignore=Y buffer=256000000 rows=n

Bbudget用戶所有表,表結構、數據和budget相同

imp userid="\"sys/sys as sysdba"\" file='/backup/expdb/oa0824.dmp' log='/backup/expdb/oa0825.log' fromuser=budget touser=budget_zlgc ignore=Y

 

 

 

 


=====================================================
oracle
和mssql中復制表的比較

庫內數據復制
MS SQL Server: 
Insert into 復制表名稱 select 語句 (復制表已經存在)
select 字段列表 into 復制表名稱 from 表 (復制表不存在)

Oracle :
Insert into 復制表名稱 select 語句 (復制表已經存在)
create table 復制表名稱 as select 語句 (復制表不存在)

多表更新、刪除

一條更新語句是不能更新多張表的,除非使用觸發器隱含更新,我這里說的意思是:根據其他表數據更新你要更新的表一般形式:
MS SQL Server 
update ASET 字段1=B表字段表達式,字段2=B表字段表達式from BWHERE 邏輯表達式

Oracle 
update ASET 字段1=(select 字段表達式 from B WHERE ...),字段2=(select 字段表達式 from B WHERE ...) WHERE 邏輯表達式
從以上來看,感覺oracle沒有ms sql好,主要原因:假如A需要多個字段更新,MS_SQL 語句更簡練你知道剛學數據庫的人怎么做上面這件事情

嗎,他們使用游標一條一條的處理

====導入==導出===========
(1)導出
exp ff/ff@orcl file='d:ff.dmp' tables=customers direct=y
使用exp 輸出。輸入的為需要備份的用戶表的賬號和密碼,根據提示一直點回車就OK 結束后將會出現一個ff.DMP文件,此文件為備份數據。
導出時可以選擇導出:1.整個數據庫(需具備dba權限);2.用戶(包括表、視圖和其它);3.表(只包含表,不導出視圖);

(2)導入
create user ly identified by pw default tablespace users quota 10M on users; 
創建新用戶 用戶名為ly 密碼為pw 默認表空間為此空間,配額為10M
grant connect,resource,dba to ly;
賦予ly權限(1.連接;2.資源;3.dba權限,必須具備才能執行導入!)
grant create session,create table,create view,unlimited tablespaces to ly; 
賦予ly其它常用權限(1.登陸到服務器,2.創建表,3.創建視圖,4.無限表空間)
imp ly/ly@ORCL fromuser=ff touser=ly file='d:ff.dmp' constraints=n
使用 imp 輸入。輸入需要導入的用戶的用戶名和密碼 然后點回車,根據提示一直到再次要求你輸入用戶名的地方。

=================

sql_server不同數據庫間復制表

不同數據庫表結構 和數據的復制 : 
目標數據庫不存在要導入的表時: 
example: 
xuexiao為目標數據庫,teaching為源數據庫,dbo.course_list已經存在於teaching,想在沒有此表的xuexiao庫中復制一個用下面的語句完成

: 
select * into xuexiao.dbo.course_list from teaching.dbo.course_list 

不同數據庫之間復制表的數據的方法

當表目標表存在時: 
insert into 目的數據庫..表 select * from 源數據庫..表

當目標表不存在時: 
select * into 目的數據庫..表 from 源數據庫..表 
=================================================
如下,表a是數據庫中已經存在的表,b是准備根據表a進行復制創建的表:

  1、只復制表結構的sql
  create table b as select * from a where 1<>1

  2、即復制表結構又復制表中數據的sql
  create table b as select * from a

  3、復制表的制定字段的sql
  create table b as select row_id,name,age from a where 1<>1//前提是row_id,name,age都是a表的列

  4、復制表的指定字段及這些指定字段的數據的sql
  create table b as select row_id,name,age from a

  以上語句雖然能夠很容易的根據a表結構復制創建b表,但是a表的索引等卻復制不了,需要在b中手動建立。

  5、insert into 會將查詢結果保存到已經存在的表中
  insert into t2(column1, column2, ....) select column1, column2, .... from t1

 

 

1、獲得單個表和索引DDL語句的方法: 

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

set   heading   off; 

set   echo   off; 

Set   pages   999; 

set   long   90000; 

  

spool   get_single.sql 

select   dbms_metadata.get_ddl( 'TABLE ', 'SZT_PQSO2 ', 'SHQSYS ')   from   dual; 

select   dbms_metadata.get_ddl( 'INDEX ', 'INDXX_PQZJYW ', 'SHQSYS ')   from   dual;

spool   off;

 

uffer=256000000

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM