inserting hex value into mysql - mysql

Can there any way to insert a hex value into MYSQL?
I also want to be able to retreive it in hex form.
For example, something like:
INSERT INTO table ( hexTag )
VALUES ( HEX(0x41) );
And if I do this, I want it to put an 'A' into the table

For that particular use case, you can either insert the hex value directly and it will be interpreted as a string, or use HEX() to input and UNHEX() to output
mysql> create table hexTable(pseudoHex varchar(50));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into hexTable values (0x41);
Query OK, 1 row affected (0.00 sec)
mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| A |
+-----------+
1 row in set (0.00 sec)
mysql> select HEX(pseudoHex) from hexTable;
+----------------+
| HEX(pseudoHex) |
+----------------+
| 41 |
+----------------+
1 row in set (0.00 sec)
mysql> delete from hexTable;
Query OK, 1 row affected (0.00 sec)
mysql> insert into hexTable values (HEX('A'));
Query OK, 1 row affected (0.00 sec)
mysql> select UNHEX(pseudoHex) from hexTable;
+------------------+
| UNHEX(pseudoHex) |
+------------------+
| A |
+------------------+
1 row in set (0.00 sec)
mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| 41 |
+-----------+
1 row in set (0.00 sec)
See these links.

Related

MySQL Insert ENUM field is left blank

So i'm trying to insert data with the query below. The columns releases_isMultipack, releases_isChase, releases_hasChase and releases_isAssortment are all enum('0', '1') type. 0 and 1 represent false and true.
INSERT INTO releases (releases_uid, releases_title, releases_releaseDate, releases_boxNumber, releases_hobbyDbId, releases_isMultipack, releases_itemNumber, releases_isChase, releases_hasChase, releases_referenceUrl, releases_componentNumber, releases_isAssortment, releases_craftProductId, releases_craftComponentId)
VALUES ('fa4d5128-407a-4c2b-8970-99a36a72b030', 'Woodsy Owl', '2021-03-26T16:05:00-07:00', '', NULL, 0, '52390', 0, 1, 'woodsy-owl-1', '52390a', 0, 9518039, 9518035)
The query works fine, however just leaves the ENUM columns blank even though a value is provided in the insert statement.
Does anyone know why this is happening?
There's a difference between 0 and '0'.
Here's a demo. I get a blank if I insert 0 because that's not the value in the enum.
mysql> create table releases (releases_isMultipack enum('0','1'));
Query OK, 0 rows affected (0.06 sec)
mysql> insert into releases values (0);
Query OK, 1 row affected, 1 warning (0.03 sec)
mysql> show warnings;
+---------+------+-----------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------+
| Warning | 1265 | Data truncated for column 'releases_isMultipack' at row 1 |
+---------+------+-----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from releases;
+----------------------+
| releases_isMultipack |
+----------------------+
| |
+----------------------+
1 row in set (0.00 sec)
But it works if I use '0':
mysql> insert into releases values ('0');
Query OK, 1 row affected (0.03 sec)
mysql> select * from releases;
+----------------------+
| releases_isMultipack |
+----------------------+
| |
| 0 |
+----------------------+

how to use mysql rewriter plugin?

I am trying to use mysql default rewrite plugin as per their instructions but it is giving me some grief. Simply put after activating rules the plugin is simply not working.
Below is my table and rules but simply queries are not reporting correct results. Can someone tell me what am i doing wrong?
Thanks.
mysql> select creditcard_number from mihir_test.creditcard_info where name = "mihir";
+-------------------+
| creditcard_number |
+-------------------+
| 1234567890123456 |
+-------------------+
1 row in set, 1 warning (0.00 sec)
mysql> insert into query_rewrite.rewrite_rules ( pattern, replacement) values ( 'select creditcard_number from mihir_test.creditcard_info where name = ?', 'select name, creditcart_number from mihir_test.creditcard_info where name = ?' );
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql>
mysql> CALL query_rewrite.flush_rewrite_rules();
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> select creditcard_number from mihir_test.creditcard_info where name = "mihir"; +-------------------+
| creditcard_number |
+-------------------+
| 1234567890123456 |
+-------------------+
1 row in set, 1 warning (0.00 sec)
mysql>

Any way of using fully qualified table name with CREATE TABLE

When I use CREATE TABLE tbl_name I can only specify a table in database I currently use.
Is there any way to CREATE TABLE tbl_name in database1 without a prior USE database1?
Try:
mysql> CREATE DATABASE `test_1`;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE `test_2`;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> CREATE TABLE `test_1`.`table_1` (`column_1` BOOL);
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE `test_2`.`table_2` (`column_1` BOOL);
Query OK, 0 rows affected (0.01 sec)
mysql> USE `test_1`;
Database changed
mysql> SHOW TABLES;
+------------------+
| Tables_in_test_1 |
+------------------+
| table_1 |
+------------------+
1 row in set (0.00 sec)
mysql> USE `test_2`;
Database changed
mysql> SHOW TABLES;
+------------------+
| Tables_in_test_2 |
+------------------+
| table_2 |
+------------------+
1 row in set (0.00 sec)

When setting system variable 'sql_warnings' to 0, why still get the warning?

Recreating SQLs:
drop table t1;
set sql_mode = '';
create table t1 (c1 int, c2 int);
set sql_warnings = 0;
SHOW VARIABLES LIKE '%sql_warnings%';
insert into t1 values(1,'b');
set sql_warnings = 1;
SHOW VARIABLES LIKE '%sql_warnings%';
insert into t1 values(1,'b');
Output:
mysql> create table t1 (c1 int, c2 int);
Query OK, 0 rows affected (0.01 sec)
mysql> set sql_warnings = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE '%sql_warnings%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_warnings | OFF |
+---------------+-------+
1 row in set, 1 warning (0.01 sec)
mysql> insert into t1 values(1,'b');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql>
mysql> set sql_warnings = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE '%sql_warnings%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_warnings | ON |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
mysql> insert into t1 values(1,'b');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.8-rc |
+-----------+
1 row in set (0.00 sec)
From the output, no matter setting sql_warnings to 0 or 1, I am always getting the warnings in the INSERT statement.
Anyone knows why?
Update:
Result for "SHOW VARIABLES LIKE '%sql_warnings%';" is added above.
Filed a bug to MySQL and they verified it as a bug from version 5.0 to 5.7.
http://bugs.mysql.com/bug.php?id=80404
I am curious why this obvious behavior is not found before.

mysql delete triggers

I think I read that the delete trigger doesn't know what data was deleted and loops over the whole table applying the trigger. Is that true?
Does that mean that the before delete loops over the whole table before the data is deleted and after delete loops over the whole table after the delete occurs?
Is there no way to loop over just the deleted records? So If 10 records are deleted loop over them?
DELIMITER $$
DROP TRIGGER `before_delete_jecki_triggername`$$
CREATE TRIGGER before_delete_triggername
BEFORE DELETE ON table
FOR EACH ROW
BEGIN
/*do stuff*/
END$$
DELIMITER ;
Thanks,
Mat
I think it was due to a confusion with FOR EACH ROW statement.
It is only for "matched records for the statement issued before trigger is invoked."
If there exists N number of records in a table and matches records for where id=x,
assuming x causes a result of less than N records, say N-5, then
FOR EACH ROW causes a loop for N-5 times only.
UPDATE:
A sample test run on the rows affected due to FOR EACH ROW statement is shown below.
mysql> -- create a test table
mysql> drop table if exists tbl; create table tbl ( i int, v varchar(10) );
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.06 sec)
mysql> -- set test data
mysql> insert into tbl values(1,'one'),(2,'two' ),(3,'three'),(10,'ten'),(11,'eleven');
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tbl;
+------+--------+
| i | v |
+------+--------+
| 1 | one |
| 2 | two |
| 3 | three |
| 10 | ten |
| 11 | eleven |
+------+--------+
5 rows in set (0.02 sec)
mysql> select count(*) row_count from tbl;
+-----------+
| row_count |
+-----------+
| 5 |
+-----------+
1 row in set (0.00 sec)
mysql>
mysql> -- record loop count of trigger in a table
mysql> drop table if exists rows_affected; create table rows_affected( i int );
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.05 sec)
mysql> select count(*) 'rows_affected' from rows_affected;
+---------------+
| rows_affected |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql>
mysql> set #cnt=0;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> -- drop trigger if exists trig_bef_del_on_tbl;
mysql> delimiter //
mysql> create trigger trig_bef_del_on_tbl before delete on tbl
-> for each row begin
-> set #cnt = if(#cnt is null, 1, (#cnt+1));
->
-> /* for cross checking save loop count */
-> insert into rows_affected values ( #cnt );
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> delimiter ;
mysql>
mysql> -- now let us test the delete operation
mysql> delete from tbl where i like '%1%';
Query OK, 3 rows affected (0.02 sec)
mysql>
mysql> -- now let us see what the loop count was
mysql> select #cnt as 'cnt';
+------+
| cnt |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
mysql>
mysql> -- now let us see the table data
mysql> select * from tbl;
+------+-------+
| i | v |
+------+-------+
| 2 | two |
| 3 | three |
+------+-------+
2 rows in set (0.00 sec)
mysql> select count(*) row_count from tbl;
+-----------+
| row_count |
+-----------+
| 2 |
+-----------+
1 row in set (0.00 sec)
mysql> select count(*) 'rows_affected' from rows_affected;
+---------------+
| rows_affected |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec)
mysql>