mysql: show truncated or modified data announced by warning - mysql

When INSERTing into a MYSQL table, I sometimes get a "Data truncated for column 'x' at row n" message and I'm unable to identify the data that was truncated or the resulting truncated value. I tried to use the column/row number but this didn't lead me to any truncated value.
Is my only option to verify the data insertion by selecting it from the target table and comparing it to the source data?

Your inserted data surpassed the maximum value for a given column type.If its from another table alter your destination table SCHEMA to be identical with the source table SCHEMA(at least for the columns which exist in both tables) and the problem will be solved.

Related

My Sql Bulk inserts loading the data with truncating

I am trying to load the data from CSV file to MYSql database through bulk insert option. Here are the below create table syntax and CSV file
CREATE TABLE discounts (
id INT NOT NULL ,
title VARCHAR(10) NOT NULL,
expired_date DATE NOT NULL,
amount VARCHAR(255 ) NOT NULL
);
CSV file format:
"475","Back","20140401","FFFF"
"476","bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","20140901","DDD"
SQL Query :
LOAD DATA INFILE 'C:\Users\karthick\Desktop\data.csv'
INTO TABLE discounts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';
In above create table syntax i have specified the column "title" data length to "10". But the value in data file for second row exceeds the length 10.
When i executed the SQL query the data are loaded successfully to MySQL database and here are the below output & My values in second row is getting truncated for the field "title". Could you please suggest how to stop loading the row without truncating it. Also it should load the next consecutive row without terminating if the data are appropriate. Please suggest
Database Output :
'475', 'Back', '2014-04-01', 'FFFF'
'476', 'bbbbbbbbbb', '2014-09-01', 'DDD'
Here is trick you may use. Assuming the maximum width title you want to persist is 100 characters, you may create the table as follows:
CREATE TABLE discounts (
id INT NOT NULL,
title VARCHAR(101) NOT NULL, -- slightly larger than desired max width
expired_date DATE NOT NULL,
amount VARCHAR(255 ) NOT NULL
);
Then load your data as you were doing. Records having titles which exceed a width of 100 would in fact have a width of 101 in your database table. Then, you may target such records for deletion:
DELETE
FROM discounts
WHERE LENGTH(title) > 100;
If you want, you can also now resize the title column to a width of exactly 100:
ALTER TABLE discounts MODIFY COLUMN title VARCHAR(100);
There might be a way to do this from LOAD DATA, but in general this tool is fairly simple and designed to just blindly load data into a MySQL table. LOAD DATA does have the ability to transform data as it is read, but I am not sure if it can block it.
As per my understanding, below are few points that you want to achieve:
1) Data should not get truncated if title length is more than specified field length as per table structure.
2) If title length is more, then that record should get skipped while doing an importing of records & rest of the process should continue ahead.
Answer as per mysql database taken into consideration:
You can make use of sql_mode as TRADITIONAL (Make MySQL behave like a “traditional” SQL database system. A simple description of this mode is “give an error instead of a warning” when inserting an incorrect value into a column. Reference: https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html)
Now after setting this mode, while doing records import, error will occur if any incorrect data or value out of range is getting to insert into table.
Next part, for out of range values their is no way to skip the error rows. You can check existing discussion link: Skip error lines while loading data to mysql table from delimited file
For skipping rows which are breaking unique constraints or possibly creating duplicate records, can be skipped using IGNORE keyword along with LOAD DATA INFILE.
Refer: https://dev.mysql.com/doc/refman/5.5/en/load-data.html

In MySQL, how to force insert into table if column not exist in field list?

I would like to insert records into a new database. I have an old database that contains fields that are no longer in the new database.
So when I execute the insertion request I get this message (field 'contract' unknown in field list).
I know it's normal but I would like to ignore this message and force the insertion.
There is no way to do this using MYSQL alone
i have a php script i use for stripping out data that there is no column for in the database table
i use SHOW COLUMNS FROM table to find out what columns are available, i then strip the data out that does not match these columns and then build the insert query to insert the data in the database minus the unavailable columns
the down side is that if you want to keep something from the orignal data and there is no column for it in the new table, then it will be discarded.

SSIS Lookup on data being imported

This is a classic does the record exist then update otherwise insert scenario. The dataflow process I am following is as follows:
OLE DB: return list of fields one of which is a calculated hashvalue on a number of the fields in the dataset
Lookup: See if the row has changed by comparing the hashvalue and another key field(allows duplicates) against the latest record in the target table for that key field.
If matches update the record, if no match then insert.
The problem I am having is that one of the records that I would match against is in the dataset I am importing and not in the target table. Therefore I always geta NO MATCH result. If I delete the row causing the error in the target table then re-run the import I do get a MATCH.
I have tried turning off the cache for the lookup with no success. Suffice to say I have searched for the answer with no luck. HELP

ERROR 1136: Column count doesn't match value count at row 1

I get the Error:
Column count doesn't match value count at row 1.
But I've checked and rechecked my query and everything seems ok:
UPDATE
table
SET
col = 'enum(''FOO'',''BAR'')'
WHERE
col1 = ''
AND
col2 = 'val2'
AND
col3 = 3;
I thought the table could have some triggers that were generating the error –I didn't design the system– but I can't find any.
I've found the same error with at least three different tables.
Note. The "enum" on line three is really supposed to be a string, not an enum type.
It could be a few things, but here are two ideas:
-There is a trigger that needs to be changed/removed.
-The value that you are updating the cell to exceeds the column length. Article on this.
Apparently there were some triggers that updated another database, I don't know why show triggers from <dbname> returned an empty row set.
Apparently, when migrating their system, they had to ask support to create the triggers for them.
Some time triggers create this issue as well. For example I have create a trigger to log the entries, when I add some field in master table and try to update the master table it display the same error. After some work around, I got the point and create the same field in log table and it fix the issue.

Empty lookup table vs non-matching lookup table (lookup transform)

Is empty lookup table the same as non-matching lookup table in lookup transform?
What would be the result if no row redirection is configured?
an empty result set or
package failure at the lookup transform
You could get #2: Package failure. It would not be able to find the row in the lookup table (since it's empty).
Edit: I should say that if you set the Error Configuration to Ignore Failure, you will get an empty rowset.