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 数据仓库ETL技术之多表插入语句的示例详解
Apr 12 Oracle
使用springboot暴露oracle数据接口的问题
May 07 Oracle
Oracle11g R2 安装教程完整版
Jun 04 Oracle
Oracle创建只读账号的详细步骤
Jun 07 Oracle
使用Oracle跟踪文件的问题详解
Jun 28 Oracle
Oracle 临时表空间SQL语句的实现
Sep 25 Oracle
详解Oracle块修改跟踪功能
Nov 07 Oracle
Oracle表空间与权限的深入讲解
Nov 17 Oracle
Oracle 触发器trigger使用案例
Feb 24 Oracle
instantclient客户端 连接oracle数据库
Apr 26 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
CI操作cookie的方法分析(基于helper类库)
2016/03/28 PHP
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】
2017/07/21 PHP
PHP5.6读写excel表格文件操作示例
2019/02/26 PHP
拖动一个HTML元素
2006/12/22 Javascript
Ext对基本类型的扩展 ext,extjs,format
2010/12/25 Javascript
解决Extjs上传图片无法预览的解决方法
2012/03/22 Javascript
js实现连续英文字符自动换行兼容ie6 ie7和firefox
2013/09/06 Javascript
node.js中的fs.writeSync方法使用说明
2014/12/15 Javascript
javascript字符串替换函数如何一次性全部替换掉
2015/10/30 Javascript
javascript实现添加附件功能的方法
2015/11/18 Javascript
JScript实现表格的简单操作
2017/08/15 Javascript
vue axios 给生产环境和发布环境配置不同的接口地址(推荐)
2018/05/08 Javascript
npm 更改默认全局路径以及国内镜像的方法
2018/05/16 Javascript
vue+elementui 对话框取消 表单验证重置示例
2019/10/29 Javascript
mpvue实现微信小程序快递单号查询代码
2020/04/03 Javascript
vue-列表下详情的展开与折叠案例
2020/07/28 Javascript
socket + select 完成伪并发操作的实例
2017/08/15 Python
python-docx修改已存在的Word文档的表格的字体格式方法
2018/05/08 Python
spark dataframe 将一列展开,把该列所有值都变成新列的方法
2019/01/29 Python
Python爬虫beautifulsoup4常用的解析方法总结
2019/02/25 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
2019/06/27 Python
Python转换时间的图文方法
2019/07/01 Python
django项目环境搭建及在虚拟机本地创建django项目的教程
2019/08/02 Python
windows下的pycharm安装及其设置中文菜单
2020/04/23 Python
Python Django路径配置实现过程解析
2020/11/05 Python
关于Python 解决Python3.9 pandas.read_excel(‘xxx.xlsx‘)报错的问题
2020/11/28 Python
上海中网科技笔试题
2012/02/19 面试题
项目副经理岗位职责
2013/12/30 职场文书
学校七一活动方案
2014/01/19 职场文书
自主招生推荐信范文
2014/05/10 职场文书
中文专业毕业生自荐信
2014/05/24 职场文书
三八节活动简报
2015/07/20 职场文书
致运动员赞词
2015/07/22 职场文书
小学体育课教学反思
2016/02/16 职场文书
超详细教你怎么升级Mysql的版本
2021/05/19 MySQL
redis protocol通信协议及使用详解
2022/07/15 Redis