Oracle's quick start

Concept understanding

Oracle: The concept of Oracle database is different from other databases. The database here is an operating system with only one library. It can be seen that Oracle has only one big database.

Example: An Oracle instance (OracleInstance) consists of a series of background processes (BackguoundProcesses) and memory structures (Memory Structures). A database can have n instances

Table space: Oracle's logical mapping user to the relevant data file (ORA or DBF file) on the physical database is established under the instance. Different instances can create users with the same name

Data file: the physical storage unit of the database

Basic grammar

Space operation

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

删除表空间
drop tablespace hello;

User action

解锁scott用户 alter user scott account unlock;

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

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

Grant roles

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

授予角色
grant dba to user;

Basic query

Date query
查询入职天数
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;

other

给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

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

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

Declaration method

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;

IF judgment in PLSQL

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 loop prints 1-10

while loop

declare
  i number(2) := 1;
begin
  while i<11 loop
     dbms_output.put_line(i);
     i := i+1;
  end loop;  
end;

exit loop

declare
  i number(2) := 1;
begin
  loop
    exit when i>10;
    dbms_output.put_line(i);
    i := i+1;
  end loop;
end;

for loop

declare
begin
  for i in 1..10 loop
     dbms_output.put_line(i);  
  end loop;
end;

Cursor usage

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

Stored procedure

Stored procedure: Stored procedure is a section of pl/sql language that has been compiled in advance and placed on the database side

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;

Storage function

计算指定员工的年薪
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;

The difference between stored functions and stored procedures

The essential difference: stored functions have return values, while stored procedures do not.

If a stored procedure wants to implement a business with a return value, we must use an out parameter.
Even if the stored procedure uses out-type parameters, it doesn't really have a return value at first,
but assigns values ​​to out-type parameters inside the stored procedure. After execution, we directly get the value of the output type parameter.

trigger

Statement-level triggers: does not include triggers 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;

Row-level triggers: include for each row is row-level triggers.

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;

Precautions

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

If you have any questions or different opinions, please leave a message and exchange together. The blogger will reply as soon as he sees it...

Insert picture description here

Guess you like

Origin blog.csdn.net/mrhs_dhls/article/details/105947495