Format Code Now
/

SQL Code Generator

FV-37

Write a table name and its columns and get ready SELECT, INSERT, UPDATE and DELETE statements with placeholders, quoted for MySQL, PostgreSQL, SQL Server, SQLite or Oracle.

Runs in your browser. Nothing is uploaded.

Table name, then one column per line: name type [pk] [ai] [nn] ...

SQL

How to use it

  1. Put the table name on the first line.
  2. Add one column per line, with a type. Mark the key with pk and an auto-increment column with ai.
  3. Choose the database and the placeholder style.
  4. Click All four, or one statement, and copy the SQL.

What goes in, what comes out

Each column line is a name, a type, and optional words. Only pk (primary key) and ai (auto-increment) change the statements: INSERT leaves ai columns out, and UPDATE and DELETE use the pk columns in the WHERE clause. SELECT is given both without a filter and with one. Values are always placeholders, such as ?, $1, @name or :name, so you pass them to a prepared statement and never build SQL from strings.

Quoting and limits

Names are quoted the way the database expects: backticks for MySQL, double quotes for PostgreSQL, SQLite and Oracle, brackets for SQL Server. PostgreSQL and Oracle treat quoted names as case sensitive, so untick Quote names if your table was created without quotes. UPDATE and DELETE need at least one pk column. The statements are not run or checked against your schema, and joins, upserts and RETURNING clauses are not generated.

Error messages and what to do

MessageWhat it meansFix
UPDATE needs a key to find the row. Mark at least one column with pk, for example: id int pk.No column is marked pk, so the statement could change every row.Add pk to the key column.

Questions people ask

How do I generate an INSERT statement from a table definition?

List the columns under the table name and click INSERT. The statement lists every column except auto-increment ones and gives each a placeholder. Choose the database to get matching quotes and placeholders.

What are placeholders and why use them?

A placeholder stands for a value that you supply when the statement runs. It keeps data separate from the SQL, which prevents SQL injection and lets the database reuse the plan.

Which placeholder style does my database use?

MySQL and SQLite with most drivers use ?. PostgreSQL uses $1, $2. SQL Server drivers typically use @name, and Oracle uses :name. Pick the one your driver expects.

Why does UPDATE say it needs a key?

Without a WHERE clause an UPDATE would change every row. Mark at least one column with pk and it is used to find the row.

Can I write columns without types?

Yes. For these statements the type is not used, so id pk ai is enough. Keep the types if you want to reuse the same list on the SQL Table Generator page, where every column needs a type.

Does it make a composite key WHERE clause?

Yes. If several columns are marked pk, UPDATE and DELETE join all of them with AND in the WHERE clause, and the key columns are left out of the SET list in the UPDATE statement.

About this tool

The first line is the table name (schema.table is accepted). Each following line is a column: name, a type, and optional words. pk marks a primary key column, ai an auto-increment column (left out of INSERT), and nn, unique, default and ref are read but only pk and ai change the statements. UPDATE and DELETE use the pk columns in WHERE, so they need at least one; SELECT also gives a version without WHERE. Values are written as placeholders for a prepared statement, never as literals. Names are quoted for the chosen database; Postgres and Oracle treat quoted names as case sensitive, so untick Quote names to let them fold the case. The statements are not run or checked against a real table, and joins, upserts and RETURNING clauses are not generated.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.