all groups > sql server (alternate) > march 2007 >
You're in the

sql server (alternate)

group:

How to Join


How to Join Dot Net Daddy
3/12/2007 7:32:13 AM
sql server (alternate):
Hello,

I have set up a database for movies. In one table (Movies) I have
movie names, and production years, and also genres. In another table
(Directors), I keep the directors and the movies they directed.
Another table (People) keeps the names of the people. Everybody will
have a unique ID. I have created a query like below to show the name
and production year of the movie, the director name and the genre of
the movie. Genres are also defined in a tabled called Genres.

SELECT Movies.Name, Movies.Year, People.Name AS Director, Genres.Genre
FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID
INNER JOIN Genres ON Movies.Genre = Genres.ID INNER JOIN People ON
Directors.ID = People.ID WHERE (Movies.ID = @MoviesID)


The problem is that it does not return any result. What might be the
problem?


Thanks in advance...
Re: How to Join Ed Murphy
3/12/2007 11:38:54 AM
[quoted text, click to view]

Based on your query, I assume that you are /not/ making the classic
violation of 1NF, which would look like:

ID | ListOfMovieIDs
---+---------------
1 | 1,2
2 | 3
3 | 3

but rather you have done it correctly:

ID | MovieID
---+--------
1 | 1
1 | 2
2 | 3
3 | 3

Personally, I would rename the ID column to DirectorID. In particular,
some tools (e.g. the Smart Linking option in Crystal Reports) will give
more useful results if you do this. Similarly for the ID columns in
the other tables.

[quoted text, click to view]

Build up the query one level at a time:

SELECT Movies.Name, Movies.Year
FROM Movies
WHERE Movies.ID = @MoviesID

If this returns zero rows, then @MoviesID is not in the Movies table.

SELECT Movies.Name, Movies.Year, Director.ID as DirectorID
FROM Movies
INNER JOIN Directors ON Movies.ID = Directors.MovieID
WHERE Movies.ID = @MoviesID

If this returns zero rows, then Movies.ID is not in the Directors
table.

SELECT Movies.Name, Movies.Year, People.Name as Director
FROM Movies
INNER JOIN Directors ON Movies.ID = Directors.MovieID
INNER JOIN People ON Directors.ID = People.ID
WHERE Movies.ID = @MoviesID

If this returns zero rows, then Directors.ID is not in the People
table. Fix all such cases, then add a foreign-key constraint to
prevent it from happening again.

SELECT Movies.Name, Movies.Year, People.Name as Director, Genres.Genre
FROM Movies
INNER JOIN Directors ON Movies.ID = Directors.MovieID
INNER JOIN People ON Directors.ID = People.ID
INNER JOIN Genres ON Movies.Genre = Genres.ID
WHERE Movies.ID = @MoviesID

If this returns zero rows, then Movies.Genre is not in the Genres
table. Fix and add constraint.

Alternatively, you can replace any/all of the INNER JOINs with
LEFT OUTER JOINs. You will then get NULLs from that branch of
the join tree, e.g. if Movies.ID is not in the Directors table
then anything you attempt to get from Directors *or* People will
Re: How to Join Dave P
3/12/2007 3:28:01 PM

Where you are joining on Directors that 1st Join There
looks like directors should be the people table...


Dave P


[quoted text, click to view]

Re: How to Join Dave P
3/12/2007 4:42:27 PM
it looks like you have many different types of peopls in ur peoples table
directors and
select * from Movies M (nolock)
join peoples d (nolock) d.directorsGroup=m.DirectorsGroup
join peoples. a (nolock) a.ActorsGroup=m.ActorsGroup
join GenRes G (nolock) g.Genres=m.Genres


above is a sample join of somthing of what your data may look like
h


[quoted text, click to view]

Re: How to Join DaveP
3/14/2007 8:11:49 PM
Im Confused about ur tables
movies MovieId(identity), other columns, DirectorId (from Directors),
genresId (from Genres)
if your table is not designd somewhat like the above, gonna be hard to link
the child tables(ref tables, directors, genres)
movie
movieid (identy)
title
genres (id from genres) could be identy in genres or another unique id
director (id from Director) could be identy in genres or another unique id
yearmade
Studio
etc

hope the above helps
DaveP
[quoted text, click to view]

Re: How to Join --CELKO--
3/16/2007 7:40:47 AM
[quoted text, click to view]

Actually, you don't; such things already exist and you can download
them.

[quoted text, click to view]

If a movie can have more than one director, then where is the
relationship table?

[quoted text, click to view]

It is nice to know you do not consider Directors to be people and put
them ina separate table :)

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

[quoted text, click to view]

What you posted is completely wrong. The data element names are too
vague to be useful and involve reserved words. You are so far off
base, you even have the magical, universal id column which changes
meaning from table to table! Tell me that you did not use an IDENTITY
in all your tables for this.

CREATE TABLE Movies
(<<industry standard if>>,
release_year INTEGER NOT NULL,
genre_code CHAR(10) NOT NULL,
etc.);

CREATE TABLE Personnel (..) -- SAG number as id?

CREATE TABLE Crew (..) -- includes role played by personnel on a movie

Start over with a relational design or your teacher will give you a
really bad grade.

[quoted text, click to view]

AddThis Social Bookmark Button