Oracle学习笔记 联系客服

发布时间 : 星期日 文章Oracle学习笔记更新完毕开始阅读24c89b2e59fafab069dc5022aaea998fcd224047

勿传网上!严禁谋利! Oracle学习笔记

常彦博

三、SQL语句的处理过程

3.1 SQL语句处理过程

用户进程sqlplus→建立连接→服务进程Server process oracleSID

↑--创建会话--Oracle server

3.2处理一条select语句

1)分析语句: ①搜索是否有相同语句 ②用hash value计算select语句是否长得一样:大小写,关键字,空格要都一样,不一样则为两条语句,则服务进程会重新分析。若为统一语句,则直接从内存拿执行计划,计算结果

③检查语法、表名、权限 ④在分析过程中给对象加锁 ⑤生成执行计划

2)绑定变量:给变量赋值 3)执行语句:

4)获取数据:将数据返回给用会进程

7

勿传网上!严禁谋利! Oracle学习笔记

常彦博

四、where子句

用where子句对表里的记录进行过滤,where子句跟在from子句后面。

4.1 where子句后面可以跟什么

跟条件表达式:列名、常量、比较运算符(单、多值运算符)、文字值;不能跟组函数!不能跟列别名!

? 注意事项:对列不经过运算的条件表达式效率会更高,建议在写where子句时尽量

不要对列进行运算。

eg:一年的固定费用为70.8元,计算年包在线时长 select base_duration*12 ann_duration from cost where base_cost*12=70.8; 没下面效率高 select base_duration*12 ann_duration from cost where base_cost=70.8/12; 4.2语法和执行顺序 语法顺序:select from where 执行顺序:from where select 4.3字符串是大小写敏感的,在比较时严格区分大小写 1)upper():函数将字符串转换成大写。 2)lower():函数将字符串转换成小写。 3)initcap():函数将字符串转换成首字符大写(是将列中的值大小写转换然后去和等号后的字符串比,而不是把转字符串转换去和列比)。 eg:哪些unix服务器上开通了os帐号huangr select unix_host,os_username from service where os_username = 'huangr';(有结果) where lower(os_username)='HUANGR';(无结果) where lower(os_username)='huangr';(有结果) where upper(os_username)='HUANGR';(有结果) 4.4 where子句后面可以跟多个条件表达式 条件表达式之间用and、or连接,也可用()改变顺序。 4.5 between and运算符 表示一个范围,是闭区间,含义为大于等于并且小于等于。 eg:哪些资费的月固定费用在5元到10元之间 select base_duration,base_cost,unit_cost from cost where base_cost >= 5 and base_cost <= 10; where base_cost between 5 and 10; 4.6 in运算符(多值运算符)

表示一个集合,是离散值,含义为等于其中任意一个值,等价于any。 eg:哪些资费的月固定费用是5.9元,8.5元,10.5元 select base_duration,base_cost,unit_cost from cost where base_cost = 5.9 or base_cost = 8.5 or base_cost = 10.5; where base_cost in(5.9,8.5,10.5); where base_cost =any(5.9,8.5,10.5); 8

勿传网上!严禁谋利! Oracle学习笔记

常彦博

4.7 like运算符

在字符串比较中,可用like和通配符进行模糊查找。

1)通配符:%表示0或多个字符; _表示任意“一个”字符(要占位的)。

? 注意事项:若要查找%和_本身,则需要escape进行转移。 eg:哪些unix服务器上的os帐号名是以h开头的 select os_username from service where os_username like 'h%' eg:哪些unix服务器上的os帐号名是以h_开头的 select os_username from service where os_username like 'h\\_%' escape '\\'; 4.8 is null运算符 测试null值需要用is null。 1)null不能用等于号“=”和不等于号“<>”跟任何值比较,包括它自身。所以不能用“=”和“<>”来测试是否有空值。 2)即:null=null是不成立的;null不等于 null也不成立;null和任何值比较都不成立。 eg:列出月固定费用是5.9元,8.5元,10.5元或者没有月固定费。 select base_duration,base_cost,unit_cost from cost where base_cost in (5.9,8.5,10.5,null);(错误) where base_cost in (5.9,8.5,10.5) or base_cost is null;(正确) 4.9比较和逻辑运算符(单值运算符) 1)比较运算符:= > >= < <= 2)SQL比较运算符:between and、in、like、is null 3)逻辑运算符:and、or、not 4.10多值运算符all、any 1)>all:大于所有的,等价于 >(select max()?)。 2)>any:大于任意的,等价于 >(select min()?)。 4.11运算符的否定形式 1)比较运算符:<> != ^= 2)SQL比较运算符:not between and not in not like is not null ? 注意事项: ? in相当于=or =or =or等价于any ? not in等价于 <>and <>and <>and等价于<>all ? not between and 小于下界 or 大于上界

? 集合中有null,对in无影响;但对not in有影响,有一个就没有返回值! eg:哪些资费信息的月固定费用不是5.9元,8.5元,10.5元 select base_duration,base_cost,unit_cost from cost where nvl(base_cost,0) <> 5.9 and nvl(base_cost,0) <> 8.5 and nvl(base_cost,0) <> 10.5; where nvl(base_cost,0) not in (5.9,8.5,10.5); 9

勿传网上!严禁谋利! Oracle学习笔记

常彦博

五、order by子句

select语句输出的结果安记录在表中的存储顺序显示,order by子句能够改变记录的输出顺序。

order by子句对查询出来的结果集进行排序,即对select子句的计算结果排序。

5.1语法和执行顺序

语法顺序:select from where order by

执行顺序:from where select order by

5.2升降序

ASC-升序,可以省略,默认值 order by nvl(base_cost,0); DESC-降序 order by unix_host,create_date desc; ? 注意事项:order by是select语句中最后一个子句 5.3 null值在排序中显示 1)被排序的列如果包含null值,用ASC方式null值的在最后; 2)用DESC方式null在最前面; 5.4 order by后面可以跟什么 可以跟列名、列别名、列位置(数字)、表达式、函数。 order by 1:表示列位置为1的列 select 1 from:表示常量1 eg:按年固定费用从大到小的顺序显示资费信息 方式一: select id,base_cost*12 ann_cost,base_duration ann_duration from cost order by base_cost desc; 方式二: select id,base_cost ann_cost,base_duration ann_duration from cost order by base_cost*12 desc;排序的效果和上面是一样的,但前一个效率高。 5.5多列排序 order by子句后面可以跟多列,而order by后面的列可以不出现在select后面。结果集先按第一列升序排列,若列值一样,再按第二列降序排列。 eg:按unix服务器ip地址升序,开通时间降序显示业务帐号信息 select id,unix_host,os_username,create_date from service order by unix_host,create_date desc; 10