pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedDEFAULT confusion
9+ messages / 7 participants
[nested] [flat]
* DEFAULT confusion
@ 1999-09-07 23:28 Hroi Sigurdsson <hroi@get2net.dk>
0 siblings, 0 replies; 9+ messages in thread
From: Hroi Sigurdsson @ 1999-09-07 23:28 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 9+ messages in thread
* DEFAULT confusion
@ 1999-09-08 02:39 Hroi Sigurdsson <hroi@ninja.dk>
0 siblings, 3 replies; 9+ messages in thread
From: Hroi Sigurdsson @ 1999-09-08 02:39 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [SQL] DEFAULT confusion
@ 1999-09-08 07:15 Moray McConnachie <moray.mcconnachie@computing-services.oxford.ac.uk>
parent: Hroi Sigurdsson <hroi@ninja.dk>
2 siblings, 0 replies; 9+ messages in thread
From: Moray McConnachie @ 1999-09-08 07:15 UTC (permalink / raw)
To: Hroi Sigurdsson <hroi@ninja.dk>; 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.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [SQL] DEFAULT confusion
@ 1999-09-08 09:25 Herouth Maoz <herouth@oumail.openu.ac.il>
parent: Hroi Sigurdsson <hroi@ninja.dk>
2 siblings, 1 reply; 9+ messages in thread
From: Herouth Maoz @ 1999-09-08 09:25 UTC (permalink / raw)
To: Hroi Sigurdsson <hroi@ninja.dk>; pgsql-sql
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
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [SQL] DEFAULT confusion
@ 1999-09-08 12:43 Hroi Sigurdsson <hroi@ninja.dk>
parent: Herouth Maoz <herouth@oumail.openu.ac.il>
0 siblings, 0 replies; 9+ messages in thread
From: Hroi Sigurdsson @ 1999-09-08 12:43 UTC (permalink / raw)
To: Herouth Maoz <herouth@oumail.openu.ac.il>; +Cc: pgsql-sql
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
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [SQL] DEFAULT confusion
@ 1999-09-08 13:51 Thomas Mack <mack@ips.cs.tu-bs.de>
0 siblings, 0 replies; 9+ messages in thread
From: Thomas Mack @ 1999-09-08 13:51 UTC (permalink / raw)
To: pgsql-sql
>> 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.
>
Well - normally you specify your column names in an insert, if you
don't want to fill all values or if you are unsure about the sequence
of attributes:
------------------------------------------------------------
mack=> CREATE SEQUENCE stuff_seq;
CREATE
mack=> CREATE TABLE stuff (
mack-> id INTEGER DEFAULT NEXTVAL('stuff_seq') NOT NULL,
mack-> name TEXT,
mack-> number INTEGER
mack-> );
CREATE
mack=> insert into stuff (name, number) values ('something', 123);
INSERT 1403035 1
mack=> select * from stuff;
id|name |number
--+---------+------
1|something| 123
(1 row)
mack=>
------------------------------------------------------------
Thomas Mack
TU Braunschweig, Abt. Informationssysteme
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [SQL] DEFAULT confusion
@ 1999-09-08 15:22 Ross J. Reedstrom <reedstrm@wallace.ece.rice.edu>
parent: Hroi Sigurdsson <hroi@ninja.dk>
2 siblings, 1 reply; 9+ messages in thread
From: Ross J. Reedstrom @ 1999-09-08 15:22 UTC (permalink / raw)
To: Hroi Sigurdsson <hroi@ninja.dk>; +Cc: 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@rice.edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [SQL] DEFAULT confusion
@ 1999-09-08 17:29 Hroi Sigurdsson <hroi@ninja.dk>
parent: Ross J. Reedstrom <reedstrm@wallace.ece.rice.edu>
0 siblings, 0 replies; 9+ messages in thread
From: Hroi Sigurdsson @ 1999-09-08 17:29 UTC (permalink / raw)
To: Ross J. Reedstrom <reedstrm@wallace.ece.rice.edu>; +Cc: pgsql-sql
"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
^ permalink raw reply [nested|flat] 9+ messages in thread
* RE: [SQL] DEFAULT confusion
@ 1999-09-08 18:08 Jackson, DeJuan <djackson@cpsgroup.com>
0 siblings, 0 replies; 9+ messages in thread
From: Jackson, DeJuan @ 1999-09-08 18:08 UTC (permalink / raw)
To: Hroi Sigurdsson <hroi@ninja.dk>; +Cc: pgsql-sql
> -----Original Message-----
> From: Hroi Sigurdsson [SMTP:hroi@ninja.dk]
> Sent: Tuesday, September 07, 1999 9:40 PM
> To: pgsql-sql@postgreSQL.org
> Subject: [SQL] DEFAULT confusion
>
> 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);
Change the above line to:
INSERT INTO stuff(name, number) VALUES ('sometext', 123);
Hope this helps,
DEJ
> 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
>
> ************
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~1999-09-08 18:08 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-09-07 23:28 DEFAULT confusion Hroi Sigurdsson <hroi@get2net.dk>
1999-09-08 02:39 DEFAULT confusion Hroi Sigurdsson <hroi@ninja.dk>
1999-09-08 07:15 ` Moray McConnachie <moray.mcconnachie@computing-services.oxford.ac.uk>
1999-09-08 09:25 ` Herouth Maoz <herouth@oumail.openu.ac.il>
1999-09-08 12:43 ` Hroi Sigurdsson <hroi@ninja.dk>
1999-09-08 15:22 ` Ross J. Reedstrom <reedstrm@wallace.ece.rice.edu>
1999-09-08 17:29 ` Hroi Sigurdsson <hroi@ninja.dk>
1999-09-08 13:51 Re: [SQL] DEFAULT confusion Thomas Mack <mack@ips.cs.tu-bs.de>
1999-09-08 18:08 RE: [SQL] DEFAULT confusion Jackson, DeJuan <djackson@cpsgroup.com>
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