PostgreSQL:创建自增序列id,分区表,分区表子表
生活随笔
收集整理的這篇文章主要介紹了
PostgreSQL:创建自增序列id,分区表,分区表子表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1)創建自增序列seq
- 2)創建分區表主表
- 3)創建分區表子表
- 4)分區表數據插入
- 5)分區表查詢
1)創建自增序列seq
CREATE SEQUENCE if not exists public.test_id_seqINCREMENT 1START 5MINVALUE 1MAXVALUE 9223372036854775807CACHE 1;
2)創建分區表主表
-- 創建表 根據ds分表(ds為yyyyMMdd格式)
create table test
(id bigint NOT NULL DEFAULT nextval('test_id_seq'::regclass),ip text,name text,create_time timestamp with time zone NOT NULL,ds bigint
) PARTITION BY LIST (ds);
-- 創建注釋
COMMENT ON TABLE test IS '分區表測試';
COMMENT ON COLUMN test.ip IS '訪問ip';
COMMENT ON COLUMN test.name IS '名稱';
COMMENT ON COLUMN test.create_time IS '創建時間';
3)創建分區表子表
-- 創建分區子表
-- create table %s_%s partition of %s for values in (%s)
create table test_20200924 partition of test for values in(20200924);
4)分區表數據插入
insert into test(ip,name,ds) values('1.1.1.1','good',20200924);
insert into test(ip,name,ds) values('1.1.1.1','good2',20200924);
insert into test(ip,name,ds) values('1.1.1.1','good3',20200924);
5)分區表查詢
select name,to_char(create_time,'yyyymmddhh24mi') as period from test where ds in (20200921,20200922,20200923,20200924) and create_time <='2020-09-24 15:30:00.000' and create_time >= '2020-09-21 00:00:00.000' group by name,to_char(create_time,'yyyymmddhh24mi') order by name,period
總結
以上是生活随笔為你收集整理的PostgreSQL:创建自增序列id,分区表,分区表子表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PostgreSql、MySql字段值为
- 下一篇: 【Mysql】日期、行变列(IF、CAS