How to import a CSV file into MariaDB and validate the rows
To import a CSV into MariaDB, tell LOAD DATA INFILE how the file is formatted and what order the fields are in. Use LOCAL when your SQL client reads the file and omit it when the database server reads it. Stage the CSV before writing to the main table so you can inspect warnings, row counts, formats, and duplicates.
Identify which machine reads the file
With LOCAL, the path is on the machine running your SQL client. If you run mariadb through SSH on another computer, that computer reads the file. Both the server’s local_infile setting and the client must permit LOCAL. Without LOCAL, the server process reads the file, so the account needs the FILE privilege and the server must have file access and satisfy restrictions such as secure_file_priv. MariaDB Docs · MariaDB LOAD DATA INFILE
| Statement | Reads the file | Requirements |
|---|---|---|
| LOAD DATA LOCAL INFILE | SQL client | LOCAL enabled on client and server, client file access |
| LOAD DATA INFILE | Database server | FILE privilege, server read access and permitted path |
The command below connects to a trusted local practice server. Adjust the user name, port, and database. Leaving the value off --password prompts for it. Use LOCAL only on a trusted connection that needs it. If policy disables it on a shared server, use an approved import method. MariaDB Docs · MariaDB command-line client
mariadb --host=127.0.0.1 --port=3306 --user=import_user --password --local-infile=1 --default-character-set=utf8mb4 learning
Prepare the CSV header, encoding, and quotes
Create items.csv in C:/csv-import/ and save it as UTF-8 without a byte order mark, with LF line endings. If your directory or system differs, put the actual absolute path in the SQL. The first row is a header. Quote a name containing a comma and double any double quote inside a quoted field. Export settings vary, so inspect the actual file in a text editor. MariaDB Docs · RFC 4180
sku,name,quantity
A001,"Pencil, black",10
A002,"Memo ""small""",0
A003,Notebook,12
Create the staging and destination tables
You need the practice database learning and CREATE, INSERT, and SELECT privileges. If the tables already exist, use a fresh practice database. csv_stage_demo is a regular table that stores even quantity as text. Load into the empty table once and make sure the VARCHAR lengths cover the source data. If server_local_infile is 0, changing the client option alone will not enable LOCAL.
USE learning;
CREATE TABLE csv_stage_demo (
sku VARCHAR(100),
name VARCHAR(200),
quantity_text VARCHAR(100)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE csv_items_demo (
sku VARCHAR(100) PRIMARY KEY,
name VARCHAR(200) NOT NULL,
quantity INT UNSIGNED NOT NULL,
CHECK (quantity <= 999999)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
SELECT @@global.local_infile AS server_local_infile;
Check warnings and rows right after loading
The LOAD DATA statement below uses commas as separators and double quotes for enclosure and doubled quotes. IGNORE 1 LINES skips the header; it is separate from the IGNORE option for duplicate keys. Remove the line for headerless files. For CRLF files, use a \r\n line separator. For other encodings, convert the file correctly or specify its actual character set.
LOAD DATA LOCAL INFILE 'C:/csv-import/items.csv'
INTO TABLE csv_stage_demo
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(sku, name, quantity_text);
SHOW WARNINGS;
SELECT sku, name, quantity_text FROM csv_stage_demo ORDER BY sku;
SELECT COUNT(*) AS loaded_rows FROM csv_stage_demo;
Run SHOW WARNINGS in the same connection immediately after the load. This example should have no warnings and loaded_rows should be 3. The comma in the A001 name and the quotes in A002 should survive, with quantity strings of 10, 0, and 12. A success message still needs to be checked against the source file and the values in the table. MariaDB Docs · MariaDB SHOW WARNINGS
Validate values and duplicates before inserting
Proceed only when all three queries below are empty. The first finds blank SKUs or names and quantities with the wrong length or non-digit characters. This example accepts only one to six ASCII digits, excluding negative values, decimals, and surrounding spaces. The second query checks duplicates within the file. The third checks existing destination keys. The SKU collation determines whether letter case matters.
SELECT sku, name, quantity_text
FROM csv_stage_demo
WHERE sku IS NULL OR TRIM(sku) = ''
OR name IS NULL OR TRIM(name) = ''
OR quantity_text IS NULL
OR CHAR_LENGTH(quantity_text) NOT BETWEEN 1 AND 6
OR quantity_text REGEXP '[^0-9]';
SELECT sku, COUNT(*) AS duplicate_count
FROM csv_stage_demo GROUP BY sku HAVING COUNT(*) > 1;
SELECT s.sku
FROM csv_stage_demo AS s
JOIN csv_items_demo AS t ON t.sku = s.sku;
Run the block below on the small practice file. inserted_rows should be 3 and the quantities 10, 0, and 12. After ROLLBACK, remaining_rows should be 0. The block only rolls back the InnoDB INSERT. For a real import, review the validated result in the same connection and choose COMMIT to keep it. Concurrent writes can still cause a conflict after previewing the data. Roll back if the INSERT fails.
START TRANSACTION;
INSERT INTO csv_items_demo (sku, name, quantity)
SELECT sku, name, CAST(quantity_text AS UNSIGNED)
FROM csv_stage_demo;
SELECT ROW_COUNT() AS inserted_rows;
SELECT sku, name, quantity FROM csv_items_demo ORDER BY sku;
ROLLBACK;
SELECT COUNT(*) AS remaining_rows FROM csv_items_demo;
Import a file with only some columns
If the file has only some fields, list the corresponding columns in file order. Save the two lines below as names.csv in the same folder in UTF-8 with LF and no header. Execute the next SQL block. The missing sku is NULL and quantity receives its default of 0. There is no need to pad the CSV with commas.
Pencil
Notebook
CREATE TABLE csv_name_only_demo (
sku VARCHAR(100) NULL,
name VARCHAR(200),
quantity INT NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
LOAD DATA LOCAL INFILE 'C:/csv-import/names.csv'
INTO TABLE csv_name_only_demo
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY '\n'
(name);
SHOW WARNINGS;
SELECT sku, name, quantity FROM csv_name_only_demo ORDER BY name;
Diagnose errors and decide when to retry
If the file cannot be found, check LOCAL and the file path first. For column counts or truncation warnings, check delimiters, the header, line endings and column sizes. For garbled text, compare the input encoding, LOAD DATA character set and destination column character set. Strict mode alone may not stop a conversion problem with LOCAL. For automated imports, stop before the final insert on warnings, row count mismatches or invalid data, and check earlier writes before retrying. MariaDB Docs · MariaDB local_infile




