I only want to update a vale in my database if it is different. Reading through the Oracle docs on UPDATE, it says...
...the UPDATE statement updates columns of existing rows in the named table with new values.
Since it doesn't say it won't overwrite identical values, should I take this statement literally? Does this mean MySQL does some sort of Boolean matching check for me?
No, MySQL won't overwrite identical values.
Lets say we insert some data:
insert into foo(id,val1,val2,val3) values (0,1,2,3);
Query OK, 1 row affected (0.00 sec)
If you update it with the same values:
update foo set id=0, val1=1, val2=2, val3=3 where id=0;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Take a look on servers response 0 rows affected
an sql query would update even identical value by practically substituting them. Anyway, you can structure your sql to avoid it will substitute the identical value. (I think also that the latter way would be more time consuming then the normal procedure and maybe useless for the final result)
Related
The INSERT ... SELECT... ON DUPLICATE KEY UPDATE returns as affected-rows a number derived from (inserted count) + (updated count) * 2, and this is well documented in multiple places.
However in the output of the MySQL Command-Line Tool, I've noticed this extra info:
> INSERT INTO ...
-> SELECT ... FROM ...
-> ON DUPLICATE KEY UPDATE ...
-> ;
Query OK, 97 rows affected (0.03 sec)
Records: 2425 Duplicates: 28 Warnings: 0
Namely, the numbers Records: and Duplicates:.
Analyzing have determined:
The 97 rows affected is affected-rows (a.k.a. ROW_COUNT()).
Records: 2425 is the number of rows fetched by the SELECT part.
Duplicates: 28 is the number of rows actually changed by the ON DUPLICATE KEY UPDATE part.
Consequently:
affected-rows - Duplicates * 2 is the number of rows actually inserted.
Records - affected-rows - Duplicates is the number of rows duplicated but not changed (i.e. values were set to the same value).
Which brings us to the question: How does one obtain these numbers Records and Duplicates in a program? (I'm using MySQL Connector/J if that helps answer the question.)
Possibly for Records:, issuing a SELECT on FOUND_ROWS() directly after the INSERT ... SELECT ... ON DUPLICATE KEY UPDATE is one way.
I have no idea where Duplicates: comes from.
The C api does not provide direct access to these values (or the underlying information to calculate these values) as numbers, as it does with mysql_affected_rows().
You have however access to that message using mysql_info():
mysql_info()
const char *mysql_info(MYSQL *mysql)
Description
Retrieves a string providing information about the most recently executed statement, but only for the statements listed here. For other statements, mysql_info() returns NULL. The format of the string varies depending on the type of statement, as described here. The numbers are illustrative only; the string contains values appropriate for the statement.
INSERT INTO ... SELECT ...
String format: Records: 100 Duplicates: 0 Warnings: 0
[...]
UPDATE
String format: Rows matched: 40 Changed: 40 Warnings: 0
Return Values
A character string representing additional information about the most recently executed statement. NULL if no information is available for the statement.
You can/have to parse these (query dependent) strings if you need access to those values in detail. The mysql client simply displays this message as it is.
Unfortunately, not every api, including the MySQL Connector/J, implements or relays this feature, so those detailed values seem to not be accessable here.
When I try to create a database which already exists,
CREATE DATABASE IF NOT EXISTS test;
Query OK, 1 row affected (0.00 sec)
CREATE DATABASE IF NOT EXISTS test;
Query OK, 1 row affected, 1 warning (0.00 sec)
Why does it show 1 row affected message second time , even though it is not creating a new database with the same name?
Although the CREATE DATABASE IF NOT EXISTS test; command won't directly modify rows in an exiting instance of the test database, it will affect the actual details stored internally in the mysql database, or possibly in one of the derived meta views, like the information_schema or performance_schema etc.
The reported Query OK, 1 row affected (0.00 sec) is referring to a row in one of these internal data constructs. When you reissue the CREATE DATABASE command, and it fails gracefully thanks to the IF NOT EXISTS clause, it is still likely to store meta-data internally, maybe an accumulating field that counts warnings or similar, or even just a 'last acted on' timestamp against this database's row. In any case the stored data in this record is changed, and is reflected as an 'affected' row.
I want to show the result of rows affected after update, insert, or delete in mysql. I have put
DELETE FROM A WHERE ID='1';
SELECT ROW_COUNT();
With the ROW_COUNT the last statement, but the result show me is 0.
If you want to know number of rows affected by delete query in PHPMYADMIN then by running your query it will show you the result see below screenshot :
As #Flash Thunder said PHPmyadmin does not allow multiple queries sent at once
If you want to see the affected rows then you can also write a script using PHP which will exceute you sql query and returns the number of affected rows
Just to be clear.
phpMyAdmin is written in PHP, and PHP does not allow multiple queries sent at once... if you are sending two queries separately, second query is on new connection, so it has no access to previous query information. That's why SELECT ROW_COUNT(); returns 0.
But by default phpMyAdmin returns affected rows count in information after query. It probably uses mysql(i)_affected_rows() function.
FOUND_ROWS() returns number of tables in database when there was no previous query.
mysql> use hunting;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
| 24 |
+--------------+
1 row in set (0.00 sec)
mysql> show tables;
(...)
24 rows in set (0.00 sec)
I'm used to MSSQL, not Mysql, so sorry for this probably stupid question. I'm trying update my password for a backup database I'm getting setup by running the following query:
update users set password = md5('pass') where username = 'admin'
When I run this it says
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Why is a row matching but not changing?
It means that the value was not changed. It was probably not changed because the column was already equal to the md5 hash of pass.
Is it possible to get the duplicate count when executing MySQL "INSERT IGNORE" statement via JDBC?
For example, when I execute an INSERT IGNORE statement on the mysql command line, and there are duplicates I get something like
Query OK, 0 rows affected (0.02 sec)
Records: 1 Duplicates: 1 Warnings: 0
Note where it says "Duplicates: 1", indicating that there were duplicates that were ignored.
Is it possible to get the same information when executing the query via JDBC?
Thanks.
I believe you can retrieve this by issuing SHOW WARNINGS after your insert.
http://dev.mysql.com/doc/refman/5.0/en/show-warnings.html
You can use the ROW_COUNT() and FOUND_ROWS() functions to determine the number of inserted rows and duplicates when doing INSERT IGNORE.
For Example :
SELECT ROW_COUNT(), FOUND_ROWS()
INTO myRowCount, myFoundRows;
myInsertedRows = myRowCount;
myDuplicateRows = myFoundRows - myInsertedRows;
COMMIT;
You can get the rows affected value via JDBC. Just subtract it from the number of rows you inserted.
Bear in mind that rows affected also includes rows that are indirectly affected by the query. So, if there are any triggers that affect other rows, rows affected will include those rows as well.
See: http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#executeUpdate()