Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Foreign key constraints in MySQL Workbench

Wednesday, July 25th, 2012 by Servage

In this previous article you were introduced to MySQL Workbench, which essentially is a pretty great app to work with data modelling for MySQL databases.

In MySQL you can work with different indexes, keys and contraints, which are tools that help you build and maintain logical relationships between entries and tune your database for performance. One of those is the foreign key, which is automatically added to rows when using the relations functionality in MySQL workbench.

What are foreign keys

If you are used to manually create your database on your hosting account, for example using phpMyAdmin or similar, you might not create foreign keys. Their purpose is to indicate how a given row relates to a row – usually the ID – in another table. This is useful when having relations between records. For example every User has an Address, every Address has a Country etc. Those keys can be added, but it’s not required. Furthermore actions and contrainsts can be added if you wish.

What does Workbench do with foreign keys

The Workbench app exports the SQL for your data model including foreign keys by default. This might lead to unexpected behaviour, if your are not intending to use them, or simply are unfamiliar with them. For those of you who don’t care about foreign key constraints, you can simply remove them from the exported SQL, by selecting the proper checkbox right before exporting with Workbench.

So you can still use the nice visual modelling tools of Workbench, but export the SQL without the foreign keys.

Foreign key constraints in MySQL Workbench, 3.7 out of 5 based on 3 ratings
Categories: Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.