1、sqlpus 连接
[root@db-server ~]$su - oracle ##切换oracle用户
[oracle@db-server ~]$ sqlplus /nolog ##启动客户端进程
SQL>conn zyl/zyl@2022 ##普通用户登录
SQL>conn / as sysdba ##管理员登录
Connected.
SQL>
2、启动或关闭数据库
SQL>startup open; ##启动Oracle服务
ORACLE instance started.
Total System Global Area 2455228416 bytes
Fixed Size 2255712 bytes
Variable Size 620758176 bytes
Database Buffers 1811939328 bytes
Redo Buffers 20275200 bytes
Database mounted.
Database opened.
SQL>
SQL>shutdown immediate; ##关闭Oracle服务
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL>
3、查看监听状态
[oracle@db-server ~]$ lsnrctl status ##查看监听状态
[oracle@db-server ~]$ lsnrctl start ##启动
[oracle@db-server ~]$ lsnrctl stop ##关闭
4、查看数据状态
SQL> select status from v$instance;
STATUS
------------
OPEN
SQL>
5、查看数据库版本
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
SQL>
6、查看数据库语言环境
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.WE8MSWIN1252
SQL>
7、查看ORACLE实例状态
SQL> select instance_name,host_name,startup_time,status,database_status from v$instance;
INSTANCE_NAME
----------------
HOST_NAME STARTUP_T
---------------------------------------------------------------- ---------
STATUS DATABASE_STATUS
------------ -----------------
orcl
db-server 26-NOV-22
OPEN ACTIVE
SQL>
8、查看数据库归档模式(未开启归档模式)
##非归档模式(NOARCHIVELOG) 和归档模式(ARCHIVELOG)
SQL> select name,log_mode,open_mode from v$database;
NAME LOG_MODE OPEN_MODE
--------- ------------ --------------------
ORCL NOARCHIVELOG READ WRITE
SQL>
9、开启闪回及归档模式
1、查看闪回恢复区
SQL> show parameter recover;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /u01/app/oracle/recovery_area
db_recovery_file_dest_size big integer 4182M
db_unrecoverable_scn_tracking boolean TRUE
recovery_parallelism integer 0
SQL>
2、开启数据库归档模式,并开启闪回功能
##关闭数据库,启动到mount状态
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 2455228416 bytes
Fixed Size 2255712 bytes
Variable Size 620758176 bytes
Database Buffers 1811939328 bytes
Redo Buffers 20275200 bytes
Database mounted.
SQL>
##开启归档
SQL> alter database archivelog;
Database altered.
SQL>
##开启闪回
SQL> alter database flashback on;
Database altered.
SQL>
SQL> alter database open;
Database altered.
SQL>
3、确认是否已经启用闪回
SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
SQL>
10、关闭闪回及归档
SQL> shutdown immediate;
SQL> startup mount;
##关闭归档
SQL> alter database noarchivelog;
##关闭闪回
SQL> alter database flashback off;
SQL> alter database open;
##清空闪回目录
SQL> alter system set db_recovery_file_dest='';
##查看日志模式
SQL> archive log list;
##查看闪回设置
SQL> show parameter db_recovery_file;
##查看闪回时间设置
SQL> show parameter db_flashback;
11、查看及增加表空间大小
1、查看表空间的名字及文件所在位置
SQL> select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;
TABLESPACE_NAME FILE_ID
------------------------------ ----------
FILE_NAME
--------------------------------------------------------------------------------
TOTAL_SPACE
-----------
SYSAUX 2
/u01/app/oracle/oradata/orcl/sysaux01.dbf
520
SYSTEM 1
/u01/app/oracle/oradata/orcl/system01.dbf
4000
TABLESPACE_NAME FILE_ID
------------------------------ ----------
FILE_NAME
--------------------------------------------------------------------------------
TOTAL_SPACE
-----------
UNDOTBS1 3
/u01/app/oracle/oradata/orcl/undotbs01.dbf
50
USERS 4
/u01/app/oracle/oradata/orcl/users01.dbf
TABLESPACE_NAME FILE_ID
------------------------------ ----------
FILE_NAME
--------------------------------------------------------------------------------
TOTAL_SPACE
-----------
5
2、增大所需表空间大小
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/users01.dbf' resize 4000m;
Database altered.
3、设置表空间自动扩展
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/users01.dbf' autoextend on next 100m maxsize 10000m;
Database altered.
SQL>
4、增加数据文件个数
SQL> alter tablespace users add datafile '/u01/app/oracle/oradata/orcl/users02.dbf' size 1000m;
Tablespace altered.
SQL>
12、查看数据库是否设置了数据库的回收站(recyclebin 功能)
## 10g可以用:alter system set recyclebin=off; alter system set recyclebin=on;来关闭和开启
## 11g以上可以用:alter system set recyclebin=off deferred; alter session set recyclebin=on deferred;来关闭和开启
SQL> show parameter recyclebin;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
recyclebin string ON
SQL>
13、闪回误删除的表—t1
SQL>create table t1_temp as select * from t1;
Table created.
SQL> select * from t1_temp;
ID NAME
---------- --------------------------------
1 jack
2 tom
3 harry
SQL>
##drop table t1_temp; 删除t1_temp表
SQL> drop table t1_temp;
Table dropped.
10g可以用:alter system set recyclebin=off; 和alter system set recyclebin=on;来关闭和开启
11g可以用:alter system set recyclebin=off deferred; 和alter session set recyclebin=on deferred;来关闭和开启
SQL> show recyclebin; ## 此时回收站显示的即为我们删除掉的t1_temp表。
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T1_TEMP BIN$7qhmFTe5JWHgUzy1qMCPEA==$0 TABLE 2022-11-30:11:08:07
SQL>
1、 查询回收站中的表数据
SQL> select * from "BIN$7qhmFTe5JWHgUzy1qMCPEA==$0";
ID NAME
---------- --------------------------------
1 jack
2 tom
3 lucy
SQL>
2、闪回:
SQL> flashback table t1_temp to before drop;
Flashback complete.
SQL>
3、检查闪回结果
SQL> select * from t1_temp;
ID NAME
---------- --------------------------------
1 jack
2 tom
3 lucy
SQL>
14、闪回表中记录到某一时间点
SQL> alter table t1_temp enable row movement;
SQL> flashback table t1_temp to timestamp to_timestamp('2022-10-15 21:17:47','yyyy-mm-dd hh24:mi:ss');
15、查看回收站中对象
SQL> select object_name,original_name,type from recyclebin;
no rows selected
SQL>
16、清空回收站中对象
SQL> purge recyclebin;
Recyclebin purged.
SQL>
17、还原回收站中的对象,将表另外命名test01
SQL> flashback table t1_temp to before drop rename to t1_temp01;
18、查看当前会话
SQL> select sid,serial#,username,program,machine,status from v$session;
19、查看DDL锁
SQL> select * from dba_ddl_locks where owner = 'oracle';
no rows selected
SQL>
20、检查等待事件
SQL> select sid, a.username, event, wait_class, t1.sql_text from v$session a, v$sqlarea t1 where wait_class <> 'idle' and a.sql_id = t1.sql_id;
SID USERNAME
---------- ------------------------------
EVENT
----------------------------------------------------------------
WAIT_CLASS
----------------------------------------------------------------
SQL_TEXT
--------------------------------------------------------------------------------
196 SYS
SQL*Net message to client
Network
select sid, a.username, event, wait_class, t1.sql_text from v$session a, v$sqlar
ea t1 where wait_class <> 'idle' and a.sql_id = t1.sql_id
SID USERNAME
---------- ------------------------------
EVENT
----------------------------------------------------------------
WAIT_CLASS
----------------------------------------------------------------
SQL_TEXT
--------------------------------------------------------------------------------
SQL>
21、检查数据文件状态
SQL> select file_name,status from dba_data_files;
FILE_NAME
--------------------------------------------------------------------------------
STATUS
---------
/u01/app/oracle/oradata/orcl/users01.dbf
AVAILABLE
/u01/app/oracle/oradata/orcl/undotbs01.dbf
AVAILABLE
/u01/app/oracle/oradata/orcl/sysaux01.dbf
AVAILABLE
FILE_NAME
--------------------------------------------------------------------------------
STATUS
---------
/u01/app/oracle/oradata/orcl/system01.dbf
AVAILABLE
/u01/app/oracle/oradata/orcl/users02.dbf
AVAILABLE
SQL>
22、检查表空间使用情况
SELECT UPPER(F.TABLESPACE_NAME) "Table_space_name",
D.TOT_GROOTTE_MB "Table_space_size(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "Space_used(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100, 2),
'990.99') "Use_Rate",
F.TOTAL_BYTES "free_space(M)",
F.MAX_BYTES "top_size(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 4 DESC;
23、增加表空间大小
ALTER TABLESPACE SYSTEM ADD DATAFILE '/u01/app/oracle/oradata/orcl/system03.db' SIZE 100M AUTOEXTEND OFF;