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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:01:41+00:00 2026-05-10T15:01:41+00:00

If I have a table in MySQL which represents a base class, and I

  • 0

If I have a table in MySQL which represents a base class, and I have a bunch of tables which represent the fields in the derived classes, each of which refers back to the base table with a foreign key, is there any way to get MySQL to enforce the one-to-one relationship between the derived table and the base table, or does this have to be done in code?

Using the following quick ‘n’ dirty schema as an example, is there any way to get MySQL to ensure that rows in both product_cd and product_dvd cannot share the same product_id? Is there a better way to design the schema to allow the database to enforce this relationship, or is it simply not possible?

CREATE TABLE IF NOT EXISTS `product` (     `product_id` int(10) unsigned NOT NULL auto_increment,     `product_name` varchar(50) NOT NULL,     `description` text NOT NULL,     PRIMARY KEY  (`product_id`) ) ENGINE = InnoDB;  CREATE TABLE `product_cd` (     `product_cd_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,     `product_id` INT UNSIGNED NOT NULL ,     `artist_name` VARCHAR( 50 ) NOT NULL ,     PRIMARY KEY ( `product_cd_id` ) ,     INDEX ( `product_id` ) ) ENGINE = InnoDB;  ALTER TABLE `product_cd` ADD FOREIGN KEY ( `product_id` )      REFERENCES `product` (`product_id`)      ON DELETE RESTRICT ON UPDATE RESTRICT ;  CREATE TABLE `product_dvd` (     `product_dvd_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,     `product_id` INT UNSIGNED NOT NULL ,     `director` VARCHAR( 50 ) NOT NULL ,     PRIMARY KEY ( `product_dvd_id` ) ,     INDEX ( `product_id` ) ) ENGINE = InnoDB;  ALTER TABLE `product_dvd` ADD FOREIGN KEY ( `product_id` )      REFERENCES `product` (`product_id`)      ON DELETE RESTRICT ON UPDATE RESTRICT ; 

@Skliwz, can you please provide more detail about how triggers can be used to enforce this constraint with the schema provided?

@boes, that sounds great. How does it work in situations where you have a child of a child? For example, if we added product_movie and made product_dvd a child of product_movie? Would it be a maintainability nightmare to make the check constraint for product_dvd have to factor in all child types as well?

  • 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. 2026-05-10T15:01:42+00:00Added an answer on May 10, 2026 at 3:01 pm

    To make sure that a product is or a cd or a dvd I would add a type column and make it part of the primary key. In the derived column you add a check constraint for the type. In the example I set cd to 1 and you could make dvd = 2 and so on for each derived table.

    CREATE TABLE IF NOT EXISTS `product` ( `product_id` int(10) unsigned NOT NULL auto_increment, 'product_type' int not null, `product_name` varchar(50) NOT NULL, `description` text NOT NULL, PRIMARY KEY (`product_id`, 'product_type') ) ENGINE = InnoDB;  CREATE TABLE `product_cd` ( `product_id` INT UNSIGNED NOT NULL , 'product_type' int not null default(1) check ('product_type' = 1) `artist_name` VARCHAR( 50 ) NOT NULL , PRIMARY KEY ( `product_id`, 'product_type' ) , ) ENGINE = InnoDB;  ALTER TABLE `product_cd` ADD FOREIGN KEY ( `product_id`, 'product_type' )  REFERENCES `product` (`product_id`, 'product_type')  ON DELETE RESTRICT ON UPDATE RESTRICT ; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer cat /etc/issue Or cat /etc/fedora-release as suggested by @Bruce ONeel May 11, 2026 at 6:28 am
  • added an answer Use the explode() function and separate by either spaces or… May 11, 2026 at 6:28 am
  • added an answer SELECT * FROM MyTable WHERE NLSSORT(MyField, 'NLS_SORT = Latin_AI') =… May 11, 2026 at 6:28 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.