Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在引用表中找不到引用列作为第一列出现的索引_Mysql_Sql_Foreign Keys - Fatal编程技术网

Mysql 在引用表中找不到引用列作为第一列出现的索引

Mysql 在引用表中找不到引用列作为第一列出现的索引,mysql,sql,foreign-keys,Mysql,Sql,Foreign Keys,我正在按照链接创建mysql表 但我总是犯这个错误 FOREIGN KEY (serial_no) REFERENCES tag_master(orig_serial_no) ON UPDATE CASCADE ON DELETE RESTRICT: Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in th

我正在按照链接创建mysql表

但我总是犯这个错误

FOREIGN KEY (serial_no) REFERENCES tag_master(orig_serial_no) ON UPDATE CASCADE ON DELETE RESTRICT:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
以下是表格:

create table tag_master(
        orig_part_no VARCHAR(70) CHARACTER SET ascii NOT NULL,
        orig_serial_no VARCHAR(56) CHARACTER SET ascii NOT NULL,
        regist_part_no VARCHAR(70) CHARACTER SET ascii,
        regist_serilal_no VARCHAR(56) CHARACTER SET ascii,
        regist_comment VARCHAR(126) CHARACTER SET ascii,
        PRIMARY KEY (orig_part_no, orig_serial_no)
) ENGINE=INNODB;
部件号
读卡器id
的外键工作正常,但不知何故,我无法将外键
序列号
创建为
原始序列号
。 我尝试了
altertable tag\u log ADD FOREIGN KEY(serial\u no)引用tag\u master(orig\u serial\u no)上的更新CASCADE上的DELETE RESTRICT但它仍然不起作用,没有任何意义

desc tag\u log
表如下所示:

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| part_no           | varchar(70)  | NO   | MUL | NULL    |                |
| serial_no         | varchar(56)  | NO   |     | NULL    |                |
| access_date       | date         | YES  |     | NULL    |                |
| latitude          | float        | YES  |     | NULL    |                |
| longitude         | float        | YES  |     | NULL    |                |
| reader_id         | varchar(70)  | NO   | MUL | NULL    |                |
| current_part_no   | varchar(105) | YES  |     | NULL    |                |
| current_serial_no | varchar(70)  | YES  |     | NULL    |                |
| current_comment   | varchar(128) | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
操作系统: Ubuntu 18.04

MySQL:
mysql版本14.14发行版5.7.29,适用于Linux(x86_64),使用EditLine包装器
服务器版本:5.7.29-0ubuntu0.18.04.1(Ubuntu)
此外键

FOREIGN KEY (serial_no) 
    REFERENCES tag_master(orig_serial_no) 
    ON UPDATE CASCADE ON DELETE RESTRICT
。。。需要在
标记主控(原始序列号)
上建立索引,或者至少在
原始序列号
首先出现的位置建立复合索引。在当前设置中,情况并非如此,
标记主控
的主键是
(零件号,序列号)
(相关列显示在第二个位置,而不是第一个位置)

解决此问题的一种方法是更改
tag\u master
主键中列的顺序:

PRIMARY KEY (orig_serial_no, orig_part_no)
这要求您还更改
tag\u log
中引用两列的复合外键中的列顺序:

FOREIGN KEY(serial_no, part_no) 
    REFERENCES tag_master(orig_serial_no, orig_part_no) 
    ON UPDATE CASCADE ON DELETE RESTRICT
然后可以创建附加外键,如中所示


但是,也就是说,我不认为创建这个额外的外键有什么意义,因为您已经有了另一个覆盖它的键
(外键(序列号,部分号)
)。很可能,您不需要额外的约束,因为它提供的功能完全由复合键提供。

适用于我。。。我也试过在线编译器,但它不能在我的机器上运行。我的是mysql版本14.14发行版5.7.29,用于Linux(x86_64),使用EditLine包装器
PRIMARY KEY (orig_serial_no, orig_part_no)
FOREIGN KEY(serial_no, part_no) 
    REFERENCES tag_master(orig_serial_no, orig_part_no) 
    ON UPDATE CASCADE ON DELETE RESTRICT