postgresql sql中的串行数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9972377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:47:49  来源:igfitidea点击:

Serial data type in sql

sqldatabasepostgresql

提问by IT_info

I am using postgresql and I want to make a coloumn of data type 'serial' which will generate automatically (starting from 1000 and steps of 100).

我正在使用 postgresql,我想制作一个数据类型为“串行”的列,它将自动生成(从 1000 开始,步长为 100)。

Any help of how can I do it?

我该怎么做有什么帮助?

回答by Micha? Niklas

PostgreSQL has very good documentation on this. You can create sequence: http://www.postgresql.org/docs/current/static/sql-createsequence.htmland then create table with SERIALcolumn: http://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL

PostgreSQL 在这方面有很好的文档。您可以创建序列:http: //www.postgresql.org/docs/current/static/sql-createsequence.html然后用SERIAL列创建表:http: //www.postgresql.org/docs/current/static/datatype -numeric.html#DATATYPE-SERIAL

CREATE SEQUENCE my_table1_seq START 1000 INCREMENT 100;
CREATE TABLE table1 (id integer NOT NULL DEFAULT nextval('my_table1_seq'), txt varchar(1000));
INSERT INTO table1 (txt) VALUES ('zorro1');
INSERT INTO table1 (txt) VALUES ('zorro2');
INSERT INTO table1 (txt) VALUES ('zorro3');
SELECT * FROM table1;