MySql – Relationships

Dealing with relationships in MySql is not as straightforward as either MS Access or as you would have hoped for. Creating simple primary keys isn’t too difficult

 CREATE TABLE `student` ( contact_id INT(10), name VARCHAR(40), birthdate DATE, PRIMARY KEY (contact_id) ); 

However foreign keys are more complex and require the following code in the table creation code:

FOREIGN KEY (foreign_id) REFERENCES foreign_table (foreign_id)

This will ensure that you can only add foreign_ids when they exist in the foreign table.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.