pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ross J. Reedstrom <reedstrm@wallace.ece.rice.edu>
To: Hroi Sigurdsson <hroi@ninja.dk>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] DEFAULT confusion
Date: Wed, 8 Sep 1999 10:22:58 -0500
Message-ID: <19990908102258.B24655@wallace.ece.rice.edu> (raw)
In-Reply-To: <37D5CC72.9A7CBD49@ninja.dk>
References: <37D5CC72.9A7CBD49@ninja.dk>

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@rice.edu> 
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St.,  Houston, TX 77005



view thread (9+ messages)  latest in thread

Message-ID: <19990908102258.B24655@wallace.ece.rice.edu>
Permalink:  ../19990908102258.B24655@wallace.ece.rice.edu/
Also on:    postgresql.org/message-id/19990908102258.B24655@wallace.ece.rice.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: reedstrm@wallace.ece.rice.edu, hroi@ninja.dk
  Subject: Re: [SQL] DEFAULT confusion
  In-Reply-To: <19990908102258.B24655@wallace.ece.rice.edu>
  References: <37D5CC72.9A7CBD49@ninja.dk> <19990908102258.B24655@wallace.ece.rice.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox