[英]how to create a table with two primary keys where one is foreign key in one table and the other on another table?
I have a table with two primary keys.我有一个带有两个主键的表。 municipality tables has two entities: municipality_code and municipality_names both primary keys.市政表有两个实体:市政代码和市政名称都是主键。 there is one table postcode which has municipality_code as a foreign key.有一个表邮政编码,其中有 cityity_code 作为外键。 on the other hand there is another table which has municipality_name as a foreign key.另一方面,还有另一个表,其中有市政名称作为外键。 How can I connect them?我怎样才能连接它们?
You can only have one primary key.您只能有一个主键。 But a UNIQUE NOT NULL
constraint is just as good as a primary key and can serve as target of a foreign key constraint.但是UNIQUE NOT NULL
约束和主键一样好,可以作为外键约束的目标。
While that solves your immediare problem, I'd suggest that you change the data model so that you always use the same column as target of foreign keys.虽然这解决了您的直接问题,但我建议您更改数据模型,以便始终使用相同的列作为外键的目标。 It makes your data model more understandable.它使您的数据模型更易于理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.