MySQL 在 phpMyAdmin 中添加约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9499675/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:18:36  来源:igfitidea点击:

Adding constraints in phpMyAdmin

mysqlforeign-keysphpmyadminrelational-database

提问by Alex

I feel like I'm being stupid, but I can't find anywhere on the phpMyAdmin interface to add constraints to foreign keys e.g. CASCADE ON DELETE

我觉得我很愚蠢,但我在 phpMyAdmin 界面上找不到任何地方来向外键添加约束,例如 CASCADE ON DELETE

I've looked for similar questions on here and on the phpMyAdmin wiki but I can't find anything about it.

我在这里和 phpMyAdmin wiki 上寻找过类似的问题,但我找不到任何关于它的信息。

I realise I could do this via the query interface, but I'd like to know how to do it through the graphical interface.

我意识到我可以通过查询界面来做到这一点,但我想知道如何通过图形界面来做到这一点。

回答by utsikko

First, you should have your storage engine as InnoDB. Then select a table and go to 'Structure' tab.

首先,你的存储引擎应该是 InnoDB。然后选择一个表并转到“结构”选项卡。

Under the table you will see 'Relation view', click it. From there you could add constraints.

在表格下方,您将看到“关系视图”,单击它。从那里你可以添加约束。

回答by Arun Yokesh

CASCADE

级联

Whenever rows in the master (referenced) table are deleted (resp. updated), the respective rows of the child (referencing) table with a matching foreign key column will get deleted (resp. updated) as well. This is called a cascade delete (resp. update[2]).

每当主(引用)表中的行被删除(相应更新)时,具有匹配外键列的子(引用)表的相应行也将被删除(相应更新)。这称为级联删除(或更新[2])。

RESTRICT

严格

A value cannot be updated or deleted when a row exists in a foreign key table that references the value in the referenced table. Similarly, a row cannot be deleted as long as there is a reference to it from a foreign key table.

当外键表中存在引用被引用表中的值的行时,无法更新或删除值。同样,只要有来自外键表的引用,就不能删除行。

NO ACTION

没有行动

NO ACTION and RESTRICT are very much alike. The main difference between NO ACTION and RESTRICT is that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the check before trying to execute the UPDATE or DELETE statement. Both referential actions act the same if the referential integrity check fails: the UPDATE or DELETE statement will result in an error.

NO ACTION 和 RESTRICT 非常相似。NO ACTION 和 RESTRICT 之间的主要区别在于,NO ACTION 会在尝试更改表后进行参照完整性检查。RESTRICT 在尝试执行 UPDATE 或 DELETE 语句之前进行检查。如果引用完整性检查失败,两个引用操作的行为相同:UPDATE 或 DELETE 语句将导致错误。

SET NULL

置空

The foreign key values in the referencing row are set to NULL when the referenced row is updated or deleted. This is only possible if the respective columns in the referencing table are nullable. Due to the semantics of NULL, a referencing row with NULLs in the foreign key columns does not require a referenced row.

更新或删除引用行时,引用行中的外键值将设置为 NULL。仅当引用表中的相应列可以为空时,这才是可能的。由于 NULL 的语义,在外键列中具有 NULL 的引用行不需要引用行。

回答by umutekici

Firstly, you should choose storage engine as InnoDB.

首先,您应该选择存储引擎为 InnoDB。

Follow this way: click database_name -> More -> Designer

按照这种方式:点击database_name -> More -> Designer