在 MySQL 中使用 REFERENCES 关键字
Use of REFERENCES keyword in MySQL
我在 MySQL 中有以下 table 定义。它表示 tables 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 的正确用法吗?如果我将 table 定义更改为以下定义,列的行为是否会有所不同?
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)
);
MySQL 仅处理属于 FOREIGN KEY
规范的 REFERENCES
子句。这是来自 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 中有以下 table 定义。它表示 tables 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 的正确用法吗?如果我将 table 定义更改为以下定义,列的行为是否会有所不同?
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)
);
MySQL 仅处理属于 FOREIGN KEY
规范的 REFERENCES
子句。这是来自 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
存储引擎)。