插入最后一个值 [英] Last value inserted

查看:60
本文介绍了插入最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我遇到了SERIAL字段的麻烦。


我有五张桌子。父表和四个孩子

表。当我在父表中执行INSERT时,我的
有一个ID(生成)序列(SERIAL字段),

我必须使用此ID来引用所有的孩子

表。


好​​吧,一旦我在父表中执行INSERT,我怎么能知道(b $ b)我知道(对于确定)

序列生成了哪个数字ID?


简单的例子:


---- --------------------------------------------------

CREATE TABLE parent(id SERIAL,描述CHAR(50));

-------------------- ----------------------------------


所以,


---------------------------------------- --------------

INSERT INTO parent(descrip)VALUES(''project 1'');

---- --------------------------------------------------


我现在(确定)有价值是如何通过

生成填充字段ID的序列?


(有很多用户使用该软件相同的

时间,所以我无法在序列上使用last_value()

函数。)


最好的问候,


Marcelo Pereira

巴西


__________________________________________________ _____

Yahoo! AcessoGrátis - Internetrápidaegrátis。 Instale o discador agora! http://br.acesso.yahoo.com/


---------------------------(广播结束)----------- ----------------

提示4:不要'杀'-9''邮政局长

解决方案

我认为最好的方法不是使用SERIAL字段,而是使用INTEGER

字段和序列:


CREATE SEQUENCE parent_seq;

CREATE TABLE parent(id INTEGER,descrip CHAR(50));

所以当你想在父元素上插入表,你从序列中获得下一个

值然后你在父母和孩子中插入

表你获得的价值:


newId:= SELECT nextval(''parent_seq'')

INSERT INTO parent(id,descrip)VALUES(newId,''XXXX'');

INSERT INTO child_1(...,...,parentId)VALUES(...,...,newId);

INSERT INTO child_2(...,...,parentId )VALUES(...,...,newId);

INSERT INTO child_3(...,...,parentId)VALUES(...,...,newId);

希望它有所帮助。


MaRCeLO PeReiRA写道:

大家好,

我遇到了SERIAL领域的麻烦。

我有五张桌子。父表和四个子表
。当我在父表中执行INSERT时,我有一个ID(生成)序列(SERIAL字段),
我必须使用此ID来引用所有子表


好吧,一旦我在父表中执行了INSERT,我怎么知道(肯定)
序列生成了哪个数字?
<简单的例子:

------------------------------------ ------------------
CREATE TABLE parent(id SERIAL,描述CHAR(50));
---------- --------------------------------------------

所以,

-------------------------------------- ----------------
INSERT INTO parent(descrip)VALUES(''project 1'');
---------- --------------------------------------------

我现在(确定)有价值是如何通过
生成字段ID的序列生成的?

(有很多用户在同一时间使用该软件) >时间,所以我无法在序列上使用last_value()
函数。)

最佳注册ards,

Marcelo Pereira
巴西


_________________________________________________ ______
雅虎! AcessoGrátis - Internetrápidaegrátis。 Instale o discador agora! http://br.acesso.yahoo.com/
< br ----> ---------------------------(播出结束)--------------- ------------
提示4:不要杀死-9''邮政局长



- -------------------------(播出结束)-------------------- -------

提示2:你可以使用取消注册命令一次性取消所有列表

(发送取消注册YourEmailAddressHere到 ma ******* @ postgresql.org


在星期四,2004-11-11 09:59-0300,MaRCeLO PeReiRA写道:

大家好,

我遇到了SERIAL字段的麻烦。

我有五张桌子。父表和四个子表
。当我在父表中执行INSERT时,我有一个ID(生成)序列(SERIAL字段),
我必须使用此ID来引用所有子表


好吧,一旦我在父表中执行了INSERT,我怎么知道(肯定)
序列生成了哪个数字?
<简单的例子:

------------------------------------ ------------------
CREATE TABLE parent(id SERIAL,描述CHAR(50));
---------- --------------------------------------------

所以,

-------------------------------------- ----------------
INSERT INTO parent(descrip)VALUES(''project 1'');
---------- --------------------------------------------

我现在(确定)有价值是如何通过
生成字段ID的序列生成的?

(有很多用户在同一时间使用该软件) >时间,所以我无法在sequ上使用last_value()
函数最好的问候,

Marcelo Pereira
巴西



我刚刚问了同样的问题大约一两个星期前的问题,我从Jonathan Daugherty得到了一个

的回复,他帮助我完成了初步查询,

和PHP我能够想出:

http:// blog .planetargon.com / index.ph ... nsert_id_.html


这个在几个星期前就在名单上:

- get_sequence(schema_name,table_name,column_name)

创建或替换函数get_sequence(text,text,text)返回
文本AS''
SELECT seq.relname :: text
FROM pg_class src,pg_class seq,pg_namespace,pg_attribute,
pg_depend

pg_depend.refobjsubid = pg_attribute.attnum AND
pg_depend.refobjid = src.oid AND
seq.oid = pg_depend.objid AND
src.reln amespace = pg_namespace.oid AND
pg_attribute.attrelid = src.oid AND
pg_namespace.nspname =


1 AND
src.relname =

Hi guys,

I am in troubles with a SERIAL field.

I have five tables. A parent table and four child
tables. When I do the INSERT in the parent table, I
have an ID (generated) by the sequence (SERIAL field),
and I have to use this ID to reference all child
tables.

Well, once I do an INSERT in the parent table, how can
I know (for sure) which number id was generated by the
sequence?

Simple example:

------------------------------------------------------
CREATE TABLE parent(id SERIAL, descrip CHAR(50));
------------------------------------------------------

So,

------------------------------------------------------
INSERT INTO parent (descrip) VALUES (''project 1'');
------------------------------------------------------

How can I now (for sure) with value was generated by
the sequence to fill the field ID?

(There is lots of users using the software at the same
time, so I am not able to use the last_value()
function on the sequence.)

Best Regards,

Marcelo Pereira
Brazil

__________________________________________________ _____
Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! http://br.acesso.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 4: Don''t ''kill -9'' the postmaster

解决方案

I think the best way would be not to use a SERIAL field, but an INTEGER
field and a sequence:

CREATE SEQUENCE parent_seq;
CREATE TABLE parent(id INTEGER, descrip CHAR(50));
So when you want to insert on the parent table, you obtain the next
value from the sequence and then you insert in the parent and child
tables the value you obtained:

newId:=SELECT nextval(''parent_seq'')
INSERT INTO parent(id, descrip) VALUES (newId, ''XXXX'');
INSERT INTO child_1(..., ..., parentId) VALUES (..., ..., newId);
INSERT INTO child_2(..., ..., parentId) VALUES (..., ..., newId);
INSERT INTO child_3(..., ..., parentId) VALUES (..., ..., newId);
hope it helps.

MaRCeLO PeReiRA wrote:

Hi guys,

I am in troubles with a SERIAL field.

I have five tables. A parent table and four child
tables. When I do the INSERT in the parent table, I
have an ID (generated) by the sequence (SERIAL field),
and I have to use this ID to reference all child
tables.

Well, once I do an INSERT in the parent table, how can
I know (for sure) which number id was generated by the
sequence?

Simple example:

------------------------------------------------------
CREATE TABLE parent(id SERIAL, descrip CHAR(50));
------------------------------------------------------

So,

------------------------------------------------------
INSERT INTO parent (descrip) VALUES (''project 1'');
------------------------------------------------------

How can I now (for sure) with value was generated by
the sequence to fill the field ID?

(There is lots of users using the software at the same
time, so I am not able to use the last_value()
function on the sequence.)

Best Regards,

Marcelo Pereira
Brazil

_________________________________________________ ______
Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! http://br.acesso.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 4: Don''t ''kill -9'' the postmaster


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


On Thu, 2004-11-11 at 09:59 -0300, MaRCeLO PeReiRA wrote:

Hi guys,

I am in troubles with a SERIAL field.

I have five tables. A parent table and four child
tables. When I do the INSERT in the parent table, I
have an ID (generated) by the sequence (SERIAL field),
and I have to use this ID to reference all child
tables.

Well, once I do an INSERT in the parent table, how can
I know (for sure) which number id was generated by the
sequence?

Simple example:

------------------------------------------------------
CREATE TABLE parent(id SERIAL, descrip CHAR(50));
------------------------------------------------------

So,

------------------------------------------------------
INSERT INTO parent (descrip) VALUES (''project 1'');
------------------------------------------------------

How can I now (for sure) with value was generated by
the sequence to fill the field ID?

(There is lots of users using the software at the same
time, so I am not able to use the last_value()
function on the sequence.)

Best Regards,

Marcelo Pereira
Brazil

I just asked this same question about a week or two ago and I got a
response from Jonathan Daugherty who helped me with the initial query,
and in PHP I was able to come up with:

http://blog.planetargon.com/index.ph...nsert_id_.html

This was on the list a few weeks ago:
-- get_sequence(schema_name, table_name, column_name)

CREATE OR REPLACE FUNCTION get_sequence (text, text, text) RETURNS
text AS ''
SELECT seq.relname::text
FROM pg_class src, pg_class seq, pg_namespace, pg_attribute,
pg_depend
WHERE
pg_depend.refobjsubid = pg_attribute.attnum AND
pg_depend.refobjid = src.oid AND
seq.oid = pg_depend.objid AND
src.relnamespace = pg_namespace.oid AND
pg_attribute.attrelid = src.oid AND
pg_namespace.nspname =


1 AND
src.relname =


这篇关于插入最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆