Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 370CC2E0115 for ; Fri, 30 Nov 2007 07:53:48 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 37531-03 for ; Fri, 30 Nov 2007 07:53:37 -0400 (AST) Received: from beaver.fastcrypt.com (beaver.fastcrypt.com [64.7.141.17]) by postgresql.org (Postfix) with ESMTP id 55AFC2E00D3 for ; Fri, 30 Nov 2007 07:53:43 -0400 (AST) Received: from [192.168.1.14] (dcdsl.ebox.com [64.7.143.116]) by beaver.fastcrypt.com (Postfix) with ESMTP id 2188B1580521; Fri, 30 Nov 2007 07:05:09 -0500 (EST) Cc: pgsql-jdbc@postgresql.org Message-Id: <0F50756B-C295-4488-96F5-4FBFE73EB383@fastcrypt.com> From: Dave Cramer To: =?ISO-8859-1?Q?Christian_Schr=F6der?= In-Reply-To: <474FEED9.2060000@deriva.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Apple Message framework v915) Subject: Re: Serial data type Date: Fri, 30 Nov 2007 06:53:32 -0500 References: <474FF9E7.90AD.0080.0@klinik.uni-regensburg.de> <474FEED9.2060000@deriva.de> X-Mailer: Apple Mail (2.915) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/96 X-Sequence-Number: 17851 Christian, You can use the keyword DEFAULT to get the default value in insert into sertest (id) values (DEFAULT); You can also do insert into sertest(id) values (nextval('sertest_id_seq')); Dave On 30-Nov-07, at 6:07 AM, Christian Schr=F6der wrote: > Christian Rengstl wrote: >> 1) How can I find out if there is a serial field in a table, as >> getColumnType() in ResultSetMetaData does not return a field =20 >> indicating >> SERIAL as far as I could see? >> > Creating a column with type serial is simply a shortcut for creating =20= > an integer column and setting up a sequence which generates the =20 > default value (see chapter 8.1.4 in den PostgreSQL docs): > > chschroe=3D# create temp table temp(x serial); > NOTICE: CREATE TABLE will create implicit sequence "temp_x_seq" for =20= > serial column "temp.x" > CREATE TABLE > chschroe=3D# \d temp > Table "pg_temp_7.temp" > Column | Type | Modifiers > --------+---------+-------------------------------------------------- > x | integer | not null default nextval('temp_x_seq'::regclass) > > >> 2) Why does it not work to issue a query like INSERT INTO >> x(serial_field) VALUES(NULL) as the default of the "serial_field" is >> nextval() anyway? >> > PostgreSQL behaves different from e.g. MySQL. When you set a column =20= > to "null" in MySQL and this column has a default value this default =20= > is instead inserted in the column. In PostgreSQL this leads to an =20 > error. (I don't know which is compliant to the standard, but I =20 > assume PostgreSQL's behaviour is correct.) > If you want a column to get its default value you have to omit it in =20= > the insert statement. Of course, this only makes sense if your table =20= > has more than this column. Consider the following example: > > chschroe=3D# create temp table temp(x serial, foo text); > NOTICE: CREATE TABLE will create implicit sequence "temp_x_seq" for =20= > serial column "temp.x" > CREATE TABLE > chschroe=3D# insert into temp(foo) values ('bar'); > INSERT 0 1 > chschroe=3D# select * from temp; > x | foo > ---+----- > 1 | bar > (1 row) > > This works fine, whereas the following doesn't work at all: > > chschroe=3D# insert into temp values (null, 'bar'); > ERROR: null value in column "x" violates not-null constraint > > So it's not a jdbc problem, but a general misunderstanding in the =20 > way PostgreSQL handles default values. > > Regards, > Christian > > --=20 > Deriva GmbH Tel.: +49 551 489500-42 > Financial IT and Consulting Fax: +49 551 489500-91 > Hans-B=F6ckler-Stra=DFe 2 http://www.deriva.de > D-37079 G=F6ttingen > > Deriva CA Certificate: http://www.deriva.de/deriva-ca.cer > > > ---------------------------(end of =20 > broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org