I want to synchronize multiple tables. I want to copy a row from one table and update it to another table but I donot want to specify the column names. This is because I donot want to hard code the sql query for every table separately.
I have tried several variants, it worked with insert, but I cannot find a solution for it with Update
insert into t2 select * from t1 where sno=2;
In this code, I donot need to pass the columns names to Insert, it automatically knows the columns list. I want to achieve the same with Update where I donot want to pass the columns list inside SET portion.
you always need to use SET and assign columns one by one. However you can use subquery for updating your source table using another table
update
Table1 as T1
inner join (
select *
from Table2
where ...
) as t2 on t1.Id = t2.Id
set T1.something = t2.something
Well, if the columns are the same, you could do:
delete from t2
where exists (select 1 from t1 where t1.id = t2.id);
insert into t2
select *
from t1;
I don't see why update would be used to replace data (although I would still list all the columns).
If the columns are not the same, then you need to list out the ones that change.
Related
In SQL (whichever variant you like, say MySQL or MonetDB) it's very intuitive and straightforward to do:
CREATE TABLE t2 AS SELECT c1 FROM t1;
and get the selection result as a new column in a new table. But what if you want the result as a new column in the same table (table t1)? Let's assume c1 is of type INT and may be null. We would need to write:
ALTER TABLE t1 ADD c2 INT;
UPDATE TABLE t1 SET c2 = c1;
and that's the easy version, since if it's a non-null column we would need to initialize the new column with some value; and if the data comes from multiple tables I'd need some kind of inner query in the UPDATE (if it would be at all possible).
Can I select-into-a-column with a single command somehow?
You use join:
UPDATE t1 JOIN
t2
ON t2.? = t1.?
SET t1.c2 = t2.c1;
The ? is a placeholder for the column used to identity which row in t2 should update which row in t1.
I want to query one table to see whether there exist any rows with 'A' type.
so I use this sql:
SELECT EXISTS(select * from %T where type = 'A');
then I need to update another table's column value to the above result. In order to prevent an insert with 'A' type happen during update, I am thinking to use a lock. but lock is very expensive, is there other alternative way to do this without locking?
If we have to use lock, I am thinking if table has already had type A, there is no need to lock insert during update because the result will still be 1. only prevent insert when there is no row with type A. How to do that?
Thanks!
Since you want to update a table when there is a type 'A', normally I would suggest you do the update or check every time right after inserting or deleting a row.
Or you could do something like this:
UPDATE table1 t1
LEFT JOIN table2 t2 ON t1.type = t2.type
SET t1.column1 = 1
WHERE t2.type IS NOT NULL
or
UPDATE table1 t1
SET t1.column1 = 1
WHERE EXISTS (
SELECT ...
FROM table2
WHERE type = 'A'
);
Please give me more details such as what is your table like and what exactly you want to do so we can discuss further.
I have been trying to move some data which relates to a specific column from one table to another. They both have a matching objectID.
So what I am trying to do is:
TABLE 1
ObjectID
Field with Data
TABLE 2
ObjectID
FIELD with NEW column
So the object ID relate to each other. All it is I am trying to do is move the data from Table 1 to Table 2 with the new column.
I have tried following but cant seem to get it all working. Is there anything that can be suggested that may help or point in me the right direction.
update Table2 a
Set a.NewColumn = (Select *
From Table1 b WHERE a.OBJECTID = b.OBJECTID
)
Hm, maybe I don't get it but why don't you use a INSERT?
INSERT INTO TableB(...columns...)
SELECT ...columns...
FROM TableA
You can use join update syntax for this, need to make sure that the Table2 already has data and you are updating a new column in there from Table1
update Table2 t2
join Table1 t1 on t1.OBJECTID = t2.OBJECTID
set t2.NewColumn = t1.Field
You can write query like this.
INSERT INTO Table2(ObjectID,Field)
SELECT ObjectID,Field
FROM Table1.
And you can put any default value in extra column.
I am trying to find all fields in one table that are empty and populate them with data from another table. Here is what I have:
UPDATE t1 SET col1 = (
SELECT col99
FROM t2
WHERE t1.product_ID = t2.product_ID
) WHERE col1 IS NULL
which works perfectly for updating all the blank fields in col1 of t1. But I also need to check for blanks in other fields and update them. I don't want the query to update all the fields each time any one of them is blank, just the blank field. I could run multiple queries but I have to imagine there is a cleaner, better way.
Thanks,
Matthew
Try:
UPDATE t1
SET
col1 = COALESCE(col1, (SELECT col99 FROM t2 WHERE t1.product_ID = t2.product_ID)),
col2 = COALESCE(col2, (SELECT ...)),
-- etc.
Should not perform better than your initial solution. It's just doing the job with only one UPDATE query instead of several.
I need to update the content of one data field in a table with the content of another field in a table every time two separate fields, one on each table, match up. I've been trying this syntax but I just can't get it to work properly without giving me an error.
UPDATE table1
SET field1 = table2.field1
FROM Table1,Table2
WHERE Table1.entry = Table2.entry
update ... from is sql server's syntax. In MySQL you can just use multiple tables directly:
update
table1 t1
join table2 t2 on t2.field = t1.field
set
t1.field1 = t2.matchingfield
where
t1.whatever = t2.whatever
All is detailed on the MySQL update reference page.