我正在尝试设置一个MySQL数据库,我的客户和业务都有地址。如何单独使用地址表,可以通过外键关系链接到客户和业务表?
非常感谢任何帮助!
感谢。
答案 0 :(得分:1)
使用派对模型。一方是抽象的组织或个人。使用表继承对此进行建模。
使用MailingAddress和Party之间的联结表。
使用单表继承:
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);