Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9081561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:20:38+00:00 2026-06-16T20:20:38+00:00

I have an InnoDB MySQL database with a table that needs to be able

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:20:40+00:00Added an answer on June 16, 2026 at 8:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my MySQL installation I have one InnoDB database that I know will grow
Should a database table that contains two columns that are foreign keys have a
I have the following three (InnoDB-) tables in a MySQL database: Entity (DLID) Category
I am having issues with an InnoDB mysql database. E.g., I have two tables,
I have a mySQl innodb database which has a couple of tables which store
I have inherited a system built on a MySQL database using InnoDB tables. The
I have a MySQL innodb database at 1.9GB, showed by following command. SELECT table_schema
I have an InnoDB MySQL database. When I export a SQL dump, how does
I have an enormous MySQL (InnoDB) database with millions of rows in the sessions
I have MySQL InnoDB table utf-8 encoded. This table has only id and name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.