程序员

Oracle笔记

作者:admin 2021-04-13 我要评论

一、创建表空间及用户授权 -- 创建表空间 create tablespace zhous --文件的名字 datafile f:\zhou.dbf --文件的储存地址 size 100 m --文件大小 autoextend on ...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

一、创建表空间及用户授权

-- 创建表空间

create tablespace zhous --文件的名字
datafile 'f:\zhou.dbf' --文件的储存地址
size 100m --文件大小
autoextend on next 10m; --每次扩容大小 


--删除表空间
drop tablespace zhous;


--创建用户
create user zhouxs --用户名字
identified by 123123 --用户密码
default tablespace zhous --默认表空间

--授权
connect --连接角色
resource --开发者角色
dba --超级管理

grant dba to zhouxs; --给用户赋予超级管理员权限

二、对表的基本操作

--创建数据表
create table person(
       pid number(21),
       pname varchar(25)
)

--修改表结构
alter table person add(sex number(2)); --添加一列

alter table person modify sex varchar(2); --修改表字段类型

alter table person rename column sex to sexs; --修改列名称

alter table person drop column sexs; --删除一列

insert into person values('1','张三'); --插入数据

delete from person; --删除整个表
drop from person; --删除表结构

truncate table person; --先删除表再创建表

 
update person set pname='李四' where pid=1 --修改

select * from person; --查询



--创建序列 每次自增1
create sequence s_person;

insert into person values(s_person.nextval,'王五')

三、函数

3.1、单行函数

--scott用户,密码tiger
--解锁scott用户
alter user scott account unlock;

--解锁用户密码(也可以设置密码)
alter user scott identified by tiger;

select * from emp e


--单行函数:作用于一行,返回一个值
--字符函数
select upper('hello') from dual --小写转大写
select lower('HELLO') from dual --小写转大写

--数值函数
select round(45.6,-1) from dual --四舍五入,后面显示保留的小数
select trunc(46.7,-1) from dual --直接截取。不看后面的数字

--日期函数
--查出所偶有员工距离现在有多少天
select sysdate-e.hiredate from emp e 

--算出明天此刻
select sysdate+1 from dual

--算出所有员工入职距离现在有多少个月
select months_between(sysdate,e.hiredate) from emp e;

--算出所有员工入职距离现在有多少年
select months_between(sysdate,e.hiredate)/12 from emp e;

--算出所有员工入职距离现在有多少周
select round((sysdate-e.hiredate)/7) from emp e;

--日期转换函数,日期转字符串
select to_char(sysdate,'fm yyyy-mm-dd hh24:mi:ss') from dual;

--字符串转日期
select to_date('2021-4-2 20:58:56','fm yyyy-mm-dd hh24:mi:ss') from dual;

--通用函数
--null值运算结果都为null
select e.sal*12+nvl(e.comm,0) from emp e;

3.1、多行函数

-多行函数 作用域多行函数,返回一个值

select count(1) from emp; --查询总数量

select sum(e.sal) from emp e; --求和

select min(e.sal) from emp e; --求最大值

select avg(e.sal) from emp e; --求平均值

select max(e.sal) from emp e; --求最大值

四、条件表达式

--条件表达式

--相当于switch

select e.ename,
       case e.ename
         when 'SMITH' then '史密斯'
           when 'ALLEN' then '艾伦'
             else '无名'
               end
 from emp e;
 
 
 --判断工资范围
 select e.sal,
          case
       when e.sal>3000 then '高收入'
         when e.sal>1500 then '中等收入'
           else '低收入'
             end
from emp e;


--oracle专用表达式
select e.ename,
 decode(e.ename,
 'SMITH','史密斯',
 'ALLEN','艾伦',
 '无名') "中文名" 
from emp e;


五、复杂查询和分页查询

--分组查询,group by后面的列才能出现在select 后面,想要出现在group by后面加聚合函数
select e.deptno,avg(e.sal) from
emp e group by e.deptno;



--查询部门平均工资大于2000的人
--所有条件不能使用别名来判断
select e.deptno,avg(e.sal) from
emp e group by e.deptno having avg(e.sal)>2000;

--where过滤分组前的数据,having过滤分组之后的数据
select e.deptno,avg(e.sal) from
emp e 
where e.sal>800
group by e.deptno;


--查询部门大于800的员工工资,再查询除部门平均工资大于2000的人
select e.deptno,avg(e.sal) 
from emp e 
where e.sal>800
group by e.deptno having avg(e.sal)>2000;


--笛卡尔积
select * from emp e,dept d;

--等值连接
select * from emp e,dept d where e.deptno=d.deptno;

--内连接
select * from emp e inner join dept d on e.deptno=d.deptno;

--外连接
--右连接
select * from emp e right join dept d on e.deptno=d.deptno;


--左连接
select * from emp e left join dept d on e.deptno=d.deptno;


--oracle专用的外连接
select * from emp e,dept d where e.deptno(+)=d.deptno;


--自连接
select e1.ename,e2.mgr
from emp e1,emp e2
where e1.mgr=e2.empno


--子查询
select * from emp where sal in
(select sal from emp where ename='scott')

--查询工资和10号部门任意工资一样的员工
select * from emp where sal in
(select sal from emp where deptno=10)


--查询每个部门的最低工资,最低工资员工姓名,员工所在部门
select t.deptno,t.msal,e.ename,d.dname 
from (
select deptno,min(sal) msal from emp group by deptno
)t,emp e,dept d
where t.deptno =e.deptno and t.msal=e.sal
and e.deptno =d.deptno


--分页查询
--rownum行号
--查询一行记录,就会在该行加上一个行号,从一开始,依次递增,不能跳着走
select rownum,e.* from emp e order by e.sal desc

select rownum, t.* from (
select rownum,e.* from emp e order by e.sal desc
) t;

select * from (
select rownum rn,tt.* from(
select * from emp  order by sal desc
) tt where rownum<11 
)  where rn>5

六、视图和索引

--创建视图,必须有管理员权限
create table emp as select * from scott.emp;

select * from emp;
--创建视图
create view v_emp as select ename,job from emp;

--查询视图
select * from v_emp;

--修改视图[不推荐]
update v_emp set job='222' where ename='smith';

--创建只读视图
create view v_emp1 as  select ename,job from emp with read only;

--视图的作用
--①屏蔽敏感字段
--②保证数据的及时统一



--索引
--普通索引
create index idx_name on emp(ename)

select * from emp where ename='KING' --触发单列索引

--复合索引
create index idx_name on emp(ename,job)

select * from emp where ename='KING' and job='xx'; --触发索引

select * from emp where ename='KING' or job='xx'; --不触发索引

七、PLSQL编程

plsql是对sql语言的扩展,使得sql具有过程化编程的特性,灵活高效,主要用来编写过程函数和存储函数。

7.1、变量定义和赋值

--声明方法
--赋值操作  := 也可以 into查询语句赋值
declare
   i number(3):=1; --赋值
   name varchar(10):='张三'; --引用型变量
   ena emp.ename%type;
   emprow emp%rowtype; --记录型变量
begin
  dbms_output.put_line(i);
  dbms_output.put_line(name);
  select ename into ena from emp where empno=7788;
  select * into emprow from emp where empno=7788;
  dbms_output.put_line(ena);
  dbms_output.put_line(emprow.ename ||'的工作为:'||emprow.job);
end;

7.2、PLSQL条件判断和循环

--PLSQL--IF判断 dbms_output.put_line--输出语句
declare
     i number(3):=&ii;
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;


--PLSQL--循环
--三种循环输出1-10
declare
  i number(2):=1;
begin
  while i<11 loop --i<10为退出条件
       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 cl is select * from emp; --创建游标,游标的数据来自emp表
  emprow emp%rowtype; --用来保存一行数据
begin --开始
  open cl; --打开游标
       loop --开始循环
         fetch cl into emprow; --将遍历结果放到变量
         exit when cl%notfound; --没找到退出
         dbms_output.put_line(emprow.ename); --输出信息
         end loop; --结束循环
         close cl; --关闭游标
  end; --退出
  
--游标:可以存放多个对象,多行记录。
--输出emp表中的所有数据
declare
  cursor cl is select * from emp; --创建游标,游标的数据来自emp表
  emprow emp%rowtype; --用来保存一行数据
begin --开始
  open cl; --打开游标
       loop --开始循环
         fetch cl into emprow; --将遍历结果放到变量
         exit when cl%notfound; --没找到退出
         dbms_output.put_line(emprow.ename); --输出信息
         end loop; --结束循环
         close cl; --关闭游标
  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;
  
select * from emp where deptno=10;

九、存储过程、存储函数

9.1、存储过程

存储过程就是提交编译好的plsql放到数据库端可以直接被调用。

--存储过程
create or replace procedure pl (eno emp.empno%type)
is
begin
  update emp set sal=sal+1000 where empno=eno;
  commit;
  end;

declare
begin
 pl(7788);
  end;
  
--存储过程输出参数
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;

--测试
declare
  yearsal number(10);
begin
  p_yearsal(7788,yearsal);
  dbms_output.put_line(yearsal);
end;


--in 和out的区别
--涉及到into查询语句或者:=赋值的参数,都需要out来修饰

9.2、存储函数

--存储函数
--计算员工年薪
create or replace function f_years_sals (eno emp.empno%type) return number --返回类型为number
is
       s number(10); --定义变量用来接收返回结果
begin
  select sal*12+nvl(comm,0) into s from emp where empno=eno;
  return s;
end;

--测试
declare
  s number(10);
begin
  s :=f_years_sals(7788);
   dbms_output.put_line(s);
  end;
  

存储过程和存储函数的区别

语法区别:关键字不一样。

本质区别:存储函数有返回值(比存储过程多了两个返回值),存储过程没有返回值。

十、触发器

10.1、触发器的概念

当我们做增删改的时候如果需要做出什么操作,可以使用触发器。

10.2、触发器分类

语句级触发器:不包含for each row 的触发器。

行级触发器:包含or each row 的触发器,加触发器是为了old或者new对象。

--语句级触发器
create or replace trigger t1
after
insert
on person
declare
begin
   dbms_output.put_line('新员工入职');
  end;
  
insert into person values(4,'小黑');

--行级触发器
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;

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;
    
select * from person;


insert into person(pname) values('星星')
;原文链接:https://blog.csdn.net/qq_44982298/article/details/115410715

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 四两拨千斤——你不知道的VScode编码Ty

    四两拨千斤——你不知道的VScode编码Ty

  • 我是如何在 Vue 项目中做代码分割的

    我是如何在 Vue 项目中做代码分割的

  • position:sticky 粘性定位的几种巧妙应

    position:sticky 粘性定位的几种巧妙应

  • 从零到一搭建React组件库

    从零到一搭建React组件库

腾讯云代理商
海外云服务器