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).

Left, the EER diagram: EMPLOYEE on top, a circle marked d or o under it, and lines from the circle down to ENGINEER and MANAGER, each with a subset sign. A double line from EMPLOYEE to the circle means total, a single line partial. Right, a two by two grid. Disjoint and partial: Smith, Wong and Zelaya allowed, Borg not (he is both). Overlapping and partial: all four allowed. Disjoint and total: only Smith and Wong. Overlapping and total: Smith, Wong and Borg; Zelaya is not allowed because he is in no subclass.
The circle says whether one employee may be in two subclasses; the line says whether he may be in none.

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 Ssn is 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_type says 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, Eflag and Mflag (option 8D) (Demo: C, one table with a type column and Demo: overlapping in one table needs flags).
The four sample employees, overlapping and partial, stored three ways. A: EMPLOYEE with 4 rows, ENGINEER with 101 Civil and 104 Software, MANAGER with 102 level 2 and 104 level 1; 3 tables, 8 rows, no NULL. B: ENGINEER with Smith and Borg, MANAGER with Wong and Borg, so Borg's Name is stored twice, and Zelaya has no table; 2 tables, 4 rows. C: one EMPLOYEE table with Eflag and Mflag columns and 4 rows, holding 4 NULL cells.
Same four employees: A splits each one over several tables, B copies Borg and loses Zelaya, C keeps everyone in one row with NULLs.

The counters under the tables give the cost of each choice:

MappingConstraintTablesRowsNULL cellsNote
Aoverlapping, partial380Borg has a row in all three tables
Boverlapping, partial240Borg's Name stored twice; Zelaya lost
C, type columndisjoint, partial135Zelaya's row is NULL in all three type-specific columns
C, flagsoverlapping, partial144flags 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:

MappingDisjointTotal
Aonly with triggers: the Ssn is in two tables✗ can't be declared: the rule spans EMPLOYEE and the subclass tables
Bonly 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.

OperationABC
Q1 names of all employees1 table2 tables, UNION1 table
Q2 engineers with Eng_type2 tables, 1 join1 table1 table + WHERE
Q3 everything about 1023 tables, 2 LEFT JOINs2 statements1 row
W1 insert an engineer2 INSERTs, parent first1 INSERT1 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.

On the mind map

Open the Databases & Storage mind mapDatabases& StorageSchema and storage: how rows land on diskSchema and storageIndexes: find rows without a full scanIndexesRunning a query: parse, plan, executeRunning a queryCost-based optimizer: estimate rows, pick cheapest planCost-based optimizerQuery rewrites: same answer, simpler queryQuery rewritesTransactions: ACID: isolation and recoveryTransactionsApps and connections: the client side of the databaseApps and connectionsMongoDB: documents, replica sets, shardsMongoDBRelational, document, graphRelational, document, graphone résumé as tables, JSON and a graphNormalizationNormalizationeach fact once: no partial, transitive depsClosure, Keys, BCNFClosure, Keys, BCNFBCNF can lose an FD; 3NF never has toRow vs column storeRow vs column storerows for lookups, columns for SUMStar schema & OLAP cubeStar schema & OLAP cuberoll-up = GROUP BY; slice = WHEREParquet file formatParquet file formatFooter min/max skips row groupsLSM vs B+ treeLSM vs B+ treeLSM: fast writes; B+ tree: fast readsSQLite internalsSQLite internalsone file of pages, a b-tree per tableER model to tablesER model to tablesentities, relations, keys become tablesEER specialization mappingDisjoint in A/B needs triggers; C uses CHECKIntegrity constraints & FKsIntegrity constraints & FKs1062 dup, 1452 no parent, 1451 has childrenOpen the Databases & Storage mind mapDatabases & Storagehow rows land on diskSchema and storageRelational, document, graphRelational, document, graphone résumé as tables, JSON and a graphNormalizationNormalizationeach fact once: no partial, transitive depsClosure, Keys, BCNFClosure, Keys, BCNFBCNF can lose an FD; 3NF never has toRow vs column storeRow vs column storerows for lookups, columns for SUMStar schema & OLAP cubeStar schema & OLAP cuberoll-up = GROUP BY; slice = WHEREParquet file formatParquet file formatFooter min/max skips row groupsLSM vs B+ treeLSM vs B+ treeLSM: fast writes; B+ tree: fast readsSQLite internalsSQLite internalsone file of pages, a b-tree per tableER model to tablesER model to tablesentities, relations, keys become tablesEER specialization mappingDisjoint in A/B needs triggers; C uses CHECKIntegrity constraints & FKsIntegrity constraints & FKs1062 dup, 1452 no parent, 1451 has children
This page on the Databases & Storage mind map · 58 pages in 8 branches · open it to see the whole map and test yourself in recall mode.