Oracle的快速入门

概念了解

Oracle: Oracle数据库的概念和其它数据库不一样,这里的数据库是一个操作系统只有一个库。可以看作是 Oracle 就只有一个大数据库。

实例: 一个 Oracle实例(OracleInstance)有一系列的后台进程(BackguoundProcesses)和内存结构(Memory Structures)组成。一个数据库可以有 n 个实例

表空间: Oracle 对物理数据库上相关数据文件(ORA 或者 DBF文件)的逻辑映射用户是在实例下建立的。不同实例可以建相同名字的用户

数据文件: 数据库的物理存储单位

基础语法

空间操作

创建表空间
create tablespace hello
datafile 'c:\hello.dbf'
size 100m
autoextend on
next 10m;

删除表空间
drop tablespace hello;

用户操作

解锁scott用户 alter user scott account unlock;

解锁scott用户的密码【此句也可以用来重置密码】alter user scott identified by tiger;

创建用户
create user tableSpace
identified by tableSpace
default tablespace tableSpace;

授予角色

角色类型
connect:   连接角色,基本角色
resource:  开发者角色
dba:     超级管理员角色

授予角色
grant dba to user;

基础查询

日期查询
查询入职天数
select sysdate-e.hiredate from emp e;

明天此刻
select sysdate+1 from dual;

查询入职月数
select months_between(sysdate,e.hiredate) from emp e;

查询入职周数
select round((sysdate-e.hiredate)/7) from emp e;

日期转字符串(fm可去掉0)
select to_char(sysdate, 'fm yyyy-mm-dd hh24:mi:ss') from dual;

字符串转日期
select to_date('2020-2-24 17:2:32', 'fm yyyy-mm-dd hh24:mi:ss') from dual;

其他

给emp表中员工起中文名
    select e.ename, 
       case e.ename
         when 'SMITH' then '曹贼'
           when 'ALLEN' then '大耳贼'
             when 'WARD' then '诸葛小儿'
               --else '无名'
                 end
    from emp e;


oracle专用外连接(查询部门全部信息和员工部分信息)
select * from emp e, dept d where e.deptno(+) = d.deptno;


emp表工资倒叙排列后,每页五条记录,查询第二页。
select * from(
    select rownum rn, tt.* from(
          select * from emp order by sal desc
    ) tt where rownum<11
) where rn>5

创建索引

单列索引(单行函数,模糊查询,都会影响索引的触发,条件必须是索引列中的原始值)
create index idx_ename on emp(ename);

复合索引(复合索引中第一列为优先检索列,要触发复合索引必须包含有优先检索列中的原始值)
create index idx_enamejob on emp(ename, job);

声明方法

declare
    i number(2) := 10;
    s varchar2(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;

PLSQL中的IF判断

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

while循环

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;

游标使用

给指定部门员工涨工资
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;

存储过程

存储过程: 存储过程就是提前已经编译好的一段pl/sql语言,放置在数据库端

create or replace procedure p1(eno emp.empno%type)

is/as

begin
   update emp set sal=sal+100 where empno = eno;
   commit;
end;

测试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;

存储函数在调用的时候,返回值需要接收
declare
  s number(10); 
begin
  s := f_yearsal(7788);
  dbms_output.put_line(s); 
end;

存储函数与存储过程区别

本质区别:存储函数有返回值,而存储过程没有返回值。

如果存储过程想实现有返回值的业务,我们就必须使用out类型的参数。
即便是存储过程使用了out类型的参数,起本质也不是真的有了返回值,
而是在存储过程内部给out类型参数赋值,在执行完毕后,我们直接拿到输出类型参数的值。

触发器

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

插入一条记录,输出新增
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;

行级触发器:包含有for each row的就是行级触发器。

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;

注意事项

1.数据增删改操作要手动提交
2.操作表只用到列名时前面要加column
3.如果null值和任意数字做算术运算,结果都是null
4.Oracle中除了起别名都用单引号,如果别名非要加,就加双引号
5.分组查询中,出现在group by后面的原始列,才能出现在select后面
没有出现在group by后面的列,除非是聚合函数
6.所有条件都不能使用别名来判断
7.rownum行号:当我们做select操作的时候,每查询出一行记录,就会在该行上加上一个行号从1开始,依次递增,不能跳着走。
8.rownum行号不能写上大于一个正数

如有疑问或不同见解,欢迎留言共同交流,博主看到后会在第一时间回复哦…

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/mrhs_dhls/article/details/105947495
今日推荐