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;
作者:程序员自由之路
出处:https://www.cnblogs.com/54chensongxia/p/14207474.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?