Oracle常用总结
1、查看表空间详细信息
select * from dba_data_files order by tablespace_name
create tablespace HEATPUMP logging datafile 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\heatpump01.DBF' size 200m autoextend on next 200m maxsize unlimited extent management local;
select t.tablespace_name, segment_name,sum(bytes)/1024/1024 from user_extents t group by segment_name,t.tablespace_name
alter table TABLE_NAME move tablespace TABLESPACENAME
select t.tablespace_name, 'alter table '|| table_name ||' move tablespace tablespacename;' from user_tables t;
alter index INDEX_NAME rebuild tablespace TABLESPACENAME
select t.tablespace_name, 'alter index '|| index_name ||' rebuild tablespace tablespacename;' from user_indexes t;
create tablespace hr2 logging datafile 'E:\oracle\product\10.2.0\oradata\test\hr2_data1.dbf' size 80m autoextend on next 200m maxsize 20480m extent management local blocksize 8k;
--默认8K,可以修改,其作用见"三"中说明;
alter tablespace USERS add datafile 'E:\oracle\product\10.2.0\oradata\USER_data3.dbf' size 200m autoextend on next 200m maxsize unlimited;
9.创建表
create table TESTDATA2 ( PRIMARYKEY CHAR(30) not null, HOWLONG FLOAT not null, SENSORVALUE varchar2(4000) ) tablespace TBS_USER pctfree 10 initrans 1 storage ( initial 20M minextents 1 maxextents unlimited );
-- Create/Recreate primary, unique and foreign key constraints ,注意修改默认表空间 alter table TESTDATA2 add constraint PK_TESTDATA_2 primary key (PRIMARYKEY, HOWLONG); --tablespace TBS_USER;
drop tablespace TBS_USER including contents; drop tablespace TBS_USER including contents; --DROP TABLESPACE "index01" INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS; DROP TABLESPACE HR2 INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS; --HR1,HR2,TBS_USER alter database datafile 'E:\oracle\product\10.2.0\oradata\TBS_USER_DATA1.dbf' offline drop;
SELECT osuser, username, sql_text from v$session a, v$sqltext b where a.sql_address =b.address order by address, piece;
select * from v$sqlarea t order by t.LAST_ACTIVE_TIME desc;
SELECT * FROM (SELECT PARSING_USER_ID EXECUTIONS,SORTS,COMMAND_TYPE, DISK_READS,sql_text FROM v$sqlarea ORDER BY disk_reads DESC ) WHERE ROWNUM<11;
select * from dba_data_files order by tablespace_name
select dbf.tablespace_name,dbf.totalspace "总量(M)",dbf.totalblocks as 总块数,dfs.freespace "剩余总量(M)",dfs.freeblocks "剩余块数", (dfs.freespace / dbf.totalspace) * 100 "空闲比例" from (select t.tablespace_name,sum(t.bytes) / 1024 / 1024 totalspace,sum(t.blocks) totalblocks from dba_data_files t group by t.tablespace_name) dbf, (select tt.tablespace_name,sum(tt.bytes) / 1024 / 1024 freespace,sum(tt.blocks) freeblocks from dba_free_space tt group by tt.tablespace_name) dfs where trim(dbf.tablespace_name) = trim(dfs.tablespace_name);
select segment_name,sum(bytes)/1024/1024 from user_extents group by segment_name;
SELECT * FROM dba_data_files WHERE tablespace_name = 'USERS';
ALTER DATABASE DATAFILE 'E:\oracle\product\10.2.0\oradata\orcl\USERS_2.DBF' RESIZE 15000M;
ALTER DATABASE datafile 'E:\oracle\product\10.2.0\oradata\orcl\USERS_2.DBF' autoextend ON NEXT 100M maxsize 20000M;
SELECT * FROM user_ts_quotas;
select 'alter table '|| table_name ||' move tablespace tablespacename;' from user_all_tables;
select 'alter index '|| index_name ||' rebuild tablespace tablespacename;' from user_indexes;
select count(*) from testdata2; select max(t.howlong) from testdata2 t; select min(t.howlong) from testdata2 t; select t.PRIMARYKEY,HOWLONG from testdata2 t order by howlong; select t.PRIMARYKEY,HOWLONG,t.SENSORVALUE from testdata2 t where t.howlong<0.0003;
truncate table testdata2; --select t.PRIMARYKEY,HOWLONG,sensorvalue from testdata2 t where t.howlong <0.00008 order by howlong; select * from v$session_wait_history; select * from v$sqlarea;
select * from v$session_wait where event = 'direct path read temp'; select count(*) from sensorconfig;
25.查询出所有的用户表
select * from user_tables
可以查询出所有的用户表
通过表名过滤需要将字母作如下处理select * from user_tables where table_name = upper('表名')
因为无论你建立表的时候表名名字是大写还是小写的,create语句执行通过之后,对应的user_tables表中的table_name字段都会自动变为大写字母,所以必须通过内置函数upper将字符串转化为大写字母进行查询,否则,即使建表语句执行通过之后,通过上面的查询语句仍然查询不到对应的记录。
26.查询出用户所有表的索引
select * from user_indexes
select * from user_indexes where uniqueness='NONUNIQUE'
select * from user_indexes where uniqueness='UNIQUE'
select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name='NODE'
30.查询表的主键
select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' AND cu.table_name = 'NODE'
select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name=au.constraint_name and cu.table_name='NODE'
select * from user_constraints c where c.constraint_type = 'R' and c.table_name='STAFFPOSITION'
select * from user_cons_columns cl where cl.constraint_name = 外键名称
select * from user_cons_columns cl where cl.constraint_name = 外键引用表的键名
select * from user_tab_columns where table_name=upper('表名');方法二:
select cname,coltype,width from col where tname=upper('表名');
show parameter sga;
alter system set sga_max_size=164M scope=spfile;
36.创建触发器,其中定义变量
CREATE OR REPLACE TRIGGER "BEFOREINSERT_INSSTATUS" before insert on INSTRUMENT_STATUS for each row declare nrow number; begin select count(*) into nrow from INSTRUMENT_STATUS t where (t.siteid= :new.siteid) and t.instrumentid= :new.instrumentid; if nrow > 0 then delete from INSTRUMENT_STATUS t where (t.siteid= :new.siteid) and t.instrumentid= :new.instrumentid; end if; end;