How to turn a CSV file into CREATE TABLE and INSERT statements
By Hammad Rasool · November 11, 2026
Quick answer: read the CSV header as column names, choose a type for each column by checking every value (not just the first row), write one CREATE TABLE, then write multi-row INSERT statements of about 100 to 1,000 rows each with quotes escaped for your database. The CSV to SQL converter does all of this in the browser. Before you run the file, check four things: numbers above 2,147,483,647, dates with a trailing Z, empty cells, and the client character set.
How this was tested: on 10 November 2026 the sample file below was converted with ToolStash's own converter code and loaded into MySQL 8.4.9 (default strict SQL mode) and SQLite 3.50.6 on Windows 11. Every error message and result on this page is copied from those runs. Testing found two bugs in the converter, a column typed INT that needed BIGINT and a date format MySQL rejects, and both were fixed before this guide went live. PostgreSQL, SQL Server and Oracle were not tested; the notes on them come from their manuals and are marked as such.
The sample CSV
Eleven columns and four rows, picked to break things: a comma and doubled quotes inside quoted fields, an apostrophe, a line break, a backslash, an accented letter, leading zeros, a number too big for INT, three different date-time styles and empty cells.
sku,name,price,qty,in_stock,added_on,updated_at,zip,phone,big_id,note
A-001,"Mug, large",12.50,40,true,2026-01-15,2026-11-01 09:30:00,04501,03001234567,9007199254740993,"says ""hi"""
A-002,Tea 'Gold' 250g,4.99,0,false,2026-02-03,2026-11-02T14:05:00Z,60000,03211234567,3000000000,"line one
line two"
A-003,Café filter,0.125,7,TRUE,2026-03-21,2026-11-03 18:00:00+05:00,54000,,12,C:\temp\file
A-004,Empty note,3,,false,03/04/2026,,,,,Step 1: Check the file before converting it
- Header row. The first line becomes the column names. If the file has no header, the converter names the columns
column_1,column_2and so on; rename them before running the SQL. - Delimiter. Files saved by Excel in many European locales use semicolons. The converter picks comma, semicolon, tab or pipe by counting them in the first line.
- Quoting. A field that contains the delimiter, a quote or a line break must be wrapped in double quotes, with inner quotes doubled (
"says ""hi"""). This is the RFC 4180 rule, and a file that ignores it gives you shifted columns. - Encoding. Save the file as UTF-8. Older Excel versions save "CSV" in the Windows code page, which turns accented letters into question marks long before SQL is involved.
Step 2: Choose a type for each column
A column gets a number, boolean or date type only when every non-empty value fits it. One odd value turns the column into text, which is the safe choice: text loads, a wrong number type does not. These are the rules the converter applies and what they did to the sample file:
| Values in the column | Type (MySQL) | Sample column |
|---|---|---|
| Whole numbers within ±2,147,483,647 | INT | qty |
| Any whole number above that, up to 9,223,372,036,854,775,807 | BIGINT | big_id |
| Numbers with a decimal point | DECIMAL(18,4) | price |
Only true / false, any case | BOOLEAN | in_stock |
Only YYYY-MM-DD | DATE | none (one value is 03/04/2026) |
YYYY-MM-DD HH:MM[:SS], optional T, fraction or offset | DATETIME | updated_at |
| Numbers with a leading zero (zip codes, phone numbers) | VARCHAR(255) | zip, phone |
| Anything else, or a mix | VARCHAR(255), or TEXT if a value is longer than 255 characters | sku, name, added_on, note |
The generated table:
CREATE TABLE `products` (
`sku` VARCHAR(255),
`name` VARCHAR(255),
`price` DECIMAL(18,4),
`qty` INT,
`in_stock` BOOLEAN,
`added_on` VARCHAR(255),
`updated_at` DATETIME,
`zip` VARCHAR(255),
`phone` VARCHAR(255),
`big_id` BIGINT,
`note` VARCHAR(255)
);Three choices are worth a second look before you run it:
added_onbecame text because one row says03/04/2026, which could be 3 April or 4 March. Fix that value in the CSV and the column becomesDATE; don't let a tool guess.DECIMAL(18,4)keeps four decimal places. MySQL stored1.23456as1.2346and only raised a note (Note 1265 Data truncated for column 'x'), not an error, even in strict mode. Widen the scale if you need more.- For SQLite the converter writes
REAL, a floating-point type, so12.50came back as12.5. For money, store whole cents in anINTEGERcolumn instead. - Generated types are a starting point. Add
PRIMARY KEY,NOT NULLand tighterVARCHARlengths yourself; a CSV can't tell you which column is the key.
Step 3: Write the INSERT statements
The converter's MySQL output for the sample file:
INSERT INTO `products` (`sku`, `name`, `price`, `qty`, `in_stock`, `added_on`, `updated_at`, `zip`, `phone`, `big_id`, `note`) VALUES
('A-001', 'Mug, large', 12.50, 40, TRUE, '2026-01-15', '2026-11-01 09:30:00', '04501', '03001234567', 9007199254740993, 'says "hi"'),
('A-002', 'Tea ''Gold'' 250g', 4.99, 0, FALSE, '2026-02-03', '2026-11-02T14:05:00+00:00', '60000', '03211234567', 3000000000, 'line one
line two'),
('A-003', 'Café filter', 0.125, 7, TRUE, '2026-03-21', '2026-11-03 18:00:00+05:00', '54000', NULL, 12, 'C:\\temp\\file'),
('A-004', 'Empty note', 3, NULL, FALSE, '03/04/2026', NULL, NULL, NULL, NULL, NULL);Each value is written according to its column type:
- Text goes in single quotes, and an apostrophe inside it is doubled:
'Tea ''Gold'' 250g'. That rule is standard SQL and works in every database. - MySQL only: a backslash is an escape character in MySQL string literals, so it is doubled. Without that,
C:\temp\filewould load asC:plus a tab character plusempfile. PostgreSQL, SQLite and SQL Server keep a backslash as an ordinary character, so the converter leaves it alone for them. - Numbers and booleans are written without quotes. An empty cell becomes NULL.
- Column names are quoted for the target database: backticks for MySQL, square brackets for SQL Server and double quotes for PostgreSQL, SQLite and Oracle. That lets a header such as
orderorunit pricework as a column name. - For SQL Server, text with non-ASCII characters gets an
Nprefix (N'Café filter') so it is read as Unicode.
MySQL 8.4.9 loaded all four rows. The stored values:
sku name price qty in_stock added_on updated_at zip big_id
A-001 Mug, large 12.5000 40 1 2026-01-15 2026-11-01 09:30:00 04501 9007199254740993
A-002 Tea 'Gold' 250g 4.9900 0 0 2026-02-03 2026-11-02 19:05:00 60000 3000000000
A-003 Café filter 0.1250 7 1 2026-03-21 2026-11-03 18:00:00 54000 12
A-004 Empty note 3.0000 NULL 0 03/04/2026 NULL NULL NULLStep 4: Batch the rows
What makes a load slow is the number of commits, not the number of statements. Without an explicit transaction, every INSERT commits on its own and waits for the disk. To measure it, a 2,000-row, four-column CSV was converted with one, 100 and 1,000 rows per statement and loaded into an empty table, two or three runs each:
| How the 2,000 rows were sent | MySQL 8.4.9 | SQLite 3.50.6 |
|---|---|---|
| 1 row per INSERT, each committed on its own | 156 s | 10.1–10.7 s |
| 1 row per INSERT, all in one transaction | 0.8–0.9 s | 0.17–0.18 s |
| 100 rows per INSERT (20 statements) | 1.9–2.5 s | 0.18 s |
| 1,000 rows per INSERT (2 statements) | 0.6–1.7 s | 0.10–0.11 s |
On this machine each MySQL commit took about 78 ms (200 single-row inserts with autocommit took 15.7 s), so 2,000 separate commits cost over two and a half minutes. Batching cuts the number of commits; one transaction cuts it to one. Do both for big files. The seconds depend on the disk and on settings such as innodb_flush_log_at_trx_commit (1 on the test server, the default), so read the ratios, not the absolute times.
MySQL trap: put START TRANSACTION after the CREATE TABLE. A DDL statement such as CREATE TABLE commits the open transaction, so with START TRANSACTION at the top of the file every following INSERT went back to committing on its own. The first timing run here fell into exactly that and took 229 seconds instead of under one. SQLite doesn't have this problem because its CREATE TABLE runs inside the transaction.
Each database also has a limit on how big one statement can be:
- MySQL: a statement can't be bigger than
max_allowed_packet, which was 64 MB (67,108,864 bytes) on the test server, the 8.4 default. Batches of 1,000 rows stay far below that unless the rows hold large text. - SQL Server: one
VALUESlist takes at most 1,000 rows, so the converter never writes more than 1,000 per statement for SQL Server (from the Microsoft documentation; not tested here). - Oracle before 23ai has no multi-row
VALUESlist, so the converter writes oneINSERTper row. Wrap the file in a single transaction and commit at the end.
Step 5: Run the file
MySQL, as tested:
mysql -h 127.0.0.1 -u app_user -p --default-character-set=utf8mb4 shop < products.sqlKeep --default-character-set=utf8mb4. On this Windows machine the client otherwise reported character_set_client as cp850, read the UTF-8 file in that code page, and stored Café filter as Caf├® filter: 12 characters instead of 11, with no error or warning.
SQLite, as tested:
sqlite3 shop.db ".read products.sql"PostgreSQL and SQL Server (not tested here; options from each tool's documentation). Both commands stop at the first error instead of loading half the file:
psql -h localhost -U app_user -d shop -v ON_ERROR_STOP=1 -f products.sqlsqlcmd -S localhost -d shop -E -b -i products.sqlFive errors you can hit, and the fix for each
ERROR 1264: Out of range value for column
This was the first run of the sample file, before the converter fix: ERROR 1264 (22003) at line 15: Out of range value for column 'big_id' at row 1. The column was typed INT, which tops out at 2,147,483,647, and the CSV held 3,000,000,000. In strict mode MySQL rejects the whole statement, so no row from that batch is saved. Fix: use BIGINT. IDs exported from other systems, Unix timestamps in milliseconds and phone numbers without a leading zero are the usual causes.
ERROR 1292: Incorrect datetime value
ERROR 1292 (22007): Incorrect datetime value: '2026-11-02T14:05:00Z' for column 'updated_at'. MySQL 8.4 accepts the T separator and an offset written as +05:00, but rejects a trailing Z and an offset without the colon (+0500). The converter now rewrites Z as +00:00 and +0500 as +05:00 for MySQL.
Be aware of what MySQL does with an offset: it converts the value to the session time zone and drops the offset. The session here was Pakistan time (UTC+5), so 14:05:00Z was stored as 19:05:00, and with SET time_zone = '+00:00' the value 14:05:00+05:00 was stored as 09:05:00. If the times must stay in UTC, set the session time zone first. PostgreSQL's documentation says the opposite happens there: a time zone in a TIMESTAMP (without time zone) literal is silently ignored. Use TIMESTAMPTZ if the offsets matter.
Empty cells become NULL, not empty text
A CSV has no way to tell an empty string from a missing value, so the converter writes NULL for both. Row A-004 loaded with NULL in qty, updated_at and note. If a column is declared NOT NULL, MySQL stops with ERROR 1048 (23000): Column 'note' cannot be null and rejects the whole statement, including the valid rows in the same batch. Give the column a default or replace NULL afterwards with UPDATE ... SET note = '' WHERE note IS NULL.
Accented letters turn into garbage, with no error
This is the character-set problem from step 5. MySQL can't tell that the bytes were read with the wrong code page, so it stores them without complaint. Check a row with SELECT name, CHAR_LENGTH(name) after loading: a length one or two higher than expected is the giveaway.
Rows silently rounded
DECIMAL(18,4) rounds a fifth decimal place with only a note. Check SHOW WARNINGS after a load, or set the scale from the data before running the file.
INSERT statements or a bulk loader?
Generated INSERTs run anywhere you can run SQL: a hosting control panel, phpMyAdmin, a migration file or a seed script in a repository. For a large file on a server you control, a bulk loader such as LOAD DATA, \copy or BULK INSERT is faster and skips the SQL file altogether. The guide to importing a CSV into MySQL, PostgreSQL, SQL Server and SQLite covers each loader and its permission errors. If the data starts as JSON instead of CSV, the JSON to SQL converter uses the same type rules.
FAQ
Can I convert a CSV to SQL INSERT statements in Excel?
Yes, with a formula such as ="('"&A2&"', "&B2&")," copied down a column, but it doesn't escape apostrophes or backslashes and doesn't write NULL for empty cells. That is fine for a few clean rows and risky for anything else.
Is my data uploaded when I use the converter?
No. The CSV to SQL converter runs in your browser; the file is not sent to a server.
How many rows can one INSERT statement have?
SQL Server allows 1,000 rows per VALUES list. MySQL limits the statement size through max_allowed_packet (64 MB by default in 8.4), not the row count. In the test above, 100 and 1,000 rows per statement both loaded the 2,000 test rows without problems on MySQL and SQLite.
Why did my zip codes lose their leading zero?
Because the column was typed as a number. Values such as 04501 must be stored as text. The converter types any column with a leading-zero number as VARCHAR; if you wrote the table yourself, change the column type and reload.
How do I get the data back out as CSV?
See how to export a MySQL table to CSV, or paste INSERT statements into the SQL to CSV converter.