SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UPDATE in order to normalize
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

PalaDolphin
Starting Member

USA
4 Posts

Posted - 08/18/2012 :  14:02:34  Show Profile  Reply with Quote
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

India
47157 Posts

Posted - 08/18/2012 :  15:16:13  Show Profile  Reply with Quote
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

USA
4 Posts

Posted - 08/19/2012 :  12:49:26  Show Profile  Reply with Quote
INNER JOIN! I knew it was something like that.

-=- PalaDolphin
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 08/19/2012 :  13:46:47  Show Profile  Reply with Quote
hope it solved your issue

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000