CSV to SQL: a ready-to-run import script
Database clients have import wizards, but they are not always available, and sometimes you just want a script you can review, commit or send to a colleague. This CSV to SQL converter reads your file and writes plain SQL: an optional CREATE TABLE whose column types are inferred from the data, followed by INSERT statements for every row.
Example
order_id,customer,total,paid,placed_at,ship_date
1001,O'Brien Ltd,249.99,true,2024-05-02 14:30:00,2024-05-04
1002,Acme,80,false,2024-05-03 09:15:00,
With the table name orders and MySQL quoting:
CREATE TABLE `orders` (
`order_id` INT,
`customer` VARCHAR(255),
`total` DECIMAL(18,6),
`paid` BOOLEAN,
`placed_at` DATETIME,
`ship_date` DATE
);
INSERT INTO `orders` (`order_id`, `customer`, `total`, `paid`, `placed_at`, `ship_date`) VALUES (1001, 'O''Brien Ltd', 249.99, TRUE, '2024-05-02 14:30:00', '2024-05-04');
INSERT INTO `orders` (`order_id`, `customer`, `total`, `paid`, `placed_at`, `ship_date`) VALUES (1002, 'Acme', 80, FALSE, '2024-05-03 09:15:00', '');
Look at the customer name: SQL string literals are delimited by single quotes, so the apostrophe in O'Brien is written twice. Numbers and booleans go in unquoted, and every other value becomes a quoted string.
Column type detection
When Include CREATE TABLE is ticked, the converter scans every column and picks the narrowest type that fits all of its non-empty cells:
| Column contents | Chosen type |
|---|---|
| Whole numbers | INT, or BIGINT above 2,147,483,647 |
| Any decimals | DECIMAL(18,6) |
| Only true/false | BOOLEAN |
| Dates like 2024-05-04 | DATE |
| Date and time | DATETIME with backticks, TIMESTAMP with ANSI quotes, DATETIME2 with brackets |
| Other text | VARCHAR(255), or TEXT if any value is longer |
These guesses come from a sample of your data, not from its meaning. Review them, and add a primary key, constraints and realistic string lengths before the table goes live. If you untick Detect numbers & booleans, numbers stay strings, so those columns fall back to VARCHAR(255) and their values are quoted; dates are still recognized.
Options explained
- Table name – the target table,
my_tableunless you change it. - Identifier quoting – pick the style of your database:
`backticks`for MySQL and MariaDB,"double quotes"for PostgreSQL, SQLite and other ANSI-style engines,[brackets]for SQL Server. With None, plain names such asorder_idare written bare, and only a header likefull namefalls back to double quotes. - Include CREATE TABLE – switch it off to append rows to a table that is already defined.
- Single multi-row INSERT – groups all rows into one statement, one parenthesized tuple per line. That loads noticeably faster, but a huge statement can hit limits such as MySQL's
max_allowed_packet, so split big files. - Nest dotted headers and Flatten nested objects – only relevant for dotted headers; with both at their defaults, columns keep their names.
Good uses and limits
Generated INSERT scripts are ideal for seed data, fixtures in a test database, small reference tables such as countries or plan tiers, and one-off migrations that you want to keep in version control. They are readable, easy to diff and run in any SQL client.
For very large files, the native bulk loaders are faster: LOAD DATA INFILE in MySQL, COPY in PostgreSQL and BULK INSERT in SQL Server read the CSV directly. If you run a long script anyway, put it inside a transaction; then one bad row rolls everything back instead of leaving a partial import.
Column names that are reserved words, such as order or group, must be quoted. Pick the quoting style of your database instead of None whenever headers might clash with SQL keywords.
Things to check before running the script
- Empty cells become empty strings (
''), notNULL. In aDATEor numeric column, strict databases reject that, as withship_dateabove. Replace''withNULLfor those columns. - Booleans on SQL Server – T-SQL uses
BITcolumns holding 1 or 0, so edit the generatedBOOLEANtype and theTRUE/FALSEvalues before running the script there. - IDs with leading zeros stay text, so a column mixing
0042and42becomesVARCHAR. That is usually correct for codes.
Tidy the generated script with the SQL formatter. Starting from an API response instead of a spreadsheet? JSON to SQL handles that case, and SQL to CSV extracts rows from existing INSERT statements.
Privacy
The SQL is generated in your browser and the CSV is not uploaded, so customer or order data stays with you. Loading a file by URL and saving a share link are the only actions that involve our server.