简体   繁体   English

多个主/外键

[英]Multiple Primary/Foreign Keys

I'm going to be honest, I'm a bit hazy on how primary and foreign keys work. 老实说,我对主键和外键的工作方式有些困惑。 I was told to setup my database by someone on here so that I had 7 tables, one for organizations, one for categories, services, and cultures, and three cross reference tables between organization on one hand, and categories, services and cultures on the other. 有人告诉我在这里设置数据库,这样我就有7个表,一个用于组织,一个用于类别,服务和文化,一方面是三个组织之间的交叉引用表,另一方面是组织之间的类别,服务和文化。其他。

Take the org_culture_xref table, for instance. 以org_culture_xref表为例。 I have two columns, one is org_id, which is the same as the org_id column (primary key) of the organization table. 我有两列,一列是org_id,它与组织表的org_id列(主键)相同。 The other is cult_id, which is the same as the cult_id column (primary key) of the culture table. 另一个是cult_id,与文化表的cult_id列(主键)相同。

I believe both of the columns of the org_culture_xref table are primary and foreign keys. 我相信org_culture_xref表的两列都是主键和外键。 However, this doesn't seem to allow me to have multiple values for both of these columns. 但是,这似乎不允许我为这两个列都使用多个值。 I want to be able to have several relationships between organizations and cultures - as in every organization can be associated with multiple cultures and every culture can be associated with multiple organizations. 我希望能够在组织和文化之间建立几种关系-因为在每个组织中都可以与多种文化相关联,并且每种文化都可以与多个组织相关联。

How do I ensure that I can have multiple values for both columns? 如何确保两个列都可以有多个值?

What you're talking about is a Many-To-Many relationship. 您所说的是多对多关系。 You're on the right path using a cross-reference table. 使用交叉引用表,您的位置正确。

It's good to review how foreign and primary keys work; 最好回顾一下外键和主键的工作方式。 there can only be one primary key per table, but there can be multiple foreign keys. 每个表只能有一个主键,但是可以有多个外键。 However, note that a primary key doesn't have to be limited to one column; 但是,请注意,主键不必仅限于一列。 you can have a primary key that spans two, three, or more columns. 您可以拥有一个跨越两列,三列或更多列的主键。

Your solution here is to have two foreign key, one for each column/table relationship, and one primary key that spans across both tables. 您的解决方案是拥有两个外键,一个用于每个列/表关系,一个主键跨两个表。

Here's an example of a table I used at one time, which links cities and counties in a many-to-many relationship. 这是我一次使用的表格示例,该表格以多对多关系链接了城市和县。

mysql> show create table xref_cities_counties\G
*************************** 1. row ***************************
       Table: xref_cities_counties
Create Table: CREATE TABLE `xref_cities_counties` (
  `city_id` int(10) unsigned NOT NULL,
  `county_id` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY  (`city_id`,`county_id`),
  KEY `city_id` (`city_id`),
  KEY `county_id` (`county_id`),
  CONSTRAINT `fk_xrefcitiescounties_cityid` FOREIGN KEY (`city_id`) REFERENCES `florida_cities` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_xrefcitiescounties_countyid` FOREIGN KEY (`county_id`) REFERENCES `florida_counties` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql>
mysql>
mysql> describe xref_cities_counties;
+-----------+---------------------+------+-----+---------+-------+
| Field     | Type                | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| city_id   | int(10) unsigned    | NO   | PRI |         |       |
| county_id | tinyint(3) unsigned | NO   | PRI |         |       |
+-----------+---------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql>

I'd suggest some extra reading on the topic. 我建议对该主题进行一些额外的阅读。 It seems like you're on a good start. 看来您的开端很好。

The primary key for your cross ref table will be both of the columns, meaning that the combination of two ids must be unique but you can have repeats of individiual ids in either column. 交叉引用表的主键将是这两列,这意味着两个ID的组合必须唯一,但是在任一列中都可以重复单个ID。

Here is a statement to create such a table: 这是创建此表的语句:

CREATE TABLE `x` (
  `a_id` varchar(6) NOT NULL default '',
  `b_id` varchar(6) NOT NULL default '',
  PRIMARY KEY  (`a_id`,`b_id`)
)

The issue itself and the cross ref table approach to many-to-many relationships applies to most (if not all) relational databases, but since I haven't used mysql in around 10 years I got that code from here , which seems to have a detailed discussion of the topic with mysql specific code. 问题本身和用于多对多关系的交叉引用表方法适用于大多数(如果不是全部)关系数据库,但是由于大约十年来我没有使用mysql,所以我从这里得到了该代码,带有mysql特定代码的主题的详细讨论。

Each column of org_culture_xref is a foreign key: org_id is a foreign key to organization , and cult_id is a foreign key to culture . org_culture_xref每一列都是外键: org_idorganization的外键,而cult_idculture的外键。 The two of them together are the primary key of org_culture_xref . 他们两个在一起org_culture_xref的主键。

So, something along these lines: 因此,遵循以下原则:

CREATE TABLE org_culture_xref
(
  org_id NUMERIC(10) NOT NULL,
  cult_id NUMERIC(10) NOT NULL,
  PRIMARY KEY (org_id, cult_id),
  FOREIGN KEY (org_id) REFERENCES organization (org_id),
  FOREIGN KEY (cult_id) REFERENCES culture (cult_id)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM