Re: [SQL] DEFAULT confusion - Mailing list pgsql-sql

From Moray McConnachie
Subject Re: [SQL] DEFAULT confusion
Date
Msg-id 003001bef9c9$e813b160$0102a8c0@public.ox.ac.uk
Whole thread Raw
In response to DEFAULT confusion  (Hroi Sigurdsson <hroi@ninja.dk>)
List pgsql-sql
> Suppose i have the following sequence, table and index:
>
> CREATE SEQUENCE stuff_seq;
> CREATE TABLE stuff (
>         id      INTEGER DEFAULT NEXTVAL('stuff_seq') NOT NULL,
>         name    TEXT,
>         number  INTEGER
> );
> CREATE UNIQUE INDEX stuff_id ON tabel(id);
>
> Then to properly insert rows i have to
>
> INSERT INTO tabel VALUES (NEXTVAL('tabel_seq'), "sometext", 123);
>
> I can't just
> INSERT INTO tabel VALUES (NULL, "something", 123);
>
> Then what is the point of the DEFAULT clause? In other words: How do I
> get away with not specifying anything for id?

It's not ideal, but if you make the sequence the last field in the table,
e.g.

CREATE TABLE tabel (this int2,that text,id serial)

, then you can do a

INSERT INTO tabel VALUES (5,'whatever);

& that works. I would love to know if there is a 'proper' solution, though.




pgsql-sql by date:

Previous
From: "Rudolph, Michael"
Date:
Subject: Failed Regression Tests due to SQL-Errors
Next
From: Herouth Maoz
Date:
Subject: Re: [SQL] DEFAULT confusion