I have two tables with different schemas:
Base A, table T1:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`uid` int(11) NOT NULL DEFAULT '0',
`language` varchar(12) NOT NULL DEFAULT ''
Base B, table T2:
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Type` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`UserID` int(11) NOT NULL,
`Name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
I need to transfer data from T1 to T2 in this way:
A.T1.id -> B.T2.ID
A.T1.title -> B.T2.Name
A.T1.uid -> B.T2.UserID
As you can see fields A.T1.language and B.T2.Type are not needed.
I think I should do this migration through dump of CSV. But this is all I have come up to.
Any idea?
UPDATE
Thank you guys for your answers. Please forgive me for not being clear enough, I should have emphasized that my tables are in different bases, and even on different servers. So it is not as easy as to just insert fields from one table into another.
You can do it with a combination of UPDATE and SELECT query. However since TABLE 1 has the column title which is of type VARCHAR(255) and TABLE 2 has the column Name which is of type VARCHAR(100) might give a problem.
The following query can do this migration however any row with column title having length more than 100 will be SHORTENED to 100.
INSERT INTO T2
(ID, Name, UserID)
SELECT id, SUBSTR(title, 0, 100), uid
FROM T1
Use the INSERT ... SELECT syntax as in
INSERT INTO `B`.`T2` (`ID`, `Name`, `UserID`)
SELECT `id`, `title`, `uid` FROM `A`.`T1`
Are they on the same database? In that case:
INSERT INTO T2 (ID, Name, UserID)
SELECT id, title, uid FROM T1
I there any reason why you have to add the limitation to your int fields such as INT(10) instead of just INT?
The explanation of your data transfer does not coincide with your base tables?
Anyway, the problem you might run int here is that some of your column limitations are different so you either have to make them the same or SUBSTRING them into the destination table if the string from the source column is longer than the destination column for instance if you try to insert "This is my string" into a VARCHAR(10) column, you will get a truncate error.
To insert the data into the destination table you can use this:
INSERT INTO [Destination Table] (ID, Name, uid)
SELECT
ID,
SUBSTRING(title, 0, 100) as 'Name',
uid
FROM
[Source Table]
This will work yet you will be sacrificing the data on the name column. I would suggest giving your destination columns the same data type and limitations as your source table.
Related
I'm new to MySQL & I try to enter records to mysql table. I'm getting following error
INSERT INTO advertising.discountauthorizationrequst SET DARDateTime=cast('2003-01-13 16:50:32' as datetime), `DARPubCode`=trim('DD'), `DARPubDate`=cast('2022-05-08' as date), `DARAutUser`=trim("U0001"), `DARDeviceID`=trim('123456789ABCDEFGHIJKL987456'), `DARMessage`=trim("This Is Test Message"), `DARGranted`=("0"), `DARUser`=trim("DATAENTRYUSERNAME") Error Code: 1054. Unknown column 'DARDateTime' in 'field list'
I listed my INSERT statement below. Someone please help me to solve this issue. I'm using mysql workbench 8.0.
Columns:
DARDateTime datetime PK
DARPubCode varchar(3) PK
DARPubDate date PK
DARAutUser varchar(5)
DARDeviceID varchar(50)
DARMessage varchar(100)
DARGranted varchar(1)
DARUser varchar(50) PK
Here is script
INSERT INTO `advertising`.`discountauthorizationrequst`
SET
`DARDateTime`=cast('2003-01-13 16:50:32' as datetime),
`DARPubCode`=trim('DD'),
`DARPubDate`=cast('2022-05-08' as date),
`DARAutUser`=trim("U0001"),
`DARDeviceID`=trim('123456789ABCDEFGHIJKL987456'),
`DARMessage`=trim("This Is Test Message"),
`DARGranted`=("0"),
`DARUser`=trim("DATAENTRYUSERNAME");
Edited..
Table Inspactor - DDL
CREATE TABLE `discountauthorizationrequst` (
`DARDateTime` datetime NOT NULL,
`DARPubCode` varchar(3) NOT NULL,
`DARPubDate` date NOT NULL,
`DARAutUser` varchar(5) DEFAULT NULL,
`DARDeviceID` varchar(50) DEFAULT NULL,
`DARMessage` varchar(100) DEFAULT NULL,
`DARGranted` varchar(1) DEFAULT NULL,
`DARUser` varchar(50) NOT NULL,
PRIMARY KEY (`DARDateTime`,`DARPubCode`,`DARPubDate`,`DARUser`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
You are actually confusing the SQL commands and coming up with a hybrid of them. The INSERT command most commonly is done in two ways..
insert into SomeTable
( these, columns )
values
( oneValue, anotherValue)
or
insert into SomeTable( these, columns )
select oneColumn, secondColumn
from SomeOtherTable
where SomeCondition
The UPDATE command is based on an EXISTING record that you want to change
Update SomeTable set
thisColumn = SomeValue,
anotherColumn = SomeOtherValue
where SomeCondition
So, what you appear to be doing would be written as
INSERT INTO advertising.discountauthorizationrequst
( DARDateTime,
DARPubCode,
DARPubDate,
DARAutUser,
DARDeviceID,
DARMessage,
DARGranted,
DARUser
)
values
(
cast('2003-01-13 16:50:32' as datetime),
'DD',
'2022-05-08',
'U0001',
'123456789ABCDEFGHIJKL987456',
'This Is Test Message',
'0',
'DATAENTRYUSERNAME'
)
Notice the readability with formatting, you can see each column that is needed followed by the explicit values (which could be parameterized during code later) are in the same ordinal context. So, if you ever needed to add a new column to the insert, easy to do with the same ordinal position in the values provided secondarily to it.
As for the 3rd column, by providing a string in YYYY-MM-DD, SQL typically auto-converts to a date format. Other fields, you dont need to explicitly TRIM() everything. If parameterized, you would pass the trimmed VALUE, when you get to that point in your development.
I found the mistake that I made. I created triggers for the above table. After I deleted those triggers its working.
I have a MYSQL table, with 5 columns in it:
id bigint
name varchar
description varchar
slug
Can I get MySQL to automatically generate the value of slug as a 256 Bit Hash of name+description?
I am now using PHP to generate an SHA256 value of the slug prior to saving it.
Edit:
By automatic, I mean see if it's possible to change the default value of the slug field, to be a computed field that's the sha256 of name+description.
I already know how to create it as part of an insert operation.
MySQL 5.7 supports generated columns so you can define an expression, and it will be updated automatically for every row you insert or update.
CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug varchar(64) AS (SHA2(CONCAT(name, description), 256)) STORED NOT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
If you use an earlier version of MySQL, you could do this with TRIGGERs:
CREATE TRIGGER MySlugIns BEFORE INSERT ON MyTable
FOR EACH ROW SET slug = SHA2(CONCAT(name, description));
CREATE TRIGGER MySlugUpd BEFORE UPDATE ON MyTable
FOR EACH ROW SET slug = SHA2(CONCAT(name, description), 256);
Beware that concat returns NULL if any one column in the input is NULL. So, to hash in a null-safe way, use concat_ws. For example:
select md5(concat_ws('', col_1, .. , col_n));
Use MySQL's CONCAT() to combine the two values and SHA2() to generate a 256 bit hash.
CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`description` varchar(50) NOT NULL,
`slug` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `mytable` (`name`,`description`,`slug`)
VALUES ('Fred','A Person',SHA2(CONCAT(`name`,`description`),256));
SELECT * FROM `mytable`
OUTPUT:
COLUMN VALUE
id 1
name Fred
description A Person
slug ea76b5b09b0e004781b569f88fc8434fe25ae3ad17807904cfb975a3be71bd89
Try it on SQLfiddle.
I would like to copy a SQL's row into the same table.
But in my table, I've a 'text' column.
With this SQL:
CREATE TEMPORARY TABLE produit2 ENGINE=MEMORY SELECT * FROM produit WHERE pdt_ID = 'IPSUMS';
UPDATE produit2 SET pdt_ID='ID_TEMP';
INSERT INTO produit SELECT * FROM produit2;
DROP TABLE produit2;
I get this error :
#1163 - The used table type doesn't support BLOB/TEXT columns
Here is my table :
pdt_ID varchar(6)
pdt_nom varchar(130)
pdt_stitre varchar(255)
pdt_accroche varchar(255)
pdt_desc text
pdt_img varchar(25)
pdt_pdf varchar(10)
pdt_garantie varchar(80)
edit_ID varchar(7)
scat_ID int(11)
pdt_asso1 char(3)
pdt_asso2 char(3)
pdt_online tinyint(4)
It's possible to help me to duplicate row ? How?
You can't store TEXT-columns (which really are blobs) in memory tables. See here
Depending on your ultimate goal, you may insert a md5-hash of the TEXT-column instead to preserve entity identity. Otherwise you need to put pdt_desc and such into another table and refer to it's primary key - that will save you some storage/memory too.
I have two MySQL tables and want to insert multiple records instead of creating one by one, get id and insert related records
here are the tables:
CREATE TABLE `visit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip_address` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `visitmeta` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`page_visit_id` int(11) NOT NULL,
`key` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
Currently I insert one record on visit, get its id and insert records on visit meta. Is there a way to create a new record into visit and in the same query create visit meta records?
It's not possible to insert records in two tables with a single query, but you can do it in just two queries using MySQL's LAST_INSERT_ID() function:
INSERT INTO visit
(ip_address)
VALUES
('1.2.3.4')
;
INSERT INTO visitmeta
(page_visit_id, key, value)
VALUES
(LAST_INSERT_ID(), 'foo', 'bar'),
(LAST_INSERT_ID(), 'baz', 'qux')
;
Note also that it's often more convenient/performant to store IP addresses in their raw, four-byte binary form (one can use MySQL's INET_ATON() and INET_NTOA() functions to convert to/from such form respectively).
I want to copy all the fields from the temp_sales table to sales table (after specific field). I want to do it quickest way as possible.. How to do this in SQL?
CREATE TABLE IF NOT EXISTS `temp_sales` (
`field1` varchar(10) NOT NULL,
`field2` varchar(20) NOT NULL,
`field3` varchar(20) NOT NULL,
`field4` varchar(50) NOT NULL,
`field5` varchar(50) NOT NULL,
`field6` varchar(50) NOT NULL,
`field7` varchar(50) NOT NULL,
`field8` tinyint(2) NOT NULL,
`field9` tinyint(2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13692 ;
In other word: There are some fields in the sales table. I want to add more new fields in the sales table from temp_sales (without data).
This is a 3-Step process:
.1. You need some unique or primary key field in temp_sales, that links a row in temp_sales to the corresponding row in sales (again via a unique/primary key)
.2. You need a DDL statement such as
ALTER TABLE `sales`
ADD COLUMN `field1`VARCHAR(10) AFTER `whatever`,
ADD COLUMN `field2`VARCHAR(10) AFTER `fields2`,
...
.3. You need a DML statement such as
UPDATE `sales`
INNER JOIN `temp_sales` ON `sales`.`keyfield`=`temp_sales`.`keyfield`
SET `sales`.`field1`=`tempsales`.`field1`,
`sales`.`field2`=`tempsales`.`field2`,
...
You can do this:
mysql> create table new_table_name as(select * from existing_table_name);
//It will also populate existing table data to new table if present
//If old table is empty then it will create same copy of that table
In your case:
mysql> create table sales as(select * from temp_sales);