我有一件工作要为小型企业建立数据库。
目前我已经设置了5张桌子:
我要做的任务之一是创建一个关联表,该表将链接到“客户”表并显示2个客户之间的关联。
关联表列:
输出应为“ Customer1是Customer2的AssocationType”,例如“ Dave是Jim的会计师”
如何设置它,以便Customer1和Customer2来自客户表?我认为可能与外键有关,但我不确定。
答案 0 :(得分:0)
您可以设置外键:
alter table associations add constraint fk_associations_customer1
foreign key (customer1_id) references customers (customer_id);
alter table associations add constraint fk_associations_customer2
foreign key (customer2_id) references customers (customer_id);
应将外键设为主键,因此需要定义customers
,这样:
create table customers (
customer_id int primary key, -- perhaps identity, serial or autoincrement depending on your database
. . .
);
您会注意到命名约定:
_id
。