oracle学习笔记(二)


一、多行函数又称组合函数(Group Functions)、聚合函数

1、 Types of Group Functions
avg、count、max、min、stddev、sum、variance
avg 求平均数
select avg(nvl(列1,0)) from 表1
count求行数
在where条件中不允许使用聚合函数,但可以使用having avg(列1)>1000
having所起的作用和where一样

二、子查询Subqueries

查询前10行数据
oracle: select * from 表名 where rownum<=10;
sql: select top 10 * from 表名
单行子查询
select * from 表1 where 工资列1>(select avg(工资列1) from 表1)
多行子查询
select * from 表1 where 工资列1 in(select min(工资列1) from 表1 group by 部门列)

三、自定义变量

set verify on/off
show all
help show/set

column lie justify left

四、数据操作语句

1、insert插入语句
向表2里插入数据
oracle:insert into (select 列1,列2 from 表2)values('XXX','XXX');
oracle/sql:insert into(列1,列2)values('XXX','XXX');
从另一个表里复制数据
oracle/sql:insert into 表(列1,列2)select 列1,列2 from 表2

2、update语句
都为: update table set column1='...'[ ,column2='...'] where ...
嵌入子查询的修改
update table set column1=(select column2 form table where columnid=1) where column1='...'

delete删除语句
delete [from] table [where condition]

merge 合并语句
oracle:
merge into 表1 a using 表2 b on (a.id=b.id)
when matched then
update set
a.name=b.name,
a.other=b.other
when not matched then
insert values(b.id,b.name,b.other);
sql:合并insert,update
方法1:
declare @ROWCOUNT int
set @ROWCOUNT=(select count(*) from tb_name where name1='5')
if @ROWCOUNT!=0
update tb_name set name2='55555555' where name1='5'
else
insert into tb_name(name1,name2) values('5','插入')
方法2:
update tb_name set name2='55555555' where name1='6'
if @@ROWCOUNT=0
insert into tb_name(name1,name2) values('6','插入')

五,事务: 隐式、显式的事务

commit提交事务
rollback 回滚事务
locking锁
对并发性系统自动加锁,事务提交后、或回滚后自动解锁。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3