How to Add a Foreign Key to MySQL

How to Add a Foreign Key to MySQL thumbnail
How to Add a Foreign Key to MySQL

MySQL is a free database server used to create dynamic web pages. Foreign keys link to a primary key in your tables. These keys are columns in your MySQL tables. They link related records in separate tables (e.g., a customer linked to an order in a separate "orders" table). You create foreign keys using the MySQL console included with the database engine.

Instructions

    • 1

      Click the Windows "Start" button and click "All Programs." Click "MySQL" and then click "MySQL Workbench" to open your main console.

    • 2

      Click "Edit SQL Script" in the main console window. This opens a SQL editor where you create the foreign key.

    • 3

      Type the following code into your editor:

      alter table table_name add constraint key_name foreign key

      references tbl_name primary_key

      The first line sets the foreign key. Replace "table_name" with the name of your table. "Key_name" is the name of the column in your table you want to set as the foreign key. The second line is the primary key information to link to the foreign key. Replace "tbl_name" with the table that contains the primary key and "primary_key" with the primary key column name.

    • 4

      Click "Run" to run your SQL statement. The foreign key is created, and your tables are set up with the linked attributes.

Related Searches:

References

  • Photo Credit Comstock/Comstock/Getty Images

Comments

You May Also Like

  • Foreign Key MySQL Tutorial

    A foreign key is a column in one table that points to the primary key column of another table. Using a foreign...

  • MySQL Primary Key Tutorial

    MySQL is a open-source database application. Like other databases, data is organized into tables of related data. To indicate which column is...

  • Tutorial: MySQL Database Design

    Learning how to use and manage MySQL and understand all its technical concepts and terminology is only the first step in learning...

  • Primary & Foreign Keys Explained

    Primary and foreign keys are the main structures that connect database tables. Primary keys are unique values in a table that identify...

  • How to Add Foreign Key Oracle SQL

    Use the ALTER TABLE command when adding a foreign key constraint in an Oracle SQL database. A foreign key is a field...

  • How to Add a Foreign Key Constraint

    Adding a foreign key constraint to a table can improve data integrity in your database application. You can use Microsoft SQL Server...

  • How to Remove Primary Key from MySql

    A primary key provides your MySQL database table with a unique indexable value. A primary key's value is always unique, or an...

  • Uses of a Foreign Key

    Uses of a Foreign Key. A foreign key, in terms of a relational database, identifies a column from one table in your...

  • How to Add Multiple Foreign Keys in SQL

    SQL Server is a relational database solution, which means you can link tables using primary and foreign keys. The primary key connects...

  • MySQL Relational Tutorial

    MySQL is a popular open-source relational database engine that is favored for its relative ease of configuration and use, its high speed...

  • How to Change a Primary Key in MySQL

    A primary key is used to identify each row in a table. It is a unique value, and means that there is...

  • How to Create Table With Foreign Key

    Comments. You May Also Like. Foreign Key MySQL Tutorial. Creating a foreign key in MySQL is a part of referential integrity in...

  • How to Make a Foreign Key

    In SQL tables, you can use foreign keys to connect similar columns in two different tables. For example, a table used to...

  • How to Create a Relational Database Without Access

    One of the most popular relational databases currently available is an open source package called MySQL. It is sponsored by the Oracle...

  • MySQL Joins Select Count Tutorial

    When using the Count statement in MySQL, the programmer may need to join a table to acquire the correct count. For instance,...

Related Ads

Featured