在 MySQL 中,通过 FK 什么是“约束”?做?

发布于 2024-10-06 03:02:46 字数 1914 浏览 3 评论 0原文

我查看了 MySQL 5.1 文档,但仍然无法弄清楚这一点——因为我注意到输入 MySQL 的代码和系统输出的代码之间存在差异。

代码示例 01 和 02 之间有什么区别,即 02 在 FOREIGN KEY 之前添加了 CONSTRAINT - 为什么,它有什么作用?

CODE_SAMPLE_01:

FOREIGN KEY (TABLE_02_nID_FK__TABLE_01_sID_PK) REFERENCES TABLE_01(TABLE_01_sID_PK),

上下文:

CREATE TABLE `TABLE_02` (
  `TABLE_02_sID_PK` int(8) NOT NULL,
  `TABLE_02_nID_FK__TABLE_01_sID_PK` int(8) NOT NULL,
  `TABLE_02_cID` int(8) NOT NULL,
  `TABLE_02_data01` varchar(128) default NULL,
  `TABLE_02_data02` varchar(128) NOT NULL,
  `create_timestamp` DATETIME DEFAULT NULL,
  `update_timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`TABLE_02_sID_PK`),
  FOREIGN KEY (TABLE_02_nID_FK__TABLE_01_sID_PK) REFERENCES TABLE_01(TABLE_01_sID_PK),
  INDEX `TABLE_02_nID_FK__TABLE_01_sID_PK` (`TABLE_02_nID_FK__TABLE_01_sID_PK`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CODE_SAMPLE_02:

CONSTRAINT `table_02_ibfk_1` FOREIGN KEY (`TABLE_02_nID_FK__TABLE_01_sID_PK`) REFERENCES `table_01` (`TABLE_01_sID_PK`)

上下文:

CREATE TABLE `table_02` (
  `TABLE_02_sID_PK` int(8) NOT NULL,
  `TABLE_02_nID_FK__TABLE_01_sID_PK` int(8) NOT NULL,
  `TABLE_02_cID` int(8) NOT NULL,
  `TABLE_02_data01` varchar(128) DEFAULT NULL,
  `TABLE_02_data02` varchar(128) NOT NULL,
  `create_timestamp` datetime DEFAULT NULL,
  `update_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`TABLE_02_sID_PK`),
  KEY `TABLE_02_nID_FK__TABLE_01_sID_PK` (`TABLE_02_nID_FK__TABLE_01_sID_PK`),
  CONSTRAINT `table_02_ibfk_1` FOREIGN KEY (`TABLE_02_nID_FK__TABLE_01_sID_PK`) REFERENCES `table_01` (`TABLE_01_sID_PK`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I've looked at the MySQL 5.1 docs, and still can't figured this out -- that being I noticed a difference between the code I input into MySQL and output code by the system.

What is the difference between the code sample 01 and 02, meaning 02 has added CONSTRAINT before FOREIGN KEY -- why, and what's it do?

CODE_SAMPLE_01:

FOREIGN KEY (TABLE_02_nID_FK__TABLE_01_sID_PK) REFERENCES TABLE_01(TABLE_01_sID_PK),

CONTEXT:

CREATE TABLE `TABLE_02` (
  `TABLE_02_sID_PK` int(8) NOT NULL,
  `TABLE_02_nID_FK__TABLE_01_sID_PK` int(8) NOT NULL,
  `TABLE_02_cID` int(8) NOT NULL,
  `TABLE_02_data01` varchar(128) default NULL,
  `TABLE_02_data02` varchar(128) NOT NULL,
  `create_timestamp` DATETIME DEFAULT NULL,
  `update_timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`TABLE_02_sID_PK`),
  FOREIGN KEY (TABLE_02_nID_FK__TABLE_01_sID_PK) REFERENCES TABLE_01(TABLE_01_sID_PK),
  INDEX `TABLE_02_nID_FK__TABLE_01_sID_PK` (`TABLE_02_nID_FK__TABLE_01_sID_PK`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CODE_SAMPLE_02:

CONSTRAINT `table_02_ibfk_1` FOREIGN KEY (`TABLE_02_nID_FK__TABLE_01_sID_PK`) REFERENCES `table_01` (`TABLE_01_sID_PK`)

CONTEXT:

CREATE TABLE `table_02` (
  `TABLE_02_sID_PK` int(8) NOT NULL,
  `TABLE_02_nID_FK__TABLE_01_sID_PK` int(8) NOT NULL,
  `TABLE_02_cID` int(8) NOT NULL,
  `TABLE_02_data01` varchar(128) DEFAULT NULL,
  `TABLE_02_data02` varchar(128) NOT NULL,
  `create_timestamp` datetime DEFAULT NULL,
  `update_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`TABLE_02_sID_PK`),
  KEY `TABLE_02_nID_FK__TABLE_01_sID_PK` (`TABLE_02_nID_FK__TABLE_01_sID_PK`),
  CONSTRAINT `table_02_ibfk_1` FOREIGN KEY (`TABLE_02_nID_FK__TABLE_01_sID_PK`) REFERENCES `table_01` (`TABLE_01_sID_PK`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2024-10-13 03:02:47

可选的 CONSTRAINT 关键字允许您指定外键的名称。没有它,将自动生成一个名称。

此名称可以在 INFORMATION_SCHEMA TABLE_CONSTRAINTS 中看到桌子。

The optional CONSTRAINT keyword allows you to specify a name for the foreign key. Without it, a name will be generated automatically.

This name can be seen in the INFORMATION_SCHEMA TABLE_CONSTRAINTS table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文