Oracle数据库基础知识(二)
视图
---视图的概念:视图就是提供一个查询的窗口,所有数据来自于原表。
---查询语句创建表
create table emp as select * from scott.emp;
select * from emp;
---创建视图【必须有dba权限】
create view v_emp as select ename, job from emp;
---查询视图
select * from v_emp;
---修改视图[不推荐]
update v_emp set job='CLERK' where ename='Albert';
commit;
---创建只读视图
create view v_emp1 as select ename, job from emp with read only;
---视图的作用?
---第一:视图可以屏蔽掉一些敏感字段。
---第二:保证总部和分部数据及时统一。
索引
--索引的概念:索引就是在表的列上构建一个二叉树
----达到大幅度提高查询效率的目的,但是索引会影响增删改的效率。
单列索引
创建单列索引
create index idx_ename on emp(ename);
---单列索引触发规则,条件必须是索引列中的原始值。
---单行函数,模糊查询,都会影响索引的触发。
select * from emp where ename='SCOTT'
复合索引
创建复合索引
create index idx_enamejob on emp(ename, job);
---复合索引中第一列为优先检索列
---如果要触发复合索引,必须包含有优先检索列中的原始值。
select * from emp where ename='SCOTT' and job='xx';---触发复合索引
select * from emp where ename='SCOTT' or job='xx';---不触发索引
select * from emp where ename='SCOTT';---触发单列索引。
pl/sql编程语言
---pl/sql编程语言是对sql语言的扩展,使得sql语言具有过程化编程的特性。
---pl/sql编程语言比一般的过程化编程语言,更加灵活高效。
---pl/sql编程语言主要用来编写存储过程和存储函数等。
---声明方法
---赋值操作可以使用:=也可以使用into查询语句赋值
declare
i number(2) := 10;
s varchar2(10) := '小明';
ena emp.ename%type;---引用型变量
emprow emp%rowtype;---记录型变量
begin
dbms_output.put_line(i);
dbms_output.put_line(s);
select ename into ena from emp where empno = 7788;
dbms_output.put_line(ena);
select * into emprow from emp where empno = 7788;
dbms_output.put_line(emprow.ename || '的工作为:' || emprow.job);
end;
pl/sql中的if判断
---输入小于18的数字,输出未成年
---输入大于18小于40的数字,输出中年人
---输入大于40的数字,输出老年人
declare
i number(3) := ⅈ
begin
if i<18 then
dbms_output.put_line('未成年');
elsif i<40 then
dbms_output.put_line('中年人');
else
dbms_output.put_line('老年人');
end if;
end;
pl/sql中的loop循环
用三种方式输出1到10是个数字
while循环
declare
i number(2) := 1;
begin
while i<11 loop
dbms_output.put_line(i);
i := i+1;
end loop;
end;
exit循环
declare
i number(2) := 1;
begin
loop
exit when i>10;
dbms_output.put_line(i);
i := i+1;
end loop;
end;
for循环
declare
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;
游标
可以存放多个对象,多行记录。
---输出emp表中所有员工的姓名
declare
cursor c1 is select * from emp;
emprow emp%rowtype;
begin
open c1;
loop
fetch c1 into emprow;
exit when c1%notfound;
dbms_output.put_line(emprow.ename);
end loop;
close c1;
end;
-----给指定部门员工涨工资
declare
cursor c2(eno emp.deptno%type)
is select empno from emp where deptno = eno;
en emp.empno%type;
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;
----查询10号部门员工信息
select * from emp where deptno = 10;
存储过程
--存储过程:存储过程就是提前已经编译好的一段pl/sql语言,放置在数据库端
--------可以直接被调用。这一段pl/sql一般都是固定步骤的业务。
----给指定员工涨100块钱
create or replace procedure p1(eno emp.empno%type)
is
begin
update emp set sal=sal+100 where empno = eno;
commit;
end;
select * from emp where empno = 7788;
----测试p1
declare
begin
p1(7788);
end;
----通过存储函数实现计算指定员工的年薪
----存储过程和存储函数的参数都不能带长度
----存储函数的返回值类型不能带长度
create or replace function f_yearsal(eno emp.empno%type) return number
is
s number(10);
begin
select sal*12+nvl(comm, 0) into s from emp where empno = eno;
return s;
end;
----测试f_yearsal
----存储函数在调用的时候,返回值需要接收。
declare
s number(10);
begin
s := f_yearsal(7788);
dbms_output.put_line(s);
end;
---out类型参数如何使用
---使用存储过程来算年薪
create or replace procedure p_yearsal(eno emp.empno%type, yearsal out number)
is
s number(10);
c emp.comm%type;
begin
select sal*12, nvl(comm, 0) into s, c from emp where empno = eno;
yearsal := s+c;
end;
---测试p_yearsal
declare
yearsal number(10);
begin
p_yearsal(7788, yearsal);
dbms_output.put_line(yearsal);
end;
----in和out类型参数的区别是什么?
---凡是涉及到into查询语句赋值或者:=赋值操作的参数,都必须使用out来修饰。
存储过程和存储函数的区别
---语法区别:关键字不一样,
------------存储函数比存储过程多了两个return。
---本质区别:存储函数有返回值,而存储过程没有返回值。
----------如果存储过程想实现有返回值的业务,我们就必须使用out类型的参数。
----------即便是存储过程使用了out类型的参数,起本质也不是真的有了返回值,
----------而是在存储过程内部给out类型参数赋值,在执行完毕后,我们直接拿到输出类型参数的值。
----我们可以使用存储函数有返回值的特性,来自定义函数。
----而存储过程不能用来自定义函数。
----案例需求:查询出员工姓名,员工所在部门名称。
----案例准备工作:把scott用户下的dept表复制到当前用户下。
create table dept as select * from scott.dept;
----使用传统方式来实现案例需求
select e.ename, d.dname
from emp e, dept d
where e.deptno=d.deptno;
----使用存储函数来实现提供一个部门编号,输出一个部门名称。
create or replace function fdna(dno dept.deptno%type) return dept.dname%type
is
dna dept.dname%type;
begin
select dname into dna from dept where deptno = dno;
return dna;
end;
---使用fdna存储函数来实现案例需求:查询出员工姓名,员工所在部门名称。
select e.ename, fdna(e.deptno)
from emp e;
触发器
就是制定一个规则,在我们做增删改操作的时候,
----只要满足该规则,自动触发,无需调用。
----语句级触发器:不包含有for each row的触发器。
----行级触发器:包含有for each row的就是行级触发器。
-----------加for each row是为了使用:old或者:new对象或者一行记录。
语句级触发器
----插入一条记录,输出一个新员工入职
create or replace trigger t1
after
insert
on person
declare
begin
dbms_output.put_line('一个新员工入职');
end;
---触发t1
insert into person values (1, '小红');
commit;
select * from person;
行级别触发器
---不能给员工降薪
---raise_application_error(-20001~-20999之间, '错误提示信息');
create or replace trigger t2
before
update
on emp
for each row
declare
begin
if :old.sal>:new.sal then
raise_application_error(-20001, '不能给员工降薪');
end if;
end;
----触发t2
select * from emp where empno = 7788;
update emp set sal=sal-1 where empno = 7788;
commit;
----触发器实现主键自增。【行级触发器】
---分析:在用户做插入操作的之前,拿到即将插入的数据,
------给该数据中的主键列赋值。
create or replace trigger auid
before
insert
on person
for each row
declare
begin
select s_person.nextval into :new.pid from dual;
end;
--查询person表数据
select * from person;
---使用auid实现主键自增
insert into person (pname) values ('a');
commit;
insert into person values (1, 'b');
commit;
Oracle数据库基础知识(二)相关推荐
- oracle数据库基础知识总结,oracle数据库基础知识学习笔记
oracle数据库基础知识学习笔记 一.oracle数据库类型: Char: 字符型(最大长度2000,定长.不足时以空格补充) Varchar2:字符型 最大长度 4000,变长,实际长度由存储的 ...
- oracle数据库中基础知识,oracle数据库基础知识
oracle数据库基础知识 -- End loop --1 declare pnum number(4):=0; begin while pnum < 10 loop dbms_output.p ...
- oracle数据库sql基础知识,Oracle数据库基础知识为内部培训资料.doc
PAGE 1 课程 IL001100 ORACLE数据库基础知识 ISSUE1.0 开心Java整理 IL001100 ORACLE数据库基础知识 ISSUE1.0 目录 PAGE 1 PAGE 45 ...
- oracle数据库基础知识总结,oracle知识点总结(一)
关键字: oracle,database Oracle SQL(Oracle 9i 9.2.0.1.0) 一.DataBase 保存数据,以表的形式表现数据 二.SQL SQL(structure q ...
- Oracle数据库基础知识+sql语句练习
文章目录 四.数据库 4.1 Oracle 准备知识 SQL基本分类 Oracle数据类型 基本用法 事务 事务特性: 隔离性问题: 隔离级别: 锁 锁的介绍 锁的分类 锁的类型 锁等待和死锁 查看是 ...
- Oracle数据库基础知识(一)
创建表空间 create tablespace xiaomifeng1010 datafile 'c:\xiaomifeng1010.dbf' size 100m autoextend on next ...
- Oracle数据库基础(二)
一.Oracle字符串操作 1.1 字符串类型 1.1.1 CHAR 和 VARCHAR2 类型 表示字符串数据类型,用来在表中存放字符串信息,比如姓名,职业,地址等. CHAR存放定长字符,即存不满 ...
- oracle数据库基础知识总结,我见过的最值得收藏的Oracle数据库知识点总结(III)...
16.select语句和update语句 --select语句的基本用法 select * from myusers; --修改李四的工资为5000 --修改李连杰的工资为10000 update m ...
- oracle 数据库基础知识复习
1. 单表的数据查询 1.1指定字段的数据记录查询 select field1,field2,... FROM 表名 WHERE CONDITION 例: select t.stuname,t.age ...
最新文章
- android 弹幕时间戳,【存档】B站直播数据包分析连载(2018-12-11更新/2020-04-12废止)...
- libevent中的bufferevent
- 实战SSM_O2O商铺_23【商铺列表】Controller层开发
- PathRemoveFileSpec函数
- SAP CRM和Cloud for Customer里客户主数据的层级维护
- Base64压缩UUID长度替换Hibernate原有UUID生成器
- 吉大计算机学院周柚,周柚
- SSH项目搭建-02-配置文件
- zsh fg: no job control in this shell.
- 【python】ssh密码字典攻击
- 三菱FX3U生产方案 FX3U源代码+PCB文件全套生产方案 基于STM32F10的FX3U源码, 可直接使用GXworks2软件
- 联想K31笔记本完全拆解,装不回去了。想做个电视机或者显示器
- 用帕累托图进行数据分析
- 少有人走的路,心智成熟的旅程
- Urllib2库+正则爬取内涵段子
- c++编程规范和范例
- 计算机组成原理:2. 计算机的发展及应用
- 盈一眸恬淡,在明媚的春天等你
- gm修改爆率需要重启服务器吗,传奇私服GM如何调试爆率、刷怪等
- 进程在内存中的样子,以及进程的一生
热门文章
- 8位可控加减法电路设计_100以内加减法速算方法,口算速度快一倍
- Linux:VMware Tools安装方法及共享文件夹设置方法
- ajax验证本服务器,jquery – 使用Ajax调用的http基本身份验证
- qt通过http连接mysql_Qt如何利用MySQL连接远程数据库?
- 洛谷——P1008 [NOIP1998 普及组] 三连击
- for循环的一个复制算法(Java)
- 微信小程序API之showActionSheet(操作菜单)
- TypeScript学习(一):原始数据类型的定义
- Open3d之点云体素下采样
- docker 启动tomcat_docker安装tomcat