Why does MySQL think that I'm not using a WHERE statement? - mysql

I'm trying to update a table called rep in a database called premier_products. The table's primary key is rep_num.
When I run the following statement:
update rep
set last_name = "Perry"
where rep_num = 85;
I get an error that says "You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column."
I Googled that error message and most of the responses were along the lines of "You have to use a where clause or turn off safe mode". But as you can see, I am using a where clause. Why is the error appearing if I have a where clause?
MySQL server version 5.6.20.
This image shows that rep_num is definitely my primary key:
This image shows the current rep table:

Although you save only numbers, your primary-key type is char(2) and not tinyint(2) and when you update the record you are giving numerical value instead char value in your where condition. I think thats where the indexing mechanism triggers the error and tells you, your where condition is unsafe or might yield wrong results.
in your case try
update rep
set last_name = "Perry"
where rep_num = '85';
PS: why don't you name your tables with a prefix? like tbl_rep? just a thought.

Related

How do I add a new column to MySQL via phpMyAdmin?

Coming from years of experience with MS SQL Server I though it would be easy to add a simple nullable integer column to a MySQL database table using phpMyAdmin. I simply found the part of the UI that most resembled the part of SSMS where a new column is added to a table in SQL Server, clicked add column, entered a name, selected int, and null for default value.
The table itself is a posts table created by WordPress. When I click save I get an error saying
ALTER TABLE 'wp_posts' ADD 'acserp' INT NULL DEFAULT NULL AFTER 'comment_count';
MySQL said: Documentation
#1067 - Invalid default value for 'post_date'
I really don't see what adding an integer column has to do the the post_date column unless some row in the posts table has an invalid value for post_date and mySQL does some sort of checks to make sure that noting is wrong with the rest of the table before adding anything.
It looks like the answer is that you have to use raw SQL queries and tweak the SQL_Mode setting.
SET SQL_MODE='ALLOW_INVALID_DATES';
ALTER TABLE wp_posts ADD new_table INT AFTER comment_count
Invalid default value for 'create_date' timestamp field
You forgot to define Length/Value of the integer field. It is the third column from the left. Give value to it and your issue will be resolved. Read this for more information.

Why does MySQL safe update mode not allow a LIKE predicate?

Please consider the following scenario:
I have a MySQL table called actor. (Yes, it's from the Sakila sample DB).
In the table there's a varchar column called last_name.
There is a (non-PRIMARY) KEY on the column.
MySQL safe update mode is turned on.
When I run this query, it works:
DELETE FROM actor WHERE last_name = 'foo';
When I run this query:
DELETE FROM actor WHERE last_name LIKE '%fo' OR last_name LIKE 'fo%';
It fails with this error message:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
Why does safe update mode stop the second command and allow the first command?
According to the documentation:
It is possible for UPDATE and DELETE statements to produce an error in safe-updates mode even with a key specified in the WHERE clause, if the optimizer decides not to use the index on the key column.
A test like LIKE 'fo%' will normally use the index (the index can be used to match the beginning of a column), so it shouldn't cause the error. But a test like LIKE '%fo' cannot be indexed (it doesn't specify the beginning of the column, so a full scan is necessary), so you get an error.

Mysql 5.5.54 Duplicate entry on unique key with multiple columns

We have table in production (locally I try to do same thing, everything works as expected, maybe I have newer db version).
Table contains many columns, two of them is number AND year. These columns are bound with unique constraint key.
When I do query such as
SELECT * FROM `order` WHERE number IS NULL AND YEAR = 2018
I get more than one row. And it's not important, it's correct. [I could have 100 rows with null, 2018 for example]
But when I try to insert new record with number = null and year = 2018. I get error such as
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2018' for key 'number_year_UNIQUE'
One thing which comes in mind is that null in mysql is hiddenly bound with random number, and when I try to insert it's somehow becomes duplicate for new record.
As extra info is: We use Yii1 and error appears through ActiveRecord.
EDIT
After suggeestion added profiling got bound params
They are as follow
:yp13='2018',
:yp14='',
Exactly same parameters are on local machine.
EDIT
did you tried to run the insert query from from sql console(PhpMyAdmin or similar)?
I doubt it's an issue in the php side like the way you pass null value.
The problem was in that I tried to insert empty string '' not null value, as stated above (seen on screenshot).
In code I had something like
$order = new Order();
//some other stuff
$order->number = null;
$order->save();
However, in rules (in newly added validation method, number became empty string)
Added scenario (Yii specific) and problem had gone.

sequelize you are using sql safe update error

I am getting this error randomly . I am deleting from activeusers table on the basis of username it gives me error sometime and works smoothly sometime.
here is my code for deleting
let say
data={nickname:'asad',id:1}
activeusers.destroy({where:{username:data.nickname} }).then(res=>{
});
I have turn off the sql safe update from sql workbench but problem still exists
how can i permanently get rid from this error
This is a common problem with updates in MySQL. Here is your query:
DELETE FROM ActiveUsers WHERE username = 'mazhar.hayat#ibexglobal.com'
The error stems from that you are not using a primary key column in the WHERE clause. You would also see this error if you had no WHERE clause at all. MySQL has a mode which views a DML query like this as unsafe, because it is broad and runs the risk of corrupting your data.
There is a hack solution to this which might work. You could modify the query to mention the primary key column as follows:
DELETE
FROM ActiveUser
WHERE username = 'mazhar.hayat#ibexglobal.com' AND id=id
This might spoof MySQL into thinking the query is safe, because it mentions the primary key column id in the WHERE clause.
But what I would recommend to you is turning off safe updates mode directly in MySQL. Edit your startup script and make sure that --safe-updates and --i-am-a-dummy are not mentioned in the script.
Edit:
If you wanted to handle this from destroy, then the query option is the only option:
Post.findAll(
{ where: ["username = ? AND id = id", data.nickname] }
).success()

Error: Duplicate entry '' for key 'email'

I have seen this error with people running php scripts before but this is happending to me in phpmyadmin ??
Error
SQL query:
UPDATE `cl56-goldeng`.`users` SET `email` = '' WHERE `users`.`id` =118
MySQL said: Documentation
#1062 - Duplicate entry '' for key 'email'
It works fine if I give the field another value, but if I clear the field and press enter I get the above error.
The table itself looks like this :
On your table cl56-goldeng.users, the field email was specified on creation to not allow more than 1 of the same value to be allowed into it. This is done using the UNIQUE identifier on table creation in MySQL. You can see more on the UNIQUE identifier at this link.
You have 2 options that you could go about doing.
First would be to remove the unique constraint on the email field. This entirely depends on your logic in your code, but seeing as emails should almost always be unique, this is not suggested.
You can drop a unique key by running the command:
alter table [table-name] drop index [unique-key-index-name];
Second, would be to use NULL instead of an empty string. My assumption is that you are setting an empty string when the users email does not exist. In this scenario, it would be better to use NULL, and then check for that when retrieving data from the database.
You can insert a NULL value by using the NULL identifier in your MySQL statement, like such:
INSERT INTO users (firstName,lastName,email)
VALUES ('Bob','Ross',NULL);
And then check for a NULL value in whatever language you are accessing this data from.
You have a unique constraint on your email field. Either rethink your logic or drop the unique constraint.
Thats because you may have declare the email as unique key, and once you enter one row of empty email, it wont except another empty email