在PostgreSQL中,如何将最后一个id插入表中?
在MS SQL中有SCOPE_IDENTITY().
请不要建议我使用这样的东西:
select max(id) from table
Run Code Online (Sandbox Code Playgroud) 有没有办法从最后插入的行中获取值?
我正在插入一个PK将自动增加的行,我想得到这个PK.只有PK才能保证在表中是唯一的.
我正在使用Java和JDBC和PostgreSQL.
在sql server我喜欢这样:
insert into foo(name) values('bob')
select @@identity;
Run Code Online (Sandbox Code Playgroud)
所以我得到一个查询/标量结果显示
如何用postgres?
这是场景:
create table a (
id serial primary key,
val text
);
create table b (
id serial primary key,
a_id integer references a(id)
);
create rule a_inserted as on insert to a do also insert into b (a_id) values (new.id);
Run Code Online (Sandbox Code Playgroud)
b我正在尝试创建一条引用a插入表时的记录a。但我得到的是new.idnull,因为它是从序列自动生成的。我也尝试了触发器AFTER插入FOR EACH ROW,但结果是相同的。有办法解决这个问题吗?
postgresql ×4
sql ×2
database ×1
insert ×1
java ×1
jdbc ×1
lastinsertid ×1
primary-key ×1
sql-server ×1
triggers ×1