pgsql的一些语法

计算两个时间相差的天数#

SELECT date_part('day',cast(now() as TIMESTAMP)-cast(create_time as TIMESTAMP)) FROM t_module_xxx;

create_time 是 timestamp 类型。可以通过date_part计算两个时间相差几天,几分钟,几秒钟等。

具体可以参考这篇文章

可重复执行修改表结构语法#

添加约束

alter table public.t_module_xxx drop constraint if EXISTS unique_xxxx;
alter table public.t_module_xxx add constraint unique_xxxx unique(col1,col2);

建表语句#


-- postgresql建议使用小写
drop table if exists public.t_module_xxx;
drop sequence if exists seq_t_module_xxx;
-- 创建序列
create sequence seq_t_module_xxx increment 1 minvalue 1 maxvalue 9223372036854775807 start 1 cache 1 cycle;
-- 创建表
create table public.t_module_xxx
(
  order_id            integer default nextval('seq_t_pcar_order') not null,
  user_id             integer not null,
  loan_amount         numeric(12, 2),
  loan_apply_no       varchar(255),
  loan_apply_status   varchar(2),
  loan_apply_time     timestamp without time zone,
  loan_no             varchar(255),
  loan_success_time   timestamp without time zone,
  loan_status         varchar(2),
  -- 金额类型
  down_payment_amount numeric(12, 2),
  create_time         timestamp without time zone default now(),
  update_time         timestamp without time zone
);
-- 创建注释
comment on table public.t_module_xxx is '订单表';
comment on column public.t_module_xxx.order_id is '主键';
--设置主键
alter table t_module_xxx add constraint pk_t_pcar_order primary key (order_id);

-- 授权语句
grant select, insert, update, delete on public.t_module_xxx to xxxxxx;
grant select on public.t_module_xxx to xxxxxx;
posted @   程序员自由之路  阅读(458)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩