我在 MySQL 中有以下表定义。它表示表Licenses 和License 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 acceptsREFERENCES
clauses only when specified as part of a separateFOREIGN KEY
specification.
因此,第一个版本不会强制执行外键关系,第二个版本会强制执行(假设您使用的是 InnoDB
存储引擎)。
关于mysql - MySQL中REFERENCES关键字的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33256965/