PostgreSQL创建主键自增序列并运用到SQL中

创建自增长序列(可以用SQL创建也可以用工具Navicat)

SQL创建序列(Sequences)(注意序列名用小写,不然会提示不存在)

CREATE SEQUENCE "public"."process_time_seq" 
INCREMENT 1
MINVALUE  1
MAXVALUE 999999999
START 1
CACHE 1;

SELECT setval('"public"."process_time_seq"', 1, false);

ALTER SEQUENCE "public"."process_time_seq" OWNER TO "postgres";

Navicat创建序列(Sequences):

PostgreSQL创建主键自增序列并运用到SQL中

 

-- 查询序列:

SELECT nextval('process_time_seq');

--为主键设置Sequences:

alter table "public"."MACHINE_PROCESS_TIME" alter column "ID" set default nextval('process_time_seq');

--放入XML中

insert into "MACHINE_PROCESS_TIME" (
	"ID",
	"FACTORY_ID",
	"WORKSHOP_ID",
	"LINE_ID",
	"STATION_ID",
	"PROCESS_TIME",
	"PROCESS_DATE",
	"CREATE_DATE")values(
	nextval('process_time_seq'),
	#{factoryId,jdbcType=INTEGER},
	#{workshopId,jdbcType=INTEGER},
	#{lineId,jdbcType=INTEGER},
	#{stationId,jdbcType=VARCHAR},
	#{processTime,jdbcType=FLOAT},
	#{processDate,jdbcType=DATE},
	now()) 

 

上一篇:最新Python开发简单爬虫课程项目实战(完整)


下一篇:mybatis批量插入/更新时mapper的配置方法,oracle和mysql数据库配置方法不同