Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Foreign key in My Sql from phpmyadmin?

I am totally new to php and mysql. I have already one table in mysql .now i want to add foreign key. how to alter table and add foreign key using phphmyadmin?

like image 401
Ritesh Avatar asked Dec 20 '13 11:12

Ritesh


4 Answers

The form to put up the fk is under: table -> structure -> relationship overview

you need to setup index keys for the foreign keys

enter image description here

sorry this is in german but is the same position of link in other languages

like image 67
Daniel W. Avatar answered Oct 14 '22 04:10

Daniel W.


You can do it by following steps

  1. Change the storage engine of table to Inodb by going to operations section >> Table options >> Storage Engine >> Inodb.

  2. Go to table structure >> Below the structure click on Relation view.

  3. Now in the primary key column change the column which will be related to this table.

  4. Select On Delete and On Update dropdown fields as per requirement.

Wolla you are done

like image 44
Viswanath Polaki Avatar answered Oct 14 '22 02:10

Viswanath Polaki


you can easily google it.

ALTER TABLE yourtable
ADD CONSTRAINT fkey
FOREIGN KEY (P_Idq)
REFERENCES Personstable(P_Idq)

through phpmyadmin

like image 5
R R Avatar answered Oct 14 '22 02:10

R R


If you want to use phpMyAdmin to set up relations you have to do 2 things. First of all you have to define an index on the foreign key column in the referring table ( Then, go to relation view (in the referring table) and select the refereed column and the on update and on delete actions.I think foreign keys are really useful if you have multiple tables linked to one another, in particular your delete scripts will become very short if you set the referencing options correctly. Make sure both of the tables have the InnoDB engine selected.

Also refer: http://www.binarytides.com/create-foreign-key-phpmyadmin/

like image 1
Sathish D Avatar answered Oct 14 '22 03:10

Sathish D