How to Insert a Record into a SQL Table: Step by Step
In this article
You insert a record into a SQL table with the INSERT INTO statement. The shortest working form is INSERT INTO table_name (column1, column2) VALUES ('value1', 12); — name the table, list the columns in parentheses, then supply a matching list of values in the same order. Run that against your database (in a SQL client, a migration, or from application code) and the row is added. The rest of this guide covers how to get the statement right, how to insert several rows at once, how to read back the result, and what to fix when the database rejects it.
What You'll Need
- A database you can write to, and an account with INSERT permission on the target table. Read-only users will get a permission error even when the SQL is perfect.
- A way to run SQL: a database client (such as pgAdmin, MySQL Workbench, DBeaver, SQL Server Management Studio or the
psql/mysql/sqlite3command-line tools), or your application's code and connection settings. - The exact table name and column names. Names are case-sensitive on some databases (PostgreSQL folds unquoted names to lowercase) and case-insensitive on others, so copy them from the table definition rather than typing from memory. Most clients show columns under a "Columns" tab, or you can run
DESCRIBE table_name;in MySQL,\d table_namein psql, or query the information schema. - Allowed values for each column: data types, maximum lengths, whether NULL is allowed, and any defaults. Foreign-key columns need an existing row in the referenced table.
- Ideally, a non-production database to test in first. Menu and button labels in these clients vary slightly by version and platform, so the wording below may differ a little from what you see.
Step-by-Step
- Connect to the right database and schema. Many servers hold several databases and several schemas inside each. Inserting into the wrong one is the most common reason a row "disappears" — it was added, just somewhere else. In a client, check the connection name in the title bar and the database or schema selector. From the command line, connect with the database name in the command.
- Inspect the table definition. Confirm the column names, the data types, which columns are
NOT NULL, which have defaults, and which are auto-generated (identity or serial primary keys). You need this before writing values, because the column list must match exactly. - Write the column list and the matching value list. Always spell out the columns rather than relying on positional insert. Full syntax:
The number of values must equal the number of columns, and each value must be in the correct position.INSERT INTO customers (first_name, last_name, email, signup_date) VALUES ('Ana', 'Lopez', 'ana@example.com', '2026-04-01'); - Match data types and quote text properly. Single quotes for text and dates, no quotes for numbers. Dates are safest in ISO format (
'YYYY-MM-DD') — locale formats like04/01/2026are ambiguous and may be rejected or misread. Escape an inside apostrophe by doubling it:'O''Brien'. - Leave auto-generated columns out. Do not supply a value for an identity, auto-increment or serial primary key unless you have a specific reason. The database fills it in. Never pass the literal text
NULLin quotes —'NULL'is a string, while unquotedNULLis the actual empty value. - Insert several rows in one statement. Most databases accept multiple value tuples, which is faster and atomic — either all rows go in or none do:
The multi-rowINSERT INTO customers (first_name, last_name, email) VALUES ('Ana', 'Lopez', 'ana@example.com'), ('Ben', 'Okafor', 'ben@example.com'), ('Chen', 'Wei', 'chen@example.com');VALUESform may not work in older versions of some systems, and very large batches are better split into chunks. - Run the statement. Most clients show a row count on success (for example "1 row affected"). If your client opens a transaction automatically, the change is not permanent until you commit — confirm the commit setting before you rely on the insert.
- Read the row back. Do not assume. Run a SELECT filtered on the values you just inserted, and include the primary key if you can. This proves the row landed with the values you expect, not silently coerced ones.
- Get the new key if you need it. To use the generated ID in application code, use the returning mechanism your database provides:
INSERT ... RETURNING idin PostgreSQL and SQLite,OUTPUT INSERTED.idin SQL Server, or the last-insert-ID function in MySQL and the driver's insert-ID method. Never guess the ID from a MAX() query — concurrent inserts make that unreliable. - Insert from application code with parameters. When building the statement in an app, use placeholders (
$1,?, or named parameters) and pass values separately. This prevents SQL injection and handles quoting correctly. Never concatenate user input into SQL.
Common Mistakes
- Column and value counts differ. "Column count doesn't match value count" means exactly that — recount both lists.
- Omitting the column list. A positional insert breaks the moment someone adds a column or the column order differs from what you assumed.
- Missing required columns. Omitting a
NOT NULLcolumn with no default gives a constraint error. - Foreign key with no parent row. The referenced value must already exist in the parent table.
- Duplicate unique values. Inserting an email or username that already exists in a UNIQUE column fails. Use an upsert if you want "insert or update" behaviour.
- String values in numeric columns, or numbers in quoted text columns. Some databases coerce silently, which hides bugs; others reject it.
- Forgetting to commit. In a client with manual transactions, an uncommitted insert is invisible to other sessions and vanishes if you disconnect.
- Trusting "success" without selecting back. A trigger or default may have changed the stored value.
Tips
- Test the statement inside a transaction when possible: run
BEGIN, insert, check with a SELECT, thenROLLBACKif anything is wrong orCOMMITif it is right. - Insert into a copy of the table (or a backup database) first for anything destructive or bulk.
- Batch large loads inside one transaction rather than row by row; it is dramatically faster.
- Prefer explicit, ISO-format dates and UTC timestamps so timezone differences do not distort records.
- If you need "insert unless it exists", learn your database's upsert syntax —
ON CONFLICTin PostgreSQL and SQLite,ON DUPLICATE KEY UPDATEin MySQL,MERGEin SQL Server — rather than checking first and inserting after, which races. - Keep a short migration or seed script for repeating the same insert on other environments.
Troubleshooting
- Syntax error near a keyword or quote. Usually an unescaped apostrophe in a name, or a missing parenthesis. Check every quoted string is closed.
- NULL constraint violation. A required column was omitted or given
NULL. Add the value or give the column a default. - Unique or primary key violation. The row already exists. Query the table for the conflicting value before trying again.
- Foreign key constraint fails. Insert the parent row first, or correct the ID you referenced.
- Permission denied. Ask the database administrator for INSERT rights on that table, or use an account that has them.
- "Column does not exist". The name is misspelled, belongs to another table, or is stored differently from how you typed it. Copy it from the definition.
- Data too long or type mismatch. The value exceeds the column's length or is the wrong type. Shorten or convert it.
- Statement succeeds but you cannot see the row. Check the database and schema, and check whether the transaction was committed.
- Nothing works after changes. Restart your client, reconnect, and confirm the table still exists and was not renamed by someone else.
Frequently Asked Questions
What is the difference between INSERT INTO ... VALUES and INSERT INTO ... SELECT?
VALUES supplies literal data; INSERT INTO target (cols) SELECT cols FROM source copies rows from a query. Both add rows to the same table; use SELECT when the data already lives in another table.
How do I insert a NULL value?
Write the unquoted word NULL, or omit the column from the list entirely if it allows NULL or has a default. Quoted 'NULL' is just text.
How do I insert today's date and time?
Use the database's own function — CURRENT_DATE or CURRENT_TIMESTAMP — rather than typing a date, so the value is set when the statement runs.
Can I insert without specifying a primary key?
Yes, when the key is auto-generated. The database assigns the next value.
What happens if the insert fails partway through a multi-row statement?
Inside a transaction, the whole statement rolls back. Without an explicit transaction, behaviour depends on the database's autocommit setting.
Can I undo an insert?
Only with DELETE of the affected row before committing, or by rolling back an open transaction. Once committed, the row must be deleted or updated explicitly — there is no undo button.