How to Link an FK in MySQL
The foreign key (FK) of one table links to the primary key of another MySQL table. You use these keys to create queries that join the tables, so you can view a list of data from one query instead of several queries that retrieve data from multiple tables. Use the "join" statement to link the two tables. Together with the "select" statement, the join statement connects two or more tables to query a database and preserve database performance.
Instructions
-
-
1
Click the Windows "Start" button and select "All Programs." Click "MySQL" in the program groups, then click "Query Browser." The Query Browser program lets you create SQL statements for your database.
-
2
Log in to the database server using your username and password provided by the database administrator. After you log in, click the "New Query" button.
-
-
3
Type the select statement in the query editor. The following code shows you how to link a foreign key in a query:
select * from customers join orders on customers.customerid=orders.customerid
In the example above, the primary key is in the customers table. The foreign key is in the orders table. The two keys link together, so you can view the data in both tables in the results.
-
4
Click the "Execute" button. The query executes and the results display at the bottom of the Query Browser window.
-
1