I have a movies Postgres database, I want to efficiently list all movies played by an actor, And list all actors in a movie.
Currently I'm using these two SELECT statements (Which I bileive there is better way to do that):
SELECT title FROM movies,castings WHERE movies.id = castings.movieid and castings.actorid = (SELECT id FROM actors where name = $name and sname = $sname);
SELECT name, sname FROM actors,castings WHERE actors.id = castings.actorid and castings.movieid = (SELECT id FROM movies where title = $title);
The Database:
CREATE SEQUENCE directors_id_seq;
CREATE TABLE directors (
id BIGINT PRIMARY KEY DEFAULT nextval('directors_id_seq'),
name TEXT,
sname TEXT
);
CREATE SEQUENCE actors_id_seq;
CREATE TABLE actors (
id BIGINT PRIMARY KEY DEFAULT nextval('actors_id_seq'),
name TEXT,
sname TEXT
);
CREATE SEQUENCE movies_id_seq;
CREATE TABLE movies (
id BIGINT PRIMARY KEY DEFAULT nextval('movies_id_seq'),
title TEXT NOT NULL,
year INT NOT NULL,
votes INT,
score INT,
director BIGINT NOT NULL REFERENCES directors (id)
);
CREATE TABLE castings (
movieid BIGINT REFERENCES movies (id),
actorid BIGINT REFERENCES actors (id),
ord INT,
PRIMARY KEY (movieid, actorid)
);
How can I list all movies played by an actor? How can I list all actors played on a movie?
-- Thanks,