How to Insert Values Into a SQL Table: Step-by-Step
In this article
To insert values into a SQL table, use the INSERT INTO statement: name the table, list the columns you are filling, and supply matching values in the same order. For example, INSERT INTO users (first_name, last_name, email) VALUES ('Ana', 'Lopez', 'ana@example.com'); adds one row. You need a database you can connect to, the table already created, and a client such as the command-line tool, a GUI like DBeaver or pgAdmin, or your application's database library. The exact syntax is the same across MySQL, PostgreSQL, SQL Server, SQLite and Oracle, with small differences noted below.
What You'll Need
- A running database server (or a local file for SQLite) and a database that contains the target table.
- Connection details: host, port, database name, username and password, or a connection already saved in your client.
- The table's structure: column names, data types, and which columns are nullable or have defaults. Run
DESCRIBE table_name;in MySQL or\d table_namein PostgreSQL (or check your client's table view) to see it. - Permission to insert: your database user needs the INSERT privilege on that table.
- Values formatted for each column's type: numbers without quotes, text and dates in single quotes, NULL written as the keyword
NULLwithout quotes.
Step-by-Step
- Connect to the right database. Open your SQL client and connect using the saved connection or the host, port, username and password. If your client does not select a database automatically, run
USE database_name;in MySQL or SQL Server, or connect to the specific database in PostgreSQL. Working in the wrong database is the most common reason a table "does not exist." - Inspect the table before writing the statement. List the columns and their types so your values match. For example, if
created_atis a timestamp with a default of the current time, you can leave it out entirely and the database fills it. Knowing which columns are NOT NULL prevents failed inserts. - Write the INSERT with an explicit column list. The safest form names every column you are supplying:
INSERT INTO products (name, price, stock) VALUES ('Desk Lamp', 24.99, 40);The column list and the value list must have the same number of items, in the same order. Naming columns means the statement still works if someone later adds a column to the table. - Match data types and quoting. Put single quotes around text and date literals, leave numbers unquoted, and use
NULLfor missing values. Escape an apostrophe inside text by doubling it:'O''Brien'. In MySQL you can also use a backslash, but doubling works everywhere. - Insert several rows in one statement. Add more value groups separated by commas:
INSERT INTO products (name, price, stock) VALUES ('Mug', 9.50, 100), ('Notebook', 4.25, 250);This is faster than separate statements and is atomic in most databases, so either all rows go in or none do. - Insert only some columns. Omit columns that allow NULL or have a default.
INSERT INTO users (email) VALUES ('sam@example.com');works if the other columns are nullable or defaulted. Any column that is NOT NULL with no default must be included. - Run the statement and check the result. Your client should report rows affected (for example, "1 row affected"). Then verify with a SELECT:
SELECT * FROM products WHERE name = 'Desk Lamp';If the row is there, the insert succeeded. If your client is in autocommit mode (the default in most tools), the change is already saved; otherwise commit it. - Commit if you are inside a transaction. If you started a transaction with
BEGIN;or your client runs in manual-commit mode, finish withCOMMIT;to make the insert permanent. Until then, other sessions will not see the row, and closing the connection without committing rolls it back. - Retrieve generated IDs when needed. If the table has an auto-increment or identity primary key, you often need the new ID. In MySQL use
SELECT LAST_INSERT_ID();right after the insert. In PostgreSQL addRETURNING id;to the INSERT. In SQL Server useOUTPUT INSERTED.id. In SQLite calllast_insert_rowid().
Common Mistakes
- Column and value counts do not match. The database rejects the statement with an error like "Column count doesn't match value count." Recount both lists.
- Forgetting the column list.
INSERT INTO products VALUES (...)requires a value for every column in table order, including auto-generated IDs. It breaks as soon as the table changes. - Quoting numbers or not quoting text.
'24.99'may be converted, butDesk Lampwithout quotes is read as a column name and fails. - Violating a constraint. Duplicate values in a UNIQUE or PRIMARY KEY column, NULL in a NOT NULL column, or a value too long for a VARCHAR all stop the insert.
- Foreign key order. Insert the parent row before the child row that references it, or the foreign key check fails.
- Assuming the change is saved. In a transaction you have not committed, the row exists only in your session.
Tips
- Always name columns explicitly; it makes statements self-documenting and resistant to schema changes.
- Use parameterised queries in application code (
?or$1placeholders) instead of building SQL strings. This prevents SQL injection and handles quoting for you. - Test on a development copy first when inserting many rows or running a script that modifies production data.
- For bulk loads of thousands of rows, use the database's bulk loader (for example
COPYin PostgreSQL orLOAD DATAin MySQL) rather than a huge INSERT. - Keep a transaction around multi-row inserts so a failure halfway does not leave partial data.
Troubleshooting
If the insert fails, read the error message first; it usually names the column or constraint. "Table doesn't exist" means you are in the wrong database or the name is misspelled. "Unknown column" means a column name in your list is wrong. "Duplicate entry" means a unique constraint is violated; check whether the row already exists. "Cannot insert NULL" means a required column was omitted. "Foreign key constraint fails" means the referenced parent row is missing or you are inserting in the wrong order. "Permission denied" means your user lacks INSERT rights. If the statement reports success but SELECT shows nothing, you are probably in an uncommitted transaction or querying a different database or schema. If a value looks wrong after insert, compare its type with the column definition; implicit conversions can round numbers or truncate text.
Frequently Asked Questions
Can I insert a row without listing columns?
Yes, but you must then supply a value for every column in the table's defined order, including auto-generated ones. Listing columns is safer and clearer.
How do I insert the current date and time?
Use the database's function: NOW() in MySQL, CURRENT_TIMESTAMP in PostgreSQL and SQL Server, or datetime('now') in SQLite. Or let a column default fill it.
What is the difference between INSERT INTO and INSERT IGNORE?
INSERT IGNORE (MySQL) skips rows that would cause a duplicate-key error. PostgreSQL and SQLite use ON CONFLICT DO NOTHING, and SQL Server has no direct equivalent, so check for duplicates first.
Can I insert multiple rows at once?
Yes. Add comma-separated value groups after VALUES. All major databases support this, and it is faster than one statement per row.
Why did my insert succeed but the data is missing?
Most often the transaction was not committed, or you are querying a different schema or database. Check your client's autocommit setting and the active database.
How do I copy rows from one table to another?
Use INSERT INTO target (col1, col2) SELECT col1, col2 FROM source WHERE ...; The selected columns must match the target list in number and type.