[英]Creating Association between two entries in SQL table
我有一项工作要为小型企业设置数据库。
目前我有5个表设置:
我的任务之一是创建一个关联表,该关联表将链接到Customers表并显示2个客户之间的关联。
关联表的列:
输出应该是“Customer1是Customer2的AssocationType”,例如“Dave是Jim的会计”
如何设置它以便Customer1和Customer2来自客户的表? 我想可能是外国钥匙,但我不确定。
您可以设置外键:
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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.