mysql - MySQL中REFERENCES关键字的使用

标签 mysql reference foreign-keys many-to-many foreign-key-relationship

我在 MySQL 中有以下表定义。它表示表LicensesLicense Server 之间的M:N 关系。

create table if not exists  served_license
(
    served_license_id       smallint            not null,
    version                 varchar(16)         not null,
    quantity                smallint            not null,
    expiry_date             char(16)            not null,
    license_server_id       tinyint             not null    references  license_server(license_server_id),
    license_id              varchar(64)         not null    references  license(feature, product),
    primary key (served_license_id)
);

这是关键字REFERENCES的正确用法吗?如果我将表定义更改为下面的定义,列的行为是否会有所不同?

create table if not exists  served_license
(
    served_license_id       smallint            not null,
    version                 varchar(16)         not null,
    quantity                smallint            not null,
    expiry_date             char(16)            not null,
    license_server_id       tinyint             not null,
    license_id              varchar(64)         not null,
    primary key (served_license_id),
    foreign key (license_server_id) references  license_server(license_server_id),
    foreign key (license_id) references license(feature, product)
);

最佳答案

REFERENCES子句属于FOREIGN KEY规范时,MySQL仅处理它们。这是 manual 的引用:

MySQL parses but ignores “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification.

因此,第一个版本不会强制执行外键关系,第二个版本会强制执行(假设您使用的是 InnoDB 存储引擎)。

关于mysql - MySQL中REFERENCES关键字的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33256965/

相关文章:

java - 使用 protected block 同步 Hibernate 插入

mysql - SQL中使用SET计算百分比时出现错误 "operand should contain 1 column(s)"

mysql - 为什么我的数据被截断了?警告 |第1265章列的数据被截断

javascript - 如何将查询响应分配给 global.arrays,而不是 global.arrays 数组中的值?

django - 除了使用通用外键来处理类似的模型树之外,还有其他方法吗?

mysql - 如何在 Electron 中实现一个 promise-mysql 连接池

.net - SpecificVersion false 导致编译错误

javascript - 为什么这种变量赋值方法会引起问题?

MySQL 外键,无法创建表(errno : 150)

sql-server - 是否有一种直接的方法可以使用 SMO 确定外键关系的基数?