oracle中常见的sql应用

字段添加注释

comment on table 表名 is 'xxxxxx';  
comment on column 表名.字段名 is 'xxx';  

唯一索引约束

create unique index 索引名 on 表名(列);  

主键约束

alter table 表名 add constraint 约束名 primary key (列) using index 索引名;

创建索引

create index 索引名 on 表名(列);  

创建同义词

create public synonym 名 for 表名;  

授权

grant select , insert, update, delete on 表名 to 用户;  

递归sql

select * from T where 条件1 connect by 条件2 start with 条件3;  

其中connect by 与 start with 语句的先后顺序不影响查询的结果, 条件1是根据条件2、条件3选择出来的数据进行过滤,条件2是指定构造树条件,条件3作为搜索起点的条件;

plsql脚本里运用数组

type example_type is record(  
c_1 user.T.c1%type,  
c_2 user.T.c2%type,  
c_3 user.T.c3%type);--创建记录类型  
type T_example_table is table of example_type  index by binary_integer;  
example_array  example_type ;--后面使用  
example_array2  example_type ;--用于初始化  
--数组  
type example_type is table of user.T.columen%type index by binary_integer;  
example_array  example_type ;--后面使用  
example_array2  example_type ;--用于初始化  
select t.column bulk collect into example_array from T t where ....   

其中bulk collect into 可以将结果一次性地加载到collections中,而不是通过cursor逐条处理,所有into的变量必须是collections,可以在select into,fetch into, returning into语句中使用;

sql里的with .. as

with temp_table as (select * from t1 )select * from temp_table ;  

是在内存中建了一张虚表,会话结束,临时表会被清除

行转列

原表:                                                             sql实现 :

                                 

select p_id as pid ,  
       sum(case s_id when 01 then p_num end) as s1_id ,  
       sum(case s_id when 02 then p_num end) as s2_id ,  
       sum(case s_id when 03 then p_num end) as s3_id   
from T group by p_id order by p_id;  

猜你喜欢

转载自blog.csdn.net/tianmlin1/article/details/79395870