pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
steps to ensure referential integrity
5+ messages / 5 participants
[nested] [flat]

* steps to ensure referential integrity
@ 2003-09-22 15:44 Jodi Kanter <jkanter@virginia.edu>
  2003-09-23 16:52 ` Re: steps to ensure referential integrity scott.marlowe <scott.marlowe@ihs.com>
  2003-09-23 20:20 ` Re: steps to ensure referential integrity Mark Carew <markcarew@magicwanddept.com.au>
  0 siblings, 2 replies; 5+ messages in thread

From: Jodi Kanter @ 2003-09-22 15:44 UTC (permalink / raw)
  To: pgsql-admin

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 <mailto:jkanter@virginia.edu>

 

 

 

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: steps to ensure referential integrity
  2003-09-22 15:44 steps to ensure referential integrity Jodi Kanter <jkanter@virginia.edu>
@ 2003-09-23 16:52 ` scott.marlowe <scott.marlowe@ihs.com>
  2003-09-23 17:41   ` Re: steps to ensure referential integrity aturner@neteconomist.com
  1 sibling, 1 reply; 5+ messages in thread

From: scott.marlowe @ 2003-09-23 16:52 UTC (permalink / raw)
  To: Jodi Kanter <jkanter@virginia.edu>; +Cc: pgsql-admin

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




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: steps to ensure referential integrity
  2003-09-22 15:44 steps to ensure referential integrity Jodi Kanter <jkanter@virginia.edu>
  2003-09-23 16:52 ` Re: steps to ensure referential integrity scott.marlowe <scott.marlowe@ihs.com>
@ 2003-09-23 17:41   ` aturner@neteconomist.com
  2003-09-24 14:11     ` Re: steps to ensure referential integrity Robert Treat <xzilla@users.sourceforge.net>
  0 siblings, 1 reply; 5+ messages in thread

From: aturner@neteconomist.com @ 2003-09-23 17:41 UTC (permalink / raw)
  To: scott.marlowe <scott.marlowe@ihs.com>; +Cc: Jodi Kanter <jkanter@virginia.edu>; pgsql-admin

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




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: steps to ensure referential integrity
  2003-09-22 15:44 steps to ensure referential integrity Jodi Kanter <jkanter@virginia.edu>
  2003-09-23 16:52 ` Re: steps to ensure referential integrity scott.marlowe <scott.marlowe@ihs.com>
  2003-09-23 17:41   ` Re: steps to ensure referential integrity aturner@neteconomist.com
@ 2003-09-24 14:11     ` Robert Treat <xzilla@users.sourceforge.net>
  0 siblings, 0 replies; 5+ messages in thread

From: Robert Treat @ 2003-09-24 14:11 UTC (permalink / raw)
  To: aturner@neteconomist.com; +Cc: scott.marlowe <scott.marlowe@ihs.com>; Jodi Kanter <jkanter@virginia.edu>; pgsql-admin

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




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: steps to ensure referential integrity
  2003-09-22 15:44 steps to ensure referential integrity Jodi Kanter <jkanter@virginia.edu>
@ 2003-09-23 20:20 ` Mark Carew <markcarew@magicwanddept.com.au>
  1 sibling, 0 replies; 5+ messages in thread

From: Mark Carew @ 2003-09-23 20:20 UTC (permalink / raw)
  To: pgsql-admin

________________________________
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 ;





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2003-09-24 14:11 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-09-22 15:44 steps to ensure referential integrity Jodi Kanter <jkanter@virginia.edu>
2003-09-23 16:52 ` scott.marlowe <scott.marlowe@ihs.com>
2003-09-23 17:41   ` aturner@neteconomist.com
2003-09-24 14:11     ` Robert Treat <xzilla@users.sourceforge.net>
2003-09-23 20:20 ` Mark Carew <markcarew@magicwanddept.com.au>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox