简体   繁体   中英

How to model Address relationship with Customer and business ?

I am trying to setup a MySQL database where I have customer and business both of whom have address. How can I have an address table separately which could be linked to both Customer and Business tables via Foreign key relationship?

Would appreciate any help!

Thanks.

  1. Use the party model. A party is an abstract organization or individual. Use Table Inheritance to model this.

  2. Use a junction table between MailingAddress and Party.

Using Single Table Inheritance:

create table party (
  party_id int primary key,
  type varchar(20) not null, --this should be a char(1) or smallint pointing to a lookup table
  name varchar(255) not null
);

insert into party values 
(1, 'Organization', 'Acme, Inc'),
(2, 'Individual', 'John Doe');

create table mailing_address (
  address_id int primary key,
  address varchar(255) not null --add other address fields
);

insert into mailing_address values
(1, '123 Wall Street...'),
(2, '456 Main Street...');

create table party_mailing_address (
  party_id int,
  mailing_address_id int,

  primary key (party_id, mailing_address_id),

  foreign key (party_id) references party(party_id),
  foreign key (mailing_address_id) references mailing_address(address_id)
);

--assign addresses to the parties:
insert into party_mailing_address values
(1,1),
(2,2);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM