X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 7BED4D1B510 for ; Tue, 23 Sep 2003 17:45:51 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 25874-05 for ; Tue, 23 Sep 2003 14:45:21 -0300 (ADT) Received: from mx1.neteconomist.com (mx1.neteconomist.com [65.122.18.6]) by svr1.postgresql.org (Postfix) with ESMTP id 602DED1B519 for ; Tue, 23 Sep 2003 14:45:19 -0300 (ADT) Received: from mx1.neteconomist.com (localhost.localdomain [127.0.0.1]) by mx1.neteconomist.com (8.12.8/8.12.8) with ESMTP id h8NHfBWg019581; Tue, 23 Sep 2003 13:41:11 -0400 Received: (from aturner@localhost) by mx1.neteconomist.com (8.12.8/8.12.8/Submit) id h8NHfArV019579; Tue, 23 Sep 2003 13:41:10 -0400 Date: Tue, 23 Sep 2003 13:41:10 -0400 From: aturner@neteconomist.com To: "scott.marlowe" Cc: Jodi Kanter , Postgres Admin List Subject: Re: steps to ensure referential integrity Message-ID: <20030923174110.GG30555@neteconomist.com> References: <3F6F18F7.4000400@virginia.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.1i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/375 X-Sequence-Number: 10463 On Tue, Sep 23, 2003 at 10:52:27AM -0600, scott.marlowe wrote: > On Mon, 22 Sep 2003, Jodi Kanter wrote: > > > I have inherited a database that was originally created in postgres > > version 6.? There were no steps taken at that time to implement > > constraints to ensure referential integrity. There are no FK > > constraints, PK constraints, etc.. > > Most of what currently exists is done in the existing code. I would like > > to change this and am interested in getting suggestions. I expect that > > PK constraints on each table as well as all FK constraints should be in > > place. Anything else that I should consider? > > I have indexes currently set up on a variety of fields, including PK > > fields. Do the constraints automatically generate indexes on these > > fields such that I should remove the ones created at the end of my script? > > Thanks in advance for the suggestions! > > I would first upgrade to a reasonably new version of Postgesql 6.5.x is > pretty old. 7.3.4 or so should be a good choice. You'll find that you > can add the pk/fk constraints in the conversion process, and still have a > faster database with fk/pk under 7.3 than you'd have with no fk/pk > constraints in 6.5.3 > Creating a primary key also creates a unique index on that field(s). Depending on how normalised the data schema is, you may also have to create stored procedures to gaurentee data integrity. If the data is not currently good, you may have to remove bad data to get the constraints to check. I'm no guru, but I do something like this to at least check: select count(distinct(id)) from table; vs select count(id) from table; if the numbers are different, you have dup ids. To get a list: select id,count(*) from table group by (id); I hope someone can figure out how how to put a where on that so it only returns those above 1, but I personaly don't know how, so I'll use my friend the shell: psql database -Uuser -c 'select id,count(*) from table gropu by id' | grep -v '1$'; Alex Turner