MySQL - Add Foreign Key Contraint to table field

 

Add Foreign Key Contraints to MySQL Field

A quick howto for adding foreign keys to a mysql table field.

To alter a table to include a foreign key...
For example, assume

table
locations: id, location, location_id

table
user: id, username, password, location_id

To add foreign key to users table referring to location table:

alter table users add constraint foreign key (location_id) references locations(location_id);