The below post gives the solution to add foreign key in database table in Magento 2.
One can establish a relation between two tables with the foreign key.
Add foreign key in a column and establish the relation of that table with another table.
To add foreign key constraint in the database table can be a complex process but not anymore with the solution here:
Method to add foreign key in database table in Magento 2:
addForeignKey( $installer->getFkName( 'child_table', 'main_id', 'main_table', 'entity_id' ), 'main_id', $installer->getTable('main_table'), 'entity_id', \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE )
Here child_table is a name of the foreign key table.
main_table is the name of the parent table.
That’s it.
Feel free to share the solution via social media profiles with fellow developers.
Thank you.