300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 【原创】 PostgreSQL 实现MySQL 的auto_increment 字段

【原创】 PostgreSQL 实现MySQL 的auto_increment 字段

时间:2019-03-18 04:38:44

相关推荐

【原创】 PostgreSQL 实现MySQL 的auto_increment 字段

独角兽企业重金招聘Python工程师标准>>>

MySQL 里面有auto_increment 自增字段,PostgreSQL 没有自增字段这一说法,但是有单独的对象:序列。 我们可以用序列或者其他土方法来是实现这样的语法。

1. 用序列来实现

先来创建一个步长为2的序列,最大值为10000,每次产生100个值。t_girl=# create sequence ytt.ytt_s1 start with 1 increment by 2 maxvalue 10000 ;CREATE SEQUENCE创建一个测试表。t_girl=# create unlogged table ytt.tmp_3 (id int not null, log_date date); CREATE TABLE改变表tmp_3的列id 默认值是序列ytt_s1的下一个值。t_girl=# alter table tmp_3 alter id set default nextval('ytt_s1');ALTER TABLEt_girl=# \d tmp_3Unlogged table "ytt.tmp_3"Column | Type | Modifiers ----------+---------+----------------------------------------------id | integer | not null default nextval('ytt_s1'::regclass)log_date | date OK,我们试着插入几条记录。t_girl=# insert into tmp_3(log_date) select generate_series('-01-01'::timestamp,now(),'1 day');INSERT 0 14t_girl=# select * from tmp_3;id | log_date ----+------------1 | -01-013 | -01-025 | -01-037 | -01-049 | -01-0511 | -01-0613 | -01-0715 | -01-0817 | -01-0919 | -01-1021 | -01-1123 | -01-1225 | -01-1327 | -01-14(14 rows)

2. 同样是用序列,但是在原表上添加了触发器。

我们先重置原来ID的默认值。t_girl=# alter table tmp_3 alter id set default 0;ALTER TABLE清空测试表:t_girl=# truncate table tmp_3;TRUNCATE TABLE现在的序列已经增长到了27,如下t_girl=# \d ytt_s1;Sequence "ytt.ytt_s1"Column| Type | Value ---------------+---------+--------sequence_name | name | ytt_s1last_value | bigint | 27start_value | bigint | 1increment_by | bigint | 2max_value| bigint | 10000min_value| bigint | 1cache_value | bigint | 1log_cnt | bigint | 19is_cycled| boolean | fis_called| boolean | t现在重置下序列。t_girl=# alter sequence ytt_s1 restart with 1;ALTER SEQUENCEt_girl=# \d ytt_s1Sequence "ytt.ytt_s1"Column| Type | Value ---------------+---------+--------sequence_name | name | ytt_s1last_value | bigint | 1start_value | bigint | 1increment_by | bigint | 2max_value| bigint | 10000min_value| bigint | 1cache_value | bigint | 1log_cnt | bigint | 0is_cycled| boolean | fis_called| boolean | f创建一个触发器函数。create or replace function sp_increment_tmp_3()returns trigger as$ytt$beginnew.id := nextval('ytt_s1');return new;end;$ytt$ language plpgsql;创建触发器。create trigger tr_insert_tmp_3before insert on ytt.tmp_3for each row execute procedure sp_increment_tmp_3();OK。 再次插入后,看看结果如何。t_girl=# insert into tmp_3(log_date) select generate_series('-01-01'::timestamp,now(),'1 day');INSERT 0 14t_girl=# select * from tmp_3;id | log_date ----+------------1 | -01-013 | -01-025 | -01-037 | -01-049 | -01-0511 | -01-0613 | -01-0715 | -01-0817 | -01-0919 | -01-1021 | -01-1123 | -01-1225 | -01-1327 | -01-14(14 rows)

3. 其实也是用序列实现,但是有系统提供的类型serial(其实也就是默认的一个序列)。

现在呢,删掉之前的触发器和触发器函数。然后创建一张新表。看看下面,跟第一种是一样的,只不过是系统自己创建了一个序列。t_girl=# create unlogged table ytt.tmp_3 (id smallserial not null, log_date date);CREATE TABLEt_girl=# \d tmp_3;Unlogged table "ytt.tmp_3"Column | Type | Modifiers ----------+----------+----------------------------------------------------id | smallint | not null default nextval('tmp_3_id_seq'::regclass)log_date | date|t_girl=# \d tmp_3_id_seqSequence "ytt.tmp_3_id_seq"Column| Type | Value ---------------+---------+---------------------sequence_name | name | tmp_3_id_seqlast_value | bigint | 1start_value | bigint | 1increment_by | bigint | 1max_value| bigint | 9223372036854775807min_value| bigint | 1cache_value | bigint | 1log_cnt | bigint | 0is_cycled| boolean | fis_called| boolean | fOwned by: ytt.tmp_3.id

本文出自 “上帝,咱们不见不散!” 博客,请务必保留此出处http://yueliangdao0608./397025/1351557

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。