UPDATE statement format analogous to INSERT statement - mysql

I know that the UPDATE statement has the following format:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
I was wondering whether there exists a format analogous to INSERT statement, something like this:
UPDATE table_name
SET (column1,column2,column3,...)
VALUES (value1,value2,value3,...)
WHERE some_column=some_value;
Is this a valid query?
I'm using MySQL.

The short answer - no.
All the variants of the update syntax have a set clause of the form colulmn1=..., column2=... etc. For a complete list of the supported syntax variants, you can check out the documentation.

Related

How do I use an IF statement with an ALTER statement in MySQL?

I'm looking to add a column only if it does not already exist before. The motivation for this is that we can upgrade any version of a production instance to the latest version.
This is what I'm trying, but I keep getting a syntax error near the IF statement:
use database_name
SELECT #rowcount:=COUNT(column_name)
FROM information_schema.columns
WHERE table_name = 'table_name'
AND column_name = 'column_2';
IF #rowcount < 1 THEN
ALTER TABLE table_name
ADD COLUMN column_2 VARCHAR(42) DEFAULT 'abcd',
END IF;
commit;
What am I doing wrong?
You asked:
How do I use an IF statement with an ALTER statement in MySQL?
You Can't Do That™. MySQL's DML and DDL aren't as well integrated as other makes and models of table server.
You can't put DML inside stuff like IF statements or transactions. If you need to do that sort of operation you'll have to use an application programming language to issue simpler SQL.
I think you should change
IF #rowcount < 0 THEN
ALTER TABLE table_name
ADD COLUMN column_2 VARCHAR(42) DEFAULT 'abcd',
END IF;
and check if table exist.
As #Ollie Jones mentions and from looking at Using an IF Statement in a MySQL SELECT query, it looks like it's not possible to use if statements outside of sprocs or functions.
I followed the steps on http://www.cryer.co.uk/brian/mysql/howto_add_column_unless_exists.htm:
Create the sproc (see link)
Call the sproc when wanting to add a new column to a table
Alternatively, as #Ollie Jones mentions, you can do it at an application level, but I'm using .sql files in my case. It also saves on making multiple calls to the DB vs. having it done at the server level.

Is there any reason to use INSERT INTO tbl_name () VALUES(); syntax instead of SET col_name = col_value,...?

I'm personally prefer to use the following syntax:
INSERT INTO tab_name
SET
col1 = value1,
col2 = value2
I prefer this syntax as it gives me the full control over the correspondence between column names and the assigning values. It is better visualized from my point of view.
However most of the examples and howto's in the Internet (and on the Stack Overflow as well) uses the INSERT VALUES syntax like the following:
INSERT INTO tab_name (col1,col2) VALUES (value1,value2)
I'm agree that it can be slightly shorter. But in spite of this I hardly imagine how this can be handy. Especially if the number of inserting (updating) columns is more than 3 or 4. It can easily lead to mistakes if I will need to add or exclude one or more columns to/from the query.
So the question is: is there any reason to use INSERT/UPDATE () VALUES () syntax over INSERT/UPDATE SET?
Because the SET
INSERT INTO tab_name
SET
col1 = value1,
col2 = value2
is not SQL standard.
If you have to use other DBMS you can not use this syntax.
The first is SQL standard, the second is MySQL's extension.
Feel free to check also:
MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET

Use Replace Clause in Insert Stataement for MYSQL

I am using MySQL database. I want to insert data into it. But one column data contains special characters. (backslash) I want to replace it with Double backslash and then execute Insert query.
Can we do it using Insert Query?
While looking for the answer I came across
UPDATE your_table
SET your_field = REPLACE(your_field, '/', '//')
WHERE your_field LIKE '%articles/updates/%'
So it is possible use Replace in Update query.
Can we do the same in insert query? Please Let me know if you can help me.
You can use the following:
REPLACE INTO table_name(column_name1,column_name2,…)
VALUES(value1,value2,…)
More info from:
http://www.mysqltutorial.org/mysql-replace.aspx
You can use a BEFORE INSERT TRIGGER on your table
CREATE TRIGGER trigger_name
BEFORE INSERT
ON my_table
FOR EACH ROW
SET NEW.col_name = IF(NEW.col_name = '/','//',NEW.col_name);

mysql_query INSERT in to table where

I want to insert into a table where user id = to something
And change o to 1 which means user is online
Tried this
mysql_query("INSERT INTO `20s` VALUES('','','','','','','',1) WHERE `uid`='$user_id ");
But that doesn't get me anywhere. What's the right syntax?
Also what's the best way to keep a record of online friends in the database?
TIP:it's better to use update here
Correct syntax for insert query is:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
but you need here is update query, so you may update already existing row:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
You only need to change one column bit that show online so dont change other columns:
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
There is no WHERE clause in INSERT statements (see MySQL documentation).
If you want to update a value in an existing row, use UPDATE:
UPDATE `20s` SET `online`=1 WHERE user_id=your_user_id
mysql_ functions are deprecated, use PDO or mysqli_ instead!
MySQL also supports REPLACE INTO, which follows the same syntax as INSERT. Be careful though, columns that you do not supply will be set to their defaults.
In your case:
REPLACE INTO `20s` VALUES('','','','','','','',1) WHERE `uid`= '$user_id'
(Plus, you are missing a closing quote ' at the end of your query)
Use update query instead of insert
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id

SQL query- Update if exists, insert otherwise

I need to write an SQL query for MySQL so that a row is updated if it exists, but inserted if it does not.
i.e.
If row exists...
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
If it does not exist...
INSERT INTO Table1 VALUES (...)
Can this be done in one query?
i believe you need to reverse your logic in order for it to work:
insert into a table - if it exists (same key) then update it.
this can be achieved by the ON DUPLICATE statement like so:
INSERT INTO Table1 VALUES(...)
ON DUPLICATE KEY UPDATE column=column+1
check the manual here
Use the INSERT... ON DUPLICATE KEY UPDATE syntax.
See the manual
(For searching purposes, btw, this is usually referred to as an "upsert")