恢复到了1个小时之前的数据!很简单。
注意使用管理员登录系统:
select * from 表名 as of timestamp sysdate-1/12 //查询两个小时前的某表数据!既然两小时以前的数据都得到了,继续怎么做,知道了吧。。
如果drop了表,怎么办??见下面:
drop table 表名;
数据库误删除表之后恢复:( 绝对ok,我就做过这样的事情,汗 )不过要记得删除了哪些表名。
flashback table 表名 to before drop;
2.查询得到当前数据库中锁,以及解锁:
查锁
SELECT /*+ rule */ s.username,
decode(l.type,'TM','TABLE LOCK',
'TX','ROW LOCK',
NULL) LOCK_LEVEL,
o.owner,o.object_name,o.object_type,
s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser
FROM v$session s,v$lock l,dba_objects o
WHERE l.sid = s.sid
AND l.id1 = o.object_id(+)
AND s.username is NOT NULL;
解锁
alter system kill session 'sid,serial';
如果解不了。直接倒os下kill进程kill -9 spid
ORA-28000:账户被锁定
因为密码输入错误多次用户自动被锁定.
解决办法:alter user user_name account unlock;
3.关于查询数据库用户,权限的相关语句:
- 1.查看所有用户:
- select*fromdba_user;
- select*fromall_users;
- select*fromuser_users;
- 2.查看用户系统权限:
- select*fromdba_sys_privs;
- select*fromall_sys_privs;
- select*fromuser_sys_privs;
- 3.查看用户对象权限:
- select*fromdba_tab_privs;
- select*fromall_tab_privs;
- select*fromuser_tab_privs;
- 4.查看所有角色:
- select*fromdba_roles;
- 5.查看用户所拥有的角色:
- select*fromdba_role_privs;
- select*fromuser_role_privs;
1.查看所有用户:
select * from dba_user;
select * from all_users;
select * from user_users;
2.查看用户系统权限:
select * from dba_sys_privs;
select * from all_sys_privs;
select * from user_sys_privs;
3.查看用户对象权限:
select * from dba_tab_privs;
select * from all_tab_privs;
select * from user_tab_privs;
4.查看所有角色:
select * from dba_roles;
5.查看用户所拥有的角色:
select * from dba_role_privs;
select * from user_role_privs;
4.几个经常用到的oracle视图:注意表名使用大写....................
- 1.查询oracle中所有用户信息
- select*fromdba_user;
- 2.只查询用户和密码
- selectusername,passwordfromdba_users;
- 3.查询当前用户信息
- select*fromdba_ustats;
- 4.查询用户可以访问的视图文本
- select*fromdba_varrays;
- 5.查询数据库中所有视图的文本
- select*fromdba_views;
- 6.查询全部索引
- select*fromuser_indexes;
- 查询全部表格
- select*fromuser_tables;
- 查询全部约束
- select*fromuser_constraints;
- 查询全部对象
- select*fromuser_objects;
1. 查询oracle中所有用户信息
select * from dba_user;
2. 只查询用户和密码
select username,password from dba_users;
3. 查询当前用户信息
select * from dba_ustats;
4. 查询用户可以访问的视图文本
select * from dba_varrays;
5. 查询数据库中所有视图的文本
select * from dba_views;
6.查询全部索引
select * from user_indexes;
查询全部表格
select * from user_tables;
查询全部约束
select * from user_constraints;
查询全部对象
select * from user_objects;
5.查看当前数据库中正在执行的语句,然后可以继续做很多很多事情,例如查询执行计划等等
- (1).查看相关进程在数据库中的会话
- Selecta.sid,a.serial#,a.program,a.status,
- substr(a.machine,1,20),a.terminal,b.spid
- fromv$sessiona,v$processb
- wherea.paddr=b.addr
- andb.spid=&spid;
-
- (2).查看数据库中被锁住的对象和相关会话
- selecta.sid,a.serial#,a.username,a.program,
- c.owner,c.object_name
- fromv$sessiona,v$locked_objectb,all_objectsc
- wherea.sid=b.session_idand
- c.object_id=b.object_id;
-
- (3).查看相关会话正在执行的SQL
- selectsql_textfromv$sqlareawhereaddress=
- (selectsql_addressfromv$sessionwheresid=&sid);
(1).查看相关进程在数据库中的会话
Select a.sid,a.serial#,a.program, a.status ,
substr(a.machine,1,20), a.terminal,b.spid
from v$session a, v$process b
where a.paddr=b.addr
and b.spid = &spid;
(2).查看数据库中被锁住的对象和相关会话
select a.sid,a.serial#,a.username,a.program,
c.owner, c.object_name
from v$session a, v$locked_object b, all_objects c
where a.sid=b.session_id and
c.object_id = b.object_id;
(3).查看相关会话正在执行的SQL
select sql_text from v$sqlarea where address =
( select sql_address from v$session where sid = &sid );
6.查询表的结构:表名大写!!
select t.COLUMN_NAME,
t.DATA_TYPE,
nvl(t.DATA_PRECISION, t.DATA_LENGTH),
nvl(T.DATA_SCALE, 0),
c.comments
from all_tab_columns t, user_col_comments c
whEre t.TABLE_NAME = c.table_name
and t.COLUMN_NAME = c.column_name
and t.TABLE_NAME = UPPER('OM_EMPLOYEE_T')
order by t.COLUMN_ID
7.行列互换:
- 建立一个例子表:
- CREATETABLEt_col_row(
- IDINT,
- c1VARCHAR2(10),
- c2VARCHAR2(10),
- c3VARCHAR2(10));
- INSERTINTOt_col_rowVALUES(1,'v11','v21','v31');
- INSERTINTOt_col_rowVALUES(2,'v12','v22',NULL);
- INSERTINTOt_col_rowVALUES(3,'v13',NULL,'v33');
- INSERTINTOt_col_rowVALUES(4,NULL,'v24','v34');
- INSERTINTOt_col_rowVALUES(5,'v15',NULL,NULL);
- INSERTINTOt_col_rowVALUES(6,NULL,NULL,'v35');
- INSERTINTOt_col_rowVALUES(7,NULL,NULL,NULL);
- COMMIT;
- 下面的是列转行:创建了一个视图
- CREATEviewv_row_colAS
- SELECTid,'c1'cn,c1cv
- FROMt_col_row
- UNIONALL
- SELECTid,'c2'cn,c2cv
- FROMt_col_row
- UNIONALL
- SELECTid,'c3'cn,c3cvFROMt_col_row;
- 下面是创建了没有空值的一个竖表:
- CREATEviewv_row_col_notnullAS
- SELECTid,'c1'cn,c1cv
- FROMt_col_row
- wherec1isnotnull
- UNIONALL
- SELECTid,'c2'cn,c2cv
- FROMt_col_row
- wherec2isnotnull
- UNIONALL
- SELECTid,'c3'cn,c3cv
- FROMt_col_row
- wherec3isnotnull;
建立一个例子表:
CREATE TABLE t_col_row(
ID INT,
c1 VARCHAR2(10),
c2 VARCHAR2(10),
c3 VARCHAR2(10));
INSERT INTO t_col_row VALUES (1, 'v11', 'v21', 'v31');
INSERT INTO t_col_row VALUES (2, 'v12', 'v22', NULL);
INSERT INTO t_col_row VALUES (3, 'v13', NULL, 'v33');
INSERT INTO t_col_row VALUES (4, NULL, 'v24', 'v34');
INSERT INTO t_col_row VALUES (5, 'v15', NULL, NULL);
INSERT INTO t_col_row VALUES (6, NULL, NULL, 'v35');
INSERT INTO t_col_row VALUES (7, NULL, NULL, NULL);
COMMIT;
下面的是列转行:创建了一个视图
CREATE view v_row_col AS
SELECT id, 'c1' cn, c1 cv
FROM t_col_row
UNION ALL
SELECT id, 'c2' cn, c2 cv
FROM t_col_row
UNION ALL
SELECT id, 'c3' cn, c3 cv FROM t_col_row;
下面是创建了没有空值的一个竖表:
CREATE view v_row_col_notnull AS
SELECT id, 'c1' cn, c1 cv
FROM t_col_row
where c1 is not null
UNION ALL
SELECT id, 'c2' cn, c2 cv
FROM t_col_row
where c2 is not null
UNION ALL
SELECT id, 'c3' cn, c3 cv
FROM t_col_row
where c3 is not null;
8.下面可能是dba经常使用的oracle视图吧。呵呵
- 1.示例:已知hash_value:3111103299,查询sql语句:
- select*fromv$sqltext
- wherehashvalue='3111103299'
- orderbypiece
- 2.查看消耗资源最多的SQL:
- SELECThash_value,executions,buffer_gets,disk_reads,parse_calls
- FROMV$SQLAREA
- WHEREbuffer_gets>10000000ORdisk_reads>1000000
- ORDERBYbuffer_gets+100*disk_readsDESC;
- 3.查看某条SQL语句的资源消耗:
- SELECThash_value,buffer_gets,disk_reads,executions,parse_calls
- FROMV$SQLAREA
- WHEREhash_Value=228801498ANDaddress=hextoraw('CBD8E4B0');
- 4.查询sql语句的动态执行计划:
- 首先使用下面的语句找到语句的在执行计划中的address和hash_code
- SELECTsql_text,address,hash_valueFROMv$sqlt
- where(sql_textlike'%FUNCTION_T(表名大写!)%')
- 然后:
- SELECToperation,options,object_name,costFROMv$sql_plan
- WHEREaddress='C00000016BD6D248'ANDhash_value=664376056;
- 5.查询oracle的版本:
- select*fromv$version;
- 6.查询数据库的一些参数:
- select*fromv$parameter
- 7.查找你的session信息
- SELECTSID,OSUSER,USERNAME,MACHINE,PROCESS
- FROMV$SESSIONWHEREaudsid=userenv('SESSIONID');
- 8.当machine已知的情况下查找session
- SELECTSID,OSUSER,USERNAME,MACHINE,TERMINAL
- FROMV$SESSION
- WHEREterminal='pts/tl'ANDmachine='rgmdbs1';
- 9.查找当前被某个指定session正在运行的sql语句。假设sessionID为100
- selectb.sql_text
- fromv$sessiona,v$sqlareab
- wherea.sql_hashvalue=b.hash_valueanda.sid=100
1.示例:已知hash_value:3111103299,查询sql语句:
select * from v$sqltext
where hashvalue='3111103299'
order by piece
2.查看消耗资源最多的SQL:
SELECT hash_value, executions, buffer_gets, disk_reads, parse_calls
FROM V$SQLAREA
WHERE buffer_gets > 10000000OR disk_reads > 1000000
ORDERBY buffer_gets + 100 * disk_reads DESC;
3.查看某条SQL语句的资源消耗:
SELECT hash_value, buffer_gets, disk_reads, executions, parse_calls
FROM V$SQLAREA
WHERE hash_Value = 228801498AND address = hextoraw('CBD8E4B0');
4.查询sql语句的动态执行计划:
首先使用下面的语句找到语句的在执行计划中的address和hash_code
SELECT sql_text, address, hash_value FROM v$sql t
where (sql_text like '%FUNCTION_T(表名大写!)%')
然后:
SELECT operation, options, object_name, cost FROM v$sql_plan
WHERE address = 'C00000016BD6D248' AND hash_value = 664376056;
5.查询oracle的版本:
select * from v$version;
6.查询数据库的一些参数:
select * from v$parameter
7.查找你的session信息
SELECT SID, OSUSER, USERNAME, MACHINE, PROCESS
FROM V$SESSION WHERE audsid = userenv('SESSIONID');
8.当machine已知的情况下查找session
SELECT SID, OSUSER, USERNAME, MACHINE, TERMINAL
FROM V$SESSION
WHERE terminal = 'pts/tl' AND machine = 'rgmdbs1';
9.查找当前被某个指定session正在运行的sql语句。假设sessionID为100
select b.sql_text
from v$session a,v$sqlarea b
where a.sql_hashvalue=b.hash_value and a.sid=100
9.树形结构connect by 排序:
- 查询树形的数据结构,同时对一层里面的数据进行排序
- SELECTlast_name,employee_id,manager_id,LEVEL
- FROMemployees
- STARTWITHemployee_id=100
- CONNECTBYPRIORemployee_id=manager_id
- <SPANstyle="BACKGROUND-COLOR:#ff0000">ORDERSIBLINGSBYlast_name;</SPAN>
- 下面是查询结果
- LAST_NAMEEMPLOYEE_IDMANAGER_IDLEVEL
-
- King1001
- Cambrault1481002
- Bates1721483
- Bloom1691483
- Fox1701483
- Kumar1731483
- Ozer1681483
- Smith1711483
- DeHaan1021002
- Hunold1031023
- Austin1051034
- Ernst1041034
- Lorentz1071034
- Pataballa1061034
- Errazuriz1471002
- Ande1661473
- Banda1671473
查询树形的数据结构,同时对一层里面的数据进行排序
SELECT last_name, employee_id, manager_id, LEVEL
FROM employees
START WITH employee_id = 100
CONNECT BY PRIOR employee_id = manager_id
ORDER SIBLINGS BY last_name;
下面是查询结果
LAST_NAME EMPLOYEE_ID MANAGER_ID LEVEL
------------------------- ----------- ---------- ----------
King 100 1
Cambrault 148 100 2
Bates 172 148 3
Bloom 169 148 3
Fox 170 148 3
Kumar 173 148 3
Ozer 168 148 3
Smith 171 148 3
De Haan 102 100 2
Hunold 103 102 3
Austin 105 103 4
Ernst 104 103 4
Lorentz 107 103 4
Pataballa 106 103 4
Errazuriz 147 100 2
Ande 166 147 3
Banda 167 147 3
10.有时候写多了东西,居然还忘记最基本的sql语法,下面全部写出来,基本的oracle语句都在这里可以找到了。是很基础的语句!
- 1.在数据字典查询约束的相关信息:
- SELECTconstraint_name,constraint_type,search_condition
- FROMuser_constraintsWHEREtable_name='EMPLOYEES';
- //这里的表名都是大写!
- 2对表结构进行说明:
- descTablename
- 3查看用户下面有哪些表
- selecttable_namefromuser_tables;
- 4查看约束在那个列上建立:
- SELECTconstraint_name,column_name
- FROMuser_cons_columns
- WHEREtable_name='EMPLOYEES';
- 10结合变量查找相关某个表中约束的相关列名:
- selectconstraint_name,column_namefromuser_cons_columnswheretable_name='&tablename'
- 12查询数据字典看中间的元素:
- SELECTobject_name,object_type
- FROMuser_objects
- WHEREobject_nameLIKE'EMP%'
- ORobject_nameLIKE'DEPT%'
- 14查询对象类型:
- SELECTDISTINCTobject_typeFROMuser_objects;
- 17改变对象名:(表名,视图,序列)
- renameemptoemp_newTable
- 18添加表的注释:
- COMMENTONTABLEemployeesIS'EmployeeInformation';
- 20查看视图结构:
- describeview_name
- 23在数据字典中查看视图信息:
- selectviewe_name,textfromuser_views
- 25查看数据字典中的序列:
- select*fromuser_sequences
- 33得到所有的时区名字信息:
- select*fromv$timezone_names
- 34显示对时区‘US/Eastern’的时区偏移量
- selectTZ_OFFSET('US/Eastern')fromDUAL
- 显示当前会话时区中的当前日期和时间:
- ALTERSESSIONSETNLS_DATE_FORMAT='DD-MON-YYYYHH24:MI:SS';
- ALTERSESSIONSETTIME_ZONE='-5:0';
- SELECTSESSIONTIMEZONE,CURRENT_DATEFROMDUAL;
- SELECTCURRENT_TIMESTAMPFROMDUAL;
- SELECTCURRENT_TIMESTAMPFROMDUAL;
- 35显示数据库时区和会话时区的值:
- selectdatimezone,sessiontimezonefromdual;
- 13普通的建表语句:
- CREATETABLEdept
- (deptnoNUMBER(2),
- dnameVARCHAR2(14),
- locVARCHAR2(13));
- 15使用子查询建立表:
- CREATETABLEdept80
- ASSELECTemployee_id,last_name,
- salary*12ANNSAL,
- hire_dateFROMemployeesWHEREdepartment_id=80;
- 6添加列://altertableEMPaddcolumn(dept_idnumber(7));错误!!
- altertableEMPadd(dept_idnumber(7));
- 7删除一列:
- altertableempdropcolumndept_id;
- 8添加列名同时和约束:
- altertableEMPadd(dept_idnumber(7)
- constraintmy_emp_dept_id_fkreferencesdept(ID));
- 9改变列://注意约束不能够修改的!!
- altertabledept80modify(last_namevarchar2(30));//这里使用的是modify而不是alter!
- 24增加一行:
- insertintotable_namevalues();
- 5添加主键:
- alterTableEMPaddconstraintmy_emp_id_pkprimarykey(ID);
- 11添加一个有check约束的新列:
- altertableEMP
- add(COMMISSIONnumber(2)constraintemp_commission_ckcheck(commission>0))
- 16删除表:
- droptableemp;
- 19创建视图:
- CREATEVIEWempvu80
- ASSELECTemployee_id,last_name,salary
- FROMemployeesWHEREdepartment_id=80;
- 21删除视图:
- dropviewview_name
- 22找到工资最高的5个人。(top-n分析)(行内视图)
- selectrownum,employee_idfrom(selectemployee_id,salaryfrom
- employeesorderbysalarydesc)
- whererownum<5;
- 26建立同义词:
- createsynonym同义词名for原来的名字
- 或者createpublicsynonym同义词名for原来的名字
- 27建立序列:(注意,这里并没有出现说是哪个表里面的序列!!)
- CREATESEQUENCEdept_deptid_seq
- INCREMENTBY10
- STARTWITH120
- MAXVALUE9999
- NOCACHE
- NOCYCLE
- 28使用序列:
- insertintodept(ID,NAME)values(DEPT_ID_SEQ.nextval,'Administration');
- 29建立索引://默认就是nonunique索引,除非使用了关键字:unique
- CREATEINDEXemp_last_name_idxONemployees(last_name);
- 30建立用户:(可能有错,详细查看帮助)
- createuserusername(用户名)
- identifiedbyoracle(密码)
- defaulttablespacedata01(表空间名//默认存在system表空间里面)
- quota10M(设置大小,最大为unlimited)on表空间名//必须分配配额!
- 31创建角色:createROLEmanager
- 赋予角色权限:grantcreatetable,createviewtomanage
- 赋予用户角色:grantmanagertoDENHAAN,KOCHHAR(两个用户)
- 32分配权限:
- GRANTupdate(department_name,location_id)
- ONdepartments
- TOscott,manager;
- 回收权限
- REVOKEselect,insert
- ONdepartments
- FROMscott;
- 36从时间中提取年,月,日:使用函数extract
- selectextract(yearfromsysdate)year,extract(monthfromsysdate),
- extract(dayfromsysdate)fromdual;
- 37使用函数得到数月之后的日期:to_yminterval(‘01-02’)表示加上1年2月,不能够到天!!
- selecthire_date,hire_date+to_yminterval('01-02')ashire_date_newfromemployeeswheredepartment_id=20
- 得到多少天之后的日期:直接日期加数字!
- selecthire_date+3fromemployeeswheredepartment_id=20
- 38一般的时间函数:
- MONTHS_BETWEEN('01-SEP-95','11-JAN-94')
- ADD_MONTHS('11-JAN-94',6)
- NEXT_DAY('01-SEP-95','FRIDAY')
- LAST_DAY('01-FEB-95')
- ROUND(SYSDATE,'MONTH')
- ROUND(SYSDATE,'YEAR')
- TRUNC(SYSDATE,'MONTH')
- TRUNC(SYSDATE,'YEAR')
- 39group语句:和高级的应用语句:
- SELECTdepartment_id,job_id,SUM(salary),COUNT(employee_id)FROMemployees
- GROUPBYdepartment_id,job_id;
- 使用having进行约束:
- 1.groupbyrollup:对n列组合得到n+1种情况
- SELECTdepartment_id,job_id,SUM(salary)FROMemployeesWHEREdepartment_id<60GROUPBYROLLUP(department_id,job_id);
- 2.groupby</str
分享到:
评论