MySQL: update column if it exists else do nothing? - mysql

How to do the following:
if table has column 'name' - update 'name', else - do nothing?
My working sql in a transaction is
UPDATE tmp set name = NULL
tmp table kees the record to update and turning name into NULL is required for duplicate procedure. However, some tables do not have 'name' field.
So, is it possible to update column to null if exists, otherwise, just do nothing?

As far as I know it is not possible. Moreover I would recommend never make any modifications on a number of tables at once. In general each table is unique and setting fields to NULL basing on just a field name could lead to loss of important data.

Related

Mysql update caused entire table to be updated with same value

I need assistance trying to find a problem where 1 record/row was updated but the entire table got updated with the same value. Need to understand how this could have happened and implement a method to prevent it from doing so again.
update TABLE set pushID='1234567890' where userID='111222333' ;
What would that update statement do if the userID value equaled nothing or equaled NULL? Could that cause the update statement to update every single row with the same pushID?
IE: update TABLE set pushID='1234567890' where userID='' ;
Could a blank userID value cause this? If not, what could cause this? If so, how could I write the query statement to prevent this from happening again?
What would that update statement do if the userID value equaled nothing or equaled NULL?
If the userID is NULL, then the condition becomes userID = NULL. This will always evaluate as false. In other words, no record will be updated.
If the userID is the empty string, then the condition becomes userID = ''. This will only update records where userID is equal to the empty string. I would expect that userID is the primary key of your table, so it would be suprising the find an empty value. And even if there is one, it will be unique, so a unique record will be updated.
As you see, none of the two above use case would generate a massive update of the table. The most probable option is that the query was triggered without an actual WHERE clause, like:
update TABLE set pushID='1234567890'
In case these are altogether integer ID, you should not convert them to string with ''. Doing so may lead to unexpected results (never tried, but it seems to be a possible cause for the comparison in the WHERE condition to fail). For example:
UPDATE TABLE SET pushID=1234567890 WHERE userID=111222333;
When all records are being updated, the query lacks the WHERE condition. Make sure to have posted the correct query, because this query should update nothing when the WHERE condition fails to match.

MySQL insert only if NULL - any generalized flag present?

I have a MySQL insertion template:
INSERT INTO $0.$1 ($2$ColumnList)
VALUES ($2$ValueList) ON DUPLICATE KEY UPDATE
This is a MySQL command file (*.sqlcmd) which a program uses to randomly pass ColumnList and ValueList. So, I don't know in advance what are the values or the columns.
Now, I wish to make a conditional insertion to insert only if the columns are NULL or NOT NULL. Please note that I know I can write a MySQL statement like where mycolumn IS NULL or NOT NULL.
But are there any standard MySQL flags similar to ON DUPLICATE KEY UPDATE type, which I can specify once and for all, and that helps me to get rid of mentioning individual column names one at a time with IS NULL or IS NOT NULL?
No, there's no way to do this automatically. You need to name each column explicitly:
ON DUPLICATE KEY UPDATE col1 = IFNULL(VALUES(col1), col1), col2 = IFNULL(VALUES(col2), col2), ...
Whatever script is creating the variable $ColumnList could presumably generate this ON DUPLICATE KEY UPDATE clause from the same original data.

Disallow Nulls in mysql

I am creating a database that has a table called tblOne
I added a the column 'ID' and i forgot to disallow Nulls.
I am trying to change that with the query
ALTER TABLE tblOne
ALTER COLUMN ID int NOT NULL;
I am seeing the following error:
Cannot insert the value NULL into column 'ID', table 'city.dbo.tblOne'; column does not allow nulls. UPDATE fails.
Ive used this query before. So I'm sure there is some small mistake I'm not seeing.
The statement has been terminated.
Cannot insert the value NULL into column 'ID', table 'city.dbo.tblOne'; column does not allow nulls. UPDATE fails.
By the sounds of it the table already contains data. With that column containing NULL's already. If you try to change the column to not allow nulls, but there is already a null value in, then the modification will fail. First change the data in that column so that it is not null, and then run your ALTER statement again

Multiple set and where clauses in Update query in mysql

I don't think this is possible as I couldn't find anything but I thought I would check on here in case I am not searching for the correct thing.
I have a settings table in my database which has two columns. The first column is the setting name and the second column is the value.
I need to update all of these at the same time. I wanted to see if there was a way to update these values at the same time one query like the following
UPDATE table SET col1='setting name' WHERE col2='1 value' AND SET col1='another name' WHERE col2='another value';
I know the above isn't a correct SQL format but this is the sort of thing that I would like to do so was wondering if there was another way that this can be done instead of having to perform separate SQL queries for each setting I want to update.
Thanks for your help.
You can use INSERT INTO .. ON DUPLICATE KEY UPDATE to update multiple rows with different values.
You do need a unique index (like a primary key) to make the "duplicate key"-part work
Example:
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE b = VALUES(b), c = VALUES(c);
-- VALUES(x) points back to the value you gave for field x
-- so for b it is 2 and 5, for c it is 3 and 6 for rows 1 and 4 respectively (if you assume that a is your unique key field)
If you have a specific case I can give you the exact query.
UPDATE table
SET col2 =
CASE col1
WHEN 'setting1'
THEN 'value'
ELSE col2
END
, SET col1 = ...
...
I decided to use multiple queries all in one go. so the code would go like
UPDATE table SET col2='value1' WHERE col1='setting1';
UPDATE table SET col2='value2' WHERE col1='setting1';
etc
etc
I've just done a test where I insert 1500 records into the database. Do it without starting a DB transaction and it took 35 seconds, blanked the database and did it again but starting a transaction first, then once the 1500th record inserted finish the transaction and the time it took was 1 second, so definetely seems like doing it in a db transaction is the way to go.
You need to run separate SQL queries and make use of Transactions if you want to run as atomic.
UPDATE table SET col1=if(col2='1 value','setting name','another name') WHERE col2='1 value' OR col2='another value'
#Frits Van Campen,
The insert into .. on duplicate works for me.
I am doing this for years when I want to update more than thousand records from an excel import.
Only problem with this trick is, when there is no record to update, instead of ignoring, this method inserts a record and on some instances it is a problem. Then I need to insert another field, then after import I have to delete all the records that has been inserted instead of update.

how to change a lot of records at once in phpmyadmin

I'd like to know how to update several records at once where a certain column type is selected.
I have found how to select records. I have found how to update records. But i don't know how to do it both together for it to work.
Selecting:
SELECT * FROM users WHERE 'type'='new'
Updating:
update table
set column = 937
So basically i want to change the info in the 'column' to 937 in the 'table' if another column 'type' is 'new'.
Thanks,
You can do this by simply adding a WHERE clause to your UPDATE statement:
UPDATE `users`
SET `myColumn` = 937
WHERE `type` = 'new'
Of course, change myColumn to match your column name
You can do this with a subquery :
update users
set column = 937
where id in (select id from users where type='new')
Just change the columns name if I got them wrong.
After researching for a while I found another solution to this problem. When referencing the same table or field name in a query, the name space in MySQL ends up with duplicates and fails. To overcome this use the "AS" syntax to name the duplicate items. Also, update queries often require a key field to be used as the parameter for the where clause, this is often your auto-incremented ID field. This example provides a solution for both of these problems.
UPDATE tbl
SET field=newValue
WHERE tbl.key IN
(SELECT idNew FROM
(Select tbl.key AS idNew
FROM tbl
WHERE field=editValue) AS tblNew);
tbl - Table name being updated
field - Target field for update
newValue - Value to replace items in the target field
tbl.key - Field name for the key in your table
idNew - New name for key in your table, to replace the name and prevent failure from duplicating names in query. could be any alphanumeric string. 'id' or 'id_new' or 'id_n'
editValue - The value to change
tblNew - New name for query, to prevent duplicate table names in the query. Could be any alphanumeric string 'tbl_n' or 'tbl_new' or 'tbl_test'
This query gets the key values for all the records that match records that have values you want edited, then changes the names of the key and tbl so they can be processed by MySQL, lastly the update query runs and changes the values based on the keys provided in the sub-query.
Hopefully this saves someone else a few hours!