easy way to insert same value into 2 columns

Started by Sebastian Böckover 22 years ago12 messagesgeneral
Jump to latest
#1Sebastian Böck
sebastianboeck@freenet.de

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

#2Csaba Nagy
nagy@ecircle-ag.com
In reply to: Sebastian Böck (#1)
Re: easy way to insert same value into 2 columns

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.

Show quoted text

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

#3Sebastian Böck
sebastianboeck@freenet.de
In reply to: Csaba Nagy (#2)
Re: easy way to insert same value into 2 columns

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

#4Shridhar Daithankar
shridhar_daithankar@persistent.co.in
In reply to: Sebastian Böck (#3)
Re: easy way to insert same value into 2 columns

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

#5Richard Huxton
dev@archonet.com
In reply to: Sebastian Böck (#3)
Re: easy way to insert same value into 2 columns

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

#6Jacob Vennervald
jvennervald@proventum.net
In reply to: Sebastian Böck (#3)
Re: easy way to insert same value into 2 columns

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

#7Sebastian Böck
sebastianboeck@freenet.de
In reply to: Jacob Vennervald (#6)
Re: easy way to insert same value into 2 columns

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

#8Csaba Nagy
nagy@ecircle-ag.com
In reply to: Sebastian Böck (#3)
Re: easy way to insert same value into 2 columns

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.

Show quoted text

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

#9Sebastian Böck
sebastianboeck@freenet.de
In reply to: Csaba Nagy (#8)
Re: easy way to insert same value into 2 columns

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

#10Sebastian Böck
sebastianboeck@freenet.de
In reply to: Richard Huxton (#5)
Re: easy way to insert same value into 2 columns

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

#11Shridhar Daithankar
shridhar_daithankar@persistent.co.in
In reply to: Sebastian Böck (#10)
Re: easy way to insert same value into 2 columns

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

#12Bruno Wolff III
bruno@wolff.to
In reply to: Sebastian Böck (#9)
Re: easy way to insert same value into 2 columns

On Thu, Oct 16, 2003 at 13:53:27 +0200,
Sebastian Boeck <sebastianboeck@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.