oracle-分页序列

day 04

伪表  dual
*****************************
伪列  = 假列 = 根本不存在的列
*:不属于表中的真正的列,数据库查询的表添加的列
rownum = 行号  分页的依据
       这是Oracle对查询动态结果动态的编号,用来实现分页查询
       有序的整数列,每多一条自动加1
   注意:
	1)不能和order by在同一个查询语句中
		因为order by会打乱select的查询顺序
	2)不能使用表名.rownum的写法,但可以用:别名.rownum
		老大写的课件:select a.* from 
    			             (select rownum x,lyric.* from lyric where rownum<=12) a  where a.x >=10;
	3)特性
		如果rownum用在where条件之后则:
	 	rownum>=1   rownum>只能是0   rownum<任意数  

1.实现第一页数据,编号1-10
	select rownum,substr(content,1,10) || '...' from lyric
	where rownum between 1 and 10;
2.实现第二页数据,编号11-20
	select rownum,a.* from (select rownum rn,substr(content,1,10)||'..' 
	from lyric where rownum <=20)a where rn>=11;
3.实现第三页数据,编号21-30
	select rownum,a.* from (select rownum rn,substr(content,1,10)||'..'
	from lyric where rownum <=30)a where rn>=21;
4.排序效果的分页
	select rownum,a.* from (select rownum rn,substr(content,1,10)||'..' from
	(select * from lyric order by content) where rownum<=30)a where rn>=21

*****************
curpage:当前页           3
	pagesize:每页行数       25
	当前页的开始数:int startpage = (curpage-1)*pagesize+1    51
	当前页的截止数:int endpage = curpage * pagesize             75
*******************
rowid = 是映射每一行数据物理地址的唯一标识
	适用于删除完全重复的数据
	delete from lyric where rowid not in(select min(rowid) from lyric group by content);
	delete from 表 where rowid not in(select min/max(rowid) from 表 group by 字段);			
			//删除Oracle里的完全重复数据,删198
*******************
下面这些关键字可以连接两个select,但是中间没有分号
***************
联合关键字:
union  结果唯一,重复的都不要
	select * from school where name like '%大'	//结果:北大,厦大
	union						//union之后:北大,厦大
	select * from school where name like '北%';	//结果:北大
	
union all  结果不唯一,是结果就要
	select * from school where name like '%大'	//结果:北大,厦大
	union all						//unoin all之后:北大,厦大,北大
	select * from school where name like '北%';	//结果:北大
***************
intersect 求交集
	select * from school where name like '%大'
	intersect
	select * from school where name like '北%';

minus 从第一个查询结果中,减去第二个查询结果中重复出现的数据
	select * from school where name like '%大'
	minus
	select * from school where name like '北%';

in:表示条件符合查询结果中的某一个值
	条件 in (值1,值2...)
	相当于:条件 = 值1 or 条件 = 值2 or ...
	select * from school where id in (select sid from student);	
				//外面的条件和括号里面比较,相同拿出来,当作结果
					//结果:1 ETOAK,2 清华,4 厦大

not in:表示条件不能是查询结果中的任意一个值
	条件 not in (值1,值2...)
	相当于:条件 <> 值1 and 条件 <> 值2 and ...
	select * from school where id not in (select sid from student);   
		 //外面条件和括号里面比较,不同拿出来,当作结果
			//结果:2 北大

some/any:这两个用法和in相同				
	区别在于:in可以用在无符号的情况
              		   some/any用在有符号的情况
	select * from school where id = some(select sid from student);
	select * from school where id = any(select sid from student);

all:表示比所有值都大 或 都小 >all <all
							//some/any/all:效率较低,用多表查询
	***:						//in效率低:用exists代替
	>any   >min
	<any   <max
	>all      >max
	<all      <min
	select name,salary from student where
	salary < any(select salary from student where cid = 1);
	select name,salary from student where
	salary < all(select salary from student where cid = 1);

*:因为in的效率比较低,所以用exists代替				// not in/in效率低:用exists代替
exists:是否存在     not exists
	用法:不需要用列来比较,只关心后面的查询有没有结果
	select * from school s where exists
	(select * from student st where st.sid= s.id);
************************************************
序列:sequence
	一个单独的数据对象,是一个能够生成有序的整数列值的对象
	***:oracle通过调用序列的形式来实现主键自增		//Oracle需要调用,MySQL不需要调用就能设置自增变量
	***:在一个新的会话中,必须要调用下一个值才能获得当前的值
	***:seq_student.nextval会作为下一个添加的初始值		//next比较有用

create sequence 序列名
create sequence seq_test
increment by 1        --增长,一次增1	//默认值1,默认自增1
************************************
start with 1               --从1开始
minvalue 1               --最小值
maxvalue 100           --最大值
cycle                         --循环       	//默认是nocycle
nocache;                   --不缓存		//默认CACHE 是20
************************************
如何获取下一个列的值:select 序列名.nextval from dual;	//nextval
	select seq_test1.nextval from dual;
	insert into test2 values(seq_test1.nextval,'b');

如何获取当前列的值:select 序列名.currval from dual;	//currval
	select seq_test1.currval from dual;

如何查看当前用户下有多少序列
	select * from user_sequences;
*******************************************
如何删除序列
	drop sequence seq_test;

如何修改序列
	 alter sequence seq_test1 increment by 50;
********************************
1.消除延时段创建特性:是序列的新特性
	alter system set deferred_segment_creation = false;
2.创建表			//Oracle_11g新特性,不消除延时,序列会从2开始,必须建表之前就消除延时,再建表
create table test2(
id number(3),
name varchar2(20)
);
********************************
视图 view =>假表,用查询的结果动态生成一张表		//是根据查询结果动态生成的,并不真实存在,视图用关键字as创建
	视图是编译后将查询语言保存到数据库中,下次调用视图
	可以不用编译,直接获取数据			//第二次不用编译,就可以获取数据,可以提高Oracle效率
****************************
create view 视图名 as select id,name from student;		//create view 视图名 as 查询内容 from 表名;
select * from 视图名;
create view v_student as select id,name from student;
select * from v_student;

create view studentName_schoolName as
select student.name sn,school.name cn from student
left join school on student.sid = school.id;
*************************
为什么要使用视图?
	节省编译时间,提高查询效率:视图是编译后将查询语言保存到数据库中,下次调用视图,可以不用编译,直接获取数据
	屏蔽原表中的字段,避免没有权限的用户查询其他的字段(看到不该看的)
		视图中能够根据动态来源于原表(alter view 视图名 compile)
		简单的视图是可以更新原表中的数据
		复杂的视图无法更新(关联出来的数据不能只更新其中的一条)

表空间 tablespace
	oracle中的用户都有属于自己的默认的空间
	在一段内存空间中大部分存储的是表,所以称为表空间

用户的表空间
系统用户的表空间
普通用户的表空间

为什么要给普通用户创建属于自己的表空间?	//多给用户共同用一个数据库,为了解决资源争端问题
	项目中很可能与其他项目使用同一个数据库,
	多个用户在使用同一个数据库的时候有可能访问同一个数据库文件,
	就会资源争用问题,给不同的用户指定不同的表空间,就可以让他们
	使用不同的数据文件,解决资源争用问题。
发布了89 篇原创文章 · 获赞 32 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41345773/article/details/102806439
今日推荐