简体   繁体   English

SQL Developer-查询返回重复属性

[英]SQL Developer - Queries Returning Repeating Attributes

I'm a beginner in Oracle (using SQL Developer) and I decided to go ahead and start a small project just to test myself. 我是Oracle的初学者(使用SQL Developer),所以我决定继续进行一个小项目,以测试自己。 I have created 5 tables and inserted values in all of them, but when I run queries it returns me values that are not even supposed to be there so I'm not sure where I'm wrong. 我已经创建了5个表并在所有表中插入了值,但是当我运行查询时,它返回的值甚至不应该存在,所以我不确定我哪里写错了。

Here is my code: 这是我的代码:

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');

So after all of this even a simple Query such as 所以毕竟,即使是一个简单的查询,例如

SELECT Actor_fName, Actor_sName, Film_Title
FROM Actors, Film

It will return me all the names and films with repeating values (eg Leonardo DiCaprio name will appear together with "Never Let Me Go" even though he is not in the movie) 它将返回所有具有重复值的名称和电影(例如,莱昂纳多·迪卡普里奥的名字将与“永不放手”一起出现,即使他不在电影中也是如此)

Hopefully there is not much I need to edit and I won't waste your time, but I would be very thankful if you responded back with some clues/answers! 希望我不需要做太多编辑,并且我不会浪费您的时间,但是如果您以一些线索/答案作答,我将非常感谢!

Your query is a cross join rather than an inner join. 您的查询是交叉联接,而不是内部联接。

A cross join will return every row matched with every other row. 交叉联接将返回与其他行匹配的每一行。 An inner join will return only the rows that contain the relationship you specify in the on table1.columnid = table2.columnid (or a where clause that defines the relationship). 内部联接将仅返回包含您在table1.columnid = table2.columnid(或定义该关系的where子句)中指定的关系的行。

For example: 例如:

Your query 您的查询

SELECT Actor_fName, Actor_sName, Film_Title
FROM Actors, Film

will return all actor names and all film titles in all possible combinations. 将以所有可能的组合返回所有演员姓名和所有电影标题。

If, instead, you use 相反,如果您使用

SELECT Actor_fName, Actor_sName, Film_Title
FROM Actors INNER JOIN Film 
ON Film.Actor_ID = Actors.Actor_ID

You'll only get the movies where you've stated that the actor is in the movie. 您只能在声明演员在电影中的地方观看电影。

Here's a link that might help you understand joins. 这是一个可以帮助您理解联接的链接。

您可能想要加入。

SELECT * FROM Film INNER JOIN Actors ON Film.Actor_ID = Actors.Actor_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM