SQL Update Bug (Value not updated after it said that it is) - mysql

I ran the following in my sql database:
update table1 SET car_color_code="red metalic" where car_model="335i";
and it said:
Query OK, 1 row affected (0.06 sec) Rows matched: 1 Changed: 1
Warnings: 0
However, I dont see the change in the table
The table looks like this:
------------------------------------------------
| ID | model | manufacturer | car_color_code |
| 1 | 335i | BMW | NULL |
------------------------------------------------
So the null from before should be replaced with "red metalic" but it stays NULL.
However when i try to insert again it says:
Rows matched: 1 Changed: 0 Warnings: 0
And when i try to select again I still dont see the change in car_color_code column...
My table description is
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| model | varchar(255) | NO | | NULL | |
| manufacturer | varchar(255) | NO | | NULL | |
| car_color_code | varchar(255) | NO | | NULL | |
+------------------+--------------+------+-----+---------+----------------+

You might have forgot to commit. It is possible that you have exited out of MySQL server and tried the select statement after relogging in.
Make sure you commit after you update.

Related

Mysql can't select the record in the table

I have a mysql table with a null-able int column named 'status', and I have two records in the table where one of the status is 2 when the other is NULL, but when I select the records with query 'status!=2', the record (status=null) is not shown.
mysql>
mysql>
mysql> desc admin_user;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| acct_name | varchar(32) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| user_name | varchar(32) | YES | | NULL | |
| description | varchar(128) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| role | int(11) | NO | | 1 | |
| create_date | date | YES | | NULL | |
| update_date | date | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
mysql> select id, acct_name, status from admin_user;
+----+-----------+--------+
| id | acct_name | status |
+----+-----------+--------+
| 1 | letme | NULL |
| 3 | admin | 2 |
+----+-----------+--------+
2 rows in set (0.00 sec)
mysql> select id, acct_name, status from admin_user where status=2;
+----+-----------+--------+
| id | acct_name | status |
+----+-----------+--------+
| 3 | admin | 2 |
+----+-----------+--------+
1 row in set (0.00 sec)
mysql> select id, acct_name, status from admin_user where status IS NULL;
+----+-----------+--------+
| id | acct_name | status |
+----+-----------+--------+
| 1 | letme | NULL |
+----+-----------+--------+
1 row in set (0.00 sec)
mysql> select id, acct_name, status from admin_user where status!=2;
Empty set (0.00 sec)
mysql>
As you can see, the record whose status is NULL can not be selected using the query 'status!=2'. I also tried 'status<>2'. Can anybody help?
Nulls are really interesting.
The NULL value can be surprising until you get used to it.
Conceptually, NULL means “a missing unknown value” and it is treated
somewhat differently from other values.
What's more
You cannot use arithmetic comparison operators such as =, <, or <> to
test for NULL
There are lots of different ways to write this. One of them being:
select id, acct_name, status from admin_user where status IS NULL
OR status != 2
Alterantively as #shA.t suggested.
select id, acct_name, status from table1 where COALESCE(status, 0) != 2
Just check that 0 is indeed a number that doesn't appear any where else in the table.

MySQL INSERT shows more affected rows than affected

Consider the following three queries.
The first one can only return a single row as bids_buy.id is the primary key.
The second one shows an existing record in entities_has_documents for primary key 3099541982-2536988132, and the third one doesn't execute due to that existing record.
The forth one does execute as expected, but shows two affected rows.
Why does it show two affected rows, and not just one associated with primary key 3099541982-2536988132?
mysql> SELECT bb.bids_sell_id, 2536988132,"pub_bids", NOW(),506836355 FROM bids_buy bb WHERE bb.id=2453409798;
+--------------+------------+----------+---------------------+-----------+
| bids_sell_id | 2536988132 | pub_bids | NOW() | 506836355 |
+--------------+------------+----------+---------------------+-----------+
| 3099541982 | 2536988132 | pub_bids | 2016-04-16 08:19:13 | 506836355 |
+--------------+------------+----------+---------------------+-----------+
1 row in set (0.00 sec)
mysql> SELECT * FROM entities_has_documents;
+-------------+--------------+----------+---------------------+--------------+-----------+------------+-----------+-------------+
| entities_id | documents_id | type | date_added | date_removed | added_by | removed_by | purged_by | date_purged |
+-------------+--------------+----------+---------------------+--------------+-----------+------------+-----------+-------------+
| 2453409798 | 2536988132 | pub_bids | 2016-04-16 08:07:13 | NULL | 506836355 | NULL | NULL | NULL |
| 3099541982 | 2536988132 | pub_bids | 2016-04-16 08:18:53 | NULL | 506836355 | NULL | NULL | NULL |
+-------------+--------------+----------+---------------------+--------------+-----------+------------+-----------+-------------+
2 rows in set (0.00 sec)
mysql> INSERT INTO entities_has_documents(entities_id,documents_id,type,date_added,added_by)
-> SELECT bb.bids_sell_id, 2536988132,"pub_bids", NOW(),506836355 FROM bids_buy bb WHERE bb.id=2453409798;
ERROR 1062 (23000): Duplicate entry '3099541982-2536988132' for key 'PRIMARY'
mysql> INSERT INTO entities_has_documents(entities_id,documents_id,type,date_added,added_by)
-> SELECT bb.bids_sell_id, 2536988132,"pub_bids", NOW(),506836355 FROM bids_buy bb WHERE bb.id=2453409798
-> ON DUPLICATE KEY UPDATE type="pub_bids", added_by=506836355, date_added=NOW(), removed_by=NULL, date_removed=NULL;
Query OK, 2 rows affected (0.00 sec)
Records: 1 Duplicates: 1 Warnings: 0
mysql> EXPLAIN bids_buy;
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | NULL | |
| bids_sell_id | int(10) unsigned | NO | MUL | NULL | |
| stage_buy_id | int(10) unsigned | NO | MUL | NULL | |
+--------------+------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> EXPLAIN entities_has_documents;
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| entities_id | int(10) unsigned | NO | PRI | NULL | |
| documents_id | int(10) unsigned | NO | PRI | NULL | |
| type | varchar(16) | NO | MUL | NULL | |
| date_added | datetime | NO | | NULL | |
| date_removed | datetime | YES | | NULL | |
| added_by | int(10) unsigned | NO | MUL | NULL | |
| removed_by | int(10) unsigned | YES | MUL | NULL | |
| purged_by | int(10) unsigned | YES | MUL | NULL | |
| date_purged | datetime | YES | | NULL | |
+--------------+------------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
mysql>
EDIT
Per http://php.net/manual/en/pdostatement.rowcount.php
If the last SQL statement executed by the associated PDOStatement was
a SELECT statement, some databases may return the number of rows
returned by that statement. However, this behaviour is not guaranteed
for all databases and should not be relied on for portable
applications.
So, am I just seeing the number or rows returned from my SELECT statement, and not the number or rows affected by my INSERT? Why would MySQL do such a thing?
EDIT DONE
I think it is due to ON DUPLICATE KEY UPDATE modifier as the MYSQL Reference Manual 5.5 as well as MySQL Reference Manual 5.7 says
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. The affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.”.
In your case you already had a row with primary key value 3099541982-2536988132. Hence the MySQL lets you know that you are trying to insert a row with duplicate Primary or Unique key by indicating 2 rows affected. As the manual also say that ON DUPLICATE KEY UPDATE leads to the sequence of INSERT than UPDATE update command in case of duplicate key whereas it executes only the INSERT command in case the key is not present.
I hope this helps.
UPDATE
Also see this link.

Update query matching records but not updating table

I have a dataset that I'm trying to debug a few issues on, and I've run into a case where an update query matches a record in the table, but doesn't update the data. This is my dataset:
> describe genotypes;
+--------------+------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| userstamp | int(11) | YES | | NULL | |
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------+------------+------+-----+-------------------+-----------------------------+
12 rows in set (0.00 sec)
> SELECT id, userstamp FROM genotypes WHERE id = 7276;
+------+-----------+
| id | userstamp |
+------+-----------+
| 7276 | NULL |
+------+-----------+
When I try to run my query to update the userstamp field, I get the following result:
> UPDATE genotypes SET userstamp = 142 WHERE id = 7276;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
I've remove all my triggers from the table, so I'm not sure what's blocking the update.

MySQL default value error with ON DUPLICATE KEY UPDATE

Why am I getting this error? Did something change with MySQL versions which would cause this (which worked at one point) to now fail? The INSERT INTO does not specify the user_id value which would be required if an insert was done, but since id 1 already exists, this should become an UPDATE and be valid.
mysql> select * from article;
+----+---------+---------------------+-----------+-----------+
| id | user_id | published_at | title | content |
+----+---------+---------------------+-----------+-----------+
| 1 | 1 | 2011-12-10 12:10:00 | article 1 | content 1 |
| 2 | 2 | 2011-12-20 16:20:00 | article 2 | content 2 |
| 3 | 1 | 2012-01-04 22:00:00 | article 3 | content 3 |
+----+---------+---------------------+-----------+-----------+
3 rows in set (0.00 sec)
mysql> desc article;
+--------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+-------------------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(11) unsigned | NO | MUL | NULL | |
| published_at | datetime | NO | | CURRENT_TIMESTAMP | |
| title | varchar(100) | NO | | NULL | |
| content | text | NO | | NULL | |
+--------------+------------------+------+-----+-------------------+----------------+
5 rows in set (0.01 sec)
mysql> INSERT INTO article (id)
-> VALUES (1)
-> ON DUPLICATE KEY UPDATE title = 'article 1b', content = 'content 1b';
ERROR 1364 (HY000): Field 'user_id' doesn't have a default value
MySQL version 5.6.12.
You're getting error because
The user_id column is defined as NOT NULL
The user_id column doesn't have a default value specified
You don't specify its value in your query either
PS: the question is irrelevant to the ON DUPLICATE KEY UPDATE clause - it would be the same error if you didn't use it as well.
PPS: regardless of if the ON DUPLICATE KEY UPDATE triggered - your insert should satisfy all the constraints
Set a default value to the user_id table with: ALTER TABLE article
ADD user_id int(11) NOT NULL DEFAULT {DEFAULT_VALUE}
or change from NOT NULL to NULL

Can I configure MySQL so that newly added columns allow nulls by default?

Our developers frequently forget to specify that nulls are allowed when adding columns to existing tables. MySQL defaults to not allowing nulls if Null is not included when using the Alter Table command to add a column
Is there a way to configure MySQL so that new columns allow nulls unless Not Null is explicitly included with the alter table command?
We use PHPMyAdmin to alter tables so a method of making all newly added fields allow nulls through PHPMyAdmin would work.
MySQL's default behavior is allowing nulls by default on alter table commands. It probably has something to do with PHPMyAdmin.
mysql> desc foo;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| client_id | int(11) | YES | | NULL | |
| item_id | int(11) | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table foo add (a char(1));
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc foo;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| client_id | int(11) | YES | | NULL | |
| item_id | int(11) | YES | | NULL | |
| a | char(1) | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
You can specify the DEFAULT NULL when creating the new column like:
ALTER TABLE table
ADD COLUMN column VARCHAR(255) DEFAULT NULL;