我有两张桌子:
CREATE TABLE IF NOT EXISTS treaties(
id INT NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
PRIMARY KEY(id)
)ENGINE=InnoDB;
和
CREATE TABLE IF NOT EXISTS items(
id INT NOT NULL AUTO_INCREMENT,
treaty INT NOT NULL,
item varchar(20),
PRIMARY KEY(id),
FOREIGN KEY (treaty) REFERENCES treaties(id)
ON UPDATE RESTRICT
ON DELETE RESTRICT
)ENGINE=InnoDB;
之后,我在每个表中插入了几行,但值treaties.id
和值items.treaty
相同。当我跑
EXPLAIN SELECT *
FROM `items`
JOIN `treaties` ON `items`.`treaty` = `treaties`.`id`
WHERE 1
我获得了:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | treaties| ALL | PRIMARY | NULL| NULL | NULL| 3 |
1 | SIMPLE | items | ALL | treaty | NULL| NULL | NULL| 4 | Using where; Using join buffer
我想如果我之间有外键items.treaty
并且treaties.id
必须使用这个键并且类型不能是 ALL。怎么了?
请帮我!
谢谢!