Reputation: 1121
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.
Upvotes: 1
Views: 1524
Reputation: 48287
Use the party model. A party is an abstract organization or individual. Use Table Inheritance to model this.
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);
Upvotes: 1