oracle常用sql语句集合
创建表abc
Create table abc (a varchar2(10),bvarchar(10));
增加一列
Alter table abc add c number;
删除一列
Alter table abc drop column c;
删除表
Drop abc;
数据控制语言,
授权select权限
Grant select on [tablename] to [user]
收回select权限:
Revoke select on [tablename] from [user]
插入
Insert into abc(a,b) values(‘aaa’,’bbb’);
修改
Update abc set b=’ttt’ where a=’aaa’;
删除
Delete from abc where a=’aaa’;
函数:
常用系统函数
Select length(‘abcdef’) from dual; 字符,如果为汉字的话还是原来的字符数
Select lengthb(‘abcdef’) from dual; 字节 如果为汉字的话则多一个
截掉空字符
Select ltrim(‘ aaa’) from dual; 截取掉左边空格字符
Select rtrim(‘aa ’) from dual 截取掉右边的空格字符
Select trim(‘ aaa ’)from dual 截取掉所有空格字符
Varchar2和char前者是可变的。后者是不可变的长度
将串里面的某一些字符取出来.从2位置开始取3个
Select substr(‘abcder’,2,3) from dual;
取后三位的写法
Select substr(‘abcde’,length(‘abcde’)-3+1,3)from dual;
取得当前时间
Select sysdate from dual;
设置当前时间的格式
Alter session set nls_date_format=’dd-mon-yyyyhh:mi:ss’;
查询当前日期
Select current_date from dual;
当前日期的下一个星期三是那一天
Select next_day(sysdate,’星期三’) from dual;
[sql] view plaincopy
转换函数
转换成字符
Select to_char(sysdate,’yyyy-mm-ddhh24:mi:ss’) from dual;
转换成日期
Select to_date(’12-3月-09’) from dual
转换成number
Select to_number(‘123’) from dual;
查看最大值
Select max(price) from books
最小值
Select min(price) from books
求和
Select sum(price) from books
平均数
Select avg(price) from books
字段的行数
Select count(price) from books
查询表的记录数
Select count(*) from dual;
聚集函数不能和where结合
当前登陆者
Select user from dual;
Decode函数
Decode(sex,’男’1,0) 性别为男的时候就为基数+1如果不为男就清零。然后字段是
男人数来显示
Selectsum(decode(sex.’男’,1,0)) 男人数,sum(decode(sex,’女’,1,0)) 女人数from tablename
将空值替换为未输入列出
Selecta1,nvl(a2,’未输入’) 标签from dual;
将a2为空的查出来
Select *from aa where a2 is null;
升序和降序
Select *from aa order by a1 asc; 升
Select *from aa order by a1 desc 降
去除重复数据
Selectdistinct a1 from aa;
求一下总金额
select sum(price*qty) from books;
进行分组查询(后面group by中的必须要在select中)
Select sum(price*qty) from books group by pub;
Group by和having结合代替where
错误的案例:
这是不对的,就像前面所说的聚合函数不能喝where相结合
Select pub,sum(price*qty) from books where sum(price)>30
所以我们用having子句去结合,如下所示
Select pub,sum(price*qty) from books group by pub having sum(price)>500
查询以a字符开头,以任意单个字符结尾的。
Select * from aa where a1 like ‘a_’
查询以什么a字符开头的记录,以任意单个字符结尾的。
Select * from aa where a1 like ‘a__’ 下划线长一些
Select * from aa where a1 like ‘a%’ 任意一些的
如果不知道字符在哪个位置。也就是模糊查询
Select * from aa where a1 like ‘%a%’;
内连接
同时显示部门名称和员工信息。Select两个表的写法,内连接。公共匹配的
数据
简化的写法
Dept表,employee表
Select d.name,e.name,sex from employee e,dept d where e.id=d.id
标准的写法:
Select d.name,e.name,sex from employee e join dept d on e.id=d.id
外连接:
左连接:
左边员工表的记录全部显示。右边表只显示匹配的,相同的部分显示一次
Select d.name,e.name,sex from employee e,dept d where e.id=d.id(+)
右连接正好相反
左连接和右连接合成为外连接
子查询
In写法和下面的exists是一个意思
Select * from employee e where id not in (select id from dept where id=’3’);
Select * from employee e where id in (select id from dept where id=’3’);
Exists写法和上面的in是一个意思,exists可以用*来匹配。
Select * from employee e where not exists (select * from d where id=e.id)
Select * from employee e where exists (select id from d where id=e.id)
union合并行数据,只是显示,不变化物理数据
select id from e
union
select id from d;
相同的行数据合并。不同的只显示一次
将两个表中合并显示。只是显示相同的行数据
Select id from e
Intersect
Select id from d
用别的表的结果集插入到别的表中
Insert into e(eid,ename) select id,name from d;
复制表
将一行数据放进去表中,把已有表的字段和数据行
放到一个表中用来复制
Create table ttt as select e.id,e.name from e where eid=’001’
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《oracle常用sql语句集合》
本文地址:http://www.xiupu.net/archives-300.html
关注公众号:
微信赞赏
支付宝赞赏