简体   繁体   English

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

[英]Is there alternative to MySQL Table with 26 Foreign Keys

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. 我有一个InnoDB MySQL数据库,其表格需要能够通过外键连接到其他26个表中的一个。 Each record will only connect to one of these 26 at a time. 每条记录一次只能连接到这26条记录中的一条。 The table will probably consist of no more than 10,000 records. 该表可能包含不超过10,000条记录。 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

This is the problem of generic foreign keys, which MySQL and friends tend not to support. 这是通用外键的问题,MySQL和朋友往往不支持。 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. 另一个,就像在Django的Content Types ,是一个连接表,每一行都有一个行id和一个指定要查找的表的字段。 Your code then has to formulate the SQL query depending on the contents of the field. 然后,您的代码必须根据字段的内容来制定SQL查询。 It works well, but has limitations: 它运作良好,但有局限性:

The downside of the first one is bloat, but it brings you the upsides of normal FKs, ie referential integrity and SQL joins etc, both of which are very valuable. 第一个的缺点是膨胀,但它带给你正常FK的优点,即参照完整性和SQL连接等,这两者都非常有价值。 You can't get those with the second method. 你不能用第二种方法获得那些。

Depends if you want to maintain foreign key constraint, you can have one table that references one of the tables by a key or table type. 取决于您是否要维护外键约束,您可以使用一个表,通过键或表类型引用其中一个表。 Problem is you will loose the foreign key constraint. 问题是你会松开外键约束。 Of course, if you can create a function based constraint, then it can work for you. 当然,如果你可以创建一个基于函数的约束,那么它可以为你工作。 Or you can enforce the relationship using a trigger. 或者您可以使用触发器强制执行关系。 Function based constraints are not available in mysql. 基于函数的约束在mysql中不可用。

Yes, you can do that. 是的,你可以这样做。 These two StackOverflow answers illustrate the underlying principles in a slightly different context. 这两个StackOverflow答案在略微不同的上下文中说明了基本原理。

Using MySQL, you'll need to replace critical CHECK() constraints with foreign key references. 使用MySQL,您需要使用外键引用替换关键的CHECK()约束。 This doesn't work in the most general case for MySQL, but it does work in this particular application. 这在MySQL的最常见情况下不起作用 ,但它在这个特定的应用程序中确实有效。

If this isn't enough information to get you going, leave me a comment, and I'll try to expand this answer a little more. 如果这还没有足够的信息让你前进,请给我留言,我会尝试更多地扩展这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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