PostgreSQL相关

  1. 表加字段
alter table 表名 add 列名 数据类型,如:
alter table tb_test add price timestamp(6);
  1. 表删除字段
ALTER TABLE table_name DROP COLUMN column_name;
  1. 改变字段数据类型
alter table 表名 alter 字段名 type 数据类型,如:
alter table tb_test alter price type varchar
  1. 更改表名
alter table 现在的表名 rename to 新表名,如:
alter table tb_test rename to tb_test_test
  1. 更改字段
alter table 表名 rename 现在的字段 to 新字段
alter table tb_test rename price to price_r
  1. 重置sequence
alter sequence 表名_id字段名_seq restart where 再次开始的id
alter sequence tb_test_id_seq restart with 1;

select max(id) from tb_test; --查出最大id
select nextval('tb_test_id_seq'); --查出下个自增id值
select setval('tb_test_id_seq',50);  --设置需求id值
  1. NOT NULL
增加 not null 约束
alter table tbl_null alter COLUMN b set not null
移除not null约束
alter table tbl_null alter COLUMN b drop not null
  1. 获取当前时间
https://www.cnblogs.com/mchina/archive/2013/04/15/3010418.html
  1. 将字段状态码转为对应文字
select 
(case
	when status = 0 then '待支付'
	when status = 1 then '支付中'
	else '' end) status
from table;	
  1. 将日期格式化为字符串
select to_char(current_date, 'yyyy-MM-dd HH:mm:ss');
  1. 设置小数保留位数
select round(10/3, 3);
  1. 按字段特殊规则排序
select * from table order by
case
 when status = 0 then -1
 when status = 3 then 0
 else 1 end,
id desc; 
  1. 查询库中的表
select * from pg_tables where schemaname = 'public' and tablename like '%package%';
  1. 查询字段数据类型
select *
from information_schema.columns
where column_name = 'column_name'
and table_name = 'table_name';
  1. 按中文字段排序
order by convert_to(field_name, 'GBK')
  1. 查询的字符串中包含「 ’ 」
如: 
select * from table_name where column_name = 'Gertie's Cake';
直接在字符串中的「 ' 」前再加一个「 ' 」转义即可
select * from table_name where column_name = 'Gertie''s Cake';

  1. 时间字符串转换
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS');
select to_date('2020-09-01','YYYY-MM-DD');
select to_timestamp('2020-09-01 17:41:30','YYYY-MM-DD HH24:MI:SS');

  1. 数组转为一个字段
select array_to_string(array(select id from table_name), ', ');

  1. 获取指定字段去重的列表
select distinct on (t.id , t,name) * from table t

  1. 查看表数据类型等
SELECT 
    a.attname as 字段名,
    format_type(a.atttypid,a.atttypmod) as 类型, 
    a.attnotnull as 非空, col_description(a.attrelid,a.attnum) as 注释   
FROM 
    pg_class as c,pg_attribute as a 
where 
    a.attrelid = c.oid 
    and 
    a.attnum>0 
    and 
    c.relname = 'table_name';
  1. in 、not in中包含null
select column_name from table_name where column_name in (1, null);
-- in条件中包含null也不会返回该字段为null的数据,自动过滤
select column_name from table_name where column_name not in (1, null);
-- not in条件中包含null会直接返回false,啥都不会返回
  1. 模糊搜索包含前后下划线(_)、百分号(%)
如搜索的文字为:_test、test%,此类会在sql中作为模糊搜索条件
需要将其传入前转义
if (StringUtils.startsWith(testName, "_") || StringUtils.startsWith(testName, "%")) {
   bean.setTestName("\\" + testName);
}
if (StringUtils.endsWith(testName, "_") || StringUtils.endsWith(testName, "%")) {
   StringBuilder sb = new StringBuilder(testName);
   StringBuilder insert = sb.insert(testName.length() - 1, "\\");
   bean.setTestName(insert.toString());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值