Skip to content
Advertisement

How to create relationships in MySQL

In class, we are all ‘studying’ databases, and everyone is using Access. Bored with this, I am trying to do what the rest of the class is doing, but with raw SQL commands with MySQL instead of using Access.

I have managed to create databases and tables, but now how do I make a relationship between two tables?

If I have my two tables like this:

and

How do I create a ‘relationship’ between the two tables? I want each account to be ‘assigned’ one customer_id (to indicate who owns it).

Advertisement

Answer

If the tables are innodb you can create it like this:

You have to specify that the tables are innodb because myisam engine doesn’t support foreign key. Look here for more info.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement