简体   繁体   中英

foreign key with composite primary key

A table has one column that refers to another table column that is composite primary key. How can I write the query for that? It shows this error. How can I resolve it.

ERROR 1050: Can't create table 'recdesk.#sql-5e8_33' (errno: 150)

SQL Statement:

ALTER TABLE `recdesk`.`facility`
  CHANGE COLUMN `organization_id` `organization_id` INT(11) NOT NULL DEFAULT 0,
  CHANGE COLUMN `facility_id` `facility_id` INT(11) NOT NULL DEFAULT 0, 

  ADD CONSTRAINT `fk_organization_id`
  FOREIGN KEY (`organization_id` )
  REFERENCES `recdesk`.`facility_type` (`organization_id` )
  ON DELETE RESTRICT
  ON UPDATE RESTRICT, 

  ADD CONSTRAINT `fk_facility_type_id`
  FOREIGN KEY (`facility_type_id` )
  REFERENCES `recdesk`.`facility_type` (`facility_type_id` )
  ON DELETE RESTRICT
  ON UPDATE RESTRICT

, ADD INDEX `fk_organization_id_idx` (`organization_id` ASC) 
, ADD INDEX `fk_facility_type_id_idx` (`facility_type_id` ASC)


ERROR: Error when running failback script. Details follow.

ERROR 1050: Table 'facility' already exists

SQL Statement:

CREATE TABLE `facility` (
  `organization_id` int(11) NOT NULL,
  `facility_id` int(11) NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `address_line1` varchar(50) DEFAULT NULL,
  `address_line2` varchar(50) DEFAULT NULL,
  `city` varchar(30) DEFAULT NULL,
  `state` varchar(2) DEFAULT NULL,
  `zip_code` varchar(9) DEFAULT NULL,
  `description` varchar(1000) DEFAULT NULL,
  `note` varchar(250) DEFAULT NULL,
  `capacity` smallint(6) DEFAULT NULL,
  `show_on_portal` char(1) DEFAULT NULL,
  `active_indicator` char(1) DEFAULT NULL,
  `primary_facility_indicator` char(1) DEFAULT NULL,
  `facility_type_id` int(11) DEFAULT NULL,
  `parent_facility_id` int(11) DEFAULT NULL,
  `create_date` datetime DEFAULT NULL,
  `show_schedule_on_portal` char(1) DEFAULT NULL,
  `show_usage_on_portal` char(1) DEFAULT NULL,
  `enable_online_reservation` char(1) DEFAULT NULL,
  `gl_code_id` int(11) DEFAULT NULL,
  `gl_code_deposit_id` int(11) DEFAULT NULL,

  PRIMARY KEY (`organization_id`,`facility_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

If the pair of columns (Organization_ID, Facility_ID) in the table Facility are a foreign key reference to the pair of columns (Organization_ID, Facility_ID) in the table Facility_Type , and this is a unique key in Facility_Type , then you need to create a single foreign key constraint:

ALTER TABLE `recdesk`.`facility`
  CHANGE COLUMN `organization_id` `organization_id` INT(11) NOT NULL DEFAULT 0,
  CHANGE COLUMN `facility_id` `facility_id` INT(11) NOT NULL DEFAULT 0, 

  ADD CONSTRAINT `fk_facility_type`
  FOREIGN KEY (`organization_id`, `facility_type`)
  REFERENCES `recdesk`.`facility_type` (`organization_id`, `facility_type`)
  ON DELETE RESTRICT
  ON UPDATE RESTRICT, 

  ADD INDEX `fk_facility_type_idx` (`organization_id` ASC, `facility_type` ASC)

This is inferred syntax, untested, but it should be close to what you need, assuming there are no unexpected restrictions in MySQL that prohibit compound foreign keys.

The concept is that you need a single foreign key reference that specifies both columns in the Facility table that refer to the matching columns in the referenced table. The index also needs to be on the pair of columns, not on a single column.

The error is

ERROR 1050: Table 'facility' already exists

You need to drop the table before executing create script.

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