我是Oracle的初学者(使用SQL Developer),我决定继续开始一个小项目来测试自己。我创建了5个表并在所有表中插入了值,但是当我运行查询时,它返回的值甚至不应该存在,所以我不确定我错在哪里。
这是我的代码:
CREATE table Directors
(
Director_ID NUMBER(10) NOT NULL PRIMARY KEY,
Genre_ID NUMBER(10),
Director_fName VARCHAR2(20) NOT NULL,
Director_lName VARCHAR2(20) NOT NULL,
FOREIGN KEY (Genre_ID) REFERENCES Details (Genre_ID)
);
CREATE table Actors
(
Actor_ID NUMBER(10) NOT NULL PRIMARY KEY,
Film_ID NUMBER(10),
Actor_fName VARCHAR2(20) NOT NULL,
Actor_lName VARCHAR2(20) NOT NULL,
FOREIGN KEY (Film_ID) REFERENCES Film (Film_ID)
);
CREATE table Film
(
Film_ID NUMBER(10) NOT NULL PRIMARY KEY,
Actor_ID NUMBER(10),
Film_Len CHAR(5) NOT NULL,
Film_Title VARCHAR2(30) NOT NULL,
FOREIGN KEY (Actor_ID) REFERENCES Actors (Actor_ID)
);
CREATE table Details
(
Genre_ID NUMBER(10) NOT NULL PRIMARY KEY,
Director_ID NUMBER(10),
Genre VARCHAR2(30) NOT NULL,
Year_Released DATE,
FOREIGN KEY (Director_ID) REFERENCES Directors (Director_ID)
);
CREATE table Studio
(
Studio_ID NUMBER(10) NOT NULL PRIMARY KEY,
Studio_name VARCHAR2(15) NOT NULL,
);
INSERT INTO Directors VALUES(1,'Martin','Scorsese');
INSERT INTO Directors VALUES(2,'Baz','Luhrmann');
INSERT INTO Directors VALUES(3,'Mark','Romanek');
INSERT INTO Actors VALUES(1,'Matthew','McConnaughy');
INSERT INTO Actors VALUES(2,'Leonardo','DiCaprio');
INSERT INTO Actors VALUES(3,'Margot','Robbie');
INSERT INTO Actors VALUES(4,'Joanna','Lumley');
INSERT INTO Actors VALUES(5,'Carey','Mulligan');
INSERT INTO Actors VALUES(6,'Tobey','Maguire');
INSERT INTO Actors VALUES(7,'Joel','Edgerton');
INSERT INTO Film VALUES(1,180,'The Wolf of Wall Street');
INSERT INTO Film VALUES(2,143,'The Great Gatsby');
INSERT INTO Film VALUES(3,103,'Never Let Me Go');
INSERT INTO Details VALUES(1,'Comedy',2013);
INSERT INTO Details VALUES(2,'Romance',2013);
INSERT INTO Details VALUES(3,'Science Fiction',2008);
INSERT INTO Studio VALUES(1,'Paramount');
INSERT INTO Studio VALUES(2,'Warner Bros');
INSERT INTO Studio VALUES(3,'Film4');
所以这毕竟是一个简单的查询,比如
SELECT Actor_fName, Actor_sName, Film_Title
FROM Actors, Film
它将以重复的价值回报我所有的名字和电影(例如Leonardo DiCaprio的名字将与“Never Let Me Go”一起出现,即使他不在电影中)
希望我没有太多需要编辑的东西,我不会浪费你的时间,但如果你回复一些线索/答案我会非常感激!
答案 0 :(得分:4)
您的查询是交叉联接而不是内部联接。
交叉连接将返回与每隔一行匹配的每一行。内部联接将仅返回包含您在on table1.columnid = table2.columnid(或定义关系的where子句)中指定的关系的行。
例如:
您的查询
SELECT Actor_fName, Actor_sName, Film_Title
FROM Actors, Film
将以所有可能的组合返回所有演员姓名和所有电影片名。
相反,如果您使用
SELECT Actor_fName, Actor_sName, Film_Title
FROM Actors INNER JOIN Film
ON Film.Actor_ID = Actors.Actor_ID
你只会在你所说的演员在电影中的地方看电影。
答案 1 :(得分:0)
您可能想要加入。
SELECT * FROM Film INNER JOIN Actors ON Film.Actor_ID = Actors.Actor_ID