Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
capella07
Starting Member
46 Posts |
Posted - 2007-06-16 : 15:05:37
|
| Hi,What I'm trying to do seems kind of complicated to me, so I'll try to do my best in explaining.I am building a Windows app in Visual Studio 2005 to add, view, delete, etc. movies. I have a database in SQL Server Express 2005 set up with tables for actors, genres, directors, and, of course, the movies themselves. In the movies table I have columns for the ID in the respective table for the movie's actors, director, and genre.I need some help in getting started in building a SELECT statement first to just display a movie's info, then after I get that going, I'm going to need to put together INSERT, UPDATE, and DELETE statements.So, for a select statement, in the movies table I have the record ID, title, director, actor1, actor2, genre. And in the director table I have a director' name in a record and that record's ID number is what is in the director field in the movies table. Likewise I have the genre's record ID from the genre table, in the movie's genre field. And the actors also.I'd appreciate it if someone could help me get started on a SELECT statement that does a select on the movies table and displays the director's and actors' names and the genre, rather than their ID from the movies table.Thanks!=====================================f u cn rd ths, u cn gt a gd jb n prgrmng |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-16 : 16:23:58
|
Assuming this is your table structure ( you may want to standardize this if it is going to be a large applications)MOVIES(MovieID,Title,DirectorID,Actor1,Actor2,GenreID)Genres(GenreID,GenreName)Directors(DirectorID,DirectorName)Select m.MovieID,m.Title,d.DirectorName,m.Actor1,m.Actor2,g.GenreNamefrom Movies m inner Join Directors don m.DirectorID = d.DirectorIDInner Join Genres gon m.GenreId = g.GenreID --A update statement needs to be ran against a individual tableUpdate mset m.DirectorID = D.DirectorIDfrom Movies m Cross join Direcectors dwhere m.Title = 'Jaws'and d.DirectorName = 'Steven Spielburg' --Notice the join I used was a Cross Join and not a Inner Join This allowed me to referance every --director ID for Spielburg. Please keep in mind this is a example and if I was actualy trying to--accomplish what I did above I would use a differant method. But this should illustrate to you --how to join tables and update with joins. |
 |
|
|
capella07
Starting Member
46 Posts |
Posted - 2007-06-17 : 10:07:40
|
Thanks, Vinnie881Here's the SELECT I finally ended up with:SELECT m.mov_Title AS Title, m.mov_Length AS [Length in Minutes], m.mov_YrReleased AS [Year Released], g.gen_Genre AS Genre, d.dir_FName + d.dir_LName AS Director, a1.act_FName + a1.act_LName AS [Starring Actor], a2.act_FName + a2.act_LName AS [Co-Starring Actor], a3.act_FName + a3.act_LName AS [Supporting Actor], a4.act_FName + a4.act_LName AS [Supporting Actor]FROM Movies AS m INNER JOIN Genres AS g ON m.mov_GenreID = g.UID_Genres INNER JOIN Directors AS d ON m.mov_DirectorID = d.UID_Directors INNER JOIN Actors AS a1 ON m.mov_ActorID1 = a1.UID_Actors LEFT JOIN Actors AS a2 ON m.mov_ActorID2 = a2.UID_Actors LEFT JOIN Actors AS a3 ON m.mov_ActorID3 = a3.UID_Actors LEFT JOIN Actors AS a4 ON m.mov_ActorID4 = a4.UID_Actors It took me a (long) while to figure out that I had to use LEFT JOIN for the subsequent actor fields, as they may not have values in the Movies table (less than four actors).I understand that the UPDATE example you used was just an illustration, but I guess I'm still not seeing how to build an UPDATE or INSERT using the JOINs.Could you (or anyone) provide a more thorough example based on the info I have in my SELECT above?Thanks a lot for your help!=====================================f u cn rd ths, u cn gt a gd jb n prgrmng |
 |
|
|
capella07
Starting Member
46 Posts |
Posted - 2007-06-17 : 11:11:55
|
Okay, been doing some brainstorming and here's what I came up with from my (very) limited T-SQL knowledge.While looking at my tables I just wrote down in pseudo-code what I was trying to do and this is what I came up with for an INSERT:INSERT INTO Movies (title, length, year, (get UID_Genres value from Genres table matching user-entered genre name & put that into mov_GenreID column in movies table), (get UID_Directors value from Directors table matching user-entered director name & put that into mov_DirectorID column in movies table), (get UID_Actors value from Actors table matching user-entered actor name & put that into mov_ActorID column in movies table (repeat this four times - once for each actor column in movies table))VALUES (@title, @length, @year, @genre, @director, @actor1, @actor2, @actor3, @actor4) I may be waaaay off base here, but in my mind and from what I understand that's essentially what I'm trying to do, but I'm not exactly sure how to implement that...Of course, then there's the issue of whether the actor, director, or genre actually exists in the respective table or not! Boy this could get quite complicated!Any suggestions?Thanks!=====================================f u cn rd ths, u cn gt a gd jb n prgrmng |
 |
|
|
capella07
Starting Member
46 Posts |
Posted - 2007-06-17 : 12:21:59
|
Nothing like having a conversation with yourself! (I look at it as thinking out loud )Well, the code below doesn't work, but I came up with it thinking I need to do something along those lines. Who knows, maybe I need to do some UNION statement or something, but I don't know the first thing about that stuff - I'm just getting how joins work!Anyway, here's the code:INSERT INTO Movies (mov_Title, mov_Length, mov_YrReleased, (SELECT g.UID_Genres FROM Genres g WHERE g.gen_Genre = @genre), (SELECT d.UID_Directors FROM Directors d WHERE d.dir_LName = @director), (SELECT a.UID_Actors FROM Actors a WHERE a.act_LName = @actor1) (SELECT a.UID_Actors FROM Actors a WHERE a.act_LName = @actor2) (SELECT a.UID_Actors FROM Actors a WHERE a.act_LName = @actor3) (SELECT a.UID_Actors FROM Actors a WHERE a.act_LName = @actor4) INNER JOIN Genres AS g ON m.mov_GenreID = g.UID_Genres INNER JOIN Directors AS d ON m.mov_DirectorID = d.UID_Directors INNER JOIN Actors AS a1 ON m.mov_ActorID1 = a1.UID_Actors LEFT JOIN Actors AS a2 ON m.mov_ActorID2 = a2.UID_Actors LEFT JOIN Actors AS a3 ON m.mov_ActorID3 = a3.UID_Actors LEFT JOIN Actors AS a4 ON m.mov_ActorID4 = a4.UID_Actors --I pretty much did the joins based on my SELECT statement I have in an earlier post...VALUES (@title, @length, @year, @genre, @director, @actor1, @actor2, @actor3, @actor4) Am I close, or way out in left field?!?! Thanks!=====================================f u cn rd ths, u cn gt a gd jb n prgrmng |
 |
|
|
capella07
Starting Member
46 Posts |
Posted - 2007-06-17 : 15:49:37
|
Okay, more "thinking out loud":Working on inserting a value in just one column in the Movies table - the Genres column for instance - I guess I want to do something like this: INSERT INTO Movies (mov_GenreID)VALUES (SELECT uid_genre FROM genres WHERE gen_genre = 'Science Fiction') I KNOW that code won't work (tried it!) but essentially what I want to do is, the value I want to insert into the mov_GenreID column in the Movies table is whatever the ID number is for 'Science Fiction' in the Genres table.Put that way, how do I do that?Anyone? (I'm getting tired of having a conversation with myself here and I'd really like to get this working!)Thanks for any help/suggestions!=====================================f u cn rd ths, u cn gt a gd jb n prgrmng |
 |
|
|
capella07
Starting Member
46 Posts |
Posted - 2007-06-17 : 16:46:22
|
Update... (This is becoming more like a journal than a forum discussion! Hopefully, though, my ramblings will help someone else.)Okay - I'm definitely getting closer! Here's what I have now:DECLARE @title nvarchar(50),@length int,@year int,@genre nvarchar(50),@director nvarchar(50),@actor1 nvarchar(50),@actor2 nvarchar(50),@actor3 nvarchar(50),@actor4 nvarchar(50)SET @title = 'War of the Worlds'SET @length = 142SET @year = 2005SET @genre = 'Science Fiction'SET @director = 'Spielberg'SET @actor1 = 'Cruise'SET @actor2 = ''SET @actor3 = ''SET @actor4 = ''INSERT INTO Movies (mov_Title, mov_Length, mov_YrReleased) VALUES (@title, @length, @year)INSERT INTO Movies (mov_GenreID) SELECT UID_Genres FROM Genres WHERE gen_Genre = @genreINSERT INTO Movies (mov_DirectorID) SELECT UID_Directors FROM Directors WHERE dir_LName = @directorINSERT INTO Movies (mov_ActorID1)SELECT UID_Actors FROM Actors WHERE act_LName = @actor1 Problem is, and I'm sure you slightly more experienced coders caught this right away, is that each of the INSERTs creates it's own new record, filling in just that INSERT's column in that row. How do I get the three subsequent INSERTs to go with the first (AND how do I add @actor2, @actor3, and @actor4 as well?!)?Ya know, with 42 people having read this (as of this writing) I'd think SOMEONE would have a suggestion (besides Vinnie881, of course - thanks, man!).Help, anyone?Thanks=====================================f u cn rd ths, u cn gt a gd jb n prgrmng |
 |
|
|
|
|
|
|
|