By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

easy way to insert same value into 2 columns

P: n/a
Hello,

whats the most simple way to insert the same value into 2 colums of
the same table?
My table is looking like this:

Spalte | Typ | Attribute
--------+---------+----------------------
id | integer | default nextval ('public.zeit_id_seq'::text)
pos_id | integer | default xxx

for the xxx i want the same value than id be inserted automatically

Regards

Sebastian
---------------------------(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 #1
Share this Question
Share on Google+
11 Replies


P: n/a
For your specific question I don't know the answer.

For this particular case you could use:

create table your_table (
id integer default nextval ('public.zeit_id_seq'::text),
pos_id integer default currval ('public.zeit_id_seq'::text),
...
);

That would work fine as long as you use inserts which don't specify id
if pos_id is not specified (otherwise the currval will throw you an
error cause it cannot be called without nextval being called).

HTH,
Csaba.

On Thu, 2003-10-16 at 11:40, Sebastian Boeck wrote:
Hello,

whats the most simple way to insert the same value into 2 colums of
the same table?
My table is looking like this:

Spalte | Typ | Attribute
--------+---------+----------------------
id | integer | default nextval ('public.zeit_id_seq'::text)
pos_id | integer | default xxx

for the xxx i want the same value than id be inserted automatically

Regards

Sebastian
---------------------------(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


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

Nov 12 '05 #2

P: n/a
Csaba Nagy wrote:
For your specific question I don't know the answer.

For this particular case you could use:

create table your_table (
id integer default nextval ('public.zeit_id_seq'::text),
pos_id integer default currval ('public.zeit_id_seq'::text),
...
);

That would work fine as long as you use inserts which don't specify id
if pos_id is not specified (otherwise the currval will throw you an
error cause it cannot be called without nextval being called).

HTH,
Csaba.


Thanks a lot, but is it save to use?

Do i always get the same value, even if an other insert is changing
the sequence >public.zeit_id_seq< just at the same time?

Regards

Sebastian
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #3

P: n/a
Sebastian Boeck wrote:
Thanks a lot, but is it save to use?

Do i always get the same value, even if an other insert is changing the
sequence >public.zeit_id_seq< just at the same time?


You can write a before insert trigger where you retrieve sequence value once and
set for two fields. That way it will ensure that both the fields would get same
value.

HTH

Shridhar
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #4

P: n/a
On Thursday 16 October 2003 11:11, Sebastian Boeck wrote:
Csaba Nagy wrote:
For your specific question I don't know the answer.

For this particular case you could use:

create table your_table (
id integer default nextval ('public.zeit_id_seq'::text),
pos_id integer default currval ('public.zeit_id_seq'::text),
...
);

That would work fine as long as you use inserts which don't specify id
if pos_id is not specified (otherwise the currval will throw you an
error cause it cannot be called without nextval being called).

HTH,
Csaba.
Thanks a lot, but is it save to use?


Not really.
Do i always get the same value, even if an other insert is changing
the sequence >public.zeit_id_seq< just at the same time?


Other processes can't interfere - the whole point of sequences is that they
are safe for this sort of thing.

Where you will have problems is that if one of the developers decides it's
more efficient to process fields backwards (zzz...pos_id, id) rather than
forwards (id, pos_id, ...zzz) then it will break.

Use a trigger here. If nothing else so you can stop people like me doing:

INSERT INTO your_table (id,pos_id) VALUES (-1,DEFAULT);

--
Richard Huxton
Archonet Ltd

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

http://archives.postgresql.org

Nov 12 '05 #5

P: n/a
> Thanks a lot, but is it save to use?

Do i always get the same value, even if an other insert is changing
the sequence >public.zeit_id_seq< just at the same time? Yes. currval() gives you the last id of the sequence in your session.
So if others log in and insert more rows it wont affect your session.

Can I ask why you would wanna do that anyway?

Jacob
On Thu, 2003-10-16 at 12:11, Sebastian Boeck wrote: Csaba Nagy wrote:
For your specific question I don't know the answer.

For this particular case you could use:

create table your_table (
id integer default nextval ('public.zeit_id_seq'::text),
pos_id integer default currval ('public.zeit_id_seq'::text),
...
);

That would work fine as long as you use inserts which don't specify id
if pos_id is not specified (otherwise the currval will throw you an
error cause it cannot be called without nextval being called).

HTH,
Csaba.


Thanks a lot, but is it save to use?

Do i always get the same value, even if an other insert is changing
the sequence >public.zeit_id_seq< just at the same time?

Regards

Sebastian
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

--
Venlig hilsen / Best regards,
Jacob Vennervald
System Developer
Proventum Solutions ApS
Tuborg Boulevard 12
2900 Hellerup
Denmark
Phone: +45 36 94 41 66
Mobile: +45 61 68 58 51

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #6

P: n/a
Jacob Vennervald wrote:
Thanks a lot, but is it save to use?

Do i always get the same value, even if an other insert is changing
the sequence >public.zeit_id_seq< just at the same time?


Yes. currval() gives you the last id of the sequence in your session.
So if others log in and insert more rows it wont affect your session.

Can I ask why you would wanna do that anyway?

Jacob


Sure!

I have several tables with an inheritance hierachy.
The first one (zeit) has a Serialfield called id.
All other tables inherit from this one, so that i have
an absolutely unique id over all tables. But if i want
to reference the id of a table in the middle of the
hierarchy-chain, it won't work, because it's not the
tables own index. Therefore i need an duplicated value.
On this column i can create another index that can be
used as an foreign key by other tables.

Are there any other ways to accomplish this?

Regards

Sebastian
---------------------------(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 #7

P: n/a
I can't 100% say it's safe to use, but no, other inserts will not
influence the value, because currval will return the last sequence value
used by the current session (i.e. the current connection). And of course
you only have concurrent inserts using different connection, right ?

HTH,
Csaba.
On Thu, 2003-10-16 at 12:11, Sebastian Boeck wrote:
Csaba Nagy wrote:
For your specific question I don't know the answer.

For this particular case you could use:

create table your_table (
id integer default nextval ('public.zeit_id_seq'::text),
pos_id integer default currval ('public.zeit_id_seq'::text),
...
);

That would work fine as long as you use inserts which don't specify id
if pos_id is not specified (otherwise the currval will throw you an
error cause it cannot be called without nextval being called).

HTH,
Csaba.


Thanks a lot, but is it save to use?

Do i always get the same value, even if an other insert is changing
the sequence >public.zeit_id_seq< just at the same time?

Regards

Sebastian
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


---------------------------(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 #8

P: n/a
Csaba Nagy wrote:
I can't 100% say it's safe to use, but no, other inserts will not
influence the value, because currval will return the last sequence value
used by the current session (i.e. the current connection). And of course
you only have concurrent inserts using different connection, right ?

HTH,
Csaba.


Yes, that' right.

Thanks

Sebastian
---------------------------(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 #9

P: n/a
Richard Huxton wrote:
On Thursday 16 October 2003 11:11, Sebastian Boeck wrote:
Csaba Nagy wrote:
For your specific question I don't know the answer.

For this particular case you could use:

create table your_table (
id integer default nextval ('public.zeit_id_seq'::text),
pos_id integer default currval ('public.zeit_id_seq'::text),
...
);

That would work fine as long as you use inserts which don't specify id
if pos_id is not specified (otherwise the currval will throw you an
error cause it cannot be called without nextval being called).

HTH,
Csaba.


Thanks a lot, but is it save to use?

Not really.

Do i always get the same value, even if an other insert is changing
the sequence >public.zeit_id_seq< just at the same time?

Other processes can't interfere - the whole point of sequences is that they
are safe for this sort of thing.

Where you will have problems is that if one of the developers decides it's
more efficient to process fields backwards (zzz...pos_id, id) rather than
forwards (id, pos_id, ...zzz) then it will break.

Use a trigger here. If nothing else so you can stop people like me doing:

INSERT INTO your_table (id,pos_id) VALUES (-1,DEFAULT);


Is it possible to create the function called by a trigger entirely
in SQL, or do i have to use another language?

Regards

Sebastian
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #10

P: n/a
Sebastian Boeck wrote:
Is it possible to create the function called by a trigger entirely
in SQL, or do i have to use another language?


You can write trigger/functions in many languages. For this particular need
pl/pgsql would be more than good enough.

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

Nov 12 '05 #11

P: n/a
On Thu, Oct 16, 2003 at 13:53:27 +0200,
Sebastian Boeck <se************@freenet.de> wrote:
Csaba Nagy wrote:
I can't 100% say it's safe to use, but no, other inserts will not
influence the value, because currval will return the last sequence value
used by the current session (i.e. the current connection). And of course
you only have concurrent inserts using different connection, right ?

HTH,
Csaba.


Yes, that' right.

Thanks


As mentioned earlier, the problem is with concurrent inserts, but rather
with which default expression gets calculated first. Today you are likely
to get predictable behavior out of this, but if you do an alter table
or perhaps upgrade to a new version of postgres, your code may break.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.