num*_*far 55 mysql indexing foreign-keys
我在创建mysql数据库中现有表的外键时遇到了一些问题.
我有桌子exp:
+-------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| EID | varchar(45) | NO | PRI | NULL | |
| Comment | text | YES | | NULL | |
| Initials | varchar(255) | NO | | NULL | |
| ExpDate | date | NO | | NULL | |
| InsertDate | date | NO | | NULL | |
| inserted_by | int(11) unsigned | YES | MUL | NULL | |
+-------------+------------------+------+-----+---------+-------+
Run Code Online (Sandbox Code Playgroud)
我不想sample_df使用以下内容创建一个名为referencing this 的新表:
CREATE TABLE sample_df (
df_id mediumint(5) unsigned AUTO_INCREMENT primary key,
sample_type mediumint(5) unsigned NOT NULL,
df_10 BOOLEAN NOT NULL,
df_100 BOOLEAN NOT NULL,
df_1000 BOOLEAN NOT NULL,
df_above_1000 BOOLEAN NOT NULL,
target INT(11) unsigned NOT NULL,
assay MEDIUMINT(5) unsigned zerofill NOT NULL,
insert_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
inserted_by INT(11) unsigned NOT NULL,
initials varchar(255),
experiment VARCHAR(45),
CONSTRAINT FOREIGN KEY (inserted_by) REFERENCES user (iduser),
CONSTRAINT FOREIGN KEY (target) REFERENCES protein (PID),
CONSTRAINT FOREIGN KEY (sample_type) REFERENCES sample_type (ID),
CONSTRAINT FOREIGN KEY (assay) REFERENCES assays (AID),
CONSTRAINT FOREIGN KEY (experiment) REFERENCES exp (EID)
);
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
ERROR 1215 (HY000): Cannot add foreign key constraint
Run Code Online (Sandbox Code Playgroud)
为了获得更多信息,我做了:
SHOW ENGINE INNODB STATUS\G
Run Code Online (Sandbox Code Playgroud)
我得到了:
FOREIGN KEY (experiment) REFERENCES exp (EID)
):
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.
Run Code Online (Sandbox Code Playgroud)
对我来说,列类型似乎匹配,因为它们都是varchar(45).(我也尝试将experiment列设置为非null,但这并没有解决它)所以我想问题必须是这样Cannot find an index in the referenced table where the referenced columns appear as the first columns.但我不太清楚这意味着什么,或者如何检查/修复它.有没有人有什么建议?这是什么意思first columns?
解:
我想出了我的问题.我对exp表(utf8_unicode_ci)有一个不同的排序规则,然后是数据库的默认排序(utf8_general_ci).所以加入
+-------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| EID | varchar(45) | NO | PRI | NULL | |
| Comment | text | YES | | NULL | |
| Initials | varchar(255) | NO | | NULL | |
| ExpDate | date | NO | | NULL | |
| InsertDate | date | NO | | NULL | |
| inserted_by | int(11) unsigned | YES | MUL | NULL | |
+-------------+------------------+------+-----+---------+-------+
Run Code Online (Sandbox Code Playgroud)
在表创建查询结束时修复了我的问题.
但我仍然对这是什么意思感到好奇exp.它与索引有关吗?
Aus*_*gen 112
只是把它扔进可能原因的混合中,当参考表列具有相同的"类型"但没有相同的签名时,我遇到了这个问题.
在我的例子中,引用的表colum是TINYINT UNSIGNED,我的引用表列是TINYINT SIGNED.对齐两列都解决了这个问题.
Ant*_*ton 20
根据http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html
MySQL需要外键和引用键的索引,以便外键检查可以快速,不需要表扫描.在引用表中,必须有一个索引,其中外键列以相同的顺序列为第一列.
InnoDB允许外键引用任何索引列或列组.但是,在引用的表中,必须有一个索引,其中引用的列被列为相同顺序的第一列.
因此,如果引用表中的索引存在并且它由多个列组成,并且所需的列不是第一个,则应该发生错误.
我们错误的原因是由于违反了以下规则:
外键和引用键中的相应列必须具有相似的数据类型.整数类型的大小和符号必须相同.字符串类型的长度不必相同.对于非二进制(字符)字符串列,字符集和排序规则必须相同.
ant*_*kiy 12
如@Anton所述,这可能是因为数据类型不同。在我的情况下,我有主键BIGINT(20)并尝试使用INT(10)设置四键
小智 11
我的是引用表和要创建的表之间的排序规则问题,所以我必须明确设置我引用的键的排序规则类型。
show table STATUS like '<table_name_here>';
Run Code Online (Sandbox Code Playgroud)
CREATE TABLE dbo.sample_db
(
id INT PRIMARY KEY AUTO_INCREMENT,
event_id INT SIGNED NOT NULL,
employee_id varchar(45) COLLATE utf8_general_ci NOT NULL,
event_date_time DATETIME,
CONSTRAINT sample_db_event_event_id_fk FOREIGN KEY (event_id) REFERENCES event (event_id),
CONSTRAINT sample_db_employee_employee_id_fk FOREIGN KEY (employee_id) REFERENCES employee (employee_id)
);
Run Code Online (Sandbox Code Playgroud)
小智 7
对我来说,这只是数据库的字符集和排序规则。我更改为 utf8_unicode_ci 并且有效
| 归档时间: |
|
| 查看次数: |
37717 次 |
| 最近记录: |