A Foreign Key Constraints is a column or a set of columns in a table that refers to the primary key or a unique key of another table. It helps to create connection between two different table.
Foreign Key Constraints

 

Here we have two different table author and book where in author table author_id is primary_key where as in book table id is primary key, In book table author_id is foreign key.

Now here is syntax and example of adding foreign key

Syntax:
ALTER TABLE TableName ADD CONSTRAINT FK_Key_Name FOREIGN KEY (column_name) REFERENCES foriegnTableName(column_name);
Example: ALTER TABLE book ADD CONSTRAINT FK_AUTHOR_ID FOREIGN KEY (author_id) REFERENCES author(author_id);

Now you will see there is Multiple Key created in in Key description.

Foreign Key

Java Hibernate Magic: Create MySQL Tables with Ease and Efficiency

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *