dqw70970 2013-01-04 14:53
浏览 118
已采纳

有没有26个外键的MySQL表替代品

I have an InnoDB MySQL database with a table that needs to be able to connect to one of 26 other tables via a foreign key. Each record will only connect to one of these 26 at a time. The table will probably consist of no more than 10,000 records. Is there an alternative way to do this?

-- -----------------------------------------------------
-- Table `db_mydb`.`tb_job`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `db_mydb`.`tb_job` (
  `job_id` INT(11) NOT NULL AUTO_INCREMENT ,
  // Removed 26 other fields that the table requires

  `job_foreignkey_a_id` INT(11) NULL DEFAULT NULL ,
  `job_foreignkey_b_id` INT(11) NULL DEFAULT NULL ,
  `job_foreignkey_c_id` INT(11) NULL DEFAULT NULL ,
  // Removed the other 23 foreign keys fields that are the same

  PRIMARY KEY (`job_id`) ,

  CONSTRAINT `fka_tb_job_tb`
    FOREIGN KEY (`job_foreignkey_a_id` )
    REFERENCES `db_mydb`.`tb_foreignkey_a` (`foreignkey_a_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fkb_tb_job_tb`
    FOREIGN KEY (`job_foreignkey_b_id` )
    REFERENCES `db_mydb`.`tb_foreignkey_b` (`foreignkey_b_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fkc_tb_job_tb`
    FOREIGN KEY (`job_foreignkey_c_id` )
    REFERENCES `db_mydb`.`tb_foreignkey_c` (`foreignkey_c_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
  // Removed the other 23 foreign keys constraints that are the same

ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

CREATE INDEX `fka_tb_job_tb` ON `db_mydb`.`tb_job` (`job_foreignkey_a_id` ASC) ;
CREATE INDEX `fkb_tb_job_tb` ON `db_mydb`.`tb_job` (`job_foreignkey_b_id` ASC) ;
CREATE INDEX `fkc_tb_job_tb` ON `db_mydb`.`tb_job` (`job_foreignkey_c_id` ASC) ;
// Removed the other 23 foreign keys indexes that are the same
  • 写回答

3条回答 默认 最新

  • doutang1884 2013-01-04 14:57
    关注

    This is the problem of generic foreign keys, which MySQL and friends tend not to support. There are two ways you can do this.

    The first, as you have done, is nullable foreign keys, one for every type.

    The other, as in Django's Content Types, is to have a join table, each row having a row id and a field that specifies the table to look up on. Your code then has to formulate the SQL query depending on the contents of the field. It works well, but has limitations:

    The downside of the first one is bloat, but it brings you the upsides of normal FKs, i.e. referential integrity and SQL joins etc, both of which are very valuable. You can't get those with the second method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'