The idea: one employee, several kinds
Some employees are engineers and have an Eng_type. Some are managers and have a Mgr_level. All of them have an Ssn and a Name. The enhanced ER (EER) model draws this as a specialization: the superclass EMPLOYEE is split into the subclasses ENGINEER and MANAGER. A subclass inherits every attribute of the superclass and adds its own. Read the other way, going from the subclasses up to what they share is a generalization; the diagram is the same. A relational database has no subclasses, only tables, so the design must choose how to store the tree. The canvas uses four sample employees: Smith (engineer), Wong (manager), Zelaya (neither) and Borg (both).
Two constraints, four combinations
A specialization carries two separate rules.
- Disjoint (d) or overlapping (o). Disjoint: an employee is in at most one subclass. Overlapping: he may be in several. Borg, an engineer and a manager, is only allowed when it is overlapping.
- Total (double line) or partial (single line). Total: every employee is in at least one subclass. Partial: some are in none. Zelaya, a clerk, is only allowed when it is partial.
The two rules are independent, so there are four combinations. Pick one with the constraint menu: the employees it forbids are marked ✗ and are not stored. When a single attribute decides the subclass (a Job_type of 'Engineer' or 'Manager'), the book calls it an attribute-defined specialization. Such a specialization is always disjoint, because one attribute holds one value.
Three ways to store it
Step 8 of the ER-to-relational mapping (Elmasri & Navathe, section 9.2) gives the options. The page calls them A, B and C.
- A: one table per class (option 8A). EMPLOYEE(Ssn, Name) holds everyone. ENGINEER(Ssn, Eng_type) and MANAGER(Ssn, Mgr_level) hold only their own attribute. Their
Ssnis both the primary key and a foreign key to EMPLOYEE (Demo: A, one table per class). - B: subclass tables only (option 8B). ENGINEER(Ssn, Name, Eng_type) and MANAGER(Ssn, Name, Mgr_level), each repeating the inherited columns. There is no EMPLOYEE table, so an employee in no subclass has nowhere to go (Demo: B, subclass tables only).
- C: one table for all. One EMPLOYEE table with every column of every class. When the specialization is disjoint, a type column
Job_typesays which subclass a row is in (option 8C). When it is overlapping, one type value is not enough, so there is one flag column per subclass,EflagandMflag(option 8D) (Demo: C, one table with a type column and Demo: overlapping in one table needs flags).
The counters under the tables give the cost of each choice:
| Mapping | Constraint | Tables | Rows | NULL cells | Note |
|---|---|---|---|---|---|
| A | overlapping, partial | 3 | 8 | 0 | Borg has a row in all three tables |
| B | overlapping, partial | 2 | 4 | 0 | Borg's Name stored twice; Zelaya lost |
| C, type column | disjoint, partial | 1 | 3 | 5 | Zelaya's row is NULL in all three type-specific columns |
| C, flags | overlapping, partial | 1 | 4 | 4 | flags are 0 or 1, never NULL |
Enforcing the constraints in MariaDB
The DDL panel shows the real MariaDB statements for each choice. The tables use InnoDB, the default engine, so the foreign keys are checked. MariaDB enforces CHECK constraints since version 10.2. But a CHECK looks at one row of one table, and MariaDB has no CREATE ASSERTION and no deferred constraints. So whether a rule can be declared depends on where the mapping puts the data:
| Mapping | Disjoint | Total |
|---|---|---|
| A | only with triggers: the Ssn is in two tables | ✗ can't be declared: the rule spans EMPLOYEE and the subclass tables |
| B | only with triggers | ✓ by construction: there is no table for "no subclass" |
| C, type column | ✓ by construction: one value per row | ✓ Job_type ... NOT NULL |
| C, flags | (overlapping) | ✓ CHECK (Eflag + Mflag >= 1) |
For A and B, disjointness needs a trigger on each subclass table. It looks into the other table and refuses the row with SIGNAL. In the mariadb client, wrap it in DELIMITER // because the body contains a semicolon:
CREATE TRIGGER mgr_disjoint BEFORE INSERT ON MANAGER
FOR EACH ROW IF EXISTS (SELECT 1 FROM ENGINEER
WHERE Ssn = NEW.Ssn) THEN SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'already an ENGINEER'; END IF;
Making Smith a manager too then fails with ERROR 1644 (45000): already an ENGINEER. In C with a type column, the same change fails on its own: setting Job_type = 'Manager' would leave his Eng_type on a manager's row, and the check eng_attr refuses it with ERROR 4025 (23000): CONSTRAINT `eng_attr` failed (Demo: disjoint across tables needs a trigger).
That check is written CHECK (Eng_type IS NULL OR Job_type <=> 'Engineer'). A CHECK only refuses a row when the condition is false, not when it is unknown, so a plain = would let a row with a NULL Job_type through. The null-safe <=> never gives unknown. Why NULL comparisons behave like this is the subject of NULL and Three-Valued Logic.
For the total rule, C refuses a clerk in no subclass with ERROR 1048 Column 'Job_type' cannot be null or, with flags, ERROR 4025 CONSTRAINT `emp_total` failed. A takes the same clerk without a word: the EMPLOYEE row is valid on its own, and the missing subclass row is something no single-row check can see. A trigger can't help either, because the subclass row is inserted after the EMPLOYEE row. The application, or a stored procedure that inserts both rows, has to keep the rule (Demo: total participation).
Same question, different joins
Pick a query or a write in the do menu and press Run. The panel shows the SQL, colours the tables it touches and counts statements, tables and joins.
| Operation | A | B | C |
|---|---|---|---|
| Q1 names of all employees | 1 table | 2 tables, UNION | 1 table |
| Q2 engineers with Eng_type | 2 tables, 1 join | 1 table | 1 table + WHERE |
| Q3 everything about 102 | 3 tables, 2 LEFT JOINs | 2 statements | 1 row |
| W1 insert an engineer | 2 INSERTs, parent first | 1 INSERT | 1 INSERT, 1 more NULL |
In B, Q1 needs UNION rather than UNION ALL, or Borg appears twice. Q3 is B's worst case: the application doesn't know Wong's subclass, so it must ask every subclass table (Demo: the same lookup in A, B and C). In A, the LEFT JOINs of Q3 rebuild the NULLs that C stores.
Choosing a mapping
- C fits when the subclasses have few attributes of their own. It needs no joins and enforces both rules with plain column constraints. The cost is a NULL for every subclass attribute a row doesn't use.
- A fits any combination and stores no NULLs and no copies. The cost is a join for every question that mixes inherited and own attributes, and a total rule the database can't check.
- B fits only a total specialization, and is best when it is also disjoint: then each employee is one row in one table. Overlapping copies the inherited columns, and partial loses employees.
See also ER Model to Tables for mapping steps 1 to 6, Inheritance Mapping for the same three layouts as an object-relational mapper sees them, Integrity Constraints and Foreign Keys for what the foreign keys of A do on delete, and UML Class Diagram for generalization in UML.
What the page leaves out
Specialization lattices and multiple inheritance (a shared subclass with two superclasses), union types (categories), and specializations several levels deep. The triggers shown cover INSERT; real ones also need BEFORE UPDATE twins in case an Ssn changes. MariaDB's CHECK can't contain a subquery, and it has no CREATE ASSERTION and no deferred constraints, which the SQL standard and the book describe; so the page shows triggers and the application instead. Real tables would also index the type or flag columns, and size the data types more carefully.