oracle琐碎笔记

Oracle知识点

  ps:由于是自己看的所以笔记比较乱,大家谅解

Commit 

rollback

Sql核心语句之select

Selct中要用到以下语句

From语句

Where语句

Group by语句

Group by 来进行分组,分组的参数如果有重复的,只返回一个行值.(比如我按id分组如果记录中有两个id1的记录,这时候只返回一行)

Having语句

将分组汇总后的查询结果限定为只有该子句的条件为真的数据行.

一般情况下having放在groupby后面执行.

Order by语句

对查询结果最终返回的结果进行排序,默认按照升序排序asc

如果想降序排,可在后边加desc,eg:order by column desc

Insert 语句

单表插入

Insert into tablename (column1,column2,.....) values (value1,value2....)

通过查询子句进行插入

Insert into tablename (column1,column2,..) select column1,column2....from tablename2

多表插入:

Insert all 

When sum_orders<10000 then

Into smal_customers

When sum_orders>=10000 and sum_orders<100000 then

Into medium_customers

Else

Into large_customers

Select customer_id ,sum(order_total) sum_orders from oe.orders group by customer_id

 

Update语句

//创建表副本的一种方法

Create table user1 as select * from user

Alter table user1 add constraint user1_pk primary key (keyid)

比如提高工资的10%

Update user set salary=salary*1.1

Delete 语句

使用where语句删除指定行

Delete from user1 where salary<100000

使用select进行删除

Delete from (select * from user1 where salary<100000)

两者结合

Delete from user1 where salary in (select salary from othertable where name=’yourname’)

Merge语句

Merge语句具有按条件获取要更新或者插入到中的数据行.然后从一个或者多个源头对表进行更新或者向表中插入行两方面的能力.

Merge into tablename t using(

Select id,salary from user1 where salary <100000

) e on (t.id=e.id) 

When matched then

Update set t.salary=e.salary where t.salary=0

Delete where (e.salary>7500)

When not matched then 

Insert (t.id,t.salary) values(e.id,e.salary*1.1)

Where (e.salary<7500)

 

分析函数:

select * from (select billcode,rank() 

over(partition by billcode order by ahd050203 desc)mm from res_00201)  where mm='02'

拼接表中某个字段

   

sql pivot 用法

PIVOT的中文意思是“在枢轴上转动”,比如对于一个2维坐标,将横坐标变成纵坐标,将纵坐标变成横坐标。反映在一个Relational Table上的意思就是:变行为列,变列为行。

   

 

Select查询规则

首先,校验语法是否符合规范,其次校验是否有权限操作数据.最后才是查数据

 

 

oracle绑定变量

 variable Test_id number

SQL> exec :Test_id:=1

 

PL/SQL procedure successfully completed

Test_id

---------

1

 

SQL> select * from sys_users s where s.state=:Test_id 

 

SQL> variable Test_id2 char

SQL> exec :Test_id2:=1

 

PL/SQL procedure successfully completed

Test_id2

---------

1

  

SQL> select * from sys_users s where s.state=:Test_id2

  2  ;

   

 

Sga--共享池

主要包括共享sql区域sql私有区域,数据字典换成,服务器结果换成,其他,保留池

锁存器(在调用库高缓存器中的某个资源时其他会话必须等待)

Sga-缓冲区缓存

在数据库块写入硬盘之前或读取硬盘之后,用来存储数据库块.

查询转换

分为以下几种:

视图合并

其是一种能将内嵌或者存储式视图展开为能够独立分析或者与查询剩余部分合并成总体执行计划的独立查询块的转换.改写后一般不包含视图

语句中包含以下情况会阻止视图合并:

包含解析函数或聚合函数(sum,min,avg,count),集合运算(union,intersect),orderby子句或rownum

不过你可以通过以下方式进行强制合并

Select /*+MERGE(v)*/ from tablename ,(select * from tablename2)v where .....

 子查询解嵌套

和合并视图不同的是位置不同.前者在where之前..后者在where语句里面

   

通过NO_UNNEST提示强制该查询按照所书写的方式进行优选.

   

谓语前推(默认为true)

吧能更多减少检索行的条件放在前面执行 where id=1(类似这种)

 可以使用rownum>1禁止谓语前置

 使用物化视图进行查询重写

该情况发生在一个查询或者查询的某一部分已经被保存为一个物化视图,可以直接使用,不需要重写

建立物化视图

   

自动触发查询重写

   

 

 

 

琐碎知识点:

//null的排序在列的最后  也可以设置nulls first显示在最前面

select AHD050205 from res_00201 order by AHD050205 nulls last

Oracle+的用法

Left join 

Select * from a left join b on a.id=b.id

等价于

Select * from a,b where a.id=b.id(+) 

Right join 

Select * from a right join b on a.id=b.id

等价于

Select * from a,b where a.id(+)=b.id 

 

 

Union intersect以及minus

Union相当于去并集(两边集合中如果有一样的,他也是会合并的,如果想要全部的可以用union all),intersect则是取交集(两个集合中共有的部分)minus则是去同求异

Round函数

   四舍五入

  

  --Oracle trunc()函数的用法
/**************日期********************/
1.select trunc(sysdate) from dual  --2011-3-18  今天的日期为2011-3-18
2.select trunc(sysdate, 'mm')   from   dual  --2011-3-1    返回当月第一天.
3.select trunc(sysdate,'yy') from dual  --2011-1-1       返回当年第一天
4.select trunc(sysdate,'dd') from dual  --2011-3-18    返回当前年月日
5.select trunc(sysdate,'yyyy') from dual  --2011-1-1   返回当年第一天
6.select trunc(sysdate,'d') from dual  --2011-3-13 (星期天)返回当前星期的第一天
7.select trunc(sysdate, 'hh') from dual   --2011-3-18 14:00:00   当前时间为14:41   
8.select trunc(sysdate, 'mi') from dual  --2011-3-18 14:41:00   TRUNC()函数没有秒的精确
/***************数字********************/
/*
TRUNC(number,num_digits) 
Number 需要截尾取整的数字。 
Num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。
TRUNC()函数截取时不进行四舍五入
*/
9.select trunc(123.458) from dual --123
10.select trunc(123.458,0) from dual --123
11.select trunc(123.458,1) from dual --123.4
12.select trunc(123.458,-1) from dual --120
13.select trunc(123.458,-4) from dual --0
14.select trunc(123.458,4) from dual  --123.458
15.select trunc(123) from dual  --123
16.select trunc(123,1) from dual --123
17.select trunc(123,-1) from dual --120

 

MONTHS_BETWEEN(DATE1,DATE2)

 

 

Sql between and用法

select ahd050204 from res_00201 where ahd050204 between 20140101 and  20140506 order by ahd050204 desc;

其中包括区间两端的值

Having的用法

Having子句对group by子句设置条件的方式与where和select的交互式方式类似

SELECT ProductID, AVG(OrderQty) AS AverageQuantity, SUM(LineTotal) AS Total

FROM Sales.SalesOrderDetail

GROUP BY ProductID

HAVING SUM(LineTotal) > $1000000.00

AND AVG(OrderQty) < 3 ;

 

Group by用法

Cube 和rollUp都会对记录进行汇总

select billcode,ahd050203 from res_00201 group by rollup(billcode,ahd050203)

 

select billcode,ahd050203 from res_00201 group by cube(billcode,ahd050203)

 

 

select deptno,job,avg(sal) from emp group by grouping sets( deptno,job);

使用grouping sets可同时得到使用sal,和deptno统计的结果

 

关于bin_to_num中参数的问题

Oracle支持的数据类型只有10进制一种,16进制必须通过字符串的方式给出。

oracle唯一接受二进制的函数是bin_to_num

SQL> select bin_to_num(1, 0, 1) from dual;

BIN_TO_NUM(1,0,1)
-----------------
                5

SQL> select bin_to_num(1, 1, 0) from dual;

BIN_TO_NUM(1,1,0)
-----------------
                6

SQL> select bitand(bin_to_num(1, 0, 1), bin_to_num(1, 1, 0)) from dual;

BITAND(BIN_TO_NUM(1,0,1),BIN_TO_NUM(1,1,0))
-------------------------------------------
                                          4

Oracle bitand( )函数:

返回两个数值型数值在按位进行AND 运算后的结果。

posted @ 2014-05-16 07:01  石奈子0314  阅读(810)  评论(0编辑  收藏  举报