INSERT INTO
Adds new row(s) into a table.
INSERT INTO table_name (col1, col2) VALUES (val1, val2);This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Adds new row(s) into a table.
The INSERT INTO statement is a core Data Manipulation Language (DML) command used to append new rows of data to a database table. From a performance perspective, batching multiple rows into a single statement is significantly more efficient than executing multiple individual statements, as it minimizes network round-trips and reduces the overhead of transaction logging and index updates. It is a critical best practice to explicitly list column names in the statement; this ensures the code remains resilient even if the underlying table schema changes (such as adding or reordering columns). For high-volume data migration, the INSERT INTO ... SELECT pattern allows for efficient server-side processing without transferring data back to the client. Edge cases include managing auto-incrementing primary keys, which should generally be omitted from the column list, and ensuring that all NOT NULL constraints and FOREIGN KEY dependencies are satisfied to avoid transaction rollbacks.
Quick reference
Syntax
INSERT INTO table_name (col1, col2) VALUES (val1, val2);
Inputs
Parameters
See it in practice
Examples
Standard Single Row Insert
INSERT INTO users (username, email, age) VALUES ('jdoe', 'jane@example.com', 28);Query OK, 1 row affected (0.01 sec)
Inserting a single record while explicitly naming the target columns to ensure reliability.
Multi-Row Bulk Insert
INSERT INTO products (name, price) VALUES
('Standard Widget', 19.99),
('Premium Gadget', 45.50),
('Value Pack', 9.99);Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0
A high-performance approach to inserting multiple records in a single database round-trip.
Insert From Select Statement
INSERT INTO archive_logs (event_type, description, log_date)
SELECT type, msg, created_at FROM system_logs
WHERE created_at < '2023-01-01T00:00:00Z';Query OK, 1250 rows affected (0.15 sec)
Migrating data from one table to another based on a query filter. Uses ISO strings for reliable date comparison.
Debug faster
Common Errors
ConstraintViolationError
Cause: Attempting to insert a value that already exists in a UNIQUE column or a PRIMARY KEY column.
Fix: Check for existing records before inserting, or use database-specific extensions like ON CONFLICT (PostgreSQL) or ON DUPLICATE KEY UPDATE (MySQL).
INSERT INTO employees (id, name) VALUES (1, 'Alice'); -- Error if ID 1 existsColumnValueMismatchError
Cause: The number of columns specified in the column list does not match the number of values provided in the VALUES clause.
Fix: Ensure every listed column has a corresponding value in the VALUES list, or remove the column name if it should use a DEFAULT value.
INSERT INTO tags (name, slug) VALUES ('Featured'); -- Missing value for 'slug'Runtime support
Compatibility
Upsert syntax varies by DB
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Adds new row(s) into a table.
ame: Target table to insert into. columns: Columns to set values for. VALUES: Values matching the column list.
ConstraintViolationError: Check for existing records before inserting, or use database-specific extensions like ON CONFLICT (PostgreSQL) or ON DUPLICATE KEY UPDATE (MySQL). ColumnValueMismatchError: Ensure every listed column has a corresponding value in the VALUES list, or remove the column name if it should use a DEFAULT value.