Keywords: MySQL Error 1265 | Data Truncation | LOAD DATA INFILE | Data Type Mismatch | Strict Mode
Abstract: This article provides an in-depth analysis of MySQL Error Code 1265 'Data truncated for column', examining common data type mismatches during data loading operations. Through practical case studies, it explores INT data type range limitations, field delimiter configuration errors, and the impact of strict mode on data validation. Multiple effective solutions are presented, including data verification, temporary table strategies, and LOAD DATA syntax optimization.
Problem Background and Error Analysis
In MySQL database operations, Error Code 1265 indicates a data truncation error, typically occurring when inserting or loading data into a table where the input data length or format does not match the target column definition. This error not only affects data integrity but can also cause entire data loading processes to fail.
Practical Case Study
Consider a specific scenario: a user encounters Error 1265 when attempting to load data from a text file into a Pickup table. The table structure is defined as follows:
CREATE TABLE Pickup (
PickupID INT NOT NULL,
ClientID INT NOT NULL,
PickupDate DATE NOT NULL,
PickupProxy VARCHAR(40),
PickupHispanic BIT DEFAULT 0,
EthnCode VARCHAR(2),
CategCode VARCHAR(2) NOT NULL,
AgencyID INT(3) NOT NULL,
PRIMARY KEY (PickupID),
FOREIGN KEY (CategCode) REFERENCES Category(CategCode),
FOREIGN KEY (AgencyID) REFERENCES Agency(AgencyID),
FOREIGN KEY (ClientID) REFERENCES Clients(ClientID),
FOREIGN KEY (EthnCode) REFERENCES Ethnicity(EthnCode)
);The data sample from the text file appears as:
1065535,7709,1/1/2006,,0,,SR,6
1065536,7198,1/1/2006,,0,,SR,7
1065537,11641,1/1/2006,,0,W,SR,24
1065538,9805,1/1/2006,,0,N,SR,17
1065539,7709,2/1/2006,,0,,SR,6
1065540,7198,2/1/2006,,0,,SR,7
1065541,11641,2/1/2006,,0,W,SR,24When using the command LOAD DATA INFILE 'Pickup_withoutproxy2.txt' INTO TABLE pickup; to load the data, the system throws the error: Error Code: 1265. Data truncated for column 'PickupID' at row 1.
Root Cause Investigation
The core cause of Error 1265 is data type mismatch. In MySQL, the signed INT data type ranges from -2147483648 to 2147483647. If values in the text file's PickupID column exceed this range or contain non-numeric characters, data truncation errors will occur.
Additionally, the LOAD DATA INFILE command defaults to using tab characters as field delimiters, while the sample data uses commas. This configuration mismatch causes parsing errors, incorrectly assigning data to columns and triggering type mismatches.
Solutions and Best Practices
Data Validation and Cleaning: First, verify that data in the text file conforms to the target column data type requirements. For INT columns, ensure all values are within the valid range and contain no extra characters.
Temporary Table Strategy: For large files, initially load data into a temporary table using lenient data types like VARCHAR. After loading, validate and transform data through SQL queries before inserting into the target table. Example code:
CREATE TEMPORARY TABLE temp_pickup (
PickupID VARCHAR(20),
ClientID VARCHAR(20),
PickupDate VARCHAR(20),
PickupProxy VARCHAR(40),
PickupHispanic VARCHAR(5),
EthnCode VARCHAR(2),
CategCode VARCHAR(2),
AgencyID VARCHAR(5)
);
LOAD DATA INFILE 'Pickup_withoutproxy2.txt'
INTO TABLE temp_pickup
FIELDS TERMINATED BY ',';
INSERT INTO Pickup (PickupID, ClientID, PickupDate, PickupProxy, PickupHispanic, EthnCode, CategCode, AgencyID)
SELECT
CAST(PickupID AS UNSIGNED),
CAST(ClientID AS UNSIGNED),
STR_TO_DATE(PickupDate, '%m/%d/%Y'),
NULLIF(PickupProxy, ''),
CAST(PickupHispanic AS UNSIGNED),
NULLIF(EthnCode, ''),
CategCode,
CAST(AgencyID AS UNSIGNED)
FROM temp_pickup
WHERE PickupID REGEXP '^[0-9]+$'
AND CAST(PickupID AS UNSIGNED) BETWEEN -2147483648 AND 2147483647;Proper LOAD DATA Configuration: Explicitly specify field delimiters to ensure correct data parsing:
LOAD DATA INFILE 'Pickup_withoutproxy2.txt'
INTO TABLE pickup
FIELDS TERMINATED BY ','
(PickupID, ClientID, PickupDate, PickupProxy, PickupHispanic, EthnCode, CategCode, AgencyID);In-Depth Extension: Strict Mode and Data Type Compatibility
MySQL's strict mode (sql_mode) significantly impacts data validation. In strict mode, any data type mismatch causes an error; in non-strict mode, MySQL attempts automatic data conversion, potentially leading to data loss or unpredictable results. Enabling strict mode in production environments is recommended to ensure data consistency.
The DECIMAL data type issue mentioned in the reference article further illustrates the importance of data formatting. For instance, in some locales, decimals may use commas instead of periods as separators, causing conflicts with database expectations. Solutions include standardizing formats at the application layer:
// PHP example: Standardizing decimal format
$ratio = number_format($value, 2, '.', '');Similarly, for dates and other complex data types, ensuring input formats match database expectations is crucial for avoiding errors.
Conclusion and Recommendations
Resolving Error 1265 requires comprehensive data validation, configuration optimization, and data type management. By preprocessing data, utilizing temporary tables, and correctly configuring load commands, such errors can be effectively prevented. During development, always validate input data and use database strict mode to catch potential issues.