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):
This is the relation scheme diagram that should be implemented:
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).
batting_average
andbowling_average
columns? for each table where these columns are present.