`

oracle学习笔记_2 sql基础

阅读更多

sql基础(oracle)

1.语言分类
  * DDL数据定义语言:create、alter、drop
  * DCL数据控制语言:grant、revoke
  * DML数据操纵语言:select、insert、delete、update

 

2.dos里使用sql(sqlplus scott/tiger)
    #varchar2<10>可变长度; char(10)定长不够补空格,浪费存储空间且查询可能查不到
  * create table abc(a varchar2(10),b char(10)); --> alter table abc add c number; --> alter table abc drop column c; --> drop table abc;
  * 复制表(oracle独有): create table ttt as (select * from ee);//根据ee表结果集,去创建ttt表,相当于复制了ee表
    create table xxx as (select eid,ename from ttt where id='001');
  * 授权: grant select on dept to tt; 授权tt用户可以查看dept表
  * 取消授权: revoke select on dept from tt; 取消授权tt用户可以查看dept表
  * 查询: select * from dept; || select * from scott.dept;其他用户访问dept表方式
  * 添加: insert into abc values('hello','xx');每列都插 || insert into abc(a,b) values('hello','xx');
  * 批量添加(oracle独有): insert into abc(eid,ename) select id,name from test;//把test表的结果集类型同的,批量添加到abc表中。
  * 修改: update abc set a='junjun',b='test123' where a='hello';
  * 删除:delete from abc where a='hello';

 

3.常用系统函数
  * 字符: length , ltrim , replace , substr , trim [例如: select length(trim('sadffssf')) from dept;]
  * 日期: Sysdate , current_date , next_day
    当前系统时间:select sysdate from dual ;
    设置时间格式:alter session set nls_date_format='dd-mon-yyyy hh:mi:ss';
    给定时间的下个星期三是哪天:select next_day(sysdate,'星期三')from dual;
  * 转换: To_char , to_date , to_number
  * 聚集函数: Sum , avg , max , min , count (不能作为条件出现在where后面)
  * 其他: user , decode , nvl
    查询登录账号: select user from dual;
    decode判断是男就加1的使用: select sum(decode(sex,'男',1,0)) 男人数,sum(decode(sex,'女',1,0)) 女人数 from dual;(下面有详解)
    判断null值标注为'未输入的':select a1,nvl(a2,'未输入的') a2 from test; ||select * from test where a2 is null
    根据升序排列字段: select * from test order by a2 asc; //desc为降序
    重复的只显示一条: select distinct a1 from test;

 

4.分组语句03-2
  * select pub,sum(price*number) from books group by pub; //前面的pub出版社一定要与后面匹配,前有后没有出错。后面可以多于前面
  * select pub,sum(price*number) from books group by pub having sum(price)>30; //聚合函数sum(price)>30不能放where后面,可以放having后,having必须在group by后面。很少用having子句,较多使用where和group by结合,除非where要接聚合函数。

 

5.模糊查询(like)
  * selct * from test where a1 like '%a%';

 

6.表连接(列相加,行相乘)
  * select t1.eid 编号,t1.ename 姓名,t1.sex 性别,t2.name 部门 from test t1,test22 t2 where t1.id=t2.id;
  * select t1.eid 编号,t1.ename 姓名,t1.sex 性别,t2.name 部门 from test t1 join test22 t2 on t1.id=t2.id;
    内连接(a join b on)、左外连接(left join on)、右外连接(right join on) [内连相同、左外连左表全显示、右外连右表全显示]
  * 只合并行(合并行数据),使用union: select eid,ename from e UNION select id,name from d;
  * INTERSECT返回两者都匹配的数据行(oracle独有): select eid,ename from e INTERSECT select id,name from d;

 

7.子查询(子查询使用'in'时,不能用'*')
  * 独立子查询(子与外层无关):select * from test where id IN(select id from test22);
  * 相关子查询:select * from test where id IN(select id from test22 where id=test.id and id='03');
  * 子查询使用EXISTS(oracle独有),子查询中可以使用'*': select * from e where EXISTS (select id from d );//EXISTS判断子查询中有数据才显示出来

 

## DECODE函数相当于一条件语句(IF).它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。函数的参数列表是由若干数值及其对应结果值组成的若干序偶形式。当然,如果未能与任何一个实参序偶匹配成功,则函数也有默认的返回值。区别于SQL的其它函数,DECODE函数还能识别和操作空值.
其具体的语法格式如下:
 DECODE(input_value,value,result[,value,result…][,default_result]);
其中:
input_value 试图处理的数值。DECODE函数将该数值与一系列的序偶相比较,以决定最后的返回结果
value  是一组成序偶的数值。如果输入数值与之匹配成功,则相应的结果将被返回。对应一个空的返回值,可以使用关键字NULL于之对应
result  是一组成序偶的结果值
default_result 未能与任何一序偶匹配成功时,函数返回的默认值
下面的例子说明了,如何读取用户CHECKUP表SEAPARK中的BLOOD_TEST_FLAG列下的项目,作为DECODE函数的实参支持值。
SELECT checkup_type,DECODE(blood_test_flag,’Y’,’Yes’,’N’,’No’,NULL,’None’,’Invalid’)FROM checkup;

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics