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?
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.
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.
Inheritance enables you to share attributes between objects such that a subclass inherits attributes from its parent class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With