Does MySQL support table inheritance?


MySQL uses foreign key constraint instead of inheritance. MySQL does not support table inheritance.

You can achieve the same with the help of foreign key constraint. Let us create a table and use the foreign key constraint. The query to create the first table is as follows −

mysql> create table Parent_Table
   -> (
   -> ParentId int,
   -> PRIMARY KEY(ParentId)
   -> );
Query OK, 0 rows affected (3.59 sec)

Now create the second table. The query to create the second table is as follows −

mysql> create table Child_Table
   -> (
   -> ChildId int references Parent_Table,
   -> PRIMARY KEY(ChildId)
   -> );
Query OK, 0 rows affected (0.73 sec)

Now add the foreign key relationship between two tables. The query is as follows −

mysql> alter table Child_Table add constraint FK_Child Foreign key(ChildId) references Parent_Table(ParentId);
Query OK, 0 rows affected (2.28 sec)
Records: 0 Duplicates: 0 Warnings: 0

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements