X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E8D1BD9A21 for ; Wed, 28 Sep 2005 13:22:44 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 35658-07 for ; Wed, 28 Sep 2005 16:22:40 +0000 (GMT) Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id 10FD2D9A87 for ; Wed, 28 Sep 2005 13:22:39 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id F1D9B31F52; Wed, 28 Sep 2005 18:22:38 +0200 (MET DST) From: "codeWarrior" X-Newsgroups: pgsql.sql Subject: Re: Why doesn't the SERIAL data type automatically have a UNIQUE CONSTRAINT Date: Wed, 28 Sep 2005 09:24:24 -0700 Organization: Hub.Org Networking Services Lines: 53 Message-ID: References: <4338961E.20100@verizon.net> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Response To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=2.236 required=5 tests=[AWL=-0.028, FORGED_HOTMAIL_RCVD2=1.079, PRIORITY_NO_NAME=1.185] X-Spam-Level: ** X-Archive-Number: 200509/278 X-Sequence-Number: 22825 "Ferindo Middleton Jr" wrote in message news:4338961E.20100@verizon.net... > Is there some reason why the SERIAL data type doesn't automatically have a > UNIQUE CONSTRAINT. It seems that the main reason for using it is so that > the value for this field keeps changing automatically and is never null so > any one record can be identified using it- So why not imply that it is > always be UNIQUE anyway. I mean, if you were to force another value on a > SERIAL field that already had that same value, the would through the > sequence tracking the the fields current value off any way, so it just > makes sense to me to not let a serial field be duplicated. Let's take a > poll. Is there anyone out there who actually uses the SERIAL data type who > would not want it to be UNIQUE? > > Ferindo > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match > You are correct... serials don't have unique constraints unless they are also defined as a primary key... It seems to me that all you need to do is make your serial value a primary key in your DDL... (which is the same as defining a unique constraint...) consider the following: CREATE TABLE sys_test ( id serial NOT NULL PRIMARY KEY, txt text not null ) WITH OIDS; INSERT INTO sys_test(txt) VALUES ('A'); INSERT INTO sys_test(txt) VALUES ('B'); -- INSERT statement #3 throws an expected error.... INSERT INTO sys_test(id, txt) VALUES (1, 'C'); // THROWS UNIQUE CONTRAINT ERROR AS EXPECTED !!! SELECT * FROM sys_test;