Thread: Insert into a table with only a SERIAL

Insert into a table with only a SERIAL

From
Date:
I am pretty much self taught using SQL, and I suspect that my problem
here is trying to do something silly.  I have since changed my tables
and avoided the problem, but I am curious as to why this happens, and
it's remotely possible I have even found a bug or two.

I created a table with only a sequence:

  CREATE TABLE aaa (
    id SERIAL,
  );

I can't insert into aaa unless I pass a value; these don't work:

  insert into aaa;
  insert into aaa values ();
  insert into aaa () values ();

But this does:

  insert into aaa values (1);

except that the value inserted is not known to the sequence.
(** This seems a bug to me **)

This also works:

  insert into aaa values (nextval('aaa_id_seq'));

but sort of defeats the purpose of using a type SERIAL rather than
hand rolling my own sequence.

--
            ... _._. ._ ._. . _._. ._. ___ .__ ._. . .__. ._ .. ._.
     Felix Finch: scarecrow repairman & rocket surgeon / felix@crowfix.com
  GPG = E987 4493 C860 246C 3B1E  6477 7838 76E9 182E 8151 ITAR license #4933
I've found a solution to Fermat's Last Theorem but I see I've run out of room o

Re: Insert into a table with only a SERIAL

From
Tom Lane
Date:
<felix@crowfix.com> writes:
> I can't insert into aaa unless I pass a value; these don't work:

>   insert into aaa;
>   insert into aaa values ();
>   insert into aaa () values ();

"INSERT INTO aaa DEFAULT VALUES" is the SQL-approved verbiage.

(Hey, I didn't design this language ;-))

            regards, tom lane

Re: Insert into a table with only a SERIAL

From
Richard Harvey Chapman
Date:
On Sun, 25 Jun 2000, Tom Lane wrote:

> "INSERT INTO aaa DEFAULT VALUES" is the SQL-approved verbiage.

Wow, the same question in two days.  Perhaps FAQ worthy?

And yes, if I had only caught up on my reading, I wouldn't have had to
have posted the second one.  Argh.

R.



Re: Insert into a table with only a SERIAL

From
Lincoln Yeoh
Date:
At 06:56 PM 27-06-2000 -0700, Richard Harvey Chapman wrote:
>On Sun, 25 Jun 2000, Tom Lane wrote:
>
>> "INSERT INTO aaa DEFAULT VALUES" is the SQL-approved verbiage.
>
>Wow, the same question in two days.  Perhaps FAQ worthy?

Actually it seems to happen quite often - all of a sudden people ask the
same thing around the same time.

Maybe part of that World Mind/Consciousness thing :).

But it could also be something like the 3 buses in a row phenomena.

For example: newbies start with a standard distribution of questions.

Some of them are answered by people on the list, some not.

Thus a bunch of newbies will reach a certain stage in their
understanding/knowledge and experience. These bunch will tend encounter
similar problems, and thus ask similar questions, some of which are answered.

This hypothesis predicts that "same question in X days" will happen more
often than in a random/unbiased case.

Cheerio,
Link.