0

I want to know how to implement classes in MySQL that represent a subset of another class type, with the purpose of having subclasses inherit fields from the parent class. Described by Martin Fowler as the concrete table inheritance pattern.

An example would be having a GradStudent class with two subclasses TeachingAsst and ResearchAsst--each of which inherit the firstName, lastName, and birthDate fields from GradStudent (see original source):

UML

This is the relation scheme diagram that should be implemented:

enter image description here

Here's what I've got so far adapted from here:

CREATE TABLE GradStudent (
    studentID INT UNSIGNED NOT NULL AUTO_INCREMENT,
    firstName VARCHAR(50) NOT NULL,
    lastName VARCHAR(50) NOT NULL,
    birthDate DATE NULL,
    UNIQUE(firstName, lastName, birthDate),
    PRIMARY KEY (studentID)
);

CREATE TABLE TeachingAsst (
    studentID INT UNSIGNED NOT NULL,
    courseAssignment VARCHAR(255) NOT NULL DEFAULT '',
    PRIMARY KEY (studentID),
    FOREIGN KEY (studentID) REFERENCES GradStudent (studentID)
);

CREATE TABLE ResearchAsst (
    studentID INT UNSIGNED NOT NULL,
    researchGrant VARCHAR(255) NOT NULL DEFAULT '',
    PRIMARY KEY (studentID),
    FOREIGN KEY (studentID) REFERENCES GradStudent (studentID)
);

Is this the correct way to create subclasses in MySQL?

To be more specific, I am not asking for the following:

  • Answers describing single table inheritance or class table inheritance patterns (discussed here)
  • Additional types of EER diagram mappings (discussed here)
  • Answers describing shared primary keys in general (discussed here)
  • Pointers/Links to textbooks and other resources that do not show MySQL code implementing inheritance (such as a list of stack exchange search results for [class-table-inheritance] or using OOP classes).
7
  • The scheme is absolutely unclear (now it looks like illogical). Describe all the details of the processes which this scheme must describe verbally, including the smallest and most insignificant. What are batting_average and bowling_average columns? for each table where these columns are present.
    – Akina
    Commented Feb 26, 2022 at 21:11
  • 1
    It's also unclear what you're asking help with since you're already implementing inheritance with your example queries via their foreign key relationships. Inheritance in relational databases isn't functionally the same as in an object oriented programming language. There's not really a concept of parent and children tables, rather they're all just siblings, and you define their relationships via foreign keys.
    – J.D.
    Commented Feb 27, 2022 at 2:39
  • @Akina Went ahead and modified the example to better represent what I'm looking to accomplish.
    – ike5
    Commented Feb 27, 2022 at 2:44
  • @J.D. I'm looking to avoid redundancy by using subclasses and want to know what's the correct way of implementing subclasses in MySQL.
    – ike5
    Commented Feb 27, 2022 at 2:49
  • As I mentioned, subclasses don't really exist as a concept in relational databases. You don't need to repeat fields when the sibling tables are related by your foreign keys. Then to access one of those fields, you would just join the two tables together.
    – J.D.
    Commented Feb 27, 2022 at 2:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.