Oracle 一些简单操作
登录oracle
以root用户切换到oracle数据库用户:su - oracle
输入sqlplus /nolog 不连接任何数据库
conn /as sysdba 用sysdba登录
startup 开启实例
shutdown 关闭实例 shutdown immediate (立即关闭实例)
conn 用户名/密码 也可以登录
查询当前实例名
select instance_name from v$instance;
show parameter instance_name
指定某个实例进行登录:sqlplus system(用户名)/jhy(密码)@orcl(实例) as sysdba
oracle实例设置开机启动:
[root@centoscms oracle]# vi /etc/oratab
orcl:/oracle/app/product/10.2.0/db_1:Y
查看监听:
lsnrctl start|stop|status (启动|停止|活动状态 监听)
查询命令:
desc dba_users; (查询dba_users表结构)
select username from dba_users; (查询当前sid下的所有用户的username)
select count(*) from username.tablename; (查询tablename表的行数)
drop user username cascade; (删除名称为username的oracle用户)
select distinct table_name from user_tab_columns; (查看当前user模式下所有表名)
select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&table_name');
查看ASM磁盘使用情况
select name,total_mb,free_mb from v$asm_disk;
select name,total_mb,free_mb from v$asm_diskgroup;
select state,name,type,total_mb, free_mb from v$asm_diskgroup_stat;
查看ASM磁盘路径
select name, path, group_number from v$asm_disk_stat;
asmcmd命令使用
在ASM中建表
表空间:
create tablespace ceshi001 datafile '+DATA2/RAC/DATAFILE/ceshi001.dbf' size 1024m autoextend on next 100m maxsize unlimited extent management local segment space management auto;(next 100m:表空间大小超过原定大小时,每次自动扩展100m,这里中间不要加size,否则会报错,提示数值无效)
commit:提交之前的操作
临时表空间:
create temporary tablespace temp001 tempfile '+DATA2/RAC/TEMPFILE/temp001.dbf' size 200m autoextend on next 20M maxsize unlimited extent management local;
临时表空间操作参考:http://www.cnblogs.com/kerrycode/p/4006840.html
查看表空间详细信息
select * from dba_data_files;
select * from dba_temp_files;
查看某表的大小
select segment_name,bytes from dba_segments where segment_type='TABLE' and segment_name='DSE_TASK1';
select * from user_tables;
查看用户下所有的表
select instance_name,status,version,database_status from v$instance;
echo $ORACLE_HOME
查询所有用户
select username from dba_users;
查看指定用户有哪些系统权限
select * from dba_role_privs where grantee=upper('username');
查看指定用户有哪些对象权限
select * from dba_tab_privs where grantee=upper('username');
创建临时表空间
CREATE TEMPORARY TABLESPACE temp001 TEMPFILE '/u01/app/oracle/oradata/temp001.dbf' SIZE 200m AUTOEXTEND ON NEXT 32m MAXSIZE 2048m EXTENT MANAGEMENT LOCAL;
指定默认临时表空间
ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp001;
创建表空间
create tablespace test001 datafile '/u01/app/oracle/oradata/test001.dbf' size 10240m AUTOEXTEND ON EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
创建用户
CREATE USER test001 IDENTIFIED BY shuyi123 DEFAULT TABLESPACE test001 temporary tablespace temp001;
create user ceshi identified by shuyi123 default tablespace ceshi001 temporary tablespace temp001;
授权
grant dba to ceshi001;
grant connect,resource to ceshi001;
grant unlimited tablespace to ceshi001;
grant create database link to ceshi001;
grant select any sequence,create materialized view toceshi001;
用户B 要 访问用户A 的表:
用system登录,将用户A的表授权给public或者 B
grant select on A.table1 to public; grant select on A.table to B;
或者用A登录,将表的select授权给public或者B
撤销权限 revoke
建表
create table dse_task1 (id number not null,task_id number,server_uuid char(1024),proxy_uuid char(1024),repo_uuid char(1024));
插入数据
insert into dse_task1 value( select rownum as id,rownum+10 as task_id,dbms_random.string('x', 40) as server_uuid,dbms_random.string('x',40) as proxy_uuid,dbms_random.string('x',40) as repo_uuid from dual connect by level<=100000);
复制表数据到新表
create table dse_task2 as select * from dse_task1;
查询表空间表容量
select segment_name,tablespace_name,bytes B, bytes/1024 KB, bytes/1024/1024 MB from user_segments where segment_type='TABLE' and tablespace_name='TEST001';
查询当前用户每个表大小
select segment_name,sum(bytes)/1024/1024 from user_extents group by segment_name
清空表数据并释放空间
truncate table dse_task2 drop storage;
// alter table dse_task2 deallocate unused keep 0
清除当前用户的回收站:purge recyclebin;
清楚所有用户的回收站:purge dba_recyclebin;
彻底删除表:drop table dse_task1 purge;
查看数据库的创建日期和归档方式
select created, log_mode from v$database;
删除用户及表空间:
删除用户:drop user xxx;
如果用户的schema中有objects,需要加cascade参数,即drop user xxx cascade;
删除表空间:
可以先将其offline:alter tablespace xx offline;
将磁盘上的数据文件一同删除:drop tablespace xxx including contents and datafiles;
drop tablespace TEST including contents and datafiles;
源文档 <http://blog.itpub.net/23862439/viewspace-1058093/>
查看数据库库对象
select owner, object_type, status , count(*) count# from all_objects group by owner, object_type, status;
查看表空间的名称及大小
SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size FROM dba_tablespaces t, dba_data_files d WHERE t.tablespace_name = d.tablespace_name GROUP BY t.tablespace_name;
select name from v$tablespace;
源文档 <http://blog.itpub.net/23862439/viewspace-1058093/>
查看表空间物理文件的名称及大小
select tablespace_name, file_id, file_name, round(bytes / (1024 * 1024), 0) total_space from dba_data_files order by tablespace_name;
select name from v$datafile;
源文档 <http://blog.itpub.net/23862439/viewspace-1058093/>
查看临时表空间路径
select TABLESPACE_NAME,FILE_NAME from dba_temp_files;
删除日志文件:
清空日志:Alter database clear logfile group 4;
处于current状态的日志文件组是不能够删除的,除非先进行强制日志切换alter system switch logfile;.
alter database drop logfile member 'f:\oracledata\redo4.log';
查询系统中的数据库日志文件组:select group#,member from v$logfile;
查看各日志文件组与日志文件成员的信息:select group#,members,archived,status from V$log;
注意:当一个日志文件组中仅剩一个日志文件的时候该日志文件时不能被删除的。