Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2005, 02:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default Need xref Fix

I recently took over a db designed by another programmer. The DB is designed to track Teachers and their Classes. One Teacher to Many Classes.

Instead of having a field in the Class table for Teacher(id), the original designer added a third table to "cross-reference" this relationship (table is called "xrefTeacherClass")...so we have...

Table: Teacher
Id, Name, Address, School, etc.

Table: Class
Id, ClassName, ClassGrade, etc.

Table: xrefTeacherClass
Id, TeacherID, ClassID

I want to get rid of this "xref" table, so I have added a column to the Class table called "TeacherID", but I have no idea how to SQL the xref values over for all the teachers and classes...

???

-------------------------
Beware of programmers with screwdrivers...
__________________
-------------------------
Beware of programmers with screwdrivers...
 
Old October 5th, 2005, 02:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there...

be aware that the table "XREF..." could be there b/c the original guy though that maybe there could be more than one teacher for a class or to have a history...

anyway i dont know where do you have this but you can do something like this:

(didnt try this dont have any sql on hand..)

select x.teacherid as newteacherid, c.* FROM xrefteacherclass as x, class as c where x.classid = c.id
(get this last query into a new table to replace class)

then you can do the update
update (lastquery) set teacherid = newteacherid
drop newteacherid from (lastquery)

HTH

Gonzalo
 
Old October 5th, 2005, 02:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

Thanks mate!

I worked it out using the following:

UPDATE tblClass SET tblClass.UID = x.TeacherID FROM tblClass As c
LEFT JOIN xrefTeacherClass As x ON x.classid = c.id

And I'll be keeping the xref table there for now. The immediate use of fixing this is for easier report generation...

-------------------------
Beware of programmers with screwdrivers...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Fix Error sgafar ASP.NET 1.0 and 1.1 Basics 0 May 30th, 2007 10:22 AM
Key() ... and XRef asearle XSLT 2 October 17th, 2006 01:13 AM
Pleeeeeeeeeeeeeez fix it!!! Prashant Sahni C++ Programming 4 August 22nd, 2006 01:55 AM
Help! How do I fix this? missusfinz Crystal Reports 1 April 1st, 2005 12:27 PM
Displaying Xref-Tables in Datagrid in VB.NET Ron Howerton VB.NET 2002/2003 Basics 0 December 15th, 2003 02:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.