php 如何从phpmyadmin在My Sql中添加外键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20703108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add Foreign key in My Sql from phpmyadmin?
提问by Ritesh
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?
我对 php 和 mysql 完全陌生。我已经在 mysql 中有一张表。现在我想添加外键。如何使用phphmyadmin更改表和添加外键?
采纳答案by Daniel W.
The form to put up the fk is under: table -> structure -> relationship overview
挂fk的表格如下:表->结构->关系概览
you need to setup index keys for the foreign keys
您需要为外键设置索引键


sorry this is in german but is the same position of link in other languages
抱歉,这是德语,但在其他语言中的链接位置相同
回答by Viswanath Polaki
You can do it by following steps
您可以按照以下步骤操作
Change the storage engine of table to Inodb by going to operations section >> Table options >> Storage Engine >> Inodb.
Go to table structure >> Below the structure click on Relation view.
Now in the primary key column change the column which will be related to this table.
Select On Delete and On Update dropdown fields as per requirement.
通过转到操作部分>>表选项>>存储引擎>>Inodb将表的存储引擎更改为Inodb。
转到表结构 >> 在结构下方单击关系视图。
现在在主键列中更改将与此表相关的列。
根据要求选择“删除时”和“更新时”下拉字段。
Wolla you are done
沃拉你完成了
回答by R R
you can easily google it.
你可以很容易地谷歌它。
ALTER TABLE yourtable
ADD CONSTRAINT fkey
FOREIGN KEY (P_Idq)
REFERENCES Personstable(P_Idq)
回答by Sathish D
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.
如果您想使用 phpMyAdmin 建立关系,您必须做两件事。首先,您必须在引用表中的外键列上定义一个索引(然后,转到关系视图(在引用表中)并选择被引用的列以及更新和删除操作。我认为外键是如果你有多个表相互链接,真的很有用,特别是如果你正确设置引用选项,你的删除脚本将变得非常短。确保两个表都选择了 InnoDB 引擎。
Also refer: http://www.binarytides.com/create-foreign-key-phpmyadmin/
另请参阅:http: //www.binarytides.com/create-foreign-key-phpmyadmin/

