Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MySQL support table inheritance?

I have this code in PostgreSQL

CREATE TABLE first (
  id serial,
  primary key(id)
);

CREATE TABLE second (primary key(id)) INHERITS (first);

What is the equivalent code for MySQL?

like image 270
sajadkk Avatar asked Oct 29 '14 06:10

sajadkk


People also ask

How does table inheritance work?

Inheritance allows a table to inherit some of its column attributes from one or more other tables, creating a parent-child relationship. This causes the child table to have each of the same columns and constraints as its inherited table (or tables), as well as its own defined columns.

What is parent table in MySQL?

The customerNumber column in the orders table links to the customerNumber primary key column in the customers table. The customers table is called the parent table or referenced table, and the orders table is known as the child table or referencing table.

What is inheritance in database?

Inheritance enables you to share attributes between objects such that a subclass inherits attributes from its parent class.


1 Answers

MySQL does not support table inheritance. The only way to approximate the functionality is by using a foreign key (which MySQL isn't too good at either):

CREATE TABLE first (
  id serial,
  PRIMARY KEY (id)
);

CREATE TABLE second (
  parent integer REFERENCES first,
  PRIMARY KEY (parent)
);

Obviously, you'd have to change any views and queries from the PostgreSQL "inheritance version" to regular multi-relation queries.

like image 78
Patrick Avatar answered Sep 21 '22 15:09

Patrick