473,387 Members | 1,545 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,387 software developers and data experts.

Parent Id

when inserting new records into parent / child tables, what is the best
recommended way of retrieving the pkey value from the parent table when
using auto incrementing fields ?
--
Eugene Vital
Any technology indistinguishable from magic is insufficiently advanced.


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
6 2319
On Thu, Oct 09, 2003 at 14:26:21 -0400,
Gene Vital <ge*******@karibe.com> wrote:
when inserting new records into parent / child tables, what is the best
recommended way of retrieving the pkey value from the parent table when
using auto incrementing fields ?


You want to use currval. currval will return the last value obtained
by nextval in the same session, so it is safe to use without any additional
locking.

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

Nov 12 '05 #2
have an example :)

Bruno Wolff III wrote:
On Thu, Oct 09, 2003 at 14:26:21 -0400,
Gene Vital <ge*******@karibe.com> wrote:
when inserting new records into parent / child tables, what is the best
recommended way of retrieving the pkey value from the parent table when
using auto incrementing fields ?

You want to use currval. currval will return the last value obtained
by nextval in the same session, so it is safe to use without any additional
locking.


--
Eugene Vital
Any technology indistinguishable from magic is insufficiently advanced.


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

http://archives.postgresql.org

Nov 12 '05 #3
On Thu, Oct 09, 2003 at 15:55:27 -0400,
Gene Vital <ge*******@karibe.com> wrote:
have an example :)

Bruno Wolff III wrote:
On Thu, Oct 09, 2003 at 14:26:21 -0400,
Gene Vital <ge*******@karibe.com> wrote:
when inserting new records into parent / child tables, what is the best
recommended way of retrieving the pkey value from the parent table when
using auto incrementing fields ?

You want to use currval. currval will return the last value obtained
by nextval in the same session, so it is safe to use without any additional
locking.


You would do something like:
insert into parent_table (pk, col1, col2) values (default, 'val1', 'val2');
insert into child_table (pk, parent, col1, col2)
values (default, currval('parent_table_pk_seq'), 'val3', 'val4');

I don't remember when being able to use default in insert statements was
added. You may need to just leave the pk columns off the list. I added them
so that you could see what the normal sequence name looks like. I also
assume that the two pk columns are declared to be of type serial. If not,
then you have to do the sequence and default creation yourself.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #4
ok, I am new to Postgres so could you give a little better explanation
of this ??

I haven't created any sequence for this I am just using a type serial
field. will I have to create a sequence for it?
Here is my code to create the tables

CREATE TABLE workstations (station_id INT4 PRIMARY KEY, name
VARCHAR(50), description VARCHAR(250))

CREATE TABLE wsoptions (option_id SERIAL PRIMARY KEY, station_id INT4
REFERENCES workstations (station_id) ON DELETE CASCADE, type
VARCHAR(20), data TEXT)

insert into workstations (name, description)
values("new", "This is a test")

insert into wsoptions (stations_id, type, data)
values( ????, "LOCATION", "10th floor outer, office 27")
thanks....

Bruno Wolff III wrote:
On Thu, Oct 09, 2003 at 15:55:27 -0400,
Gene Vital <ge*******@karibe.com> wrote:
have an example :)

Bruno Wolff III wrote:

On Thu, Oct 09, 2003 at 14:26:21 -0400,
Gene Vital <ge*******@karibe.com> wrote:
when inserting new records into parent / child tables, what is the best
recommended way of retrieving the pkey value from the parent table when
using auto incrementing fields ?
You want to use currval. currval will return the last value obtained
by nextval in the same session, so it is safe to use without any additional
locking.

You would do something like:
insert into parent_table (pk, col1, col2) values (default, 'val1', 'val2');
insert into child_table (pk, parent, col1, col2)
values (default, currval('parent_table_pk_seq'), 'val3', 'val4');

I don't remember when being able to use default in insert statements was
added. You may need to just leave the pk columns off the list. I added them
so that you could see what the normal sequence name looks like. I also
assume that the two pk columns are declared to be of type serial. If not,
then you have to do the sequence and default creation yourself.


--
Eugene Vital
Any technology indistinguishable from magic is insufficiently advanced.


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #5
On Thu, Oct 09, 2003 at 17:09:33 -0400,
Gene Vital <ge*******@karibe.com> wrote:
ok, I am new to Postgres so could you give a little better explanation
of this ??

I haven't created any sequence for this I am just using a type serial
field. will I have to create a sequence for it?
When you use the serial type a sequence is automatically created for you.
The name is tablename_columnname_seq unless that string is too long
(> 64 characters I think). The actual name used gets printed as a notice
when you create the table.

Here is my code to create the tables

CREATE TABLE workstations (station_id INT4 PRIMARY KEY, name
VARCHAR(50), description VARCHAR(250))

CREATE TABLE wsoptions (option_id SERIAL PRIMARY KEY, station_id INT4
REFERENCES workstations (station_id) ON DELETE CASCADE, type
VARCHAR(20), data TEXT)

insert into workstations (name, description)
values("new", "This is a test")

insert into wsoptions (stations_id, type, data)
values( ????, "LOCATION", "10th floor outer, office 27")


The second insert should be:
insert into wsoptions (stations_id, type, data)
values( currval('workstations_station_id_seq'),
'LOCATION', '10th floor outer, office 27')

Also note that you need to use single quotes for data values. Double
quotes are used for the names of database objects.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #6


Bruno Wolff III wrote:
On Thu, Oct 09, 2003 at 17:09:33 -0400,
Gene Vital <ge*******@karibe.com> wrote:
ok, I am new to Postgres so could you give a little better explanation
of this ??

I haven't created any sequence for this I am just using a type serial
field. will I have to create a sequence for it?

When you use the serial type a sequence is automatically created for you.
The name is tablename_columnname_seq unless that string is too long
(> 64 characters I think). The actual name used gets printed as a notice
when you create the table.


I found that in the docs after I sent out the last post.
thanks for the feed back tho :)

Here is my code to create the tables

CREATE TABLE workstations (station_id INT4 PRIMARY KEY, name
VARCHAR(50), description VARCHAR(250))

CREATE TABLE wsoptions (option_id SERIAL PRIMARY KEY, station_id INT4
REFERENCES workstations (station_id) ON DELETE CASCADE, type
VARCHAR(20), data TEXT)

insert into workstations (name, description)
values("new", "This is a test")

insert into wsoptions (stations_id, type, data)
values( ????, "LOCATION", "10th floor outer, office 27")

The second insert should be:
insert into wsoptions (stations_id, type, data)
values( currval('workstations_station_id_seq'),
'LOCATION', '10th floor outer, office 27')

Also note that you need to use single quotes for data values. Double
quotes are used for the names of database objects.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


--
Eugene Vital
Any technology indistinguishable from magic is insufficiently advanced.


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #7

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

Similar topics

1
by: ahaideb | last post by:
I have a table (relation) in my database: --------------- | parent | child | --------------- | 1 | 2 | | 1 | 3 | | 2 | 4 | | 2 | 5 ...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
7
by: David Sobey | last post by:
hi Here's some code: void SomeFunc(childClass arg); .... parentClass a; a=new childClass(); SomeFunc(a);
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
25
by: Steve Jorgensen | last post by:
Yup, Steve's full of tips, but hey, it makes him feel important, right? Ok, here goes. I've been trying to improve encapsulation by putting code in the same object as the stuff it affects, so I...
0
by: Karthick Kumar | last post by:
Hi, I need to create a simple parent/child hierarchical menu. I have alread used the Treeview control but my requirement is slightly different than the Treeview control. I already have the kind...
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
3
by: Eddie | last post by:
If FormMain = MDI parent, FormSub = Child parent, I execute FormSub from the menu like this way. FormSub^ sub = gcnew FormSub; sub->MdiParent = this; sub->Show(); This can generate child...
1
by: Yuk Tang | last post by:
Leading on from a recent topic, "How does child class access parent's variables", I would like to ask, what's the most elegant way of changing a parent/grandparent/great-grandparent's property? ...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.