How to Insert Values Into a MySQL Table: Step-by-Step
In this article
To insert values into a MySQL table, use the INSERT INTO statement. The shortest working form is INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2'); — run it from the MySQL command-line client, MySQL Workbench, phpMyAdmin, or your application code. You need a running MySQL server, a database selected, a table that already exists, and a user account with INSERT privilege on that table. This guide walks through the syntax, the common variants (single row, multiple rows, defaults, and inserting from another table), and the mistakes that cause most errors.
What You'll Need
- A running MySQL server (or MariaDB, which uses the same syntax for these statements).
- A client: the
mysqlcommand-line client, MySQL Workbench, phpMyAdmin, or a programming language driver such as MySQL Connector/Python or themysql2package for Node.js. - A database and a table that already exist. You cannot insert into a table that has not been created.
- Login credentials for a MySQL user with the INSERT privilege on the target table.
- Knowledge of the table's column names and data types — run
DESCRIBE table_name;to see them.
Step-by-Step
- Connect to the server. From a terminal, run
mysql -u your_user -pand enter your password when prompted. In MySQL Workbench, open a connection and a new query tab. In phpMyAdmin, select the database in the left sidebar and open the SQL tab. Menu labels can differ slightly by version and platform, but the SQL itself is identical everywhere. - Select the database. Run
USE your_database_name;. Without this, MySQL returns “No database selected” when you try to insert. You can also prefix the table name with the database name, as inyour_database_name.your_table. - Inspect the table structure. Run
DESCRIBE your_table;orSHOW COLUMNS FROM your_table;. Note which columns are NOT NULL, which have defaults, and which are AUTO_INCREMENT. This tells you which columns you must supply and which you can skip. - Write a single-row INSERT. The explicit-column form is the safest:
INSERT INTO your_table (name, email, age) VALUES ('Ada', 'ada@example.com', 36);. List columns in any order, but the values must match that order exactly. Text and date values go in single quotes; numbers do not need quotes. - Run the statement. Press Enter in the command-line client or click Execute/Run in your GUI. A successful insert returns a message like
Query OK, 1 row affected. - Verify the row. Run
SELECT * FROM your_table ORDER BY id DESC LIMIT 5;(adjust the column name to your primary key). Confirm the values landed in the right columns and that no unexpected defaults were applied. - Insert multiple rows in one statement. Separate value tuples with commas:
INSERT INTO your_table (name, email, age) VALUES ('Ada', 'ada@example.com', 36), ('Grace', 'grace@example.com', 41), ('Linus', 'linus@example.com', 29);. This is faster than three separate statements and is atomic — if one row fails, none are inserted. - Use DEFAULT or omit columns you do not want to set. If a column has a default value or is AUTO_INCREMENT, leave it out of the column list entirely. You can also write
DEFAULTin the values list:INSERT INTO your_table (name, created_at) VALUES ('Ada', DEFAULT);. Omitting a column that has no default and is NOT NULL causes an error. - Insert from another table. Use
INSERT INTO target_table (col1, col2) SELECT col1, col2 FROM source_table WHERE condition;. The SELECT must return the same number of columns, in the same order and compatible types. This is the standard way to copy or migrate rows. - Handle duplicate keys deliberately. If the table has a PRIMARY KEY or UNIQUE index, a duplicate value triggers error 1062. You can use
INSERT IGNOREto skip duplicates (it also suppresses other errors, so use it carefully), orINSERT ... ON DUPLICATE KEY UPDATE col = VALUES(col)to update the existing row instead. Decide which behaviour you want before running bulk inserts. - Check the result and commit if needed. If autocommit is off (common in application code and some GUI sessions), run
COMMIT;to make the insert permanent. In the command-line client, autocommit is on by default, so the row is saved immediately.
Common Mistakes
- Column and value counts not matching. Error 1136 (“Column count doesn't match value count”) means the number of columns and values differs. Count both lists.
- Forgetting quotes around strings or dates.
VALUES (Ada)is read as a column name, not text. Use'Ada'. - Using double quotes for strings. MySQL accepts them by default, but if
ANSI_QUOTESSQL mode is enabled, double quotes mean identifiers. Single quotes are the portable choice. - Omitting a required column. A NOT NULL column with no default must be supplied, or you get error 1364.
- Inserting the wrong type. Putting text into an INT column, or an invalid date like
'2024-02-31', produces an error or, in non-strict mode, a silent truncation. CheckSHOW WARNINGS;after the insert. - Assuming AUTO_INCREMENT values. Do not hard-code IDs unless you have a reason; let MySQL assign them.
- Building SQL by string concatenation in application code. This is the classic SQL injection risk. Use prepared statements with placeholders (
?or%s, depending on the driver).
Tips
- Always name the columns in the INSERT.
INSERT INTO t VALUES (...)without a column list breaks the moment someone adds or reorders a column. - Wrap multi-row inserts in a transaction when you need all-or-nothing behaviour across several statements.
- For large imports, batch rows (hundreds to a few thousand per statement) rather than one row per statement.
- Use
SHOW WARNINGS;after inserts that “succeed” but may have truncated data. - Test destructive or bulk statements on a copy of the table first, especially
INSERT ... SELECTandON DUPLICATE KEY UPDATE.
Troubleshooting
“No database selected” (error 1046): run USE your_database; first, or qualify the table name with the database name.
“Table doesn't exist” (error 1146): check spelling and case. On Linux, table names are case-sensitive by default; on Windows and macOS they usually are not.
“Access denied” (error 1142): the user lacks INSERT privilege. Ask an administrator to grant it, or connect as a user who has it.
“Duplicate entry” (error 1062): a unique or primary key value already exists. Change the value, use INSERT IGNORE, or switch to ON DUPLICATE KEY UPDATE.
“Data too long” (error 1406): the string exceeds the column's defined length. Shorten the value or alter the column type.
“Incorrect integer value” or “Incorrect date value”: the literal does not match the column type. Check the value and the column definition with DESCRIBE.
Insert seems to succeed but the row is missing: you may be connected to a different database or server than you think, or autocommit is off and you have not committed. Run SELECT DATABASE(); and check your session settings.
Frequently Asked Questions
Can I insert a row without listing column names?
Yes: INSERT INTO your_table VALUES (...); works, but you must supply a value for every column in the table's defined order. It is fragile and generally discouraged.
How do I insert the current date and time?
Use NOW() or CURRENT_TIMESTAMP as the value for a DATETIME or TIMESTAMP column, for example INSERT INTO t (created_at) VALUES (NOW());.
What is the difference between INSERT IGNORE and ON DUPLICATE KEY UPDATE?
INSERT IGNORE skips rows that would cause a duplicate-key error and leaves the existing row unchanged. ON DUPLICATE KEY UPDATE updates the existing row with new values you specify.
Can I insert multiple rows in one statement?
Yes. Separate each set of values with a comma after VALUES. This is faster and atomic compared with separate statements.
How do I insert a NULL value?
Write the keyword NULL without quotes: INSERT INTO t (name, note) VALUES ('Ada', NULL);. The column must allow NULL.
Why does my insert return “0 rows affected”?
With INSERT IGNORE or ON DUPLICATE KEY UPDATE, a duplicate that is skipped or an update that changes nothing reports 0 rows affected. Check the statement type and the existing data.