oracle应用

面试题:表空间和数据文件dbf的对应关系

每一个表空间可以对应多个数据文件,每个数据文件只能对应一个表空间

今天内容

1.常用数据库对象,重点序列对象

2.PL/SQL编程:存储过程,函数

3.数据库对象

1.表

2.视图  :是sql语句  select 语句的集合

作用:1简化业务流程,简化复杂sql语句的编写 

           2.提高安全性:只能查看自己“该看”的数据

角色权限  connect 链接       resource   对对象的数据操作 insert  update delete select

dba DDL中创建视图等其他对象的权限

视图中包含一下情况 不允许插入数据

1,使用 group 函数 group by 字句 distince 关键字

2.表达式列

3.rownum伪列

4,视图中没有查询的列,且该列不能为空,无默认值

视图的有点:  简化sql ,限制数据访问,数据更安全

3.oracle中DML,DCL,DLL的区别

https://www.2cto.com/database/201711/695361.html

面试题

练习:   统计最高  最低 平均 薪资

--创建视图

create or replace  view  view_emp
as
select  ret.* ,dept.dname
from(select emp.deptno , max(sal) ,min(sal),avg(sal) from emp
group by emp.deptno) ret
inner join dept  on ret.deptno=dept.deptno

--对视图进行DML操作

create  or replace view v_emp2
as
select *from emp
where deptno=30

--添加约束   只能修改where条件中的特定
with check option constraint ck_deptno
select *from v_emp2
update v_emp2 set sal=sal+100
where deptno=30

【数据字典】 1,基本的,不会被修改的信息,

声明为数据字典  例如:政治面貌  民族

 2.DB中数据字典

     视图中包含以下情况,不允许插入数据:

  1. 使用group函数  group by字句 distince关键字
  2. 表达式列
  3. Rownum 伪列
  4. 视图中没有查询的列,且该列不能为空,也没有默认值
  5. 视图的有点:
  6. 简化SQL
  7. 限制数据的访问,更安全

--数据字典,存储了视图的相关信息

select *from user_views

二,序列

mysql sqlserver  自增列 /标识列   特点:只属于单独的表

oracle 使用 的是序列

自动产生唯一数值  是一个可共享的对象  用于主键值的创建 

create sequence 列名

increment by  每次加几个

strat with 从几开始

maxvalue   最大值

minvalue  最小值

nocycle   不循环

cache   ? 缓存里放?个值

--序列对象的使用 m默认:从1开始每次增一,没有最大值,不循环
create table t1(cusid number(5,0) primary key,
teat varchar2(20)
)
create sequence seq_id
start with 10
increment by 2
nomaxvalue    
nocycle    --不循环
cache 10   --缓存里放10 个序列

--修改序列
alter sequence seq_test
start with 1000 --不能修改初始值
increment by -1
nocache

--删除序列

drop sequence seq_test

--使用序列,可共享

insert into t1 values(seq_id.nextval,'嘻嘻')

select *from t1
--nextval产生下一个序列值  一旦产生后,不会重复生成
select seq_id.nextval
from dual
--currval现有的序列值
select seq_id.currval
from dual
--到数据字典中查看序列
select *from
user_sequences

--强调 :

--1.使用习惯, 每个表单单独创建序列

--2已经产生的值,不会重复产生

create sequence sep_test
start with 10000
increment by -2
minvalue 0
cache 10

产生不连续的原因

--创建存储过程
create or replace procedure usp_outtime
is
begin
  dbms_output.put_line(sysdate);
  end ;
  --调用存储过程
  begin
    usp_outtime;
   end;

3.索引

可以有效地提高Oracle服务器检索记录的速度

通过快速路径访问方法迅速定位数据减少磁盘的I/O操作

独立于创建索引的表 Oracle服务器自动使用和维护索引

 1.如何优化sql语句

两种  手动创建  自动创建

商品表针对商品名称创建索引,提高效率

需要手动创建索引

2.原则 

经常用于WHERE子句或作为连接条件的列

所含数据值范围比较大的列

含有大量空值的列

表比较大,但大部分查询返回的结果集小于其总记录的2-4%

4.web程序要和DB之间交互数据

存储过程和函数的区别:存储过程无返回值  函数有返回值

存储过程可以有参数:

参数分:

  1. 输入参数 默认的  in :向存储过程内部传值
  2. 输出参数 out :相当于方法的返回值
  3. 输入、输出参数:in out 技能传入值、输出值
  4. 作业:

    使用存储过程/函数:参数: 主键  表名  pagesize(每页显示的记录数)

                     返回值:页数

函数:有返回值

--存储过程

create or replace procedure usp_empum --创建或替代程序 程序名

(

pdeptno in number  , --部门编号 需要调用时转入,默认为10部门

empnum out  number  --统计出的部门的人数

avgsal out number --统计出的平均工资

)

is   ---是

begin   --开始

select count(empno) into empnum from emp where deptno=pdeptno;   --查询语句

end;  --结束

--回调函数

declare

  dno number(2,0);

eno nuber(2,0);

avgsal number(7,2);

begin

 dno:=30;

usp_empnum(dno ,eno ,avgsal);

dbms_output.put_line(dno||'部门的人数为:'||eno||'平均工资'||avgsal);

end;

--创建一个备份表

create or replace procedure usp_cop

(

p_deptno dept.deptno%type    --参数列表

)

is

begin

create table empcopy    --复制表

as 

select * from  emp where emp.deptno=p_deptno;

end

--【说明】存储过程里的语句必须是编程语句计算并赋值及输出等

--不能是普通DML 及查询语句

--删除存储过程

drop procedure usp_copy;

--数据字典

select *from user_objects where object_type '= procedure' and status='VALID'

【重点】 函数

select *from emp for update(可以修改表)

--upper大写

select upper(ename) from emp

--lower 小写

select lower(ename) from emp

--initcap   首字母大写,其余小写

select initcap(ename) from emp

-- 统计总薪资

--nvl(comm,0)

select ename ,sal+nvl(comm,0) as totalsal from emp

--case表达式

select *from emp

select ename,

case  job

when 'SALESMAN' THEN ‘销售人员’

WHEN 'MANAGER' THEN '经理'

else '未知'

end as 职务 , sal as 薪资

from emp


-- 函数
--参数是部门编号,返回值是部门的最低薪资

create or replace function fun_sal

(p_deptno emp.deptno%type   --参数列表 不能写长度

)

return  emp.sal%type --返回数据类型

is

msal emp.sal%type;

begin

select min(sal) into msal from where deptno=p_deptno;

return emp.sal%type

end;

--调用函数

declare

pdno emp.deptno%type;

misal emp.sal%tyle;

begin

pdno :=20;

misal :=fun_sal(pdno);

dbms_output.put_line(misal);

end;

猜你喜欢

转载自blog.csdn.net/qq_42981890/article/details/84280847
今日推荐