Get Insert Statement for existing row in MySQL
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
MySQL does not provide a single built-in command that turns an existing row into a ready-made INSERT statement. In practice, you either generate the statement yourself with SQL string functions, or you use tooling such as mysqldump when the goal is migration, backup, or copying data between environments.
Generate an INSERT statement with SQL
For a known table and a small number of columns, you can build the statement directly with CONCAT and QUOTE.
QUOTE is important for strings because it escapes quotes and wraps the value correctly. Without it, generated SQL breaks on names such as O'Reilly.
Handle NULL values explicitly
QUOTE(NULL) returns NULL, which is not the same as the literal text you want in the generated statement. Use IFNULL or CASE so the output contains the SQL keyword NULL when appropriate.
That pattern becomes essential once nullable columns are involved.
Use mysqldump when the goal is export or migration
If the real requirement is "give me SQL I can replay elsewhere," mysqldump is often the better tool. It already knows how to generate valid INSERT statements.
This is usually safer than hand-building SQL for wide tables because the tool handles column ordering, escaping, and data formatting.
Wide tables are better handled with generated SQL or tooling
Hand-writing a CONCAT expression for a table with dozens of columns is hard to maintain. In those cases, you can query information_schema.columns to generate the generator query, but at that point a dump tool or an application script is often simpler.
If you still need to do it in SQL, ensure the column list in the INSERT matches the generated values exactly. Depending on implicit column order is fragile and breaks as the schema evolves.
Beware of data types that need special treatment
Numbers are easy, but dates, timestamps, binary values, JSON columns, and blobs need care. Some data types need quoting, some should not, and binary data may be easier to export with tooling than with manual SQL assembly.
For example, string-style quoting is appropriate for date and datetime values because MySQL accepts them as string literals:
But once blobs or complex binary payloads enter the picture, SQL generation becomes cumbersome quickly.
Use application code when repeatability matters
If you need this operation repeatedly, application code is often the clearest option. A script can read row data, format each column properly, and write reproducible output with tests around it. That is easier to version and safer than copy-pasting one-off SQL fragments from old shell history.
Common Pitfalls
- Building the
INSERTtext withoutQUOTE, which breaks on strings containing quotes or special characters. - Forgetting to handle
NULLvalues explicitly and generating invalid SQL. - Omitting the column list and relying on current table column order.
- Using hand-built SQL for wide tables when
mysqldumpwould be simpler and safer. - Assuming every data type can be handled with the same quoting rule.
Summary
- MySQL does not have a one-command built-in feature for this exact task.
- Use
CONCATandQUOTEfor small, targeted SQL generation. - Handle
NULLexplicitly so the output is valid SQL. - Prefer
mysqldump --no-create-info --where=...for export and migration use cases. - Be extra careful with wide tables and complex data types.

