Re: inserting values like in mySQL

From: Andrew McMillan <andrew(at)catalyst(dot)net(dot)nz>
To: Peter Asemann <Peter(dot)Asemann(at)rrze(dot)uni-erlangen(dot)de>
Cc: PostgreSQL Novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: inserting values like in mySQL
Date: 2001-05-23 10:52:40
Message-ID: 3B0B9678.D816207D@catalyst.net.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Peter Asemann wrote:
>
> Hi there!
>
> We (me and others from my group) have to convert mySQL syntax to
> PostGreSQL, and incidentally we had some problems.
>
> We have a table named users with ID, name, pass as columns.
>
> In mySQL we had the column "ID" set to auto-increment. It took us some
> time to find out how to use the "serial" feature ;-)
>
> In mySQL it was like this:
>
> insert into users values ('','peter','my_pass');
>
> In PostGreSQL this does not work. The only thing that works is
>
> insert into users (name,pass) values ('peter','my_pass');
>
> Apparently this is longer, and we'll have tables with much more columns,
> so we'll have to write much more than in mySQL, and as we're lazy people
> (all programmers are, Larry Wall says), we don't want to write a single
> character more than necessary.
>
> Is there a way to set all columns without explicitly giving their
> names? Isn't there something to indicate that the value we give to the
> database is only a dummy like the '' in mySQL?

Having created the column with 'SERIAL', PostgreSQL will actually create a sequence
called table_column_seq, and will define the column as "DEFAULT
nextval('table_column_seq')" so you can fake what it does by entering the same
default into your insert as PostgreSQL has put on the column, viz:

insert into users values( nextval('users_ID_seq'), 'peter','my_pass');

Hope this helps,
Andrew.
--
_____________________________________________________________________
Andrew McMillan, e-mail: Andrew(at)catalyst(dot)net(dot)nz
Catalyst IT Ltd, PO Box 10-225, Level 22, 105 The Terrace, Wellington
Me: +64(21)635-694, Fax: +64(4)499-5596, Office: +64(4)499-2267xtn709

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Rob Brown-Bayliss 2001-05-23 21:57:27 IGNORE-Mail relay test
Previous Message Nabil Sayegh 2001-05-23 01:36:08 Re: inserting values like in mySQL