插入最后一个值

编程入门 行业动态 更新时间:2024-10-09 11:28:19
本文介绍了插入最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我遇到了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! br.acesso.yahoo/ ---------------------------(广播结束)----------- ---------------- 提示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,我怎么知道(肯定)序列生成了哪个数字? 时间,所以我无法在序列上使用last_value()函数。) 最佳注册ards, Marcelo Pereira 巴西 _________________________________________________ ______ 雅虎! AcessoGrátis - Internetrápidaegrátis。 Instale o discador agora! br.acesso.yahoo/ ---------------------------(播出结束)--------------- ------------ 提示4:不要杀死-9''邮政局长

- -------------------------(播出结束)-------------------- ------- 提示2:你可以使用取消注册命令一次性取消所有列表 (发送取消注册YourEmailAddressHere到 ma ******* @ postgresql )

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

大家好, 我遇到了SERIAL字段的麻烦。 我有五张桌子。父表和四个子表。当我在父表中执行INSERT时,我有一个ID(生成)序列(SERIAL字段),我必须使用此ID来引用所有子表。 好吧,一旦我在父表中执行了INSERT,我怎么知道(肯定)序列生成了哪个数字? 时间,所以我无法在sequ上使用last_value()函数最好的问候, Marcelo Pereira 巴西 我刚刚问了同样的问题大约一两个星期前的问题,我从Jonathan Daugherty得到了一个 的回复,他帮助我完成了初步查询, 和PHP我能够想出: http:// blog .planetargon / 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! br.acesso.yahoo/ ---------------------------(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 childtables. When I do the INSERT in the parent table, Ihave an ID (generated) by the sequence (SERIAL field),and I have to use this ID to reference all childtables.Well, once I do an INSERT in the parent table, how canI know (for sure) which number id was generated by thesequence?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 bythe sequence to fill the field ID?(There is lots of users using the software at the sametime, so I am not able to use the last_value()function on the sequence.)Best Regards,Marcelo PereiraBrazil _________________________________________________ ______Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! br.acesso.yahoo/---------------------------(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)

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: blog.planetargon/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 =

更多推荐

插入最后一个值

本文发布于:2023-07-21 07:23:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1175410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!