oracle开窗函数分页,索引、分页、开窗函数

create table text

(

uname varchar(20),

uage int

)

select *from text

insert into text(uname,uage)values('Tom',25)

insert into text(uname,uage)values('Jerry',21)

drop table text

alter table text add AutoID int identity(1,1) primary key

select *from MyStudents

select FMath from MyStudents where FMath<90

索引的优点:

查询快

使用索引能提高查询效率,但是索引也是占据空间的,而且添加、更新、删除数据的时候也需要同步更新索引,因此会降低Insert、Update、Delete的速度。

填充因子

索引是在排序的基础上

子查询:就是把一个查询的结果作为另一个查询的数据源。

分类1.独立子查询:括号里面的查询可以单独执行

2.相关子查询: 括号里面的查询不能单独执行

--查询高二二班的学生, 学生姓名、性别、学号

Use School

select * from Student

select * from Class

select * from Student

where sClassId=

(select clsId

from class where cName='高二二班')

--相关子查询

select * from Student

where exists(select * from Class where cName='高二二班'

and Class.clsId=Student.sClassId)

--查询高二二班和高二一班的学生, 学生姓名、性别、学号

select * from Student

where sClassId in

(select clsId

from class where cName='高二二班' or cName='高二一班')

注意:

子查询返回的值不止一个。当子查询跟随在=、!=、、>= 之后,或子查询用作表达式时,这种情况是不允许的。

--查询刘备/关羽/张飞的成绩

select * from Student

select *from Score

select *from Score

where studentId in

(select sId from Student where sName in('刘备','关羽','张飞'))

select *from Score

where studentId in

(select sId from Student where sName ='刘备' or sName ='关羽' or sName ='张飞')

--删除刘备/关羽/张飞的成绩

delete from Score

where studentId in

(select sId from Student where sName ='刘备' or sName ='关羽' or sName ='张飞')

子查询可以应用在select中之外,还可以应用在delete/update中

exists判断其当中的查询语句是否有数据返回,如果有其值为真,否则为假

if(exists(select * from Student))

begin

print '有结果'

end

else

begin

print '没结果'

end

T_sql

if(exists(查询))

begin//相当于“{”

print ‘有’

end//相当于“}”

else

--分页

use Test

select * from MyStudents

--分页,每页5条记录

前提:将记录排序

select * from MyStudents

order by FId desc

--第一页

select top 5 * from MyStudents

where FId

--第二页

select top 5 * from MyStudents

where FId not in

(select top (5*4) FId from MyStudents

where FId )

order by FId

--查询第七页得数据

select *,

row_number()over(order by FId) as rnumber

from MyStudent

--查询第七页

select * from

(

select *,

row_number()over(order by FId) as rnumber

from MyStudent

)

as Tbl13

where rnumber between(5*6+1)and 5*7

--开窗函数over(),与聚合函数一起使用时,可以给每条记录一个聚合的值

select *,count(*) over() as 总记录条数

from MyStudent

表联结(jion)

--查询系统中所有的数据库

select *from sys.databases

--查看数据库是否存在

//select *from sys.databases where name='School'

if(exists(select *from sys.databases where name='School'))

begin

print'存在'

end

else

begin

print'不存在'

end

--------------------

Union ,将两个表进行联合。联结的是行(记录),使用join联结的是列(字段)

use School

select * from student

select * from Class

select

sName,

sAge,

cNmae

from student as TS

inner join Class TC

on Class.clsId=Student.sClassId

当要查询的多个列在不同的表中,进行跨表查询使用inner join

--使用子查询完成以上题

select

sName,

sAge,

班级名称=

(select cNmae from Class where Class.clsId=Student.sClassId)

from Student

--使用inner join实现的查询,就能用子查询exists,但是反过来不成立

--查询年龄超过20的姓名、年龄、班级

select

sName,

sAge,

cNmae

from student as TS

inner join Class TC

on TC.clsId=TS.sClassId

where sAge>20

--查询学生姓名、年龄、班级、成绩

select

sName,

sAge,

cNmae,

english,

math

from student as TS

inner join Class TC on TC.clsId=TS.sClassId

inner join Score TSC on TSC.sClassId=TS.sId

delete from Score where studentId=1 or studentId=4 查询出所有参加考试的同学的学生编号、姓名、考试成绩 select sId  sName,  english,  math from Student inner join Score on Score.studentId=Student.sId

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值