pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Youngblood <YoungblG@houstoncellular.com>
To: 'PostgreSQL SQL List' <pgsql-sql@postgresql.org>
Subject: Rules, triggers, ??? - What is the best way to enforce data-valid ation tests?
Date: Wed, 12 Jan 2000 11:52:51 -0600
Message-ID: <2B9713A99045D211BB4E0008C7566869DC9F8C@scorpexc1.houstoncell.com> (raw)

I am in the process of creating a large relational database. One of the key
things I need to include in this database is a system to maintain data
integrity across multiple tables.

here's an example:

Table: items
item_id		description		vendor_id		model
cost
stuff1		Stuff			stuff			s-1
14.25

Table: vendors
vendor_id	vendor			contact			phone
stuff     		Stuff Inc.		Mr. Stuff
xxx.xxx.xxxx

Table: order
order_id		item_id			qty
price
1		stuff1			3
19.95

In this example, table "items" would be populated with a list of items with
item_id being a unique key; table "vendors" would be a list of vendors with
vendor_id being a unique key; and, table "order" would be line items from an
order. These are just samples, what i'm trying to illustrate are the
relationships between the tables.

The first relationship would be between "items" and "vendors" and the second
would be between "items" and "order". The same type of relationship exists
in both examples. My goal is to make the database validate any new records
or changes to existing records such that vendor_id in "items" must already
exist in "vendors", and that item_id in "order" must already exist in
"items".

you could not add or change a record to "order" where item_id does not
already exist in "items", and, you could not add or change a record in
"items" where vendor_id does not already exist in "vendors".

There are two ways to handle this. The first way, and the way I've generally
done in the past, is making the data-validation tests part of the program.
This means I could go in to psql and manually enter invalid data. It also
means there is the possibility of the program, due to bug or other problems,
not always enforcing the integrity rules.

The second way is to place these criteria in the database itself, and this
is what I want to do. This is also where I'm at my weakest.

What is the best way to implement this. Best, to me, refers to the simplest,
most direct, and having the least impact on performance. Would it be by
using triggers? Or rules? Or keeping it in the program and not in the
database? Or something else entirely?

In the past I've tried triggers and rules and never did get them to work to
my satisfaction. Either they would work for some tasks, but I couldn't adapt
them to work in more complicated tables, or there were syntax problems and I
never got them to work at all. If anyone can help, and provide some
examples, i would greatly appreciate it.

I currently have postgres 6.5.0 on the production server, and 6.5.1 or 6.5.2
on my development boxes.

Many thanks in advance.
Greg

Gregory S. Youngblood
ext 2164
 
 




view thread (2+ messages)  latest in thread

Message-ID: <2B9713A99045D211BB4E0008C7566869DC9F8C@scorpexc1.houstoncell.com>
Permalink:  ../2B9713A99045D211BB4E0008C7566869DC9F8C@scorpexc1.houstoncell.com/
Also on:    postgresql.org/message-id/2B9713A99045D211BB4E0008C7566869DC9F8C@scorpexc1.houstoncell.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: YoungblG@houstoncellular.com
  Subject: Re: Rules, triggers, ??? - What is the best way to enforce data-valid ation tests?
  In-Reply-To: <2B9713A99045D211BB4E0008C7566869DC9F8C@scorpexc1.houstoncell.com>
  References: <2B9713A99045D211BB4E0008C7566869DC9F8C@scorpexc1.houstoncell.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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