Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 6E7F62E012A for ; Fri, 30 Nov 2007 07:06:52 -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 07580-08 for ; Fri, 30 Nov 2007 07:06:42 -0400 (AST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.179]) by postgresql.org (Postfix) with ESMTP id E35AB2E029A for ; Fri, 30 Nov 2007 07:06:47 -0400 (AST) Received: from [192.168.146.33] (p508078BC.dip.t-dialin.net [80.128.120.188]) by mrelayeu.kundenserver.de (node=mrelayeu8) with ESMTP (Nemesis) id 0ML31I-1Iy3hj1zUs-0003Im; Fri, 30 Nov 2007 12:06:46 +0100 Message-ID: <474FEED9.2060000@deriva.de> Date: Fri, 30 Nov 2007 12:07:05 +0100 From: =?UTF-8?B?Q2hyaXN0aWFuIFNjaHLDtmRlcg==?= Organization: Deriva GmbH User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: Serial data type References: <474FF9E7.90AD.0080.0@klinik.uni-regensburg.de> In-Reply-To: <474FF9E7.90AD.0080.0@klinik.uni-regensburg.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V01U2FsdGVkX1+542zTGGUsxOK/I9FhoELI0fXOOs1SdZ4gpKH CD03W92c9XuOKOg1bzFu+77GgA+c7SZ7Fad54oZI5AmnY7FCtA rmbuj8lgq366cUq53pNnA== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/95 X-Sequence-Number: 17850 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 indicating > SERIAL as far as I could see? > Creating a column with type serial is simply a shortcut for creating an integer column and setting up a sequence which generates the default value (see chapter 8.1.4 in den PostgreSQL docs): chschroe=# create temp table temp(x serial); NOTICE: CREATE TABLE will create implicit sequence "temp_x_seq" for serial column "temp.x" CREATE TABLE chschroe=# \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 to "null" in MySQL and this column has a default value this default is instead inserted in the column. In PostgreSQL this leads to an error. (I don't know which is compliant to the standard, but I assume PostgreSQL's behaviour is correct.) If you want a column to get its default value you have to omit it in the insert statement. Of course, this only makes sense if your table has more than this column. Consider the following example: chschroe=# create temp table temp(x serial, foo text); NOTICE: CREATE TABLE will create implicit sequence "temp_x_seq" for serial column "temp.x" CREATE TABLE chschroe=# insert into temp(foo) values ('bar'); INSERT 0 1 chschroe=# select * from temp; x | foo ---+----- 1 | bar (1 row) This works fine, whereas the following doesn't work at all: chschroe=# 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 way PostgreSQL handles default values. Regards, Christian -- Deriva GmbH Tel.: +49 551 489500-42 Financial IT and Consulting Fax: +49 551 489500-91 Hans-Böckler-Straße 2 http://www.deriva.de D-37079 Göttingen Deriva CA Certificate: http://www.deriva.de/deriva-ca.cer