Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Poorly designed database ... require killer SQL statement !! 2

Status
Not open for further replies.

parkers

Vendor
Oct 21, 2002
157
GB
This may not be possible however here is my scenario... actually before I start I would like to say that the root cause of the problem is a very poorly designed database however changing the structure would (of course be beneficial long term) however require an extreme amount of work updating data and query apps (CGI scripts).

I have multiple dB tables which stores names, dates etc.. of when 'tasks' have been carried out. At the centre of all of this is a the 'person' table which unfortunately has a primary key of 'name' (yes thats correct ... you cannot have two John Smiths ! ... this somehow made sense to the designer at the time !!)

e.g. showing only relevent

'person' db
name = John Smith
age = 30

'work_log' db
work_id = 1
description = Task 1
name = John Smith,01/01/2004

work_id = 2
description = Task 2
name = John Smith,awaiting

OK an extreme example however John Smith decides to change his name to John Brown ... is it possible to apply an SQL statement updating only 'John Smith' to 'John Brown' ??

Question:
Does anyone know of any SQL command which could change part of a fields value ... e.g.

John Smith
becomes
John Brown in 'person' db
becomes
John Brown,01/01/2004 in 'work_log' db
also becomes
John Brown,waiting in 'work_log' db

so in effect the query matches only a substring of the field value then replaces this substring with the new substring????

Any help would be well appreciated :)

This post may also require a re-read to understand ! ;-)
 
I should also say that the database has no referential integrity and really should have a unique reference number for names !
 
What you want to do can be done. The difficulty level depends on your version of MySQL.

In the long run, however, you will be well served to change the schema. Having values like "John Smith,awaiting" and "John Smith,01/01/2004" is simply stupid.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
You could use:
[tt]
UPDATE person
SET name='John Brown'
WHERE name='John Smith'

UPDATE work_log
SET name=REPLACE(name,'John Smith','John Brown')
WHERE name like 'John Smith,%'
[/tt]


-----
ALTER world DROP injustice, ADD peace;
 
Unfortunately this is the data I have been presented with ... agree about the schema though, it is completely stupid ! and against all database design principles ... what I did not mention is that 'name' on the 'work_log' table is not used as a foreign key in the end application which may go someway to explaining why a compound value appears ... regardless, bad design in any case.

if it helps then the version of MySQL is 3.23.41 ... does anyone ideas on the SQL side?

 
Tony ... THANKS ! just done a quick test and it worked absolutely perfectly !!

... I've been using SQL for quite a while now and have never actually needed to use REPLACE before !!

thanks again and have a star !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top