MYSQL:引用复合主键的外键

时间:2017-04-27 05:14:19

标签: mysql database database-design relational-database

鉴于我有3个表即机构,页面和page_content,如何只更新级联更新的复合主键的外键?

institutions表:

CREATE TABLE IF NOT EXISTS `institutions` (
  `inst_id` bigint(20) NOT NULL AUTO_INCREMENT unsigned,
  `inst_desc` varchar(100) NOT NULL,
  PRIMARY KEY (`inst_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=1 ;

inst_id | inst_desc
1       | Institution 1
2       | Institution 2

pages表:

CREATE TABLE IF NOT EXISTS `pages` (
  `page_code` varchar(255) NOT NULL,
  `page_desc` varchar(255) DEFAULT NULL,
  `page_url` varchar(255) DEFAULT NULL,
  `inst_id` bigint(20) NOT NULL unsigned,
  PRIMARY KEY (`page_code`,`inst_id`),
  KEY `inst_id` (`inst_id`),
  KEY `page_code` (`page_code`,`inst_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

page_code   | page_desc   | page_url    | inst_id
course-list | Course List | course/list | 1
course-list | Course List | course/list | 2

page_content表:

CREATE TABLE IF NOT EXISTS `page_content` (
  `page_content_code` varchar(255) NOT NULL,
  `page_code` varchar(255) NOT NULL,
  `text` varchar(255) DEFAULT NULL,
  `inst_id` bigint(20) NOT NULL unsigned,
  PRIMARY KEY (`page_content_code`,`page_code`,`inst_id`),
  KEY `inst_id` (`inst_id`),
  KEY `page_code` (`page_code`,`inst_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

page_content_code | page_code   | text     | inst_id
label-subject     | course-list | Subjects | 1
label-subject     | course-list | Subjects | 2

问题在于,当pages.page_code inst_id = 1更新为course-label时,所有page_content.page_code的{​​{1}}也会变为inst_id。< / p>

它应该只更新course-label page_content.page_code,因为我更新的内容是inst_id = 1 pages.page_code

0 个答案:

没有答案