新建oracle用戶和數據庫及導入導出

1 在linux機器上切換到oracle用戶,並用命令sqlplus登錄數據庫

[root@tester ~]# su - oracle

[oracle@tester ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Fri Mar 22 13:55:41 2019
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>

2 創建表空間

先查詢表空間路徑
SQL> select name from v$datafile;

NAME
--------------------------------------------------------------------------------
/opt/app/oracle/oradata/ora11g/system01.dbf
/opt/app/oracle/oradata/ora11g/sysaux01.dbf
/opt/app/oracle/oradata/ora11g/undotbs01.dbf
/opt/app/oracle/oradata/ora11g/users01.dbf

得知表空間路徑爲/opt/app/oracle/oradata/ora11g/

接着創建表空間
SQL> create tablespace customerchat logging datafile '/opt/app/oracle/oradata/ora11g/customerchat.dbf' size 100m autoextend on next 32m maxsize 500m extent management local;

Tablespace created.

3 創建臨時表空間

先查詢臨時表空間路徑
SQL> select name from v$tempfile;

NAME
--------------------------------------------------------------------------------
/opt/app/oracle/oradata/ora11g/temp01.dbf

得知臨時表空間路徑  /opt/app/oracle/oradata/ora11g/

SQL> create temporary tablespace customerchat_temp tempfile '/opt/app/oracle/oradata/ora11g/customerchat_temp.dbf' size 100m autoextend on next 32m maxsize 500m extent management local;

Tablespace created.

創建用戶密碼與上面創建的文件形成映射關係


SQL> create user customerchat identified by customerchat default tablespace customerchat temporary tablespace customerchat_temp;

User created.

5 爲用戶添加權限

初始建立的用戶沒有任何權限,不能執行任何數據庫操作,因此必須爲用戶設置權限或者角色。
被賦予了某個角色的用戶將擁有該角色所具備的權限,
常被用到的系統預定義角色:CONNECT、RESOURCE、DBA、EXP_FULL_DATABASE、IMP_FULL_DATABASE。
其中,CONNECT、RESOURCE、DBA主要用於數據庫管理,數據庫管理員需要被授予這三個角色。
一般的數據庫開發人員,需要被授予CONNECT、RESOURCE角色即可。
EXP_FULL_DATABASE、IMP_FULL_DATABASE角色分別用於操作數據庫導出、導入相關的操作。

SQL> grant connect,resource,dba,exp_full_database,imp_full_database to customerchat;

Grant succeeded.


6 查看已經創建的用戶

SQL> select username from dba_users;

USERNAME
------------------------------
CUSTOMERCHAT
SYS
SYSTEM
SCOTT
OUTLN
MGMT_VIEW
FLOWS_FILES
MDSYS
ORDSYS
EXFSYS
DBSNMP

USERNAME
------------------------------
WMSYS
APPQOSSYS
APEX_030200
OWBSYS_AUDIT
ORDDATA
CTXSYS
ANONYMOUS
SYSMAN
XDB
ORDPLUGINS
OWBSYS

USERNAME
------------------------------
SI_INFORMTN_SCHEMA
OLAPSYS
ORACLE_OCM
XS$NULL
MDDATA
DIP
APEX_PUBLIC_USER
SPATIAL_CSW_ADMIN_USR
SPATIAL_WFS_ADMIN_USR

31 rows selected.

SQL>


7 修改用戶密碼

SQL> alter user customerchat identified by 123456;

User altered.

有時用戶會處於鎖定狀態,解鎖用戶:
alter user customerchat account unlock;

8 刪除用戶

drop user 用戶名 cascade;
--刪除空的表空間,但是不包含物理文件
drop tablespace tablespace_name;
--刪除非空表空間,但是不包含物理文件
drop tablespace tablespace_name including contents;
--刪除空表空間,包含物理文件
drop tablespace tablespace_name including datafiles; 
--刪除非空表空間,包含物理文件
drop tablespace tablespace_name including contents and datafiles;
--如果其他表空間中的表有外鍵等約束關聯到了本表空間中的表的字段,就要加上CASCADE CONSTRAINTS
drop tablespace tablespace_name including contents and datafiles CASCADE CONSTRAINTS;


9 登錄

[oracle@tester ~]$ sqlplus

SQL*Plus: Release 11.2.0.1.0 Production on Fri Mar 22 14:27:22 2019

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

Enter user-name: customerchat
Enter password: 123456

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>

10 查看用戶名下的表

select * from all_tables where owner='CUSTOMERCHAT';   ##用戶名必須大寫

select * from all_tab_comments 
-- 查詢所有用戶的表,視圖等
select * from user_tab_comments   
-- 查詢本用戶的表,視圖等
select * from all_col_comments 
--查詢所有用戶的表的列名和註釋.
select * from user_col_comments 
-- 查詢本用戶的表的列名和註釋
select * from all_tab_columns 
--查詢所有用戶的表的列名等信息(詳細但是沒有備註).
select * from user_tab_columns 
--查詢本用戶的表的列名等信息(詳細但是沒有備註).

11 命令行退格方法

Linux使用sqlplus退格
使用Ctrl+Backspace

12 navicat工具登錄問題 :

ORA-12514 : TNS問題

解決方法: .修改listener.ora  添加虛線內的配置

[oracle@tester admin]$ cat listener.ora 
# listener.ora Network Configuration File: /opt/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = tester)(PORT = 1521))
    )
  )

ADR_BASE_LISTENER = /opt/app/oracle
----------------------------------------------------------------------
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /opt/app/oracle/product/11.2.0/db_1)
      (PROGRAM = extproc)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = ora11g)
      (ORACLE_HOME = /opt/app/oracle/product/11.2.0/db_1)
      (SID_NAME = ora11g)
    )
  )
  ---------------------------------------------------------------------

[oracle@tester admin]$ 

修改完後,重啓監聽即可
lsnrctl stop&&lsnrctl start


13 導入導出

一、導入dmp文件

imp
1)imp 用戶名/密碼@服務器IP:端口/服務名  file=dmp文件路徑;
imp username/[email protected]:1521/orcl file=/home/a.dmp;

2)將exp_export.dmp 中的表table1,table2導入
imp system/manager@hostname:1521/ora11g file=exp_export.dmp tables=table1,table2  

impdp
1)導到指定用戶下
impdp scott/tiger DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp SCHEMAS=scott;  

2)改變表的owner
impdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system; 
 
3)導入表空間
impdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp TABLESPACES=example;  

4)導入整個庫文件
 
impdb system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp full=y; 
 
二、導出dmp文件
※ 導入導出指定日誌輸出文件 log=exp_export.log  
※ 在導出命令後面加上 compress=y  可以對導出文件進行壓縮
1.導出整個數據庫實例下的所有數據
2.導出指定用戶的所有表
3.導出指定表

exp
1)將數據庫ORACLE完全導出
exp sysuser/[email protected]:1521/ORCL file=/home/daochu.dmp full=y;

2)將數據庫中WLP用戶與,WLPING用戶的表導出
 
exp username/[email protected]:1521/ORCL file=/home/daochu.dmp   owner=(WLP,WLPING);
 
3)將數據庫中的表table1、table2導出
exp sysuser/[email protected]:1521/ORCL file= /home/newsmgnt.dmp tables=(table1,table2);

4)將數據庫中的表table1中的字段filed1以"00"打頭的數據導出
exp system/manager@loaclhost:1521/orcl file=/home/daochu.dmp tables=(table1) query=\" where filed1 like '00%'\" log=exp_export.log;
expdp
創建邏輯目錄(默認爲oracle用戶空間下的dpdump目錄)
create directory DUMP_DIR as '/oracle/DUMP_DIR';
  
在服務器上創建該目錄,因爲Oracle並不會自動創建,如果目錄不存在導出會報錯
mkdir -p /oracle/DUMP_DIR 

給用戶授予在該目睹讀取的權限
grant read,write on directory DUMP_DIR to scott; 

1)導整個數據庫
expdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp FULL=y;

2)按用戶導
expdp scott/tiger@localhost:1521/ora11g schemas=scott dumpfile=expdp_export.dmp DIRECTORY=DUMP_DIR;
3)並行進程parallel
 
expdp scott/tiger@localhost:1521/ora11g directory=DUMP_DIR dumpfile=expdp_export.dmp parallel=40 job_name=expdp40;
 
4)按表名導
expdp scott/tiger@localhost:1521/ora11g TABLES=emp,dept dumpfile=expdp_export.dmp DIRECTORY=DUMP_DIR;

5)按查詢條件導
expdp scott/tiger@localhost:1521/ora11g directory=DUMP_DIR dumpfile=expdp_export.dmp tables=emp query='WHERE deptno=20';

6)按表空間導
expdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp TABLESPACES=temp,example;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章