简体   繁体   中英

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.

Take the org_culture_xref table, for instance. I have two columns, one is org_id, which is the same as the org_id column (primary key) of the organization table. The other is cult_id, which is the same as the cult_id column (primary key) of the culture table.

I believe both of the columns of the org_culture_xref table are primary and foreign keys. 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.

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.

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 . The two of them together are the primary key of 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)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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