How to use phpMyAdmin to set up foreign keys
A foreign key is a database relationship that links a column in one table with a column in another table to ensure the integrity of the data sex and consistency. Here is a step-by-step guide to setting up foreign keys using phpMyAdmin:
Step 1: Create the main and subtables
First, you need to create two tables: main and subtables surface. The main table will contain the parent data and the child table will contain the data associated with the parent table.
Step 2: Open phpMyAdmin
Log in to the phpMyAdmin dashboard.
Step 3: Select the main table
In the left panel, select the main table where you want to create the foreign key.
Step 4: Open the Structure tab
At the bottom of the main table, click the Structure tab.
Step 5: Add Foreign Key Column
In the "Add Foreign Key" section, enter the subtable name and foreign key column.
Step 6: Select the associated column in the child table
In the "Subtable Foreign Key" field, select the column in the child table that will be related to the main table Column association in .
Step 7: Select the foreign key type
In the "Foreign key type" drop-down menu, select the foreign key type you want to use. Typically, you would select "CASCADE", which means that when data in the main table is deleted or updated, related data in the child table will also be deleted or updated.
Step 8: Save changes
Click the Save button to save the foreign key settings.
Example:
Suppose you have a main table called "Customers" with the following columns:
There is also a subtable called "Order" with the following columns:
To set up a foreign key, you can add a foreign key column named "customer_id" in the "Customer" table, linking it to " customer_id" column. This will ensure that each order is associated with a customer, and if customer data is deleted, the corresponding order data will also be deleted.
The above is the detailed content of How to set foreign keys in phpmyadmin. For more information, please follow other related articles on the PHP Chinese website!