How to Create Foreign Key in PHPMyAdmin


Today we will see How to Create a Foreign key in PHPMyAdmin.

For that you have to make two tables, in which the primary key has been given to the first table, in the second table we will apply the foreign key, I will understand you in step by step details below.

How to Create Foreign Key

Step 1: Create First Table

CREATE TABLE categories(
cat_id int not null auto_increment primary key,
cat_name varchar(255) not null,
cat_description varchar(250)
) ENGINE=InnoDB;

Step 2: Create Second Table

CREATE TABLE products(
   prd_id int not null auto_increment primary key,
   prd_name varchar(355) not null,
   prd_price decimal,
   cat_id int not null,
   FOREIGN KEY fk_cat(cat_id)
   REFERENCES categories(cat_id)
   ON UPDATE CASCADE
   ON DELETE RESTRICT
)ENGINE=InnoDB;

Step 3: Create some Category Table in the first table

Step 4: Open the Second Table and select your Category

Result:


Leave a Comment