数据库函数

数据库函数

第一部分
1.To_char: 日期或者数字转成字符串格式
2.To_date: 把字符串转成时间格式
3.To_number:把字符串转成数字
4.“::” 转格式 eg:’234’::numeric将字符串转成numeric格式 pgsql
5.Concat/concat_ws:拼接(两个元素)
Eg:concat(‘qw’,’er’)/concat_ws(‘qw’,’er’,…)
6.‘||’:拼接(多元素亦可)eg:‘qw’||’er’  ‘qwer’
7.substr/ Substring:字符串切割 eg:substr(‘qwer’,2,3)  ‘wer’
8.Count():执行效率count()>count(1)>count(fieldname)
注:count(fieldNmae)当字段数值为null时不统计
9.Interval ‘3’ day:时间间隔为三天,一般跟在current_date/current_timestamp后面
10.Nvl():空值处理(oracle) coalesce(fieldname,’目标替换值’) pgsql
11.and和or:
Select * from tableName where a = 1 and b = 2 or c = 3;
结果是求取到所有a=1且b=2的和c=3的表数据;
注:and的优先级要高于or
12.interval:
Select current_date – interval ‘1month’; //获取当日之前一月的日期
13.sign(t):
若t>0,返回1;若t<0,返回-1,t=0,返回0;
14.lpad(field,a,b):
使field保持a长,若长度小于a左侧补b;反之只取前a长的字段
1.lpad(‘12’,5.’0’) 00012
2. lpad(‘1234567’,5.’0’) 12345
15.cast(‘’ as 类型):
Select cast(‘9.0’ sa numeric); 9
16.insert:在insert后面跟上字段名更安全
17.using:
Select a.
,b.* from a inner join b where a.id = b.id;
Select a.,b. from a.inner join b using(id);
此上两个语句的意思完全相同,id列两表均存在且为关联字段
18.事务:
开启事务:start transaction;
提交事务:commit;
回滚事务:rollback;
19.字符串复制:repeat(‘asd’,3) asdasdasd
20.string_agg(field) 将某整列变成一个字符串(可以加分隔符)
21.char_length(‘ssssss’) 返回6; 计算字符串字符个数
22.position(‘23’ in ‘2344444’) 返回1;查找指定字符串(23)在字符串中的位置,只会查找第一个
23.大小写转换:
转小写:lower(‘asdas’)ASDAS
转大写:upper(‘ASD’)asd
24.在sql中不要使用where 1=1 会导致表中的数据索引失效;垃圾条件,没必要加
25.SQL分类:
1.数据查询语言DQL:select,from,where,组成
2.数据操纵语言DML:insert,update,delete
3.数据定义语言DDL:create 不能rollback
4.数据控制语言DCL:grant,evoke,事务控制
26.between: between min and max
注:当前面的较大,后面的较小,查询无结果
27.SQL约束:
Not null:不接受null;
Unique:唯一,可空(空可重复);
Primary key:主键约束,非空且唯一,且每表只有一个;
Foreign key:外键,即一个表的外键指向其他的表;
Check:限制列中值的范围;
Default:默认值:
28.round():数值字段舍入为指定的小数位数
Round(12232.2222,2) 12232.22
29.with:用法
With fa as(select * from table) select * from fa;

第二部分

1, 创建函数:一般写下经常会使用到的一些逻辑
Create or replace function functionName() returns 返回值 as
b o d y body body
Declare
Flag integer; --常数
Begin
… --逻辑代码
Return’’;
end
b o d y body body
Language plpgsql;
2.分页:
Mysql:select * from tableName limit 10,10; //11~20条数据
Pgsql:select * from tableName limit 10 offset 20 //21~30条数据
Oracle:
3.Oracle的用户授权与回收:
授权:grant select,insert,update,delete all on tableName to userNameB
回收:revoke
4.In和exists的区别
1,in
Select * from a where id in(select aid from b) 适合于子查询(b)小于主查询
2,exists
Select * from a where exists(select I from b where b.id = a.id); 适合于子查询大于主查询
注:当两表数据量差不多时,效率差不多,任选
5.创建存储过程:(Oracle)
Create or replace procedure pro_name is
Begin
Insert ‘’’
End;
调用:
Begin pro_name() end;
6.创建定时任务:(Oracle)
Begin
Sys.dbms_job.submit(job=>:job,
What =>’存储过程名’,
Next_date =>to_date(下一时间),
Interval => ‘时间间隔’);
Commit;
End;
注:创建job亦可直接使用plsql的job模块直接创建,只需要输入相应参数。
7.分割字符串:
Regexp_split_to_array(‘12_23_34’,’’)分割成数组 注:当‘’换成’.’不能分割
Regexp_split_to_table(‘12_23_34’,’’)分割成表
8.求数组长度:
Array_length(regexp_split_to_array(‘12_34’,’
’),1)求得数组长度为2
9.切割函数:
Split_part(‘we.er,43’,’.’,3)  返回43 ‘.’分割标记位;‘3’所取部分
10.分组group by:
Select sex from user group by sex;  根据字段sex分组,结果枚举所有性别类型
11.having:
一般跟随group by之后使用 eg:group by sex having sex = ‘男’即只查出“男“
12.去重:
Select distinct name from user; 查询结果无有重名,与group by类似
注:distinct与group by极端情况

  1. 所有数据都一样,使用distinct
  2. 没有相同数据,使用group by
    13.创建视图:
    创建:Create or replace view niewName as select * from tableName;
    删除:drop view if exists viewName;
    14.索引:
    创建:Create index indexName on tableNamw(fieldName);
    删除:drop index indexName;
    查看:show index from table;
    15.delete,drop,truncate
    1.delete 可回退(每删除一行做一次记录(日志));只清空逻辑数据,不清楚物理数据(主键,索引不变)
    2.drop 删除表(结构,属性,索引)
    3.truncate 不可回退 快(有外键不可使用)效率高;清除所有数据,不能使用where子句;速度很快,但很危险;主键重新开始,索引删除;
    16.union和union all
    Union:合并多个查询结果,去除重复
    Union on:合并多个查询结果,不去除重复
    17.求指定字段在字符串中位置:
    Select position(‘张’ in ‘as张三’) 返回3
    18.trim(): 去除字段前后的空格
    19.intersect:交集
    (select * from tableA limit 10) intersect (select * from tableA limit 10 offset 10)
    20.except:求差集
    (select * from tableA limit 10) except (select * from tableA limit 10 offset 10)
    21.字符串替换:select replact(原字符串,需被替代部分示例,替换成什么);
    22.查询返回值:select 1=1; 返回’t’ ;select 1=2; 返回’f’
    23.where 和 inner join:
    Select a.,b. from A a,B b where a.id = b.id;
    Select a.* b.* from A a inner join B b on a.id = b.id;
    注:后者的效率要远高于前者的效率,且数据量越大越明显。
    24.同时查出一张表所有数据与数据数量值:
    Select a.,b. from tableA a,(select count(*) from tableA) b;
    25.查询并插入:
    Insert into tableA select * from tableA;//此时table里面的数据一变二
    26.null + 33 = null; 空值与任何值计算均为空;故而在计算之前预处理可能未空的情况
    27.排序:
    降序:Select * from tableA order by age desc;
    升序:Select * from tableA order by age asc;
    注:成本高昂,极其影响效率
    28.循环:
    1.While i<j loop
    J = i+1;
    End loop;
    1. for i<j loop
      J = i+1;
      End loop;

29.postgreSQL查询所有表名:
Select * from pg_tables; //后面可根据需要加条件查询
30.触发器(trigger)
一种由事件自动执行的特殊的存储过程,这些事件是对表的插入,更新,删除操作;
Create trigger trigger_name after/before insert/update/delete on tableName for each row execute(红色部分为操作类型) procedure function_name;
31.decode函数:
Decode(条件,判断,返回值1,返回值2) decode(1=1,true,’对’,’错’)
Decode(条件,值1,返回值1,值2,返回值2,…缺省值)
32.case when
Case when 条件1 then 值1
When 条件2 then 值2
Else 值n end
Else不可省缺,否则返回null,条件要是同一类型
33.复制表
1.仅结构:create table copyTable like table;
2.结构和数据:create tablecopyTable2 select * from table;
3.部分数据:create table copyTable2 select field1,field2 from table where field = ‘XX’;
34.行专列:
Select name,
Sum(case when zbfm = ‘age’ then value else 0 end) as age,
Sum(case when zbfm = ‘height’ then value else 0 end) as height
From test group by name having name like ‘%1’ and length(name) = 3 order by
Age desc;
35.SQL执行顺序:
1,from
2,on
3,join
4,where
5,group by
6,with
7,having
8,select
9,distinct
10,order by 代价很高
11,limit
36.排序函数:
1.rank:生成行序,可根据分数降序排名,分数相同,名次相同
Select rank() over(order by score) as rank,* from student;
注:序号不连续,如:123336 三个第三名,下个第六名
2.row_number:每一行生成行序,无重复
Select row_number() over(order by score desc) as row_num,* from student;
3.dense_rank:序号连续,分数相同,名次相同
Select dense_rank() over(order by score) as dense_rank,* from student;
注:序号连续,如:123334 三个第三名,下个第四名
4.ntile:分组处理
Select ntile(4) over(order by score desc) as ntile,* from student;
注:将学生按成绩等额分成4组,第一组为全班成绩的前25%,…
37.partition by :
Select row_number() over(partition by c_index_code order by n_index_value) ,* from table;
注:根据c_index_code分成若干组,并对组内根据n_index_value降序分组;每组单独编号,新的一组重新编号;
38.update的返回值:执行成功返回1;执行失败返回0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值