Smart Systems for Home

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

SQL INSERT INTO: How to Insert Table Values

· 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. The basic form is INSERT INTO table_name (column1, column2) VALUES (value1, value2);. This adds one new row. You can add multiple rows in one statement by separating value groups with commas. The columns you list must match the values in number, order and data type. Any column you omit must allow NULL or have a default value, otherwise the insert fails.

What You'll Need

  • A database you can write to (MySQL, PostgreSQL, SQL Server, SQLite, etc.) and a client such as the command line, phpMyAdmin, pgAdmin, Azure Data Studio or DBeaver.
  • The exact table name and column names. Names are case-sensitive on some systems and may need quoting if they contain spaces or reserved words.
  • Permission to insert rows (an INSERT privilege on that table).
  • Values in the right format: text in single quotes, numbers without quotes, dates in the format your database expects (often 'YYYY-MM-DD').

Step-by-Step

  1. Connect to the right database. Open your SQL client and select the database that contains the table. In the command line you usually run USE database_name; (MySQL) or connect with \c database_name (psql). If you skip this, you may get a “table does not exist” error even though it does.
  2. Check the table structure. Run DESCRIBE table_name; in MySQL or \d table_name in psql, or look at the columns list in your GUI. Note which columns are NOT NULL, which have defaults, and which are auto-increment or identity columns. You normally do not supply a value for an auto-increment primary key.
  3. Write the INSERT with explicit columns. List every column you are filling, then the matching values in the same order. Example: INSERT INTO customers (first_name, last_name, email) VALUES ('Ana', 'Lopez', 'ana@example.com'); Listing columns is safer than relying on table order, which can change.
  4. Quote values correctly. Put text and dates in single quotes. Do not put quotes around numbers. Escape a single quote inside text by doubling it: 'O''Brien'. In MySQL you can also use a backslash, but doubling works across engines.
  5. Insert several rows at once. Add more value groups: INSERT INTO customers (first_name, last_name, email) VALUES ('Ana','Lopez','ana@example.com'), ('Ben','Chan','ben@example.com'); This is faster than one statement per row and is easier to roll back as a unit.
  6. Run the statement and check the result. Your client should report something like “1 row affected” or “INSERT 0 1”. Then run a quick SELECT * FROM table_name WHERE ...; to confirm the row looks right.
  7. Commit if needed. If your client is in manual-commit mode (common in psql and some GUI tools), run COMMIT; so the row is saved. In autocommit mode this happens automatically.
  8. Use a transaction for bulk inserts. Wrap many inserts in BEGIN; … COMMIT; so a failure partway through does not leave half the data in. If something goes wrong, ROLLBACK; undoes the whole batch.

Common Mistakes

  • Column count does not match value count. The database rejects the statement. Count both sides carefully, especially after editing.
  • Wrong data type. Putting text in a numeric column or a badly formatted date causes an error or, worse, silent conversion. Use the format the column expects.
  • Forgetting quotes around text. VALUES (Ana) is read as a column name, not a string.
  • Omitting a required column. If a column is NOT NULL with no default, the insert fails. Either supply a value or change the table definition deliberately.
  • Duplicate primary key. Inserting an ID that already exists triggers a unique constraint error. Let auto-increment handle IDs, or check first.
  • Assuming the insert is saved. In manual-commit clients, an uncommitted insert disappears when the session closes.

Tips

  • Always name the columns in the INSERT. It protects you when the table gains a column later.
  • Use parameterised queries in application code (for example ? placeholders) instead of building SQL strings. This prevents SQL injection and handles quoting for you.
  • For large imports, use the bulk loader your database provides (for example COPY in PostgreSQL or LOAD DATA in MySQL) rather than thousands of individual INSERTs.
  • Test on a small sample first, then run the full batch inside a transaction.
  • Keep a backup or a copy of the table before bulk changes, especially on production systems.

Troubleshooting

If the statement fails, read the error message first; it usually names the column or constraint. A “column does not exist” error often means a typo, wrong database selected, or a name that needs quoting. A “cannot insert NULL” error points to a missing required column. A duplicate-key error means the value already exists; check the primary key or unique index. If the client says success but you cannot see the row, confirm you are querying the same database and schema, and check whether you need to commit. If permissions are the issue, the error mentions denied access; ask the database administrator for INSERT rights on that table. When in doubt, run the INSERT inside a transaction, inspect the result with a SELECT, and roll back if it is wrong.

Frequently Asked Questions

Can I insert a row without listing columns?

Yes, INSERT INTO table_name VALUES (...); works, but you must supply a value for every column in the table's defined order. It is fragile and not recommended.

How do I insert the current date and time?

Use the function your database provides, such as NOW() in MySQL or CURRENT_TIMESTAMP in PostgreSQL and SQL Server, inside the VALUES list.

What happens if I insert a duplicate primary key?

The statement fails with a unique constraint error and no row is added. Some databases offer an “insert or update” variant, but check your engine's syntax before relying on it.

Can I insert multiple rows in one statement?

Yes, most databases support multiple value groups separated by commas. This is more efficient than separate statements and easier to wrap in a transaction.

Why does my insert seem to disappear?

You are probably in manual-commit mode and did not run COMMIT, or you are looking at a different database or schema. Check both before assuming data loss.

How do I insert text that contains an apostrophe?

Double the apostrophe inside single quotes, for example 'O''Brien'. Parameterised queries handle this automatically in application code.