473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Does INSERT inserts always at the end ?

Hello,

well, almost everything is in the subject !

I have to fill 2 tables (more complicated than in the example !):

CREATE TABLE A (
id serial primary key,
foo text);

CREATE TABLE B (
id serial references A,
bar text);

I fill A with :
INSERT into A VALUES (DEFAULT, "toto");

Then I need to retreive the "A.id" that was given to A, in order to give it
to B.id. If I was doing this by hand, it would have been quite easy, but I'm
doing this with a script.

So, if I make a SELECT id from A; and take the last row, will it *always*
be the row that I've just inserted.

Thanks for any tip

--
Florence HENRY
Nov 23 '05 #1
5 1725
A long time ago, in a galaxy far, far away, fl************@obspm.fr (Florence HENRY) wrote:
well, almost everything is in the subject !

I have to fill 2 tables (more complicated than in the example !):

CREATE TABLE A (
id serial primary key,
foo text);

CREATE TABLE B (
id serial references A,
bar text);

I fill A with :
INSERT into A VALUES (DEFAULT, "toto");

Then I need to retreive the "A.id" that was given to A, in order to give it
to B.id. If I was doing this by hand, it would have been quite easy, but I'm
doing this with a script.

So, if I make a SELECT id from A; and take the last row, will it *always*
be the row that I've just inserted.


This won't happen "implicitly."

tutorial=# CREATE TABLE A (id serial primary key,foo text);
NOTICE: CREATE TABLE will create implicit sequence "a_id_seq" for "serial" column "a.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "a_pkey" for table "a"
CREATE TABLE
tutorial=# drop table b;
DROP TABLE
tutorial=# CREATE TABLE B (id serial references A,bar text);
NOTICE: CREATE TABLE will create implicit sequence "b_id_seq" for "serial" column "b.id"
CREATE TABLE

I can suggest a couple of ways you might do this:

1. So long as the inserts take place within the scope of the same
transaction on the same connection, it would be safe to create
the B entry via currval for the sequence.

tutorial=# begin;
BEGIN
tutorial=# insert into a (foo) values ('toto');
INSERT 2587831 1
tutorial=# insert into b (id, bar) values (currval('a_id_seq'), 'yellow brick road');
INSERT 2587832 1
tutorial=# commit;
COMMIT

Note that if you don't enclose it in BEGIN/COMMIT, the insert into b
could pick up on changes from other concurrent sessions.

2. You might create a stored procedure that creates both entries,
using currval() behind your back.

[assuming suitably-created funciton...

select make_foo_bar ('toto', 'yellow brick road');

This won't work so well if there are to be multiple associations; if
that be the case, you'd want to have an explicit external primary key,
and do something like:

select create_a ('toto', 'key-for-toto');
select link_b_to_a ('key-for-toto', 'yellow brick road');
select link_b_to_a ('key-for-toto', 'click, click');
select link_b_to_a ('key-for-toto', 'ruby shoes');

There's no magic there; the stored procedure link_b_to_a() would look
up the ID number for 'key-for-toto' in table A.
--
(reverse (concatenate 'string "moc.enworbbc" "@" "enworbbc"))
http://cbbrowne.com/info/spiritual.html
Rules of the Evil Overlord #54. "I will not strike a bargain with a
demonic being then attempt to double-cross it simply because I feel
like being contrary." <http://www.eviloverlord.com/>
Nov 23 '05 #2
On Wed, May 19, 2004 at 12:41:12PM -0400, Christopher Browne wrote:
This won't happen "implicitly."

tutorial=# CREATE TABLE A (id serial primary key,foo text);
NOTICE: CREATE TABLE will create implicit sequence "a_id_seq" for "serial" column "a.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "a_pkey" for table "a"
CREATE TABLE
tutorial=# drop table b;
DROP TABLE
tutorial=# CREATE TABLE B (id serial references A,bar text);
NOTICE: CREATE TABLE will create implicit sequence "b_id_seq" for "serial" column "b.id"
CREATE TABLE

I can suggest a couple of ways you might do this:

1. So long as the inserts take place within the scope of the same
transaction on the same connection, it would be safe to create
the B entry via currval for the sequence.
WRONG! As long as they're in the same session, it will work. The
transaction has nothing to do with it.
tutorial=# begin;
BEGIN
tutorial=# insert into a (foo) values ('toto');
INSERT 2587831 1
tutorial=# insert into b (id, bar) values (currval('a_id_seq'), 'yellowbrick road');
INSERT 2587832 1
tutorial=# commit;
COMMIT

Note that if you don't enclose it in BEGIN/COMMIT, the insert into b
could pick up on changes from other concurrent sessions.
Nope. Even without the BEGIN/COMMIT and three hours between the two
statements, it will still work. All that matters is that they're in the
same session. Check the documentation.
--
Martijn van Oosterhout <kl*****@svana.org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAq7zEY5Twig3Ge+YRAoqPAJ9+duWf1ocYfxUuh1Tbk4 rVdCAo0gCgpzqV
jAo6/jAe3zbIt0GaiPKHhkc=
=JS7I
-----END PGP SIGNATURE-----

Nov 23 '05 #3
fl************@obspm.fr (Florence HENRY) wrote:

Hello,

well, almost everything is in the subject !

I have to fill 2 tables (more complicated than in the example !):

CREATE TABLE A (
id serial primary key,
foo text);

CREATE TABLE B (
id serial references A,
bar text);

I fill A with :
INSERT into A VALUES (DEFAULT, "toto");

Then I need to retreive the "A.id" that was given to A, in order to give it
to B.id. If I was doing this by hand, it would have been quite easy, but I'm
doing this with a script.
Define what you mean by "with a script." If you've a db handle open
with, say, Perl's DBI, you could simply do a select on currval() for
the sequence and get it. This is immune to other transactions. But
if, by "script" you mean, say, from a shell script, where you're
feeding commands to psql from stdin or some-such, well... I suppose you
could "echo 'mumble; select currval(blurfl)' |psql" and capture it.
(Caveat: I haven't tried this. I'm just guessing.)

So, if I make a SELECT id from A; and take the last row, will it *always*
be the row that I've just inserted.


I was told just a few days ago to always regard data in a table as
"unordered." Going by that philosophy: No, you cannot. You
*certainly* cannot if more than one session/task is operating on the
table.

Jim

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4
js******@LinxNet.com (Jim Seymour) wrote :
Define what you mean by "with a script." If you've a db handle open
with, say, Perl's DBI, you could simply do a select on currval() for
the sequence and get it. This is immune to other transactions.


Indeed, I'm using Perl's DBI. currval does exactly what I want. Thanks to
all who pointed it to me.

Before I could read your messages, I made a workaround that worked nice also,
but could you tell me if it would always work ?

It uses the "oid" number. I've read that it used to be unique, but within
the same session, I can assume that it is always growing, can't I ?

INSERT into A VALUES (DEFAULT, "toto");

INSERT into B VALUES (
(SELECT id FROM A WHERE oid = SELECT ( max(oid) from A)),
"toto");

--
Florence HENRY
florence point henry arobasse obspm point fr
Nov 23 '05 #5
On Tue, May 25, 2004 at 01:15:14AM -0700, Florence HENRY wrote:
Indeed, I'm using Perl's DBI. currval does exactly what I want. Thanks to
all who pointed it to me.
That's the way it should be done.
Before I could read your messages, I made a workaround that worked nice also,
but could you tell me if it would always work ?

It uses the "oid" number. I've read that it used to be unique, but within
the same session, I can assume that it is always growing, can't I ?
NO! The OID will wrap eventually, it is not guarenteed to be unique.
And you can now make tables without OIDs.
--
Martijn van Oosterhout <kl*****@svana.org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAu6wLY5Twig3Ge+YRAj8XAKCWgv7KsTrhbc2MhDNwcD xHUis1GgCgjUso
xVLDoUdheUkUG5YJv4QIS/4=
=Olr8
-----END PGP SIGNATURE-----

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe | last post by:
Hey, I'm going to give some background on my situation in case anyone can point out a way around my problem altogether... for the problem itself, please skip to the bottom of the post. thanks....
4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
3
by: A Lonely Programmer | last post by:
Ok i know that using an access db is a corporate nono but i am not about to buy sql for anybody (well maybe myself someday) and an approved msde update must come AFTER i get certain things up and...
46
by: dunleav1 | last post by:
I have a process that does inserts that runs 50% slower that Oracle and Mssql. Queries normally take 50% slower than normal. DB2, Oracle, Mssql all are configured on same os, same disk array...
5
by: asdf | last post by:
I have a program that reads sorted data from a database and inserts it element by element into a set. If the data is sorted is there a faster way to insert ? Meaning is there a way to tell the...
2
by: wombat53 | last post by:
Hi Group Are there any DB2 UDB ESE DPF V8.2 users exploiting "buffered inserts" (BIND parm INSERT BUF) *and* "multi-row INSERTS" (many rows associated with the VALUES clause of the INSERT to...
4
by: Michel Esber | last post by:
Hello, Environment: db2 V8 FP 13 LUW Our application currently uses: insert into table values ('A'),('B'),...('Z') We have used CLI arrays inserts (1000 array and commit size) and...
5
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration:...
24
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.