MySQL的外键'on delete restrict'子句的确切含义

Exact Meaning of MySQL#39;s Foreign Key #39;on delete restrict#39; Clause(MySQL的外键on delete restrict子句的确切含义)
本文介绍了MySQL的外键'on delete restrict'子句的确切含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 MySQL 表:collectionsprivacy_level.
我用 外键 关系来定义它们:

I have two MySQL tables: collections and privacy_level.
I define them with a foreign key relationship as such:

CREATE TABLE collections (
  coll_id smallint NOT NULL AUTO_INCREMENT UNSIGNED,
  name varchar(30) NOT NULL,
  privacy tinyint NOT NULL UNSIGNED DEFAULT '0',
  PRIMARY KEY(coll_id),
  INDEX(privacy),
  FOREIGN KEY fk_priv (privacy) REFERENCES privacy_level (level) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB;  

 CREATE TABLE privacy_level (
   level tinyint NOT NULL UNSIGNED,
   name varchar(20) NOT NULL,
   PRIMARY KEY (level)
 ) ENGINE InnoDB;  

我的问题是关于 ON DELETE RESTRICT 子句,我无法从在线手册或谷歌搜索中得到答案.

My question is about the ON DELETE RESTRICT clause and I couldn't derive the answer from the online manual or a google search.

这是否意味着我可以从不privacy_level 中删除一行?
或者,这是否意味着我不能privacy_level 中删除一行 如果collections.privacy 中删除一行是否具有与 privacy_level.level 中的值相同的值?

Does this mean that I can never delete a row from privacy_level?
Or, does it mean that I can't delete a row from privacy_level if a row from collections.privacy has a value that is the same as a value in privacy_level.level?

也就是说,如果 privacy_levellevel = 2name = 'top secret' 但集合中没有条目.Privacy 有 privacy = 2,我可以删除 level = 2, name = 'top secret' 条目吗?还是在列范围内禁止?

That is, if privacy_level has level = 2, name = 'top secret' but no entry in collections.Privacy has privacy = 2, can I delete the level = 2, name = 'top secret' entry? Or is it forbidden on a column wide basis?

感谢您的任何见解.

推荐答案

ON DELETE RESTRICT 表示你不能删除给定的父行 如果存在引用该父行值的子行.如果父行没有引用子行,则可以删除该父行.

ON DELETE RESTRICT means you can't delete a given parent row if a child row exists that references the value for that parent row. If the parent row has no referencing child rows, then you can delete that parent row.

ON DELETE RESTRICT 几乎是多余的语法,因为无论如何这是外键的默认行为.

ON DELETE RESTRICT is pretty much superfluous syntax, because this is the default behavior for a foreign key anyway.

这篇关于MySQL的外键'on delete restrict'子句的确切含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)