I'm running MySql in ubuntu 10.10. I created a table called 'employee' having 3 field names empno, name and salary. Inserted few entities. In the middle of the process i want to change salary attribute as 'NOT NULL'. I Alter the table as
ALTER TABLE employee MODIFY salary int(10) NOT NULL;
Query executed. I wanted to test by using command,
UPDATE employee SET salary=NULL;
Query OK, 15 rows affected, 15 warnings (0.06 sec)
Rows matched: 15 Changed: 15 Warnings: 15
also gave warnings " (Code 1048): Column 'salary' cannot be null "(Repeated for every row)
But when i saw my table , All salaries were Zeros('0').
Same queries result in error instead of warning in WINDOWS XP's MySql
I checked in both INNODB and MYISAM engines but same Result.
Please help me to know what happened beside processing.
You must not have SQL_MODE set to strict on you ubuntu installation.
Issue
SET SQL_MODE='STRICT_ALL_TABLES'
or add
SQL_MODE='STRICT_ALL_TABLES'
under [mysqld] to your my.cnf on Ubuntu.
I don't see the problem, you set the column to NOT NULL, (which doesn't allow NULL values) and now it won't let you set it to NULL, which would be the expected behaviour.
The reason you have 0s in your DB is because 0 would be the result of casting NULL to an int.
Related
If i modify the max number of allowed digits in MYSQL 5.7 from double(8,2) to double(12,2), is the change immediate or will it need to process all rows??
You can test this to see if it can be changed as an instant change:
mysql> create table mytable (id serial primary key, d double(8,2));
mysql> alter table mytable modify column d double(25,2), algorithm=inplace, lock=none;
Query OK, 0 rows affected, 1 warning (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 1
The options algorithm=inplace, lock=none mean you want the alter to run as an instant change, without performing a table copy. By default, MySQL runs the DDL change in that mode if the change can be done in that mode.
If you request it explicitly, but the change cannot be done in that mode, then you'll get an error.
For example:
mysql> alter table mytable modify column d float(8,2), algorithm=inplace, lock=none;
ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported.
Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.
In this example I'm changing the 8-byte DOUBLE to a 4-byte FLOAT. Any change to the size of a data type cannot be done without copying the table. So the request to do it as an instant change fails and the error shown is returned.
So if you're in doubt about whether a given change can be done instantly, you can use this method to test it. You don't have to do the test against your production table! I did this test on my local instance, without even adding any data to the table. I just created an empty table as I showed above, and ran the DDL.
You should read https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html for details on which types of DDL changes can be done inplace.
As written in the MySQL Reference Manual all ALTER Table statements needs processing time. On top of this MySQL will convert the data:
For data type changes using CHANGE or MODIFY, MySQL tries to convert existing column values to the new type as well as possible.
Therefore all Columns will be visited.
Beside the fact that specifying number of digits for floating point data types is deprecated, a change from double(8,2) to double(12,2) doesn't change the column type, it is still a double precision 8-byte number, so not even a single row will change it's value.
Example:
CREATE TABLE t1 (a double(6,2));
INSERT INTO t1 VALUES (1234.56);
# change the precision
ALTER TABLE t1 change a a double(4,2);
INSERT INTO t1 VALUES (1234.56);
ERROR 1264 (22003): Out of range value for column 'a' at row 1
SELECT a FROM t1;
+---------+
| a |
+---------+
| 1234.56 |
+---------+
Even if 1234.56 doesn't fit in double(4,2) it is still unchanged.
I have table with:
1) Encrypted_ID varchar (256)
2) Initialization Vector(iv)varchar(256).
I would like to decrypt the column value using the key
I am using:
select Cast(AES_DECRYPT(Encrypted_ID,'Key',InitializationVector_iv)as CHAR ) as DecryptedValue from MyTable;
The result is Null.
I Also tried:
select Cast(AES_DECRYPT(AES_ENCRYPT(Encrypted_ID,'Key',InitializationVector_iv),'Key') as CHAR ) as DecryptedValue from MyTable;
The result is blob for few rows.
I'm not sure what I'm doing wrong here. Can any one help with the syntax to decrypt the column when I have:
Key
Initialization Vector value
Encrypted Column
There's actually nothing wrong with your first query, syntactically it's spot on as this worked example demonstrates.
mysql> SET ##SESSION.block_encryption_mode = 'aes-256-cbc';
mysql> create table MyTable(
-> Encrypted_ID varbinary(256),
-> InitializationVector_iv varbinary(16)
-> );
Query OK, 0 rows affected (0.93 sec)
mysql> SET #iv = RANDOM_BYTES(16);
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO MyTable SET Encrypted_ID = AES_ENCRYPT('hello','key', #iv), InitializationVector_iv = #iv;
Query OK, 1 row affected (0.17 sec)
mysql> SELECT CAST(AES_DECRYPT(Encrypted_ID,'key', InitializationVector_iv) AS CHAR) from MyTable;
+------------------------------------------------------------------------+
| CAST(AES_DECRYPT(Encrypted_ID,'key', InitializationVector_iv) AS CHAR) |
+------------------------------------------------------------------------+
| hello |
+------------------------------------------------------------------------+
1 row in set (0.00 sec)
As for why it's not working, I managed to get the query to return NULL in 2 scenarios. One, you get NULL returned if you use a different iv for encryption and decryption, so you might want to look at how you are storing as the iv. Two, you get NULL where you have the block_encryption_mode variable set differently when storing and trying to retrieve the value, check that you're not accidentally reverting to the default 'aes-128-ebc between sessions. There may be others...
The second query will fail because you need to supply the iv to both of he encryption and decryption functions, you only use it to encrypt. Also, since you are taking the values from the MyTable, Encrypted_ID will already be encrypted and the effect of this query would be to encrypt it again, before reversing that to get you back to the stored (encrypted) value.
Finally, AES is only going to use 16 bytes of the iv so you might as well make that VARBINARY(16).
AES doesn't work with MySQL Workbench in my case.
I have to use the mysql console.
MySql Workbench worked for me.
In my Case, encrypted value was encoded in base 64.
So I had to decode base 64 value and IV Using "From_base64" function.
SET block_encryption_mode = 'aes-256-cbc';
set #k = 'Key';
set #iv = From_base64('InitializationVector');
set #v = from_base64('EncryptedValue');
select CAST(AES_DECRYPT(#v, #k, #iv) AS CHAR);
Please make sure the encryption type, base 64 encoding, Hex/Unhex of the values/Iv are correct before you start working on the decryption.
Review MYSql functions
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
Hope this helps for someone.
I am trying to execute this:
UPDATE WORKS_ON
SET Hours = 5.0
WHERE Essn=99988777 AND Pno=10;
and it threw an error,
19:07:29 UPDATE WORKS_ON SET Hours=5.0 WHERE Essn=99988777 AND Pno=10 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 Queries and reconnect. 0.000 sec
I did what it asks and reconnected, now there is no error but doesn't do anything
19:36:26 UPDATE WORKS_ON SET Hours= 5.0 WHERE Essn=99988777 AND Pno=10 0 row(s) affected Rows matched: 0 Changed: 0 Warnings: 0 0.000 sec
I want all Hours for people that have Essn=999887777 and Pno=10. Hours will change to 5.0
I hope you have already unchecked safe updates in preferences.
From what you have posted it looks like there are no matching rows where Essn=99988777 and Pnp=10 at the same time.If you can provide sample data and expected results it would be better
First set SQL_SAFE_UPDATES=0, It’s because you try to update a table without a WHERE that uses a KEY column, if you use PRIMARY KEY then it would not create that kind of problem, let's try this way
SET SQL_SAFE_UPDATES=0;
UPDATE WORKS_ON
SET Hours = 5.0
WHERE Essn=99988777 AND Pno=10;
EDIT
you can modify your query to follow the rule (use PRIMARY KEY on WHERE clause), if it doesn't work, then precede the (table name with the schema name) like
UPDATE your_schemaname.WORKS_ON
SET Hours = 5.0
WHERE Essn=99988777 AND Pno=10;
I'm looking for a query in which i can update a field , with certain where clauses which reside in different tables
To make sure that i do not update all fields which represent the id i need another where clause which also checks if a service_name like '%DISK%' exists in table service_members:
However this results in (logically):
ERROR 1093 (HY000): You can't specify target table 'service_members' for update in FROM clause
My best try so far:
My initial query is not stringent enough as it matches an id/hostname , this id is present multiple times:
update service_members
set check_command_data = replace(check_command_data,'80%!90%','90%!95%')
where host_name in (select id from host where host_name like '%server-01%');
Output:
Query OK, 3 rows affected (0.00 sec)
Rows matched: 23 Changed: 3 Warnings: 0
It works of course but will update the 'new value' which matches on the same 'old value'
What i need to realize in one query:
update in table service_members setting new value for field
check_command_data (works)
host_name needs to be a select id from table host where the host_name
is like %something% (works but not stringent enough)
service_name in table service_members needs to be a select like %DISK%
I'm a bit puzzled on how to get this query working in the right way , any advice/suggestions?
Thanks in advance!
Why won't this work:
USE presentations_db; UPDATE presentations_tbl SET `date` = '2012-12-13' WHERE `date` = '2013-12-12'
I have tried everywhere I could and can't find an answer.
date is the field name so used back ticks as required. date is of DATE data type.
I managed to get it to run through the commandline. I clicked on "command line client" and it asked for a password. I then ran the sql statement and got the following result:
mysql> UPDATE `presentations_db`.`presentations_tbl`SET date_ = '2012-12-13' WHERE date_ = '2013-12-12';
Query OK, 16 rows affected (0.06 sec) Rows matched: 16 Changed: 16 Warnings: 0
When I tried to run the same query by simply running mysql though a shell it came up with an error that the db can not be edited by localhost, which is more explanatory than 'Query interrupted'. This seems to be a gripe previously covered in http://bugs.mysql.com/bug.php?id=67766.
It would be nice if somone can tell me what I am doing wrong in workbench gui. I normally do the following when trying to run queries. I click on 'Edit Table Data' and the select the database and tables. It seems like I can view and run select queries but not update queries.