Skip to content

Table inheritance: UNDER, INHERIT and superclasses

View as Markdown

A CoraDB table can be created as a subclass of one or more existing tables, its superclasses. The subclass inherits all their columns, constraints such as primary keys, and methods, and can add columns of its own. This is called inheritance; in this page a table is also called a class and a column an attribute, the terms used by the catalog.

CREATE TABLE b2b_order UNDER orders (po_number VARCHAR(20));

b2b_order has the six columns of orders followed by po_number (see DB_ATTRIBUTE, whose from_class_name shows where each column comes from). The primary key pk_orders_id and the foreign key on customer_id are inherited too: the primary key index is shared, so an id must be unique across orders and b2b_order together.

  • A subclass name must be unique and its superclasses must exist.
  • Encryption (TDE) of a table is not inherited; see Table encryption (TDE).
  • Inheritance is available for ordinary tables. Vertex and edge tables of a graph are not part of it.

By default a query on a table reads only that table's rows. ALL includes the rows of all its subclasses, ONLY states the default explicitly, and ALL … (EXCEPT … ) leaves out some subclasses:

INSERT INTO b2b_order (id, customer_id, shipping_cost, ship_address, order_date, po_number)
VALUES (90001, 102, 12.50, '1 Main St', DATE'2026-11-01', 'PO-7781');
SELECT COUNT(*) FROM orders; -- 2910
SELECT COUNT(*) FROM ONLY orders; -- 2910
SELECT COUNT(*) FROM ALL orders; -- 2911
SELECT COUNT(*) FROM ALL orders (EXCEPT b2b_order); -- 2910

A query through a superclass can use only the columns the superclass defines; read po_number from b2b_order itself. UPDATE and DELETE accept ALL in the same way.

A class attribute belongs to the table itself rather than to its rows, and has one value per table. It is declared with CLASS ATTRIBUTE before the column list and read and changed with SELECT … FROM CLASS and UPDATE CLASS:

CREATE TABLE promo CLASS ATTRIBUTE (active_count INT) (code VARCHAR(16), pct SHORT);
UPDATE CLASS promo SET active_count = 3;
SELECT active_count FROM CLASS promo; -- 3

A subclass inherits the class attribute but stores its own value: after CREATE TABLE promo_vip UNDER promo, UPDATE CLASS promo_vip SET active_count = 7 leaves promo at 3. A class attribute can have the same name as a column of the same table.

  • A table without superclasses has its columns in the order of the CREATE TABLE statement.
  • In a subclass, the inherited columns come first, in the order of their superclass, followed by the subclass's own columns. With several superclasses, the superclasses are taken in the order of the UNDER clause.
  • A column that reaches the subclass through several paths from the same original table is inherited once.
  • A column renamed with INHERIT … AS keeps the position of the original column.

Multiple inheritance and the INHERIT clause

Section titled “Multiple inheritance and the INHERIT clause”

A table can have several superclasses. If two of them have a column (or method) with the same name, the subclass cannot have both under that name; CREATE TABLE then fails until the conflict is resolved with INHERIT:

CREATE TABLE contact_info (name VARCHAR(40), phone VARCHAR(20));
CREATE TABLE shipping_info (name VARCHAR(60), ship_address VARCHAR(128));
CREATE TABLE partner UNDER contact_info, shipping_info;
ERROR: Name conflict on attribute "name" with classes "contact_info" and "shipping_info".
INHERIT resolution [, resolution ...]
resolution ::= { column_name | method_name } OF superclass_name [ AS alias ]

INHERIT names the superclass whose column is inherited. With AS, a second column of the same name is inherited too, under another name:

CREATE TABLE partner UNDER contact_info, shipping_info INHERIT name OF contact_info;
ALTER TABLE partner INHERIT name OF shipping_info AS ship_name;
SELECT attr_name, from_class_name, from_attr_name, prec
FROM db_attribute WHERE class_name = 'partner' ORDER BY def_order;
attr_name from_class_name from_attr_name prec
name contact_info NULL 40
phone contact_info NULL 20
ship_name shipping_info name 60
ship_address shipping_info NULL 128

The INHERIT clause can follow CREATE TABLE … UNDER, ALTER TABLE … ADD SUPERCLASS, ALTER TABLE … DROP SUPERCLASS, or stand alone in ALTER TABLE.

Two inherited columns with the same name must have compatible types, even when INHERIT picks one of them:

CREATE TABLE p1 (phone INT);
CREATE TABLE p2 (phone VARCHAR(20));
CREATE TABLE p3 UNDER p1, p2 INHERIT phone OF p2;
ERROR: Class "p3" has incompatible domains for "phone", inherited from classes "p2" and "p1".

When the two columns are object references to tables of the same hierarchy, the subclass inherits the one whose type is lower in the hierarchy (the more specific one).

ALTER TABLE table_name ADD SUPERCLASS superclass_name [, superclass_name ...]
[ INHERIT resolution [, resolution ...] ];

Adding a superclass to an existing table adds the superclass's columns to it:

CREATE TABLE vendor (vendor_code VARCHAR(10));
ALTER TABLE vendor ADD SUPERCLASS contact_info;
-- vendor: name, phone (from contact_info), vendor_code

ALTER TABLE table_name DROP SUPERCLASS superclass_name [, superclass_name ...]
[ INHERIT resolution [, resolution ...] ];

Dropping a superclass removes the columns inherited from it (ALTER TABLE vendor DROP SUPERCLASS contact_info leaves vendor with vendor_code only). If a column chosen with INHERIT came from the dropped superclass and another superclass has a column of the same name, name the replacement in the same statement:

CREATE TABLE a_tbl (a INT PRIMARY KEY, b INT);
CREATE TABLE b_tbl (a INT PRIMARY KEY, b INT, c INT);
CREATE TABLE c_tbl (b INT PRIMARY KEY, d INT);
CREATE TABLE a_b_c UNDER a_tbl, b_tbl, c_tbl INHERIT a OF b_tbl, b OF b_tbl;
-- a_b_c: a, b, c (from b_tbl), d (from c_tbl)
ALTER TABLE a_b_c DROP SUPERCLASS b_tbl INHERIT b OF a_tbl;
-- a_b_c: a, b (from a_tbl), d (from c_tbl)

a needs no clause here, because after b_tbl is gone only a_tbl provides it.

Changes to a superclass propagate to all its subclasses. The following changes can create or remove name conflicts in subclasses:

  • Adding or dropping a column or method
  • Adding or dropping a superclass
  • Dropping a table

CoraDB keeps the schema consistent by resolving such conflicts in the subclasses:

  • A column or method defined in a subclass takes precedence over an inherited one with the same name.

  • A resolution chosen with INHERIT is kept as long as it remains valid. When it becomes invalid, for example because the chosen column was dropped, CoraDB picks another column of the same name from the remaining superclasses.

  • When a column added to a superclass has the same name as a column a subclass already inherits from another superclass, CoraDB resolves the conflict automatically. In the following example, partner then inherits ship_address from contact_info, its first superclass, instead of shipping_info:

    CREATE TABLE partner UNDER contact_info, shipping_info INHERIT name OF contact_info;
    ALTER TABLE contact_info ADD ATTRIBUTE ship_address VARCHAR(128);

    Check db_attribute.from_class_name after such changes, and use ALTER TABLE … INHERIT … OF … to choose a different source.

  • A change in a subclass never changes its superclasses.

  • A table can be dropped even if columns of other tables use it as their type; those columns then refer to the general type object.

Because automatic resolutions depend on the order of superclasses and the history of changes, avoid reusing the same column names across superclasses of one table, and resolve conflicts explicitly with INHERIT when they cannot be avoided.

CoraDB preserves these properties of the schema across every change:

  • Class hierarchy: the tables form a directed acyclic graph with the system class object at its root. A table cannot be its own superclass, directly or indirectly, and a table without superclasses is a direct subclass of object.
  • Names: table names are unique, and the names of columns and methods are unique within a table. INHERIT … AS gives an inherited column a new name to keep this rule.
  • Inheritance: a subclass inherits every column and method of its superclasses, except where a name conflict is resolved. A column inherited from the same original table through several paths is inherited once; columns with the same name but different origins need a resolution.
  • Domains: the type of an inherited object-reference column can be narrowed in a subclass to a subclass of the original type, and widened again, but not beyond the superclass's type.