From jkanter@virginia.edu Mon Sep 22 15:34:27 2003 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 E4258D1B518 for ; Mon, 22 Sep 2003 15:34:27 +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 49928-01 for ; Mon, 22 Sep 2003 12:34:17 -0300 (ADT) Received: from mailhub.mail.Virginia.EDU (mail.Virginia.EDU [128.143.2.9]) by svr1.postgresql.org (Postfix) with ESMTP id BB3B8D1B4EE for ; Mon, 22 Sep 2003 12:34:16 -0300 (ADT) Received: from smtp.mail.Virginia.EDU (smtp.mail.Virginia.EDU [128.143.22.52]) by mailhub.mail.Virginia.EDU (Postfix) with ESMTP id 0C31A8CBA9 for ; Mon, 22 Sep 2003 11:34:16 -0400 (EDT) Received: from virginia.edu (d-137-133-128.bootp.Virginia.EDU [137.54.133.128]) by smtp.mail.Virginia.EDU (8.11.7/8.11.6) with ESMTP id h8MFYEl42120 for ; Mon, 22 Sep 2003 11:34:15 -0400 Message-ID: <3F6F18F7.4000400@virginia.edu> Date: Mon, 22 Sep 2003 11:44:55 -0400 From: Jodi Kanter User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1 X-Accept-Language: en-us MIME-Version: 1.0 To: Postgres Admin List Subject: steps to ensure referential integrity Content-Type: multipart/alternative; boundary="------------090909090603070305050401" X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/343 X-Sequence-Number: 10431 --------------090909090603070305050401 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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! Jodi -- _______________________________ Jodi L Kanter BioInformatics Database Administrator University of Virginia (434) 924-2846 jkanter@virginia.edu --------------090909090603070305050401 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit 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!
Jodi
--
________________________________

_______________________________
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
jkanter@virginia.edu


 

 

 

--------------090909090603070305050401-- From scott.marlowe@ihs.com Tue Sep 23 16:59:13 2003 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 C1D87D1BAED for ; Tue, 23 Sep 2003 16:59:13 +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 21328-05 for ; Tue, 23 Sep 2003 13:59:03 -0300 (ADT) Received: from mail1.ihs.com (mail1.ihs.com [170.207.70.222]) by svr1.postgresql.org (Postfix) with ESMTP id 8BA94D1B8C2 for ; Tue, 23 Sep 2003 13:59:02 -0300 (ADT) Received: from css120.ihs.com (css120.ihs.com [170.207.105.120]) by mail1.ihs.com (8.12.10/8.12.10) with ESMTP id h8NGvD1o000421; Tue, 23 Sep 2003 10:57:13 -0600 (MDT) Date: Tue, 23 Sep 2003 10:52:27 -0600 (MDT) From: "scott.marlowe" To: Jodi Kanter Cc: Postgres Admin List Subject: Re: steps to ensure referential integrity In-Reply-To: <3F6F18F7.4000400@virginia.edu> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-IHS-MailScanner: Found to be clean X-IHS-MailScanner-SpamCheck: X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/358 X-Sequence-Number: 10446 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 From aturner@neteconomist.com Tue Sep 23 17:45:51 2003 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 From markcarew@magicwanddept.com.au Tue Sep 23 20:31:29 2003 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 4C169D1B956 for ; Tue, 23 Sep 2003 20:31:29 +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 44606-08 for ; Tue, 23 Sep 2003 17:31:20 -0300 (ADT) Received: from news.hub.org (unknown [64.117.224.194]) by svr1.postgresql.org (Postfix) with ESMTP id 9ADBFD1B58D for ; Tue, 23 Sep 2003 17:31:18 -0300 (ADT) Received: from news.hub.org (host-64-117-224-194.altec1.com [64.117.224.194] (may be forged)) by news.hub.org (8.12.9/8.12.9) with ESMTP id h8NKVHhK077519 for ; Tue, 23 Sep 2003 20:31:17 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h8NKKnJa076045 for pgsql-admin@postgresql.org; Tue, 23 Sep 2003 20:20:49 GMT From: "Mark Carew" X-Newsgroups: comp.databases.postgresql.admin Subject: Re: steps to ensure referential integrity Date: Wed, 24 Sep 2003 06:20:52 +1000 Organization: Hub.Org Networking Services Lines: 107 Message-ID: References: <3F6F18F7.4000400@virginia.edu> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 To: pgsql-admin@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/366 X-Sequence-Number: 10454 ________________________________ Hi Jodie, This snippet is generated code from my client side data dictionary for postgresql. The sequence described is used with nextval() to obtain autoincrement key values for the primary key. Thus primary key definition does create an index as do foreign key declarations. I use the views to mimic conditional indices. Each view has a companion index generated if one suitable is not already in existance. HTH Mark Carew Brisbane Australia 07 35117911 /* client side */ IF ascan(aTokens,"AUTOINC") > 0 * ::AutoInc := TRUE * IF cColumnType == "CS" * #IFDEF SQLFILES * ::SqlDefault := ; "SELECT nextVal('" + ; lower(::oTable:filename) + ; "_seq') ;" * #ENDIF * ENDIF * ENDIF /* server side */ DROP SEQUENCE invhdr_seq ; CREATE SEQUENCE invhdr_seq START 322 ; DROP TABLE invhdr ; CREATE TABLE invhdr ( vinno int4, vindate date NOT NULL, dateent date, clientno char(12) NOT NULL, total numeric(10,2), balance numeric(10,2), allocamt numeric(10,2), gst numeric(9,2), cordno char(14), printed char(1) , origin char(1) , PRIMARY KEY ( vinno ) ,FOREIGN KEY (clientno ) REFERENCES client ON UPDATE RESTRICT ON DELETE RESTRICT ); CREATE INDEX invhdr_dateclt ON invhdr (vindate, clientno); CREATE INDEX invhdr_vindate ON invhdr (vindate); CREATE INDEX invhdr_dateent ON invhdr (dateent); DROP VIEW invhdr_unprint_view ; CREATE VIEW invhdr_unprint_view AS SELECT * FROM invhdr WHERE PRINTED = 'N' ORDER BY vinno ; CREATE INDEX invhdr_clntactv ON invhdr (clientno, vindate); DROP VIEW invhdr_clntactv_view ; CREATE VIEW invhdr_clntactv_view AS SELECT * FROM invhdr WHERE BALANCE <> cast( 0.00 as numeric ) ORDER BY clientno, vindate ; CREATE INDEX invhdr_cordno ON invhdr (cordno); DROP VIEW invhdr_cordno_view ; CREATE VIEW invhdr_cordno_view AS SELECT * FROM invhdr WHERE CORDNO <> ' ' ORDER BY cordno ; From xzilla@users.sourceforge.net Wed Sep 24 14:12:26 2003 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 E7FC2D1B547 for ; Wed, 24 Sep 2003 14:12:26 +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 52832-01 for ; Wed, 24 Sep 2003 11:11:58 -0300 (ADT) Received: from localhost.localdomain (unknown [65.217.53.66]) by svr1.postgresql.org (Postfix) with ESMTP id 90F9CD1B511 for ; Wed, 24 Sep 2003 11:11:49 -0300 (ADT) Received: from thorn.mmrd.com (thorn.mmrd.com [172.25.10.100]) by localhost.localdomain (8.12.8/8.12.8) with ESMTP id h8OD4lqd004167; Wed, 24 Sep 2003 09:04:48 -0400 Received: from gnvex001.mmrd.com (gnvex001.mmrd.com [192.168.3.55]) by thorn.mmrd.com (8.11.6/8.11.6) with ESMTP id h8OEBiZ31961; Wed, 24 Sep 2003 10:11:44 -0400 Received: from camel.mmrd.com ([172.25.5.213]) by gnvex001.mmrd.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id TD8ATWXZ; Wed, 24 Sep 2003 10:11:43 -0400 Subject: Re: steps to ensure referential integrity From: Robert Treat To: aturner@neteconomist.com Cc: "scott.marlowe" , Jodi Kanter , Postgres Admin List In-Reply-To: <20030923174110.GG30555@neteconomist.com> References: <3F6F18F7.4000400@virginia.edu> <20030923174110.GG30555@neteconomist.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Ximian Evolution 1.0.8 Date: 24 Sep 2003 10:11:44 -0400 Message-Id: <1064412704.30308.2028.camel@camel> Mime-Version: 1.0 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/397 X-Sequence-Number: 10485 On Tue, 2003-09-23 at 13:41, aturner@neteconomist.com wrote: > 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; > select count(distinct(id)), count(id), count(*) 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: > select id,count(*) from table group by id having count(*) > 1; and actually just running that would allow you to skip the check from above. HTH Robert Treat > psql database -Uuser -c 'select id,count(*) from table gropu by id' | grep -v '1$'; > > Alex Turner -- Build A Brighter Lamp :: Linux Apache {middleware} PostgreSQL