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 7869601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:22:35+00:00 2026-06-03T01:22:35+00:00

Question: Is there a way to make the foreign ID point to something more

  • 0

Question:

Is there a way to make the foreign ID point to something more generic than one specific table?

Details:

Often I run into the situation where I have several tables which have nothing to do with each other, but still need a common table (in below examples engine is innodb)

CREATE TABLE IF NOT EXISTS movies
(
   id    INT NOT NULL auto_increment,
   name  VARCHAR(100) NOT NULL ,

   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS books
(
   id    INT NOT NULL auto_increment,
   name  VARCHAR(100) NOT NULL ,

   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS songs
(
   id    INT NOT NULL auto_increment,
   name  VARCHAR(100) NOT NULL ,

   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS news_papers
(
   id    INT NOT NULL auto_increment,
   name  VARCHAR(100) NOT NULL ,

   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS scrolls
(
   id    INT NOT NULL auto_increment,
   name  VARCHAR(100) NOT NULL ,

   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS sumarian_wheat_tablets
(
   id    INT NOT NULL auto_increment,
   name  VARCHAR(100) NOT NULL ,

   PRIMARY KEY(id)
);

Now I want to keep a record of every time each is viewed like so

CREATE TABLE IF NOT EXISTS movie_history
(
   id          INT NOT NULL auto_increment,
   foreign_id  INT NOT NULL ,
   view_date   TIMESTAMP DEFAULT now(),

   FOREIGN KEY (foreign_id) REFERENCES movies ( id ),
   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS book_history
(
   id          INT NOT NULL auto_increment,
   foreign_id  INT NOT NULL ,
   view_date   TIMESTAMP DEFAULT now(),

   FOREIGN KEY (foreign_id) REFERENCES books ( id ),
   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS song_history
(
   id          INT NOT NULL auto_increment,
   foreign_id  INT NOT NULL ,
   view_date   TIMESTAMP DEFAULT now(),

   FOREIGN KEY (foreign_id) REFERENCES songs ( id ),
   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS news_paper_history
(
   id          INT NOT NULL auto_increment,
   foreign_id  INT NOT NULL ,
   view_date   TIMESTAMP DEFAULT now(),

   FOREIGN KEY (foreign_id) REFERENCES news_papers ( id ),
   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS scroll_history
(
   id          INT NOT NULL auto_increment,
   foreign_id  INT NOT NULL ,
   view_date   TIMESTAMP DEFAULT now(),

   FOREIGN KEY (foreign_id) REFERENCES scrolls ( id ),
   PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS sumarian_wheat_tablet_history
(
   id          INT NOT NULL auto_increment,
   foreign_id  INT NOT NULL ,
   view_date   TIMESTAMP DEFAULT now(),

   FOREIGN KEY (foreign_id) REFERENCES sumarian_wheat_tablets ( id ),
   PRIMARY KEY(id)
);

Is there a more correct way to handle such situations without making n new tables? I realize that I can make one history table and copy it over with CREATE TABLE...LIKE... but that still requires making n new tables, plus I have to go in and ALTER the foreign_id.

  • 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-03T01:22:36+00:00Added an answer on June 3, 2026 at 1:22 am

    My first thought is just dump the fk reference and have one history table:

    CREATE TABLE history(
       base_table    VARCHAR,
       base_table_id INT,
       view_date     TIMESTAMP DEFAULT now()
    );
    

    But I assume you want the fk to maintain the integrity (question: is this really necessary, or can this be worked around?). I guess you could accomplish this by creating a table of “pks in use”. For example:

    • create a table “keys” with columns id (autoincrement) and base_table_name
    • create a table “movies”, where id is both pk and also a fk to “keys.id” (but not an autoincrement column)
    • add a “before insert” trigger to “movies” which inserts a record into “keys” returning the generated id to be used as the id for the “movie” record
    • create a history table with a fk to “keys”
    • create a “delete” trigger on “movies” which also removes the record from “keys” if you want the integrity maintained, or cascading deletes, etc

    So the generated “id” is shared across many tables. There is a school of thought that suggests using a primary key unique across all relations within the database (an “enterprise key”), so it is not unprecedented. Instead of using sequences or autogenerated columns, sometimes a GUID or UUID is used.

    This replaces extra history tables with triggers on each base table, which might not be a great thing, depending on your environment. I haven’t done this myself, just throwing some thoughts out there, so take it for what its worth.

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

Sidebar

Related Questions

Question: Is there a way to make view PERSON for user jack point to
Question: Is there a way to check if a given font is one of
My question is: is there a way with a DataContext/Table mapping to implement some
Short question: is there a way to make a jsonp request to a server,
Question: Is there a way to make a button behave like a hyperlink inside
Question: Is there a way to make a function object in python using strings?
quick question...is there a way to make update to an iphone app (news and
A simple question - is there any way to make the g++ linker to
Question Is there a way to define a method only once in C# (in
Silly question - Is there a way to download the iPhone SDK without Xcode

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.