Gerador SQL UPDATE

Converta dados CSV em instruções SQL UPDATE com chave primária

Converting CSV Data to SQL UPDATE Statements

Bulk data corrections — updating customer emails, adjusting product prices, or fixing categorization errors — often arrive as spreadsheet exports. Converting these tabular corrections into targeted UPDATE statements requires identifying which column serves as the primary key for the WHERE clause and which columns contain the new values. The SQL UPDATE Generator transforms CSV data into UPDATE statements that modify specific rows identified by a key column, with proper escaping and dialect-appropriate syntax for MySQL, PostgreSQL, and SQLite.

Select your key column (the WHERE clause identifier), specify the target table, and receive UPDATE statements that modify only the rows matching each key value. The tool handles NULL values, type inference, string escaping, and generates safe statements that update exactly the intended rows without risking broad unfiltered updates.

Primary Key Selection and WHERE Clause

Every UPDATE statement needs a WHERE clause to target specific rows. The generator uses your designated key column to build targeted conditions:

  • Single-column key: WHERE id = 123 — most common for auto-increment or UUID primary keys
  • Composite keys: Multiple columns combined in the WHERE clause for tables without single-column identifiers
  • Safety check: The tool warns if key column values are not unique in your CSV, which would cause multiple rows to be updated unintentionally

Updates without WHERE clauses modify every row in the table — the generator always requires a key column selection to prevent this dangerous pattern.

Selective Column Updates

Not every CSV column needs to be included in the SET clause. The generator allows selecting which columns to update:

  • Skip unchanged columns: Exclude columns that should not be modified, reducing statement size and write amplification
  • NULL handling: Empty CSV cells generate SET column = NULL to explicitly clear values
  • Conditional updates: Only generate UPDATE statements for rows where at least one value differs from the current state (when comparing against a baseline export)

Safe Execution Practices

Generated UPDATE statements should be reviewed before execution. Best practices include:

  • Transaction wrapping: Execute all updates within a transaction so they can be rolled back atomically if issues are detected
  • Row count verification: Compare affected row counts against expected — if an UPDATE affects 0 rows, the key value does not exist
  • Backup first: For bulk updates, create a table backup or use SELECT INTO before applying changes
  • Test on staging: Run generated statements against a staging copy of the data before production execution
  • Incremental batching: For very large updates (thousands of rows), break into smaller batches to avoid holding locks for extended periods and to allow progress verification between batches

The generator optionally wraps output in transaction blocks and includes row count assertions as comments so you can verify expected behavior during execution. For critical data corrections, consider generating a corresponding rollback script that reverses each UPDATE by setting values back to their original state.

Code Examples

CSV to UPDATE Statement Conversion

-- Input CSV (key column: id):
-- id,email,status,discount
-- 101,newemail@example.com,active,15
-- 102,,inactive,
-- 103,updated@test.com,active,20

-- Generated SQL (MySQL):
UPDATE customers
  SET email = 'newemail@example.com', status = 'active', discount = 15
  WHERE id = 101;

UPDATE customers
  SET email = NULL, status = 'inactive', discount = NULL
  WHERE id = 102;

UPDATE customers
  SET email = 'updated@test.com', status = 'active', discount = 20
  WHERE id = 103;

Perguntas Frequentes

How do I specify which column is the primary key?

Select the primary key column from the dropdown that lists all columns detected from your CSV headers. The selected column will be used in the WHERE clause of each UPDATE statement, ensuring each row updates the correct record.

How are NULL values handled in UPDATE statements?

Empty CSV fields can be configured to generate SET column = NULL or to be skipped entirely (leaving the existing value unchanged). This gives you control over whether blank cells should clear data or preserve current values in the database.

Can I update only specific columns?

Yes. By default all non-primary-key columns are included in the SET clause, but you can select which columns to include in the generated UPDATE statements. Unselected columns will not appear in the output, leaving those database values unchanged.