Oracle笔记


Posted in Oracle onApril 05, 2021

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

-- 创建表空间

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('星星')
Oracle 相关文章推荐
Oracle创建只读账号的详细步骤
Jun 07 Oracle
快速学习Oracle触发器和游标
Jun 30 Oracle
oracle连接ODBC sqlserver数据源的详细步骤
Jul 25 Oracle
RPM包方式安装Oracle21c的方法详解
Aug 23 Oracle
Oracle 临时表空间SQL语句的实现
Sep 25 Oracle
C#连接ORACLE出现乱码问题的解决方法
Oct 05 Oracle
关于Oracle12C默认用户名system密码不正确的解决方案
Oct 16 Oracle
Oracle 触发器trigger使用案例
Feb 24 Oracle
解决Oracle数据库用户密码过期
May 11 Oracle
oracle数据库去除重复数据
May 20 Oracle
在Oracle表中进行关键词搜索的过程
Jun 10 Oracle
oracle设置密码复杂度及设置超时退出的功能
Jun 28 Oracle
oracle DGMGRL ORA-16603报错的解决方法(DG Broker)
Apr 06 #Oracle
ORACLE数据库对long类型字段进行模糊匹配的解决思路
Oracle 数据仓库ETL技术之多表插入语句的示例详解
oracle表分区的概念及操作
Apr 24 #Oracle
Oracle设置DB、监听和EM开机启动的方法
mybatis使用oracle进行添加数据的方法
Apr 27 #Oracle
使用springboot暴露oracle数据接口的问题
You might like
php fckeditor 调用的函数
2009/06/21 PHP
php文件缓存类用法实例分析
2015/04/22 PHP
thinkphp3.2同时连接两个数据库的简单方法
2019/08/13 PHP
thinkphp 框架数据库切换实现方法分析
2020/05/18 PHP
分享几种好用的PHP自定义加密函数(可逆/不可逆)
2020/09/15 PHP
JavaScript延迟加载
2021/03/09 Javascript
关闭浏览器时提示onbeforeunload事件
2013/12/25 Javascript
javascript数据类型详解
2017/02/07 Javascript
jquery图片放大镜效果
2017/06/23 jQuery
Angular 2父子组件数据传递之@ViewChild获取子组件详解
2017/07/04 Javascript
详解Angular.js中$http拦截器的介绍及使用
2017/07/04 Javascript
vue-cli项目中使用echarts图表实例
2018/10/22 Javascript
JS字符串常用操作方法实例小结
2019/06/24 Javascript
如何利用 JS 脚本实现网页全自动秒杀抢购功能
2020/10/12 Javascript
Python实现PS图像调整黑白效果示例
2018/01/25 Python
Python实现线程状态监测简单示例
2018/03/28 Python
Python线程下使用锁的技巧分享
2018/09/13 Python
python通过txt文件批量安装依赖包的实现步骤
2019/08/13 Python
解决pycharm 安装numpy失败的问题
2019/12/05 Python
基于TensorFlow中自定义梯度的2种方式
2020/02/04 Python
k-means 聚类算法与Python实现代码
2020/06/01 Python
使用HTML和CSS3绘制基本卡通图案的示例分享
2015/11/06 HTML / CSS
无毒社区工作方案
2014/05/23 职场文书
个人综合鉴定材料
2014/05/23 职场文书
十佳少先队员演讲稿
2014/09/12 职场文书
交通事故委托书范本(2篇)
2014/09/21 职场文书
单方离婚协议书范本(2014版)
2014/09/30 职场文书
旷课检讨书500字
2014/10/14 职场文书
涉外离婚协议书怎么写
2014/11/20 职场文书
模范教师材料大全
2014/12/16 职场文书
监理中标通知书
2015/04/16 职场文书
2015夏季作息时间调整通知
2015/04/24 职场文书
复活读书笔记
2015/06/29 职场文书
公安干警正风肃纪心得体会
2016/01/15 职场文书
创业计划书之美甲店
2019/09/20 职场文书
JavaScript的Set数据结构详解
2022/02/18 Javascript