Oracle(3)基础运用

    xiaoxiao2021-11-08  78

    1、decode使用

    1.1

    含义解释:  decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)

    该函数的含义如下: IF 条件=值1 THEN     RETURN(翻译值1) ELSIF 条件=值2 THEN     RETURN(翻译值2)     ...... ELSIF 条件=值n THEN     RETURN(翻译值n) ELSE     RETURN(缺省值) END IF decode(字段或字段的运算,值1,值2,值3)

           这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,否则返回值3  当然值1,值2,值3也可以是表达式,这个函数使得某些sql语句简单了许多

    使用方法:

    1.2具体用法

    select decode(dir,1,0,1) from a1_interval

    dir 的值是1变为0,是0则变为1

    比如我要查询某班男生和女生的数量分别是多少?

    通常我们这么写:

    select count(*) from 表 where 性别 = 男;

    select count(*) from 表 where 性别 = 女;

    要想显示到一起还要union一下,太麻烦了

    用decode呢,只需要一句话

    select sum(decode(性别,男,1,0)),sum(decode(性别,女,1,0)) from 表

    1.3order by对字符列进行特定的排序

    大家还可以在Order by中使用Decode。

    例:表table_subject,有subject_name列。要求按照:语、数、外的顺序进行排序。这时,就可以非常轻松的使用Decode完成要求了。

    select * from table_subject order by decode(subject_name, '语文', 1, '数学', 2, , '外语',3)

    2、Oracle中权限管理

    3、两个基础用法

    --3.1(1) create table product (        pre_id  varchar(10) primary key ,        prd_name varchar(50) not null unique,        pre_count number(10)   )   insert into product values('001','yh','1000');   insert into product_sale values(seq_cdpt.nextval,'001',100,sysdate);   select * from product;   select * from product_sale;   create table product_sale(          id  varchar(10),          pro_id varchar(10) references product(pre_id) ,--外键          sale_count number(6) ,          ss_date date            )      --(2)创建序列              Create sequence seq_cdpt         Increment by 1         Start with 1         Maxvalue 999999         Minvalue 1         Nocycle         nocache

    --3.2 模糊查询 select * from product p where p.pre_id like '12%'; --33.3sql事务管理  不会

    /* begin insert into temp values(1); savepoint a1; insert into temp values(2); savepoint a2; insert into temp values(3); savepoint a3; rollback to a2; commit; end;  */

    insert into product values('6789099','yyy','10086');

    begin savepoint a1; insert into product_sale values(seq_cdpt.nextval,'6789099',2000,sysdate); savepoint a2; commit; rollback to a1;   commit; end; select * from product_sale ; begin   rollback to a1;   commit;   end --3.4

    select p.prd_name,     (p.pre_count-s.sale_count ) lay from product p ,product_sale s where p.pre_id=s.pro_id

    select p.pre_id,p.prd_name, case when p.pre_count>500 then '存货过多' else '存货不足' end as "状态"  from product p   select p.pre_id,p.prd_name,(p.pre_count-ps.sale_count) lay, case when (p.pre_count-ps.sale_count)>500 then '存货过多' else '存货不足' end as "状态" from product p,product_sale ps where p.pre_id=ps.pro_id

     4、distinct使用:为什么用distinct时很慢?

    本来执行效率很快,但有重复数据为了筛选不重复数据,加了distinct 运行了40分钟,还是没出来结果,这是为什么呢???????

    answer1:

    distinct 这个函数可能会影响效率  你想啊  要把所有的记录一条一条的比对  看是不是重复的    消耗的时间就可想而知了

    answer2:

    运行机制不同  少量数据没什么影响 但是大量数据 尽量避免使用distinct

    answer3:

    给这个表加上索引。

    answer4:

    用group by 也行啊    在效率上完胜distinct。。

    转载请注明原文地址: https://ju.6miu.com/read-678105.html

    最新回复(0)