Oracle syntax

ORACLE

  1. Data type of database

    1. number type, numeric type, the maximum length of 38

      1. It is often defined as the number (3), number (6,2)
      2. number (6,2) represents the largest integer of 4 bits, two decimal places (8888.88)
    2. varchar2 type indicating the type of variable-length string (oracle unique)

      1. Definition Format: varchar2 (x), x represents the length need to be defined
      2. The maximum length of 4000 bytes varchar2
      3. varchar2 (20) represents the character string 20 bytes can be stored, but according to the actual length of the data, the space occupied may be transformed, but a maximum of 20 bytes
    3. char type, fixed-length string

      1. Definition Format char (x) x represents the length need to be defined
      2. The maximum length of 2000 bytes char
      3. char (20), is fixed as 20 bytes represents
    4. long type

      1. Varchar2 is extended version, storing variable-length strings, up storage 2G, but there are some limitations long types, each table can have a long type field, and can not serve as the primary key
      2. Can not be indexed, it can not appear in the query conditions, it is generally not use long
    5. Clob type of huge storage space, can be up to 4G

    6. Date Date Type

      1. To save the date and time
      2. Represents the date range, 4712 BC January 1, 9999 to December 31
  2. Four operations common database (additions and deletions to belong DML operations related to the practice, the transaction must be true in order to save the data in the database)

    1. insert
    2. update
    3. delete
    4. select
  3. Transaction control, designed to maintain data consistency

    1. Submit commit

    2. Rollback rollback

    3. Services have four characteristics

      1. Atomicity (Atomicity) refers to an operation included in the transaction, either all executed or not all
      2. Consistency (consistency) refers to operations in the transaction must be successfully contained the same time or at the same time fail.
      3. Isolation (Isolation) database means allows multiple concurrent transactions simultaneously read and write data therein, and the ability to modify the isolation inconsistent state when the transaction data concurrently, since the operation command executed result in a cross can be prevented
      4. Under persistence (durability) refers to when the end of the transaction, his impact on the database is permanent, timely encounter a system failure, the data will not be lost
  4. Oracle database operation commands

    --创建表
    --需要注意的是,如果表名(字段)是小写,那么在查询表的时候需要添加双号.否则展现的是大写
    create table "classroom"(
    	id number(4),
        name varchar2(255),
        age number(4),
        adress varchar2(255)
    )
    
    --修改表名称(前面的表示原表名,后面的表示新表名)
    rename "classroom" to "classroom2"
    --修改表结构,追加字段
    alter table "classroom" add(birthday varchar2(10))
    
    --删除表中字段
    alter table "classroom" drop(birthday)
    --删除表(删除了整张表包括表结构)
    drop table beststudent;
    
    --查看当前系统时间
    select sysdate from dual;
    
    --查看整张表
    select * from "classroom";
    --查看表中指定字段
    select id,name from "classroom";
    
    --向表中插入数据
    insert into "classroom" (id,name,age,adress) values(1,'吴鹏',18,'南京');
    --插入数据时,不指定插入字段,就需要把所有的字段全插入数据,需要注意的是在插入的数据中字段值需要使用单引号
    insert into "classroom" values(1,'吴磊',29,'常州');
    
    --查看表中的数据
    select * from "classroom";
    
    --提交后的数据无法在进行回滚
    
    --修改数据操作,修改多个数据之间用逗号隔开
    update "classroom" set name='袁昆' where id=2;
    
    --删除数据,根据条件删除数据
    delete from "classroom" where id=2;
    
    --truncate 删除表中所有数据,表结构还存在.不支持事务
    truncate table "classroom";
    
    --default关键字(用于设置字段的默认值)和not null非空约束(用于确保插入数据时,某些字段不能为空值),两种操作都需要在建表的时候设置
    create table classroom(
    	id number(4),
        name varchar2(20) not null,
        sex char(4) default 'M',
        age number(20)
    );
    
    --dual 伪表,当我们查询的内容与任何一张表都没有关系时,可以使用dual
    --查询系统日期
    select sysdate from dual;
    --length()用于返回指定内容的长度,查询字符串的长度
    select length('sdfsd') from dual;
    
    --数据库字符串拼接
    --拼接两个字段
    select concat(firstname,lastname) as 新名字 from classroom;
    --拼接两个或者多个字段
    select id||name||age as 新名字 from classroom;
    
    --大小写转换
    --大写
    select upper('helloworld') from dual;
    --小写
    select lower('HELLOWORLD') from dual;
    --单词的首字母大写,单词之间需要有空格
    select initcap('hello world') from dual;
    
    --去除左右空格
    select trim('  acv  ') from dual;
    --去除左空格
    select ltrim(' abc ') from dual;
    --去除右空格
    select rtrim('  acx  ') from dual;
    
    --补位函数
    --lpad() 左补位 如果name的长度没有10,那么使用字符串a进行补位
    select lpad(name,10,'a') from classroom;
    --rpad() 右补位
    select rpad(name,10,'a') from classroom;
    
    --substr(a,b,c) 截取字符串
    --从指定位置截取字符串,数据中的下标从1开始
    --a表示截取的字符串
    --b表示截取开始索引(包括索引位置)
    --c表示截取的个数
    select substr('thinking in java',3,6) from dual;
    --如果第三个参数不设置,或者设置超过了字符串的长度,就直接截取到末尾
    select substr('thinking in java',3,9) from dual;
    --如果第二个参数是负数,就是从倒数第几个开始截取		
    select substr('thinking in java',-3,9) from dual;
    
    --instr(str1,str2,m,n) 查看指定字符串在当前字符串中的位置
    --str1 表示需要操作的字符串
    --str2 表示在str1中寻找的字符
    --m 表示查找str2的起始位置
    --n 表示第几次出现
    select instr('thinking in java','in',4,2) from dual;
    
    --round()四舍五入 
    --round的第二个参数,表示保留几位,如果不写保留整数,如果是负数表示保留整数位
    select round(45.567,2) from dual
    
    --trunc()和round()很像,但是没有四舍五入的功能
    select trunc(45.564,2) from dual
    
    --ceil() 表示向上取整和floor() 表示向下取整
    select ceil(45.678) from dual;
    select floor(45.654) from dual;
    
    --查询系统日期
    select sysdate from dual;
    --查询系统时间(精确到毫秒)
    select systimestamp from dual;
    
    --to_date() 将字符串转成日期类型
    select to_date('2019-09-23','YYYY-MM-DD') from dual;
    --to_char() 将日期转成字符串
    select to_char(sysdate,'YYYY-MM-DD') from dual;	
    --将日期转成的字符串带有中文,需要使用双引号
    select to_char(sysdate,'YYYY"年"-MM"月"-DD"日"') from dual;	
    --日期做减法运算
    select sysdate-to_date('2017-07-07','YYYY-MM-DD') from dual;
    --last_day() 返回指定日期,所在月份的最后一天
    select last_day(sysdate) from dual;
    --add_months(date,n) 返回指定日期加上N个月后的日期
    select add_months(sysdate,2) from dual;
    --next_day() 查看下一个周几是几月几日 数字7表示周六,数字1表示周天
    select next_day(sysdate,2) from dual;
    
    --least() 从多个参数选出最小值
    ---greatest() 从多个参数选出最大值
    select least(10,20,30) from dual;
    
    --null的含义
    -- null就是没有值,不存在的意思,任何数据类型都可以为null,判断某个字段是否为null的时候,不能用等号,null和任何数字运算,结果还是null
    --显示插入null值
    insert into student2 values(1001,'初音未来',null);
    --隐式插入null值
    insert into student2(id,name) values (1002,'森之妖精');
    --判断字段值是否为null的数据
    select * from student where phone is null;
    --判断字段值不是null数据
    select * from student where phone is not null;
    --null替换
    --nvl(参数1,参数2) 如果参数1不是null,就直接使用参数1;如果参数1是null,就是用参数2,2个参数的类型要相同
    --查看smith的薪资加上奖金是多少钱
    select ename,job,sal+nvl(comm,0) from emp where ename='SMITH'
    --查看allen的薪资加上奖金,是多少钱
    select ename,job,sal+nvl(comm,0) from emp where empid=7499
    --nvl2(参数1,参数2,参数3) 
    --如果参数1不是null,就使用参数2,如果参数1是null,就使用参数3
    --查询emp表中,看看哪些员工有奖金,哪些员工没有奖金
    select ename,job,comm,nvl2(comm,'有奖金','没有奖金') from emp;
    
    --别名 as
    --查看平均工资
    select avg(sal) as avgsal from emp;
    --查看工资总和			
    select sum(sal) "SumSal" from emp;
    
    --使用< > >= <= != <> =
    
    --使用and和or
    select * from emp where sal>1000 and job='办事员';
    select * from emp where sal>1000 or job='办事员';
    --查询薪资大于1000,并且工作是salesman或者是办事员的员工数据
    select * from emp where sal>1000 and (job ='SALESMAN' or job ='办事员');
    
    --like通配符
    --模糊查询使用,支持两个通配符
    --% 表示0-多个字符
    --_ 表示一个字符
    --查看员工姓名以A开头的员工信息
    select * from emp where ename like'A%';
    --查询名字总第二个字母是A的员工信息
    select * from emp where ename like'_A';
    --查看名字是N结尾的员工信息
    select * from emp where ename like'%N';
    --查看名字中包含A的员工信息
    select * from emp where ename like'%A%';
    
    --in和not in
    --查看工作是办事员,salesman,分析师的员工信息
    select * from emp where job in('办事员','SALESMAN','分析师');
    --查看工作不是办事员,salesman,分析师的员工信息
    select * from emp where job not in('办事员','SALESMAN','分析师');
    
    --between...and...
    --查询员工薪资1500-3000之间
    select * from emp where sal between 1500 and 3000;
    
    --any()大于最小的就可以 和 all() 大于最大的才可以
    --查询薪资大于2000,3500,4000的员工
    select * from emp where sal>ANY(2000,3500,4000)
    select * from emp where sal>ALL(2000,3500,4000)
    
    --在查询条件中使用函数或者运算
    select * from emp where ename=upper('king');
    select * from emp where sal*12>30000;
    
    --去除重复
    --查看有哪些工作职位
    select distinct job from emp;
    --如果对多列进行去除重复,那么只能保证这几列的组合是不重复的
    --查看每个部门下都有哪些工作职位
    select distinct deptno,job from emp;
    
    --order by 对于查询结果按照指定的字段排序
    --默认asc,升序
    --desc,降序
    select * from emp order by sal;
    --查询30号部门的员工薪资,按照升序排列
    select * from emp where deptno=30 order by sal;
    --如果是多列排序,那么就会先按照order by后面的第一个字段有限排序,当第一个字段出现重复时,在按照第二个字段排序
    --查看员工信息,按照薪资和部门排序
    select * from emp order by sal,deptno;
    
    --聚合函数
    --聚合函数是用来进行统计工作的,可以将多行数据进行统计,然后得出一个结果.
    --聚合函数忽略null值
    --查看最大薪资和最小薪资
    select max(sal) "最大工资",min(sal) "最小工资" from emp;
    --查看平均值和总分
    select avg(sal) "平均工资",sum(sal) "总和工资" from emp;
    --统计表中有多少条数据
    select count(*) from emp;
    
    --group by子句
    --是为了聚合函数服务的,可以在统计数据时细化分组,它允许将某一个字段值一样的记录看做一组,然后进行统计,每组可以返回一个统计结果
    --查看每个部门的最高薪资	
    select max(sal),deptno from emp group by deptno;
    --group by进行分组时,被分组的字段一定要有重复值,否则分组没有意义.
    --当select中出现聚合函数,那么不在聚合函数中的其他字段必须出现在group by子句中
    --group by子句后面可以指定多个字段,会按照多个字段组合相同的数据进行分组
    select max(sal),deptno,job from emp group by deptno,job;
    
    --having子句
    --having也是用于添加过滤条件,它的过滤实际是在统计结果之后进行的,所以having是为了统计结果之后进行的,不能独立出现,一般跟在group by子句之后
    select avg(sal),deptno from emp group by deptno having avg(sal)>2000;
    
    --关联查询
    --当从多张表查询数据时,我们会建议关联关系,然后在对多张表进行查询工作,重点就是指定这些表中数据的关联关系,必须要指定关联条件,如果不指定,那么会产生笛卡尔积,后果会比较严重.
    --查询每个员工对应的部门名称
    --注意在使用别名去进行关联的话,在查询字段的时候也需要加上别名.
    select e.name,d.deptno,d.dname from emp e,dept d where e.deptno=d.deptno;
    --查询福特,所在的部门地址和名称
    select e.ename,d.dname,d.loc from dept d,emp e where e.ename='福特' and d.deptno=e.deptno
    
    --主键和外键(建表的时候主键一般必须要有,外键可以不需要)
    --主键 primary key
    --通常每张表的第一个字段就是主键,主键要求在整张表中,记录不能为空,并且不能有重复值,目的是表示每一条记录的唯一性
    --外键 foreign key
    --外键是B表中的一个字段,且字段值是来自A表的主键,一般可以形成A表和B表的关联关系
    
    --内连接
    --查询每个员工所在部门的名称
    select * from emp e join dept d on(e.deptno=d.deptno);
    
    --外连接
    --使用外连接查询时,以一张表为驱动表(数据要全部显示),该表的数据会全部体现在结果集中,但是来自关联表中的字段由于不满足连接条件没有对应的记录,所以全部显示null
    --查询所有部门的名称和地址和部门中包含的员工信息
    --右外联
    select e.empid,e.ename,d.* from emp e right outer join dept d on(e.deptno=d.deptno)
    --左外联
    select e.empid,e.ename,d.* from emp e left outer join dept d on(e.deptno=d.deptno)
    --全外连接
    select e.empid,e.ename,d.* from emp e full outer join dept d on(e.deptno=d.deptno)
    
    --自连接
    --当前表的一条记录对应当前表的多条记录,自连接的设置是为了解决同类型数据间有存在上下级关系的树状结构的保存与关联
    --查询员工的上级领导是谁
    select e.empid,e.ename,m.empid,m.ename from emp e,emp m where e.empid=m.mgr;
    --查询克拉克的上级领导是谁
    select m.empid,m.ename from emp e,emp m where e.ename='克拉克' and e.mgr=m.empid;
    
    --子查询
    --在from中出现了子查询,那么久以为这我们要讲查询结果当做一张表去看待,如果在自查寻中出现了函数或者表达式,那么一定要加上别名
    --有些查询需要建立在另一个查询的结果上进行,name先执行的查询就是子查询
    --查询和scott相同的部门的员工有谁
    select * from emp where deptno=(select deptno from emp where ename='SCOTT');
    --查询薪资比scott高的员工信息
    select sal,ename from emp where sal>(select sal from emp where ename='SCOTT')
    --查询和salesman相同部门的员工信息,下面的in表示包含
    select * from emp where deptno in(select * from emp where job='SALESMAN');
    --查询员工薪资,高于公司平均薪资的员工信息
    select sal,ename from emp where sal>(select avg(sal) as avgsal from emp);
    --查询员工薪资高于SALESMAN最低薪资的员工薪资(还可以结合any使用)
    select ename,sal from emp where sal>(select min(sal) as minsal from emp where job='SALESMAN');
    --查询员工薪资高于SALESMAN最高薪资的员工薪资(还可以结合聚合函数)
    select ename,sal from emp where sal>all(select sal as minsal from emp where job='SALESMAN');
    --查询员工薪资,高于SALESMAN和办事员的最高薪资员工信息
    select ename,sal from emp where sal>(select max(sal) from emp where job in('SALESMAN','办事员'))
    --查询员工薪资,高于部门平均薪资的员工信息
    select e.empid,e.ename,e.sal,e.deptno from emp e,(select avg(sal) avgsal,deptno from emp group by deptno) d where e.sal>d.avgsal and e.deptno=d.deptno;      
    --查询最低工资高于30号部门最低工资的部门
    select deptno,min(sal) from emp group by deptno having min(sal)>(select min(sal) from emp where deptno =30)  
    
    --子查询可以将查询结果集作为表创建出来
    --如果子查询中出现了函数或者表达式,那么一定要加上别名
    create table empdept as (select e.empid,e.ename,e.sal,d.deptno,d.dname from emp e,dept d where e.deptno=d.deptno);
    
    --分页查询
    --需要使用rownum,被称为伪列,通常用来给当前查询出来的结果进行编号的,从1开始计数
    select rownum,ename,sal,deptno from emp;
    --如果需要用rownum作为过滤条件,name就需要先使用rownum做一次子查询,然后在子查询基础之上进行过滤
    select * from(select rownum rn,ename,sal,deptno from emp) e where e.rn<3;
    --也可以直接将rownum作为过滤条件,查询rownum前三条数据(要考虑版本,新的一轮版本支持)
    select rownum ,e.* from emp e where rownum<4;
    --取出1-3页的数据
    select * from(select rownum rn,e.* from emp e) e1 where e1.rn between 1 and 3;
    --分页计算公式
    --(n-1) * pagesize+1 
    --n * pagesize
    --n代表当前页数,第一页N就是1;第二页N就是2,以此类推
    --pagesize代表每一页显示的最大条目数
    --按照薪资降序排列,取出薪资前三的员工信息
    select * from(select rownum rn,e1.* from emp e1) e2 where e2.rn between 2 and 4;
    --decode(参数,匹配1,计算1,匹配2,计算2,默认值)
    --如果没有提供默认值,当没有匹配到结果时,将返回null
    --查询工作职位如果是salesman的员工,薪资*2;如果是办事员,薪资*3;如果没有匹配到,那么返回原本工资
    select ename,job,decode(job,'SALESMAN',sal*2,'办事员',sal*3,sal) newsal,sal from emp;
    
    --先按照col1进行分组,然后在分组的基础之上按照col2排序,最后将结果进行编号,这个编号是连续且唯一的,并且是按组算的
    --使用row_number()over(partition by col1 order by col2)
    --按每个部门分组,然后按照部门的薪资排序
    select row_number()over(partition by deptno order by sal) row_id,ename,deptno,sal from emp;
    
    --先按照col1进行分组,然后在分组的基础之上按照col2排序,最后将结果进行编号,这个编号是不连续不唯一的
    --使用rank()over(partition by col1 order by col2)
    select rank()over(partition by deptno order by sal) rank_id,ename,deptno,sal from emp;
    
    --视图
    --是个数据库对象,也称之为虚表,视图看起来是一个表的样子,但它不是一个真实的表,而是一个sql与查询的结果集
    --创建视图
    create view vemp30 as select empid,ename,sal,deptno from emp where deptno=30;
    --查看视图(如果使用的不是dba用户,那么需要先登录dba用户授权)
    --grant create view to 用户名(对用户进行创建视图的权限)
    --如果在创建视图时,使用了别名,那么在该视图中,就会使用别名作为字段名
    select * from vemp30;
    --对视图进行DML操作(插入,修改,删除)
    --如果是单标视图,就会将基表数据造成污染,如果是多表联合查询视图,就会直接报错
    --删除视图(删除视图不会影响基表)
    drop view vemp30;
    --read only
    --当视图被设置为只读模式,那么就不能对他进行DML操作
    create or replace view vemp30 as select empid id,ename name,sal,deptno from emp where deptno=30 with read only;
    
    --序列 sequence
    --序列是一个数据库对象,作用是根据指定的规则生成一组数字,常用于给表中主键提供值
    --创建序列
    --(seq_emp_empid) emp表示表名 empid表示表中主键列名 1000表示从1000开始,10表示步长
    create sequence seq_emp_empid start with 1000 increment by 10;
    --nextval
    --作用是产生下一个数字,第一次使用,返回值是start with指定的值,第二次使用就会根据步长来增加数值,一旦获取下一个之后,就不能得到上一个数字了,序列是不可逆的
    select seq_emp_empid.nextval from dual;
    --currval 获取序列最后一次产生的数值,可以多次调用,不会使序列产生下一个数字
    --需要注意的时,必须在使用过一次nextval之后,才能执行currval
    select seq_emp_empid.currval from dual;
    
    --删除序列
    drop sequence seq_emp_empid;
    
    --索引
    --通常用来提高查询效率,排序效率.数据库管理系统自行维护索引的算法,我们只需要指定何时为某表的某字段添加索引,在查询时,数据库会自动使用索引,无需再查询语句中指定
    --添加索引操作,为emp表的ename字段添加索引
    create index idx_emp_ename on emp(ename)
    --删除索引
    drop index idx_emp_ename;
    
    --约束(constraint)
    --约束的全称是约束条件,是在数据表上加上数据校验规则,可以保证数据的完整性,当我们添加或者修改数据时,就必须要符合这些规则.
    --常见的页数
    --1.非空约束 not null 插入或者修改数据时,不能有null值
    --2.唯一性约束 unique 添加唯一性约束的字段,不能有重复值,但是可以有null值
    --3.主键约束 primary key(包含了1跟2) 添加主键约束的字段,不能有重复值,也不能有null值
    --4.外键约束 forign key 
    --5.检查约束 check 在插入或者修改数据时,参数值要满足check中的约束条件
    create table bestemp(
    	id number(4) primary key,
        name varchar2(20) not null,
        sex varchar2(3) check(sex='男' or sex='女'),
        sal number(7,2),
        phone varchar(13) unique
    )
    
    --PL/SQL基础
    --plsql程序块,允许嵌入sql语句,允许定义变量,使用条件语句和循环语句,能处理异常
    --编写一个简单的plsql程序块
    set serveroutput on --表示允许服务器输出plslq运行结果
    begin
    	dbms_output.put_line('hello');
    end;
    --声明变量
    --变量名必须用字母开头,长度不能超过30个字符,不能包括减号和空格,不能使用数据库关键字,变量不区分大小写
    declare 
    	temp varchar2(10);
    	sal number(7,2);
    begin
    	temp:='曹操';
    	sal:=5000;
    	dbms_output.put_line(temp);
    	dbms_output.put_line(sal);
    end;
    
    --v_name emp.ename%type
    --表示v_name的属性,是来自emp表中ename字段的属性
    --into:可以将查询到的结果,直接赋值给变量
    declare
    	v_name emp.ename%type;
    	v_sal emp.sal%type;
    begin
    	select ename,sal into v_name,v_sal form emp where empid=7788;
    	dbms_output.put_line(v_name);
    	dbms_output.put_line(v_sal)	;
    end;
    
    --if分支结构
    v_a number(2);
    v_b number(2);
    begin
    	v_a:=5;
    	v_b:=5;
    	if v_a>v_b then
    		dbms_output_line('a大于b');
    	elsif v_a<v_b then
    		dbms_output_line('a小于b');
    	else
    		dbms_output_line('a等于b');
    	end if;
    end;
    
    --case分支结构
    declare
    	v_sal emp.sal%type;
    begin
    	--只能存入的数据是单条的才能插入
    	select sal into v_sal from emp where ename='克拉克';
    	case 
    		when v_sal<2000 then 
    			dbms_output.put_line('薪资低');
    		when v_sal between 2000 and 4000 then
    			dbms_output.put_line('薪资还行');
    		when v_sal>4000 then
    			dbms_output.put_line('薪资高');
    	end case;
    end;
    
    --循环(while)
    declare
    	i number:=1;
    begin
    	while i<=5
    		loop
    			dbms_output.put_line(i);
    			i:=i+1;
    		end loop;
    end;
    
    --循环(for)
    begin
    	for j in 1..9
    		loop
    			dbms_output.put_line(j);
    		end loop;
    end;
    
    --游标(cursor)
    --就是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次一行或者一次多行浏览数据的能力,可以把游标当做一个指针,他可以指向结果中的任何位置,然后允许用户对指定位置的数据进行处理
    --创建游标步骤
    --1.声明游标
    --cursor 游标名 is select 语句
    --select语句可以是视图,也可以是多表查询的结果,也可以使用条件过滤
    --2.打开游标
    --open 游标的名称
    --3.提取游标中数据
    --fetch 游标名 into 变量名
    --4.关闭游标
    --close 游标名
    declare
    	--取出表中一行的数据
    	v_emp emp%rowtype;
    	--游标指向deptno=30的数据
    	cursor emp_cursor is select * from emp where deptno=30;
    begin
    	open emp_cursor;
    	loop
    	--把游标提取出来的数据全部放入v_emp中
    	fetch emp_cursor into v_emp;
    	exit when emp_cursor%notfound;
    	dbms_output.put_line(v_emp.empid||','||v_emp.ename||","||v_emp.job);
    	end loop;
    	close emp cursor;
    end;
    
    --for循环处理游标
    declare
    	cursor emp_cur is select * form emp;
    begin
    	for v_emp in emp_cur
    		loop
    			dbms_output.put_line(v_emp.empid||','||v_emp.ename);
    		end loop;
    end;
    
    --存储过程
    --用于在大型数据库中,一组为了完成特定功能的sql语句,一般部署在数据库服务器上,经过一次编译后再次调用不需要再次编译,用通过指定存储过程的名字并给出参数来执行它
    --启动输出
    set serveroutput on;
    --创建一个简单存储过程
    create or replace procedure hello is 
    begin
    	dbms_output.put_line('海螺我的');
    end;
    --调用存储过程
    begin
    	hello();
    end;
    
    --带参数的存储过程
    create or replace procedure update_comm(in_comm in number,in_id in number) is 
    begin
    	update emp set comm=in_comm where empid=in_id;
    end update_comm;
    --调用
    begin
    	update_comm(500,7369);
    	commit;
    end;
    
    --带返回值的存储过程
    create or replace procedure out_name(oname out varchar2,in_id in number) is
    begin
    	select ename into oname from emp where empid=in_id;
    end out_name;		
    declare
    	oname emp.ename%type;
    begin
    	out_name(oname,7369);
    	dbms_output.put_line(oname);
    end;
    
    --触发器
    --是一个在数据库时间发生时自动执行的plsql程序块,是一个独立的事务,在执行过程中如果发生异常,则整个事务会自动回滚
    --创建一个emp历史数据表,用来保存被删掉的数据.where后面的条件1=2表示只查询字段
    create table emp_his as select * from emp where 1=2;
    --创建触发器
    create or replace trigger del_emp;
    --事后触发器
    after delete on emp referencing OLD as old new as new
    --行级触发器
    for each row
    begin
    	insert into emp_his(empid,ename,job,mgr,sal,hiredate,deptno)
    	values(:old.empid,:old.ename,:old.job,:old,sal,:old,hiredate,:old.deptno);
    end;

Guess you like

Origin www.cnblogs.com/wp950416/p/12191193.html