Oracle 11g資料庫之資料匯入匯出與事物

來源:互聯網
上載者:User

在使用Oracle資料庫中對備份恢複是最關鍵的,當資料庫出現故障時可以及時恢複減少不必要的損失。下面我將簡單介紹一下匯入匯出資料庫,方案,表,和在使用的過程當中事物的作用。
1.匯出資料表
[oracle@CentOS ~]$ exp scott/system tables=t file='/home/oracle/t.dump' log='/home/oracle/t.log';    --匯出scott使用者中的t表資料(tables=可以寫多個表,full=y  全部匯出,ignore=y  表示忽略重複的資料)
Export: Release 11.2.0.1.0 - Production on Mon Feb 17 16:28:04 2014
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 done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8MSWIN1252 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table                              T          1 rows exported
Export terminated successfully without warnings.
[oracle@centos ~]$

2.匯入資料表
[oracle@centos ~]$ sqlplus
SQL*Plus: Release 11.2.0.1.0 Production on Mon Feb 17 16:29:13 2014
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Enter user-name: scott
Enter password:
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
SQL> drop table t;    --刪除我剛才匯出的表
Table dropped.
SQL> commit;
Commit complete.
SQL> quit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@centos ~]$ imp scott/system tables=t file='/home/oracle/t.dump';  --匯入資料表
Import: Release 11.2.0.1.0 - Production on Mon Feb 17 16:29:58 2014
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 conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses WE8MSWIN1252 character set (possible charset conversion)
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table                            "T"          1 rows imported
Import terminated successfully without warnings.
[oracle@centos ~]$ sqlplus
SQL*Plus: Release 11.2.0.1.0 Production on Mon Feb 17 16:30:03 2014
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Enter user-name: scott
Enter password:
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
SQL> select * from t;    --成功匯入
A
----------
1
SQL>

3.匯出使用者下的所有表(scott)
[oracle@centos ~]$ exp scott/system  file='/home/oracle/scott.dump';  --匯入scott自己的所有表(exp scott/system owner=使用者名稱 file='路徑'  --匯出其它使用者下的表)
Export: Release 11.2.0.1.0 - Production on Mon Feb 17 16:34:07 2014
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 done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8MSWIN1252 character set (possible charset conversion)
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table                          DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                            EMP        12 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                      SALGRADE          5 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table                  SYS_TEMP_FBT
. . exporting table                              T          1 rows exported
. . exporting table                          TONG1        14 rows exported
EXP-00091: Exporting questionable statistics.
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.
[oracle@centos ~]$

4.匯入使用者下的所有表(scott)
[oracle@centos ~]$ sqlplus
SQL*Plus: Release 11.2.0.1.0 Production on Mon Feb 17 16:39:53 2014
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Enter user-name: scott
Enter password:
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
SQL> drop table t;    --刪除表
Table dropped.
SQL> drop table tong1;
Table dropped.
SQL> commit;
Commit complete.
SQL> quit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@centos ~]$ imp scott/system file='/home/oracle/scott.dump';  --匯入scott使用者下的表
Import: Release 11.2.0.1.0 - Production on Mon Feb 17 16:40:39 2014
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 conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses WE8MSWIN1252 character set (possible charset conversion)
. importing SCOTT's objects into SCOTT
IMP-00015: following statement failed because the object already exists:
"CREATE TABLE "DEPT" ("DEPTNO" NUMBER(2, 0), "DNAME" VARCHAR2(14), "LOC" VAR"
"CHAR2(13))  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 6"
"5536 NEXT 1048576 MINEXTENTS 1 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DE"
"FAULT)                    LOGGING NOCOMPRESS"
IMP-00015: following statement failed because the object already exists:
"CREATE TABLE "EMP" ("EMPNO" NUMBER(4, 0), "ENAME" VARCHAR2(10), "JOB" VARCH"
"AR2(9), "MGR" NUMBER(4, 0), "HIREDATE" DATE, "SAL" NUMBER(7, 2), "COMM" NUM"
"BER(7, 2), "DEPTNO" NUMBER(2, 0))  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRAN"
"S 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 FREELISTS 1 FREELIST "
"GROUPS 1 BUFFER_POOL DEFAULT)                    LOGGING NOCOMPRESS"
IMP-00015: following statement failed because the object already exists:
"CREATE TABLE "SALGRADE" ("GRADE" NUMBER, "LOSAL" NUMBER, "HISAL" NUMBER)  P"
"CTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 104"
"8576 MINEXTENTS 1 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)      "
"            LOGGING NOCOMPRESS"
IMP-00015: following statement failed because the object already exists:
"CREATE GLOBAL TEMPORARY TABLE "SYS_TEMP_FBT" ("SCHEMA" VARCHAR2(32), "OBJEC"
"T_NAME" VARCHAR2(32), "OBJECT#" NUMBER, "RID" UROWID(4000), "ACTION" CHAR(1"
")) ON COMMIT PRESERVE ROWS "
. . importing table                            "T"          1 rows imported
. . importing table                        "TONG1"        14 rows imported
Import terminated successfully with warnings.
[oracle@centos ~]$ sqlplus
SQL*Plus: Release 11.2.0.1.0 Production on Mon Feb 17 16:40:47 2014
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Enter user-name: scott
Enter password:
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
SQL> select * from tab;    --查看錶被匯入成功
TNAME                  TABTYPE  CLUSTERID
------------------------------ ------- ----------
BONUS                  TABLE
DEPT                  TABLE
TNAME                  TABTYPE  CLUSTERID
------------------------------ ------- ----------
EMP                TABLE
SALGRADE              TABLE
SYS_TEMP_FBT              TABLE
T                  TABLE
TONG1                  TABLE
16 rows selected.
SQL>

5.匯出整個資料庫
[oracle@centos ~]$ exp system/system file='/home/oracle/database.dump' log='/home/oracle/database.log';    --匯出整個資料庫

6.事物
一、什麼是事務
      事務用於保證資料的一致性,它由一組相關的dml語句組成,該組的dml語句要麼全部成功,要們全部失敗。如:網上轉帳就是典型的要用事務來處理,用以保證資料的一致性。
二、事務和鎖
      當執行事務操作時,Oracle會在被作用的表上加鎖,防止其他使用者改表。
三、提交事務
      在執行使用commit語句可以提交事務,當執行了commit語句後,會確認事務的變化,結束事務,刪除儲存點,釋放鎖。當使用commit語句結束事務之後,其他會話將可以查看到事務變化後的新資料。
四、回退事務
    在介紹回退事務前,我們先介紹一下儲存點(savepoint)的概念和作用,儲存點是事務中的一點,用於取消部分事務,當結束事務時,會自動的刪除該事務所定義的所有儲存點。當執行rollback時,通過制定儲存點可以回退到指定的點。
五、唯讀事務
    唯讀事務是指只允許執行查詢的操作,而不允許執行任何其他dml操作的事務,使用唯讀事務可以確保使用者只能取得某時間點的資料。
設定唯讀事務:
SQL> savepoint qw;    --建立儲存點
Savepoint created.
SQL> delete from t;    --刪除表中的資料,不能commit提交
2 rows deleted.
SQL> select * from t;
no rows selected
SQL> rollback to qw;    --回退到儲存點
Rollback complete.
SQL> select * from t;    --資料回來了
A
----------
1
2
sql>set transaction read only;    --資料庫的表只能查詢不能修改和刪除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.