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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UPDATE in order to normalize

Author  Topic 

PalaDolphin
Starting Member

4 Posts

Posted - 2012-08-18 : 14:02:34
This is probably a very basic thing to do, but I'm having difficulty.

In short, what I think I want to do is UPDATE INTO based on a SELECT query in order to normalize a table (I hope I'm getting that terminology right).

I have a table `quotes` (from Criminal Minds) with columns Episode, Quote, Author, Character, FullName, Actor. I've created seperate tables with unique values for `Character` (which is just the last name) and `Author`, since there are several quotes per each author and character. These new tables have been given a auto_increment primary key, author_id and char_id respectively. I've now added those columns to the `quotes` tables so I can link them.

What I want to do is execute an SQL statement that will update the `quotes` table fields author_id and char_id based on Author and Character fields so I can eventually eliminate them.

So, for the case of `Character` table, fields Character, FullName, and Actor will be moved to that table and replaced by char_id. Same with `Author` table.

I just don't know the syntax. I think it should be something like:

UPDATE `quotes`
SET quotes.char_id = new_id
WHERE new_id=(SELECT Character.char_id
FROM Character, quote
WHERE Character.Character = quote.Character);

Is that even close to where I should go with this or what?

Sincerely,




-=- PalaDolphin

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-18 : 15:16:13
its really close!

UPDATE q
SET q.char_id = c.new_id
FROM `quotes` q
INNER JOIN Character c
ON c.lastname = q.Character


UPDATE q
SET q.author_id = a.new_id
FROM `quotes` q
INNER JOIN Author a
ON a.lastname = q.Author



I've assumed the column names so make sure you put actual names in your query based on placeholders above

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

PalaDolphin
Starting Member

4 Posts

Posted - 2012-08-19 : 12:49:26
INNER JOIN! I knew it was something like that.

-=- PalaDolphin
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-19 : 13:46:47
hope it solved your issue

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -