简体   繁体   中英

Creating composite foreign key on a primary key

I have a table called events with event_id as a primary key and a table person with person_id as a primary key.

I want to have a table that contains two columns event_id and person_id as foreign keys to the above two primary keys.

I am able to create a foreign key something like this:

create table pe(
event_id INTEGER UNSIGNED UNIQUE,
person_id INTEGER UNSIGNED UNIQUE,
FOREIGN KEY (event_id) REFERENCES events(event_id),
FOREIGN KEY (person_id) REFERENCES person(person_id)
);

but I cannot insert values like:

----------------------
event_id person_id
----------------------
1 1
1 2
2 1
2 2
----------------------

For that I need a composite foreign key.

I am not able to decide how to do that. Any suggestions or help are much appreciated!

Thanks a lot!

You need to make the combination of event_id and person_id unique. I'd just make the combination the primary key, as follows:

create table pe(
  event_id INTEGER UNSIGNED,
  person_id INTEGER UNSIGNED,
  FOREIGN KEY (event_id) REFERENCES events(event_id),
  FOREIGN KEY (person_id) REFERENCES person(person_id),
  PRIMARY KEY (event_id, person_id)
);

Because you have set individual unique key for each column. Here's how to enforce composite unique key,

create table pe
(
    event_id INTEGER UNSIGNED,
    person_id INTEGER UNSIGNED,
    FOREIGN KEY (event_id) REFERENCES events(event_id),
    FOREIGN KEY (person_id) REFERENCES person(person_id),
    UNIQUE (event_id, person_id) // <<== or it could be PRIMARY KEY
);

I played around with this schema to check what you were trying to do:

CREATE TABLE `events` (
  `event_id` int(11) NOT NULL AUTO_INCREMENT,
  `event_name` varchar(255) NOT NULL,
  PRIMARY KEY (`event_id`),
  UNIQUE KEY `event_name_UNIQUE` (`event_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

CREATE TABLE `person` (
  `person_id` int(11) NOT NULL AUTO_INCREMENT,
  `person_name` varchar(225) DEFAULT NULL,
  PRIMARY KEY (`person_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

CREATE TABLE `pe` (
  `event_id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  PRIMARY KEY (`event_id`,`person_id`),
  KEY `fk_events_has_person_person1` (`person_id`),
  KEY `fk_events_has_person_events` (`event_id`),
  CONSTRAINT `fk_events_has_person_events` FOREIGN KEY (`event_id`) REFERENCES      `events` (`event_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_events_has_person_person1` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I think your confusion lies in where you put the UNIQUE property. Each event and person needs to be unique. That is guaranteed by the primary key in the respective tables and you can also add a UNIQUE constraint in, for example, a column like person_name to ensure actual values are unique. There was nothing wrong with your foreign keys; the problem was that you added UNIQUE constraints to each field of the interim table. That's a mistake. If you want to ensure that each row of the interim table is unique then you need to add a composite primary key or as JW suggested a composite UNIQUE constraint.

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