Re: [SQL] DEFAULT confusion

From: "Ross J(dot) Reedstrom" <reedstrm(at)wallace(dot)ece(dot)rice(dot)edu>
To: Hroi Sigurdsson <hroi(at)ninja(dot)dk>
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] DEFAULT confusion
Date: 1999-09-08 15:22:58
Message-ID: 19990908102258.B24655@wallace.ece.rice.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Sep 08, 1999 at 02:39:46AM +0000, Hroi Sigurdsson wrote:
> Hello postgresql'ers (how do you pronounce that?).
>
> 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);

Multiple errors here, BTW: your table name is "stuff" not "tabel", and
"something" is a field or table name, 'something' is a quoted string.

>
> Then what is the point of the DEFAULT clause? In other words: How do I
> get away with not specifying anything for id? And how (if

Herouth and Thomas Mack have already pointed out the correct INSERT
syntax, (good rant, Herouth!) so I won't deal with this here.

INSERT INTO stuff (name,number) VALUES ('something',123);

> possible/recommendable) do I force the id value to be nothing but
> NEXTVAL('stuff_seq'), ie. not just an arbitrary number?

Here you enter the realm of triggers. To use them, you have to write
a procedure that can be used as a 'BEFORE INSERT' trigger. However,
I think you'll find properly formated INSERT statements will do most of
what you want. I just don't insert into my serial columns, ever.

Ross
--
Ross J. Reedstrom, Ph.D., <reedstrm(at)rice(dot)edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Hroi Sigurdsson 1999-09-08 17:29:05 Re: [SQL] DEFAULT confusion
Previous Message Thomas Mack 1999-09-08 13:51:39 Re: [SQL] DEFAULT confusion