I'm a beginner in Oracle (using SQL Developer) and I decided to go ahead and start a small project just to test myself. 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.
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).
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.
您可能想要加入。
SELECT * FROM Film INNER JOIN Actors ON Film.Actor_ID = Actors.Actor_ID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.