PostgreSQL:创建自增序列id,分区表,分区表子表
生活随笔
收集整理的這篇文章主要介紹了
PostgreSQL:创建自增序列id,分区表,分区表子表
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1)創(chuàng)建自增序列seq
- 2)創(chuàng)建分區(qū)表主表
- 3)創(chuàng)建分區(qū)表子表
- 4)分區(qū)表數(shù)據(jù)插入
- 5)分區(qū)表查詢
1)創(chuàng)建自增序列seq
CREATE SEQUENCE if not exists public.test_id_seqINCREMENT 1START 5MINVALUE 1MAXVALUE 9223372036854775807CACHE 1;
2)創(chuàng)建分區(qū)表主表
-- 創(chuàng)建表 根據(jù)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);
-- 創(chuàng)建注釋
COMMENT ON TABLE test IS '分區(qū)表測試';
COMMENT ON COLUMN test.ip IS '訪問ip';
COMMENT ON COLUMN test.name IS '名稱';
COMMENT ON COLUMN test.create_time IS '創(chuàng)建時(shí)間';
3)創(chuàng)建分區(qū)表子表
-- 創(chuàng)建分區(qū)子表
-- create table %s_%s partition of %s for values in (%s)
create table test_20200924 partition of test for values in(20200924);
4)分區(qū)表數(shù)據(jù)插入
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)分區(qū)表查詢
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
總結(jié)
以上是生活随笔為你收集整理的PostgreSQL:创建自增序列id,分区表,分区表子表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PostgreSql、MySql字段值为
- 下一篇: 【Mysql】日期、行变列(IF、CAS