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> create tablespace customerchat logging datafile '/opt/app/oracle/oradata/ora11g/customerchat.dbf' size 100m autoextend on next 32m maxsize 500m extent management local;


先查询表空间路径
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.

查看表空间使用情况

 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_name; -- 单位是M

 

  查看用户默认表空间

SQL> select username,default_tablespace from dba_users;   


创建表空间(数据文件名一定是单引号引起来)

 SQL> create tablespace test_tbsp datafile '/home/oracle/oradata/test_tbsp.dbf' size 100M;    

 

  修改用户表空间

 SQL> alter user user1 default tablespace test_tbsp;    

 

 查看表空间是否自动扩展

SQL> select file_name,autoextensible,increment_by from dba_data_files where tablespace_name='TEST_TBSP';    

 

 表空间开启自动扩展

SQL> alter database datafile '/home/oracle/oradata/test_tbsp.dbf' autoextend on;    

 

  表空间关闭自动扩展

SQL> alter database datafile '/home/oracle/oradata/test_tbsp.dbf' autoextend off; 



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;