Oracle数据库学习入门篇

-- 创建表空间
create tablespace pengjy
datafile 'D:\demo\oracle_demo\pengjy.dbf'
size 100m
autoextend on
next 10m;

-- 删除表空间(执行以后,在磁盘所在位置删除,不执行语句,磁盘删除显示占用错误)
drop tablespace pengjy;

-- 创建用户
create user pengjy
identified by pengjy
default tablespace pengjy;

-- 给用户授权
-- Oracle数据库中常用角色
-- connect 连接角色,基本角色
-- resource  开发者角色
-- dba   超级管理员角色

-- 给pengjy用户授予dba角色
grant dba to pengjy;

-- 常用数据类型
-- varchar (不常用)varchar2(n) 表示字符串
-- number(m,n) 数值
-- DATA 表示日期
-- CLOB 大对象(文本) 4G         例:文字资源等
-- BLOB 大对象(二进制) 4G       例: 影音资源等



-- 注意切换用户

-- 创建一个person表
create table person(
       pid number(20),
       pname varchar2(10)
);

-- 修改表结构

-- 添加一列(添加多列用逗号隔开)
alter table person add (gender number(1));

-- 修改列类型
alter table person modify gender char(1);

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

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

-- 添加一条记录
insert into person(pid, pname) values(1, '小明');
commit; -- Oracle 要手动提交事务

-- 查询表中记录
select * from person

-- 修改一条记录
update person set pname = '小马' where pid = 1;
commit;

-- 三个删除
-- 删除表中全部记录
delete from person
-- 删除表结构
drop table person;
--先删除表,在创建表,效果等同于删除表中全部记录。
-- 在数据量大的情况下,尤其在表中带有索引的情况下,该操作效率高。
-- 索引可以提高查询效率,但是会影响增删改效率
truncate table person;

-- 删除一条记录
delete from person where pid=1;

-- 序列不真的属于任何一张表,但是可以逻辑和表做绑定
-- 序列:默认从1开始,一次递增,主要用来给主键赋值使用
-- s_person.nextval 下一个值
-- s_person.currval 当前值
-- dual 虚表, 只是为了补全语法,没有任何意义。
create sequence s_person;
select s_person.currval from dual; 

-- 添加一条记录(逻辑绑定自增长)
insert into person(pid, pname) values(s_person.nextval, '小明');
commit; 

-- scott 用户, 密码tiger
-- 超级用户,可以进行各种查询练习
--解锁scott用户
alter user scott account unlock;
--解锁scott用户密码[此句也可以用来重置密码]
alter user scott identified by tiger;
-- 切换到scott用户下

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

-- 数值函数
select round(26.18, -1) from dual;   -- 四舍五入,后面列参数表示保留的位数(-1 表示向前保留一位小数,1表示向后保留)
select trunc(56.18, 2) from dual;    -- 直接截取, 不看保留位是否大于5
select mod(10, 3) from dual;         --       求余数 

-- 日期函数
-- 查询出emp表中所有员工入职距离现在几天
select sysdate-e.hiredate from emp e;          -- 日期可以直接加减 返回单位为天

-- 算出明天此时的时间
select sysdate+1 from dual;

-- 查询出emp表中所有员工入职距离现在几个月
select months_between(sysdate, e.hiredate) from emp e;

-- 查询出emp表中所有员工入职距离现在几年
select months_between(sysdate, e.hiredate)/12 from emp e;

-- 查询出emp表中所有员工入职距离现在几星期
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('2020-3-3 14:47:55','fm yyyy-mm-dd hh24:mi:ss') from dual;

--通用函数
-- 算出emp表中所有员工的年薪
-- 奖金里面有null值,如果null值和任意数字做算术计算,结果都是null
-- nvl 将null值转换为指定值,非null值不变
select e.sal*12+nvl(e.comm,0) from emp e;

-- 条件表达式
-- 条件表达式的通用写法,mysql和Oracle都可以用
-- 给emp中员工起中文名
select e.ename,
       case e.ename
         when 'SMITH' then '史密斯'
           when 'ALLEN' then '艾伦'
             when 'WARD' then '沃德'
               when 'MARTIN' then '马丁'
             --else '无名氏'
               end
from emp e;

-- 判断emp表中员工工资,如果高于3000 显示高收入,如果高于1500 低于 3000显示中等收入
-- 其余显示低收入
select e.sal,
       case 
         when e.sal>3000 then '高收入'
           when e.sal>1500 then '中等收入'
             else '低收入'
               end
from emp e;               

-- Oracle专用条件表达式
-- Oracle中除了起别名,都用单引号
select e.ename,
        decode(e.ename,
          'SMITH', '史密斯',
            'ALLEN', '艾伦',
              'WARD', '沃德',
                'MARTIN', '马丁',
                  '无名氏') "中文名"
from emp e;


-- 多行函数:[聚合函数]作用于多行,返回一个值
select count(1) from emp;  -- 查询总数量
select sum(1) from emp;    -- 工资总和
select max(1) from emp;    -- 最大工资
select min(1) from emp;    -- 最小工资
select avg(1) from emp;    -- 平均工资

-- 分组查询
-- 查询出每一个部门的平均工资
-- 分组查询中,出现在group by 后面的原始列,才能出现在select后面
-- 没有出现在group by 后面的列,想在select后面,必须加上聚合函数
-- 聚合函数有一个特性,可以把多行记录变成一个值。

-- 查询出每个部门的平均工资
select
   e.deptno, avg(e.sal)
from 
   emp e
group by 
   e.deptno;

-- 查询出每个部门的平均工资大于2000的部门信息
select 
   e.deptno, avg(e.sal) asal
from
   emp e
group by 
   e.deptno
having
    avg(e.sal) > 2000;
-- 所有条件都不能使用别名来判断
-- select ename, sal s from emp where s> 1500;
select ename, sal s from emp where sal> 1500;

-- 查询出每个部门工资高于800的员工的平均工资
select 
   e.deptno, avg(e.sal) asal
from
   emp e
where 
   e.sal>800
group by 
   e.deptno
-- where是过滤分组前的数据,having是过滤分组后的数据
-- 表现形式:where必须在group by 之前 having是在group by 之后

-- 查询出每个部门工资高于800的员工的平均工资
-- 查询出平均工资高于2000的部门
select 
   e.deptno, avg(e.sal) asal
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
right 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.ename
from
   emp e1, emp e2
where
--    e1 员工表(员工表中的领导编号)  
--    e2 领导表(领导的编号,对应员工表中的领导编号)
   e1.mgr = e2.empno(+); 
  
-- 查询出员工姓名、部门名称,员工领导姓名、领导部门
select
   e1.ename "员工姓名", e2.ename "领导姓名", d1.dname "员工部门", d2.dname "领导部门"
from
   emp e1, emp e2,dept d1, dept d2
where
   e1.mgr = e2.empno(+)
and 
   e1.deptno = d1.deptno
and
   e2.deptno = d2.deptno;
   
-- 子查询
-- 子查询返回一个值
-- 查询出工资和scoot一样的员工信息
select 
* 
from 
emp 
where 
sal in (select 
             sal 
        from 
             emp 
        where 
             ename = 'SCOTT');
  
-- 查询出工资和10号部门任意员工一样的员工信息
-- 使用 in 连接 返回的是集合,可以表示一个或者多个(推荐使用)
-- 使用 = 连接 返回的是一行数据 当返回的是一个集合时就会报错
select * from emp where sal in (select 
                                   sal
                                from
                                   emp e
                                where
                                   deptno = 10);
                                   
-- 子查询返回一张表
-- 查询出每个部门最低工资和最低工资员工姓名、该员工所在部门名称
-- 先查询每个部门的最低工资
select e.deptno, min(e.sal) "MSAL" from emp e  group by e.deptno;

-- 三表联查得到结果
select 
   t.deptno, t.msal, e.ename, d.dname
from 
   (select e.deptno, min(e.sal) msal from emp e  group by e.deptno) t, emp e, dept d
where
   t.deptno = e.deptno
and
   t.msal = e.sal
and
e.deptno = d.deptno;

-- oracle 中的分页
-- rownum行号:当我们做select操作的时候
-- 每查询出一行记录,就会在该行上加一个行号
-- 行号从1开始,一次递增,不能跳着走。

-- 排序操作会影响rownum的顺序
select rownum, e.* from emp e order by e.sal desc;
-- 如果涉及到排序,但是要使用rownum,可以使用再次嵌套查询
select rownum, t.* from(
select rownum, e.* from emp e order by e.sal desc) t;

-- emp表工资倒叙排列后,每页五条记录,查询第二页
-- rownum 行号不能写上大于一个正数
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;
-- 创建视图[必须有dba权限]
create view V_emp as select ename, job from emp;
-- 查询视图
select * from v_emp;
-- 修改视图,能修改原表的数据[不推荐]
update v_emp set job = 'CLARK' where ename='ALLEN';
commit;
-- 创建只读视图
create view V_emp1 as select ename, job from emp with read only;
-- 查询视图
select * from v_emp1;

-- 视图的作用
-- 1.可以屏蔽一些敏感字段。
-- 2.保证总部和分部数据及时统一。

-- 索引
-- 概念:在表的列上构建一个二叉树
-- 达到大幅度提高查询效率的目的,但是索引会影响增删改的效率。
-- 单列索引
--创建单列索引
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';  -- 触发单列
select * from emp where ename='SCOTT' or job='xx';  -- or 关键字是并列查询 两条查询一条触发,一条不触发,结果就是不触发

-- pl/sql编程语言
-- pl/SQL编程语言是对SQL语言的扩展,使得sql语言具有过程化(面向过程)编程的特性。
--pl/sql编程语言比一般的过程化编程语言,更加灵活高效。
--pl/sql编程语言主要用来编写存储过程和存储函数等。

-- 声明方法
-- 赋值操作可以使用 := 也可以使用into查询语句赋值 
declare
   i number(2) := 10;
   s varchar(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) := &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;

-- pl/sql中的loop循环(严格语法)
-- 用三种方式输出1到10个数字
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;

-- 触发器,就是定制一个规则,在我们做增删改操作的时候

-- 只要满足该规则,自动触发,无需调用。
-- 分类:
-- 语句级触发器:不包含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,'不能给员工降薪');  -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
update emp set sal=sal-1 where empno = 7788;
commit;
-- 查询是否降薪
select * from emp where empno = 7788;

-- 触发器实现主键自增。
-- 分析:在所有用户插入操作之前,拿到即将插入的数据
-- 给该数据中的主键列赋值。
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;

-- 存储过程
-- 定义:提前已经编译好的一段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;
  
-- 使用存储函数来实现提供一个部门编号,输出一个部门名称.dno 不加参数 表示in
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;

-- Oracle驱动包
-- Oracle10g ojdbc14.jar
-- Oracle11g ojdbc6.jar

IEDA整合Oracle数据库:

/*
    * 测试驱动连接
    * */
    @Test
    public void javaCallOracle() throws Exception {
        //加载数据库驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //得到Connection连接
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "pengjy", "pengjy");
        //得到预编译对象
        PreparedStatement pstm = connection.prepareStatement("select * from emp where empno = ?");
        //给参数赋值
        pstm.setObject(1,7788);
        //执行数据库查询操作
        ResultSet rs = pstm.executeQuery();
        //输出结果
        while(rs.next()){
            System.out.println(rs.getString("ename"));
        }
        //释放资源
        rs.close();
        pstm.close();
        connection.close();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值