Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL INSERT INTO: How to Add Values to a Table

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert values into a SQL table, use the INSERT INTO statement: name the table, list the columns in parentheses, then supply the matching values with VALUES. For example, INSERT INTO customers (first_name, last_name, email) VALUES ('Ana', 'Lopez', 'ana@example.com'); adds one row. The column list and value list must line up in the same order and count, and text and date values go in single quotes while numbers do not. Everything below assumes you already have a database connection and the right to write to the table.

What You'll Need

  • A SQL database you can connect to (MySQL, PostgreSQL, SQL Server, SQLite and others all support the same core syntax).
  • A client or tool for running queries: a command-line client, a GUI such as a database workbench, or your application's query runner.
  • The table name and its column names, which you can confirm with a DESCRIBE table_name; or by inspecting the table in your tool.
  • INSERT permission on that table for the account you are using.
  • Values that match each column's data type and any constraints (NOT NULL, UNIQUE, foreign keys).

Step-by-Step

  1. Confirm the table and its columns. Run DESCRIBE table_name; (or the equivalent in your tool) so you know the exact column names, their order and their types. This prevents the most common mismatch errors.
  2. Write the INSERT statement with an explicit column list. Start with INSERT INTO table_name (column1, column2, column3). Naming the columns is safer than relying on the table's physical order, which can change.
  3. Add the VALUES clause. Follow with VALUES (value1, value2, value3);. Put text and dates in single quotes, leave numbers unquoted, and use NULL (without quotes) for an empty value where the column allows it.
  4. Match the order and count. The first value must correspond to the first column you listed, the second to the second, and so on. Count both lists to make sure they are equal.
  5. Insert multiple rows in one statement if you like. Separate each row's values with a comma: VALUES ('a','b'), ('c','d');. This is faster than many single-row statements and is supported by the major databases.
  6. Run the query. Execute it in your client. A successful insert usually reports the number of rows affected (for example, 1 row inserted).
  7. Verify the result. Run a SELECT filtered to the new row, such as SELECT * FROM table_name WHERE email = 'ana@example.com';, to confirm the data landed correctly.
  8. Commit if you are in a transaction. If your client runs in manual-commit mode, issue COMMIT; so the change is saved. Otherwise the row may be rolled back when the session ends.

Common Mistakes

  • Column and value counts differ. This produces an error such as "column count doesn't match value count." Recount both lists.
  • Forgetting quotes around text. Unquoted strings are read as column names or cause syntax errors.
  • Quoting numbers. Most databases accept '42' for a numeric column, but relying on implicit conversion can fail or behave differently across systems.
  • Omitting a required column. Columns marked NOT NULL without a default will reject the insert.
  • Duplicating a UNIQUE value. Inserting an email or ID that already exists fails if that column has a UNIQUE or primary-key constraint.
  • Assuming auto-increment columns need a value. Usually you leave an ID column out of the list and let the database generate it.
  • Not committing. The row appears in your session but vanishes for others until you commit.

Tips

  • Always list columns explicitly. It makes statements readable and protects them from table changes.
  • Use parameterized queries (placeholders such as ? or $1) in application code rather than pasting values into the SQL string. This prevents SQL injection and handles quoting for you.
  • Test on a development copy of the table before running inserts against production data.
  • Wrap several related inserts in a transaction so they either all succeed or all roll back.
  • If you are copying data from another table, INSERT INTO ... SELECT ... avoids retyping values.

Troubleshooting

If the insert fails, read the error message first; it usually names the column or constraint at fault.

  • Syntax error near a value: check for a missing quote, comma or closing parenthesis.
  • Column count mismatch: compare the number of columns listed with the number of values supplied.
  • Cannot insert NULL: a required column was left out or set to NULL. Supply a value or a default.
  • Duplicate entry: a UNIQUE or primary-key column already holds that value. Change it or update the existing row instead.
  • Foreign key constraint fails: the referenced row does not exist yet. Insert the parent row first.
  • Permission denied: your database account lacks INSERT rights on that table; ask the database administrator.
  • Data truncated or type error: the value does not fit the column's type or length. Check the column definition and shorten or convert the value.

Frequently Asked Questions

Do I have to list every column in the INSERT statement?

No. You can list only the columns you are supplying values for, as long as every omitted column is nullable or has a default value. Columns without either must be included.

How do I insert a date or timestamp?

Put it in single quotes in a format your database accepts, such as '2024-05-01' or '2024-05-01 14:30:00'. Exact accepted formats vary by database, so check its documentation.

Can I insert several rows at once?

Yes. Add multiple value groups separated by commas after VALUES. This is supported by MySQL, PostgreSQL, SQL Server and SQLite.

What is the difference between INSERT INTO and INSERT OR REPLACE?

Plain INSERT INTO adds a new row and fails on a duplicate key. Variants such as INSERT OR REPLACE (SQLite) or ON CONFLICT (PostgreSQL) update or replace an existing row instead. Support and syntax differ by database.

Why did my inserted row disappear?

Most likely the transaction was never committed, or it was rolled back. Check your client's autocommit setting and commit explicitly if needed.

How do I insert the current date and time automatically?

Use the database's built-in function, such as NOW() or CURRENT_TIMESTAMP, in place of a literal value. The exact function name varies by database.