Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 用于创建触发器的脚本,将实现人工键的自动增量_Postgresql - Fatal编程技术网

Postgresql 用于创建触发器的脚本,将实现人工键的自动增量

Postgresql 用于创建触发器的脚本,将实现人工键的自动增量,postgresql,Postgresql,当插入到表中时,它会将主键(id)的值设置为序列中的下一个值,以确保DB的用户不需要写入正确的id(它将由触发器生成)。您不需要触发器,您需要使用生成为标识的标识列来将该列设置为自动增量 create table tablename( id int not null generated always as identity primary key , col1 , col2 , col3 ) 因此,无论何时插入到该表中,您都不必提及: insert into tablename

当插入到表中时,它会将主键(id)的值设置为序列中的下一个值,以确保DB的用户不需要写入正确的id(它将由触发器生成)。

您不需要触发器,您需要使用
生成为标识的标识列来将该列设置为自动增量

create table tablename(
 id int not null generated always as identity primary key
  , col1
  , col2 
  , col3
)
因此,无论何时插入到该表中,您都不必提及:

insert into tablename (col1, col2, col3) values (val1,val2,val3)

不需要触发器