1

I am starting to learn databases and I have problem with foreign key.

I have this two tables:

1) table with movies

create table movies (
    id_m  serial primary key,
    name varchar (20) NOT NULL,
    type varchar(20) NOT NULL,
    year smallint NOT NULL,
    availability smallint references availability(id_a) NOT NULL
)

2) table with availability

create table availability(
    id_a serial primary key,
    availability (varchar 20) NOT NULL,
)

Table movies looks like:

  1. Thor Blu-Ray 2012 1
  2. Avangers 2 Blu-ray 2014 2
  3. Gran Torino DVD 2008 1
  4. Titanic DVD 1998 2

Table availability looks like: 1. Available 2. unavailable

When I type this:

SELECT movies.id_n,movies.name,availability.availability 
FROM movies 
full outer join availability on movies.id_n = availability.id_a;

Availability only appears in first two columns:

  1. Thor Available
  2. Avangers 2 Unavailable
  3. Gran Torino
  4. Titanic DVD

Could you please help me how to get availability to next movies in my database?

1 Answer 1

1

Here you go:

SELECT * FROM movies 
join availability on movies.availability = availability.id_a;

Some more details: You joined on the pks (movies.id_n = availability.id_a), in most cases joins make sense along fk relationships (movies.availability = availability.id_a). This is all you missed. The other items are just clean-up.

I removed the unneeded full outer clause. See http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ for a better understanding of the types of joins.

Finally select * is the equivalent of "select all columns"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.