---------- Forwarded message ---------- From: Aaron Bono <[EMAIL PROTECTED]> Date: Jan 8, 2007 4:42 PM Subject: Re: [SQL] Table relationships To: Curtis Scheer <[EMAIL PROTECTED]>
On 1/8/07, Curtis Scheer <[EMAIL PROTECTED]> wrote:
I'm having trouble determining the best way to implement the following scenario for a customer database. Given the following tables what is the best way to link an address table to both the "Master" and the "Detail" tables. Basically there can be many addresses for each customermaster record and also many address for each customerdetail record. Will this require two many-to-many tables? Or is there a better solution I am missing? Is there any easy way to build in a "Default" address or would that be something to do logically in client code? Thanks, Curtis CREATE TABLE testing.customermaster ( customermasterid int4 NOT NULL DEFAULT nextval(' testing.customermatser_customermasterid_seq'::regclass), name varchar NOT NULL, description varchar, ) CREATE TABLE testing.customerdetail ( customerdetailid int4 NOT NULL, customermasterid int4 NOT NULL, notes varchar, closed bool, customerdepartment varchar(3), ) CREATE TABLE address ( addressid int4, name varchar(40), addr varchar, city varchar(42), st varchar(2), zip varchar(30) ) Possible many-to-many tables CREATE TABLE customermaster_address ( addressid int4, customermasterid int4 ) CREATE TABLE customerdetail _address ( addressid int4, customerdetailid int4 )
I hope you left out the foreign keys for simplicity. Make sure they get into your database. To answer your questions, I think it prudent to ask a few to get a better understanding of the meaning of your tables: Is there a difference between an address for the customer detail and an address for the customer? Is there some kind of significance to attaching an address to the customer detail as opposed to the customer? Attaching the address to the detail gives it a customer by referencing through the detail. Could you add a specific address to multiple customer and/or customer detail records or is the address only assigned to one? What it gets down to is that you must start with the LOGICAL data model and ask yourself what are the meaning of the relationships and what relationships make sense before you get down to creating the PHYSICAL database. -- ================================================================== Aaron Bono Aranya Software Technologies, Inc. http://www.aranya.com http://codeelixir.com ==================================================================