我正在开发一个数据库来模拟邮件订购电影服务。目前,我对此查询有疑问:
select *
from items i, casted_by c
where i.itemID = c.itemID
我的表定义如下:
CREATE TABLE Items(
itemID VARCHAR(15),
released_date DATE,
number_copies_available INT,
title VARCHAR(50),
PRIMARY KEY(itemID)
);
CREATE TABLE Casted_By(
castID VARCHAR(15),
itemID VARCHAR(15),
PRIMARY KEY(castID, itemID),
FOREIGN KEY (castID) REFERENCES Actors(castID)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (itemID) REFERENCES Movies(itemID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
我已使用包含
的CSV文件填充这些表格项目中的这些:
300000,2008/05/23,5,Iron Man 1
300001,2010/05/13,15,Iron Man 2
300002,2012/06/20,12,Iron Man 3
300003,2009/01/22,5,Hangover 1
300004,2011/05/01,15,Hangover 2
300005,2013/06/29,10,Hangover 3
300006,1997/08/14,15,Titanic
300007,2014/09/06,5,Intersteller
300008,2014/02/19,20,Birdman
300009,2014/04/14,20,American Sniper
这些在Casted_By中:
700000,300000
700000,300001
700000,300002
700001,300003
700001,300004
700001,300005
700001,300009
700002,300003
700002,300004
700002,300005
700003,300003
700003,300004
700003,300005
700004,300006
700004,300023
700004,300026
700005,300007
700006,300008
700007,300011
700008,300010
700009,300012
700010,300014
700011,300015
700012,300020
700013,300017
700013,300018
700013,300019
700014,300017
700014,300018
700014,300019
700015,300016
700016,300021
700016,300008
700017,300022
700018,300023
700018,300029
700019,300024
执行查询时,只显示一行! (碰巧是凯特温斯莱特在泰坦尼克号铸造)。但是很多其他的应该出现,就像第一个(钢铁侠中的小罗伯特唐尼)。
奇怪的是,如果我尝试
select *
from Casted_By c
where c.itemID = 300000
我返回了正确的结果,但是如果我尝试
select *
from Casted_By c
where c.itemID = '300000'
查询返回null!
如果我用c.castID替换c.itemID并使用700000,无论是否有单引号,这都不是同样的情况。