MySQL Insert Into
The INSERT INTO statement adds new rows of data into an existing MySQL table.
Adding Data with INSERT INTO
The basic form of INSERT INTO is INSERT INTO table_name (column1, column2) VALUES (value1, value2);. Naming the columns explicitly is optional but strongly recommended, since it makes the statement correct even if the table's column order ever changes.
MySQL also allows inserting several rows in a single statement by listing multiple VALUES groups separated by commas. This is typically faster than issuing one INSERT statement per row, because the server only has to parse and commit the statement once.
Handling Missing Values and Defaults
- Columns you omit from the column list receive their default value or NULL
- Never provide a value for AUTO_INCREMENT columns; let MySQL generate them
- Columns marked NOT NULL without a default must always be given a value
- Use the DEFAULT keyword to explicitly request a column's default value
- String and date literals must be wrapped in quotes, unlike numbers
Query Examples
Insert a single row
INSERT INTO customers (name, email, country)
VALUES ('Ravi Kumar', 'ravi@example.com', 'India');Insert multiple rows at once
INSERT INTO customers (name, email, country) VALUES
('Mia Chen', 'mia@example.com', 'Canada'),
('Leo Silva', 'leo@example.com', 'Brazil'),
('Zoe Adams', 'zoe@example.com', 'Australia');Insert a row relying on column defaults
INSERT INTO customers (name, email)
VALUES ('Sam Patel', 'sam@example.com');Note: If the number of values doesn't match the number of listed columns, or a NOT NULL column is left out, MySQL rejects the entire statement with an error.
Note: Always list columns explicitly in INSERT INTO rather than relying on the table's physical column order, which can silently change over time.
Exercise: MySQL Insert Into
What is the purpose of the INSERT INTO statement?