473,388 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

UPDATE multiple tables

code green
1,726 Expert 1GB
The following query produces a very slow running failure. (Usually time out).
I am not sure about the syntax. Any ideas?
Expand|Select|Wrap|Line Numbers
  1. UPDATE `table1`,`table2`,`table3` SET [all fields in all three tables to values]
  2.  WHERE `tables1`.`id` = '20439'
Apr 4 '07 #1
7 23553
Motoma
3,237 Expert 2GB
The following query produces a very slow running failure. (Usually time out).
I am not sure about the syntax. Any ideas?
Expand|Select|Wrap|Line Numbers
  1. UPDATE `table1`,`table2`,`table3` SET [all fields in all three tables to values]
  2.  WHERE `tables1`.`id` = '20439'
The EXPLAIN syntax should help you out here.
What is happening is that you are doing a 3 table join, without realizing it. You are updating tables 2 and 3 without any restrictions; all entries in these two tables are updates all the time.

In order to see what exactly you are updating, do a select on the same set:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM  `table1`,`table2`,`table3`
  2. WHERE `tables1`.`id` = '20439' 
  3.  
Another thing causing a problem is that you have a string as your ID. If that really is an integer in your table, you don't need the '' around it.
Apr 6 '07 #2
code green
1,726 Expert 1GB
Another thing causing a problem is that you have a string as your ID. If that really is an integer in your table, you don't need the '' around it.
Thanks. Didn't realise I had done that.
What is happening is that you are doing a 3 table join, without realizing it. You are updating tables 2 and 3 without any restrictions; all entries in these two tables are updates all the time.
Right so I need
Expand|Select|Wrap|Line Numbers
  1. UPDATE `table1`,`table2`,`table3` 
  2. SET [all fields in all three tables to values]
  3.  WHERE `tables1`.`id` = 20439 AND `tables2`.`id` = 20439 
  4. AND `tables3`.`id` = 20439 
So if the ID in one table does not exist the whole query will fail. I wanted to UPDATE the tables WHEREVER the ID appeared. This idea doesn't look possible without using IF EXISTS and sub queries checking for the ID in each table. What do you recommend?
Apr 10 '07 #3
Motoma
3,237 Expert 2GB
Thanks. Didn't realise I had done that.
Right so I need
Expand|Select|Wrap|Line Numbers
  1. UPDATE `table1`,`table2`,`table3` 
  2. SET [all fields in all three tables to values]
  3.  WHERE `tables1`.`id` = 20439 AND `tables2`.`id` = 20439 
  4. AND `tables3`.`id` = 20439 
So if the ID in one table does not exist the whole query will fail. I wanted to UPDATE the tables WHEREVER the ID appeared. This idea doesn't look possible without using IF EXISTS and sub queries checking for the ID in each table. What do you recommend?

You could use ORs in place of ANDs, but that will slower than hell. You should just use 3 queries.
Apr 10 '07 #4
code green
1,726 Expert 1GB
I've decided to use four. Three queries SELECT 1 FROM `table` WHERE `id` = 20439 for each of the three tables which should be fast and gives me the chance to catch any missing ids at this point. Then construct an UPDATE query live time as a php string using only the tables where the id was found. Thanks for your help Motoma.
Apr 10 '07 #5
Motoma
3,237 Expert 2GB
Glad I could help. Come back any time.
Apr 10 '07 #6
Can you share the code structure that you use in that 3 tables?..thanks...
Jul 5 '10 #7
re: UPDATE multiple tables
I've decided to use four. Three queries SELECT 1 FROM `table` WHERE `id` = 20439 for each of the three tables which should be fast and gives me the chance to catch any missing ids at this point. Then construct an UPDATE query live time as a php string using only the tables where the id was found. Thanks for your help Motoma
>>>>>>>How can i apply those 3 select and UPDATE query in VB.net?thanks..
Jul 5 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: vj | last post by:
How to Update multiple tables in a single SQL update Statement? Is there any way out? vj.
0
by: PMB | last post by:
Thank you in advance for any and all assistance. I have a small program that I'm tracking accounts rec. and I want to keep data, stored by day and have a month end. I have a form called...
2
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm running a form/table that tracks transactions and I want to update a daily and monthly table at the end of each transaction based on the...
1
by: CS | last post by:
I need to update multiple tables from one form using a command button. The info from the different text boxes should then go into the tables. Can someone help me with some example code on how to...
0
by: Remy | last post by:
Hi I have an ASP.NET 2.0 DetailView that displays data that has been joined over more than one table. Is it somehow possible to then also Update Data over multiple Tables without using a Stored...
0
by: darrel | last post by:
I'm getting the hand of VS.2005 and ASP.net 2.0 by going through a bunch of the video tutorials at ASP.net The big change for me is using the actual IDE much more than I used to with ASP.net...
5
by: Proaccesspro | last post by:
I have an Access database that contains multiple tables. How can I update a field in all of the tables with a search and replace funtion? In other words, one of the forms has a button that will...
4
by: dstorms | last post by:
I'm trying to run an update query on multiple tables, and since Access doesn't allow me to update tables from a union query, I'm writing a module as a workaround. So I've set up a temporary recordest...
2
by: Presto | last post by:
I am making a front end mdb so users can enter new members data. I can then import this into the master database on the backend and erase the existing info on the front end to keep the data...
1
by: DMAGIC448 | last post by:
I am trying to figure out how to add records to multiple tables w/ one form. I am making a database for a Home DayCare and I want to add/update records for 5 tables (Student info, Parent 1 Info,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.