X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 475DE52A11 for ; Sat, 28 May 2005 23:05:16 -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 03470-01 for ; Sun, 29 May 2005 02:05:02 +0000 (GMT) Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id A2F9552995 for ; Sat, 28 May 2005 23:04:41 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr2.postgresql.org (Postfix) with SMTP id 2FC2BF0DDB for ; Sat, 28 May 2005 21:51:02 +0100 (BST) Received: (qmail 20144 invoked by uid 500); 28 May 2005 20:52:26 -0000 Date: Sat, 28 May 2005 15:52:26 -0500 From: Bruno Wolff III To: David Pratt Cc: pgsql-admin@postgresql.org Subject: Re: Adding Serial Type Message-ID: <20050528205226.GA20085@wolff.to> Mail-Followup-To: Bruno Wolff III , David Pratt , pgsql-admin@postgresql.org References: <20050528182754.GA11909@wolff.to> <46D712FF-CFAD-11D9-AEE7-000A27B3B070@eastlink.ca> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <46D712FF-CFAD-11D9-AEE7-000A27B3B070@eastlink.ca> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.009 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/289 X-Sequence-Number: 17718 On Sat, May 28, 2005 at 16:18:30 -0300, David Pratt wrote: > > On Saturday, May 28, 2005, at 03:27 PM, Bruno Wolff III wrote: > > >On Sat, May 28, 2005 at 14:27:17 -0300, > > David Pratt wrote: > >>Pretty basic question. Is it necessary to add NOT NULL or UNIQUE NOT > >>NULL to SERIAL or is this implicit and unnecessary? > > > >Serials no longer generate a uniqie index by default. So in practice > >you will normally want to declare them as PRIMARY KEYs. However there > >are cases where you don't need this and the index is extra overhead. > > > > Alright. so would it be better form for me to to this in a > create_tables.sql > > CREATE TABLE new_table ( > id SERIAL, Its simpler to use: id SERIAL PRIMARY KEY, > description TEXT NOT NULL > ); > > And then in a create_primary_keys.sql do this for the tables requiring > it. > > ALTER TABLE new_table ADD CONSTRAINT new_table_pkey PRIMARY KEY (id); > > Does the PRIMARY KEY declaration ensure that the id values are unique? > Serial should always give me an incremented value that's different so I > am assuming it is unnecessary to use UNIQUE. Am I correct? PRIMARY KEY implies UNIQUE and NOT NULL and in Postgres will result in a unique index being created to enforce this. It will also make id the default column in new_table for foreign key references to that table.