I have as table with this column and type:
sales_channel_id BINARY(16)
In the row I have this value: 5DBACA1114B24872ACCFE679037DF670
I have written this value into another table, but this time the table column has the type VARCHAR(255). In the table I see this value: 5dbaca1114b24872accfe679037df670 (no capitals).
Now I have created another table with a column of type BINARY(16). When I make something like this to transform the data from the varchar column to the new column like this:
INSERT INTO
setting_sales_channel (sales_channel_id)
SELECT sales_channel_id from mcn_setting
I get the error: Query 1 ERROR: Data too long for column 'sales_channel_id' at row 1
Why does this happen and how can I transfer the data from the VARCHAR column into the new column which is of type BINARY(16)?
Try this.
change your query to:
INSERT INTO
setting_sales_channel (sales_channel_id)
SELECT CONVERT(sales_channel_id,BINARY(16)) from mcn_setting;
sample (MariaDB)
MariaDB [bernd]> show create table b;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| b | CREATE TABLE `b` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`bbinary` binary(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)
MariaDB [bernd]> show create table bs;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bs | CREATE TABLE `bs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`bvarchar` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)
MariaDB [bernd]> SELECT * from b;
Empty set (0.002 sec)
MariaDB [bernd]> SELECT * from bs;
+----+----------------------------------+
| id | bvarchar |
+----+----------------------------------+
| 1 | 5dbaca1114b24872accfe679037df670 |
+----+----------------------------------+
1 row in set (0.000 sec)
MariaDB [bernd]>
MariaDB [bernd]> INSERT INTO b (bbinary)
-> SELECT CONVERT(bvarchar,BINARY(16)) FROM bs;
Query OK, 1 row affected, 1 warning (0.003 sec)
Records: 1 Duplicates: 0 Warnings: 1
MariaDB [bernd]> SELECT * from bs;
+----+----------------------------------+
| id | bvarchar |
+----+----------------------------------+
| 1 | 5dbaca1114b24872accfe679037df670 |
+----+----------------------------------+
1 row in set (0.000 sec)
MariaDB [bernd]>
In my case it worked when using UNHEX now. The result looked like this:
INSERT INTO setting_sales_channel (sales_channel_id)
SELECT UNHEX(sales_channel_id) from setting;
Related
Hi all,
I have 2 column in my table (Start Date & End Date), which are actually text. I want to convert them to timestamp format but I'm not sure how to do it. The query below is what I have tried but doesn't work:
ALTER TABLE mytable
MODIFY COLUMN STR_TO_DATE(`Start Date`,"%m/%d/%Y %H:%i") TIMESTAMP,
MODIFY COLUMN STR_TO_DATE(`End Date`,"%m/%d/%Y %H:%i") TIMESTAMP;
May I know how should I alter the data type for these 2 columns in my table? Any help or advise will be greatly appreciated!
Let's say this is our initial column definition state:
mysql> SHOW CREATE TABLE mytable;
+---------+------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------+
| mytable | CREATE TABLE `mytable` (
`Start_Date` text,
`End_Date` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
First you need to use update :
UPDATE mytable SET End_Date = STR_TO_DATE(End_Date,'%m/%d/%Y %H:%i');
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> SELECT * FROM mytable;
+---------------------+---------------------+
| Start_Date | End_Date |
+---------------------+---------------------+
| 2022-10-16 10:35:00 | 2022-10-16 10:40:00 |
| 2022-10-16 09:18:00 | 2022-10-16 09:25:00 |
+---------------------+---------------------+
2 rows in set (0.00 sec)
Then Alter table to change column data type to 'timestamp':
mysql> ALTER TABLE mytable MODIFY COLUMN Start_Date TIMESTAMP, MODIFY COLUMN End_Date TIMESTAMP;
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
If we test it,
mysql> SHOW CREATE TABLE mytable;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| mytable | CREATE TABLE `mytable` (
`Start_Date` timestamp NULL DEFAULT NULL,
`End_Date` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
I have two different database DB1 and DB2. Each has two different tables Table A and Table B respectively. So in case If any new row generates in table B I want trigger fire which gives me date and time in table A at same time when new row generated in table B. Now I have created one trigger as below but it's working.
DELIMITER $$
USE `DB2`$$
DROP TRIGGER /*!50032 IF EXISTS */ `trigger_update_date_time_table_A`$$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `trigger_update_date_time_tableA` AFTER INSERT ON table_B
FOR EACH ROW
BEGIN
UPDATE DB1.table_A SET Date = CURRENT_TIMESTAMP WHERE id = NEW.id
END;
$$
DELIMITER ;
You are going to have to be a bit more specific about why you think the trigger does not produce the expected results with an example. Here's and example of how you could do that.BTW if you work your way through this you will see the trigger works as coded.
MariaDB [SANDBOX]> USE SANDBOX;
Database changed
MariaDB [SANDBOX]> SHOW CREATE TABLE T;
+-------+---------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------+
| T | CREATE TABLE `t` (
`ID` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [SANDBOX]> USE TEST;
Database changed
MariaDB [TEST]> SHOW CREATE TABLE TEST.T;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| T | CREATE TABLE `t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`price` int(11) DEFAULT NULL,
`tax` int(11) DEFAULT NULL,
`pricetax` int(11) AS (price + price *tax/100) VIRTUAL,
`DATE` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [TEST]> USE SANDBOX;
Database changed
MariaDB [SANDBOX]> SHOW CREATE TRIGGER T;
+---------+--------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Trigger | sql_mode | SQL Original Statement | character_set_client | collation_connection | Database Collation |
+---------+--------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| T | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`#`localhost` TRIGGER T AFTER INSERT ON T
FOR EACH ROW
BEGIN
UPDATE TEST.T SET Date = CURRENT_TIMESTAMP WHERE id = NEW.id ;
END | utf8mb4 | utf8mb4_general_ci | latin1_swedish_ci |
+---------+--------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
MariaDB [SANDBOX]> TRUNCATE TABLE T;
Query OK, 0 rows affected (0.22 sec)
MariaDB [SANDBOX]> SELECT * FROM T;
Empty set (0.00 sec)
MariaDB [SANDBOX]> UPDATE TEST.T SET DATE = NULL WHERE ID = 999;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [SANDBOX]> SELECT ID,DATE FROM TEST.T;
+-----+------+
| ID | DATE |
+-----+------+
| 1 | NULL |
| 999 | NULL |
+-----+------+
2 rows in set (0.00 sec)
MariaDB [SANDBOX]> INSERT INTO T (ID) VALUES (999);
Query OK, 1 row affected (0.02 sec)
MariaDB [SANDBOX]> SELECT ID,DATE FROM TEST.T;
+-----+------------+
| ID | DATE |
+-----+------------+
| 1 | NULL |
| 999 | 2018-09-13 |
+-----+------------+
2 rows in set (0.00 sec)
I was wondering how to make MySQL automatically have a column that adds 1 to every row that is made (Row 1 will have ID 1, Row 2 will get ID 2, etc.)
For example:
Every time a new user signs up on a website, they are assigned an ID number. Starting at 1, then 2, etc.
ID|Username|Password
1 |Bob |drowssaP
2 |Jill |cats
Try AUTO_INCREMENT. Documentation here
If you add, that magic word :) to your table creation declaration, it will do the magic for you :)
mysql> CREATE TABLE Users (
-> ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Username varchar(255) NOT NULL,
-> Password varchar(255) NOT NULL
-> );
Query OK, 0 rows affected (0.52 sec)
mysql> INSERT INTO Users(Username, Password) VALUES('vladimir', 'ilich_lenin');
Query OK, 1 row affected (0.12 sec)
mysql> INSERT INTO Users(Username, Password) VALUES('friedrich', 'engels');
Query OK, 1 row affected (0.04 sec)
mysql> SELECT * FROM Users;
+----+-----------+-------------+
| ID | Username | Password |
+----+-----------+-------------+
| 1 | vladimir | ilich_lenin |
| 2 | friedrich | engels |
+----+-----------+-------------+
2 rows in set (0.02 sec)
EDIT
mysql> CREATE TABLE Persons ( ID int NOT NULL PRIMARY KEY, Username varchar(255) NOT NULL, Password varchar(255) NOT NULL);
Query OK, 0 rows affected (0.25 sec)
mysql> SHOW CREATE TABLE
CREATE TABLE `Persons` (
`ID` int(11) NOT NULL,
`Username` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)
mysql> ALTER TABLE Persons MODIFY ID INTEGER NOT NULL AUTO_INCREMENT;
Query OK, 0 rows affected (0.50 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE Persons;
CREATE TABLE `Persons` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Username` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)
mysql> INSERT INTO Persons(Username, Password) VALUES('friedrich', 'engels');
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO Persons(Username, Password) VALUES('karl', 'marx');
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM Persons
-> ;
+----+-----------+----------+
| ID | Username | Password |
+----+-----------+----------+
| 1 | friedrich | engels |
| 2 | karl | marx |
+----+-----------+----------+
2 rows in set (0.00 sec)
Is there any way to accomplish below sql result as a one liner.
create table test( id int not null primary key auto_increment, name char(10));
insert into test (name) values ('voda'+ this_value_of_id);
// so select would return
MariaDB [testdb]> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | foo1 |
+----+------+
Yes, I know the other way is
begin transaction;
insert into test (name) values ('voda');
update test set name = concat('voda', id) where id = 1;
commit;
An option or approach can be via a Virtual (Computed) Columns.
Example:
MariaDB [testdb]> DROP TABLE IF EXISTS `test`;
Query OK, 0 rows affected (0.00 sec)
MariaDB [testdb]> CREATE TABLE `test` (
-> `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> `name` CHAR(10),
-> `nameid` VARCHAR(20) AS (CONCAT(`name`, `id`)) VIRTUAL
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [testdb]> INSERT INTO `test`
-> (`name`)
-> VALUES
-> ('foo'), ('voda');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [testdb]> SELECT
-> `id`,
-> `nameid` `name`
-> FROM
-> `test`;
+----+-------+
| id | name |
+----+-------+
| 1 | foo1 |
| 2 | voda2 |
+----+-------+
2 rows in set (0.00 sec)
So I have the following table:
mysql> show create table user_api_skills \G
*************************** 1. row ***************************
Table: user_api_skills
Create Table: CREATE TABLE `user_api_skills` (
`characterID` int(11) NOT NULL,
`typeID` int(11) NOT NULL,
`level` enum('0','1','2','3','4','5') NOT NULL DEFAULT '0',
`skillpoints` int(11) NOT NULL,
`currentTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`characterID`,`typeID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
And in that table a row which I am trying to insert/update:
mysql> SELECT * FROM `user_api_skills` WHERE `characterID` =93192782 AND `typeID` =3359;
+-------------+--------+-------+-------------+---------------------+
| characterID | typeID | level | skillpoints | currentTime |
+-------------+--------+-------+-------------+---------------------+
| 93192782 | 3359 | 3 | 135765 | 2013-09-30 16:58:35 |
+-------------+--------+-------+-------------+---------------------+
1 row in set (0.00 sec)
I believe my query is correctly formed and when executed it doesn't throw any errors or warnings:
mysql> INSERT INTO user_api_skills (characterID,typeID,level,skillpoints)
VALUES (93192782,3359,4,135765) ON DUPLICATE KEY UPDATE level=4,
skillpoints=135765,currentTime=NOW();
Query OK, 2 rows affected (0.22 sec)
I get 2 rows updated (as I would expect from an insert on dup update)
mysql> SELECT * FROM `user_api_skills` WHERE `characterID` =93192782 AND `typeID` =3359;
+-------------+--------+-------+-------------+---------------------+
| characterID | typeID | level | skillpoints | currentTime |
+-------------+--------+-------+-------------+---------------------+
| 93192782 | 3359 | 3 | 135765 | 2013-09-30 16:59:13 |
+-------------+--------+-------+-------------+---------------------+
1 row in set (0.00 sec)
mysql>
but the row itself only changes a single value (the currentTime). Can anybody explain why the other two fields are not updating?
Sorry, I have solved this myself. The level field is an ENUM and the query specified the new value as a number. Updating the query to the following resulted in the expected results.
mysql> INSERT INTO user_api_skills (characterID,typeID,level,skillpoints) VALUES
(93192782,3359,4,135765) ON DUPLICATE KEY UPDATE level='4', skillpoints=135765,
currentTime=NOW();
By providing a int to the update it updated to the one based number choice of the enum, so in this instance, the 4th choice is '3'.