oracle数据库学习总结,基础(2)

1.序列sequence

--创建         create sequence 序列名

create sequence 序列名                   创建序列

start with 数值                                   设置开始位置

increment by 数值                              设置步长

--删除      drop sequence 序列名

特点:默认开始是没有值的

序列名.nextval   每次执行都会增加一次,默认步长为1,序列名.currval  当前序列值,开始时是没有的

作用:作为主键使用,动态的获取主键的值,新增时极大的避免了主键冲突

eg: insert into 表名 values(序列名.nextval,xx,xx.......);

2.索引 index

作用:提升查询效率

特点:显示的创建,隐式地执行(每张表的主键,Oracle会默认给表创建索引)

扫描二维码关注公众号,回复: 5808045 查看本文章

--创建     create index 索引名  on 表名(字段名);

--删除   drop index 索引名;

3.视图 view

视图的出创建必须有dba权限

作用:保护真实表,隐藏重要字段的数据,保护数据;在视图中的操作会映射执行到真实表中,可以手动开启只读模式,在创建语句后使用关键字with read only

--创建    

create view 视图名 as select 对外提供的内容  from  真实表名

--删除

drop view 视图名

4.分页查询

关键字:rownum

rownum:oracle 对外提供的自动给查询结果编号的关键字,与每行的数据没有关系(编号从1开始),只能做小于或小于等于判断;

分页规律:每页显示m条数据,查询第n页数据

select 查询内容   from

(select rownum r,表名.查询内容   from 表名 where rownum <=n*m) 表别名

where 表别名.r>(n-1)*m;

5.pl/sql编程语言

主要用来编写存储过程和存储函数

格式:

declare    声明变量

begin   处理过程

end;

eg:    declare           

          i   number(2):=10;               --定义数值型变量i并赋值

          begin 

        dbms_output.put_line(i);          --输出语句

         end;

pl/sql的if判断:

declare   

声明变量                          (可输入变量,格式:变量名 类型:=&任意字符)

begin   

if 条件  then 执行

elseif 条件  then执行

else  执行

end if;

end;

pl/sql中的循环

1.while循环                                       

格式:

declare 

变量声明;

begin 

while 条件 loop

执行

end loop; 

end;

eg:     打印1-9

           declare                 

           i     number(2):=1;

begin

               while  i<10  loop

           dbms_output.put_line(i); 

             i:=i+1;

             end loop;

            end;

2.exit循环

格式:

declare 

变量声明;

begin 

  loop

exit when 条件;

执行;

end loop; 

end;

eg:遍历输出1-10

           declare                 

           i     number(2):=1;

begin

       loop

exit when i>10;

           dbms_output.put_line(i); 

             i:=i+1;

             end loop;

            end;

3.for循环

格式:

declare 

begin 

 for 变量名 in范围 loop

 执行;

end loop; 

end;

eg: 输出1-15

declare 

begin 

for i in 1..15  loop

dbms_output.put_line(i); 

end loop; 

end;

游标cursor

可以存放多个对象,多行记录

格式:

declare 

cursor 游标名  is select * from 表名;

变量名 表名%rowtype;

begin

open 游标名;

loop

fetch 游标名 into 变量名;

exit when 游标名 % notfound;

执行语句;

end loop;

close 游标名;

end;

eg:给指定部门涨工资

declare 

cursor c2(eno emp.deptno%type) 

is select empno from emp where deptno=eno;

en emp.empno%rowtype;

begin

open c2(10);

loop

fetch c2 into en;

exit when c2 % notfound;

update emp set sal=sal+100 where empno=en;

commit;

end loop;

close c2;

end;

其余内容还有触发器和存储过程存储函数,理解不是很深入,所以不写了。。。。

猜你喜欢

转载自blog.csdn.net/qq_38735996/article/details/89080216