如何在 WAMPserver 上的 MySQL 中添加外键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10048791/
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 a foreign key in MySQL on WAMPserver?
提问by JDelage
I can't figure how to indicate that a column is a foreign key in WAMPserver. I suppose I could write the MySQL query for that, but I would think that there is also a way to do that using the user interface (PHPMyAdmin)...?
我不知道如何在 WAMPserver 中指明一列是外键。我想我可以为此编写 MySQL 查询,但我认为还有一种方法可以使用用户界面 (PHPMyAdmin)...?
回答by clexmond
Creating a foreign key constraint relies on your storage engine being set to something that can support it (such as InnoDB). In PHPMyAdmin, you can set this in "Operations" for the table with the "Storage Engine" option. Once that's complete:
创建外键约束依赖于将存储引擎设置为可以支持它的内容(例如 InnoDB)。在 PHPMyAdmin 中,您可以使用“存储引擎”选项在表的“操作”中进行设置。一旦完成:
- Make sure you've assigned an index to the column you'll be assigning a foreign key to.
- Click on "Relation view" under the table details on the "Structure" tab.
- Assign your foreign key constraint and decide on the actions for DELETE and UPDATE.
- 确保您已为要为其分配外键的列分配了索引。
- 单击“结构”选项卡上表详细信息下的“关系视图”。
- 分配您的外键约束并决定 DELETE 和 UPDATE 的操作。
回答by Ram
you can add a foreign key for the existing table by the following query
您可以通过以下查询为现有表添加外键
ALTER TABLE sample.employee
ADD FOREIGN KEY (dno)
REFERENCES sample.department(dnumber)
here sample. employee is your current table and sample .department is existing table which has the value for your foreign key dno is current table forign key and dnumber is existing table primary key.
这里示例。员工是您当前的表,示例 .department 是现有表,其中具有外键的值 dno 是当前表的外键,而 dnumber 是现有表的主键。