Обсуждение: DEFAULT confusion

Поиск
Список
Период
Сортировка

DEFAULT confusion

От
Hroi Sigurdsson
Дата:
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);

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

In short I want to emulate MySQL's way of doing
CREATE TABLE(       id      INT NOT NULL AUTO_INCREMENT,       name    TEXT,       int     INT,       UNIQUE(id)
);

I hope I made myself sufficiently clear.

Thank you for your time.


PS.
Where can I get more information about the REFERENCES keyword and when
will it be fully working in Postgres?
PPS.
Are there any books out there that cover Postgresql?
-- 
Hroi Sigurdsson
hroi@ninja.dk


Re: [SQL] DEFAULT confusion

От
"Moray McConnachie"
Дата:
> 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.




Re: [SQL] DEFAULT confusion

От
Herouth Maoz
Дата:
At 04:39 +0200 on 08/09/1999, Hroi Sigurdsson wrote:


> INSERT INTO tabel VALUES (NEXTVAL('tabel_seq'), "sometext", 123);
>
> I can't just
> INSERT INTO tabel VALUES (NULL, "something", 123);

<RANT MODE="old teacher">
[tearing hair off in despair]

Children, how many times do I have to tell you that...
</RANT>

In an insert statement, you should always mention the names of the fields
to which you enter values. The syntax should be:

INSERT INTO table (field1, field2, field3)
VALUES (value1, value2, value3);

The SQL syntax allows you to drop the field names when you insert to *all*
of them. But this is NOT recommended, and should be used only in ad-hoc
insert statement done, say, in psql when you just have something to fix on
the fly.

In REAL applications, always mention the fields. This will prevent mixups
due to changes in the schema over time by giving you clear error messages
such as 'column not found' etc., instead of finding obscure data conversion
errors or none at all.

Now, what's the relevance of this lecture to your question?

If you don't mention the name of a field, and that field has a default
value, the default value will be used. It's that simple!

INSERT INTO stuff (name, number)
VALUES ("sometext", 123);

Will do the trick.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma




Re: [SQL] DEFAULT confusion

От
"Ross J. Reedstrom"
Дата:
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


Re: [SQL] DEFAULT confusion

От
Hroi Sigurdsson
Дата:
Herouth Maoz wrote:

> If you don't mention the name of a field, and that field has a default
> value, the default value will be used. It's that simple!

That was exactly what I needed to know. Thank you. It was that simple?
Also thanks to Constantin Simona <cconstan@itcnet.ro>, who pointed that
same thing out to me in a private mail.

-- 
Hroi Sigurdsson
hroi@ninja.dk


Re: [SQL] DEFAULT confusion

От
Hroi Sigurdsson
Дата:
"Ross J. Reedstrom" wrote:

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

Yes I noticed. I wanted to rename 'tabel' to 'stuff' in my mail to avoid
similarity to the reserved TABLE word. I guess I missed all occurrences.

-- 
Hroi Sigurdsson
hroi@ninja.dk