简体   繁体   English

如何创建具有两个主键的表,其中一个是一个表中的外键,另一个是另一个表中的外键?

[英]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.

相关问题 将一个表中的外键数组与另一表中的主键匹配? - Match an array of foreign keys in one table with primary key in another table? SQL 两个外键链接到另一个表的一个主键以从该表中提取字段 - SQL Two foreign keys linked to one primary key of another table to pull field from that table 如何使用两个外键作为一个主键将域表与第二个表连接起来? - How to join domain table with second table using two foreign keys for one primary key? 使用主键和其他键将表从一个数据库移动到另一个数据库 - Moving table from one database to another with primary key and other keys 更新一个表中的主键,这是另一表中的外键 - Update primary key in one table which is foreign key in another table 如何创建一个包含五个外键的表,其中一个作为主键(也是一个VARCHAR) - How can I create a table with five foreign keys and one of them as primary key (which is also a VARCHAR) SQL中的表能否将多列作为仅引用另一表的一个主键的外键? - Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table? Java/Mysql 从一个表中选择主键值并将它们作为外键插入到另一个表中 - Java/Mysql Selecting Primary Key values from one table and Insert them to another table as Foreign Keys 从一个表到另一个表中的单个主键有多个外键是否可以? - Is it fine to have multiple foreign keys from one table to single primary key in another table? 在 SQL 中,如何将一个表的多个列中的多个外键 ID 连接到另一个表中的单个主键和唯一的 select 一个? - In SQL, how to join multiple foreign keys IDs in multiple columns of a table to a single primary key in another table and uniquely select one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM