π What Youβll Learn
- What
INSERT INTO
is used for - Syntax to insert single and multiple rows
- Examples with real-world context
- Things to watch out for
π§© What Is INSERT INTO?
The INSERT INTO
statement is used to add new records to a database table.
It allows you to populate your table with fresh data, either one row at a time or multiple rows at once.
π§± Basic Syntax
Insert a single row (specifying columns):
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
Insert multiple rows:
INSERT INTO table_name (column1, column2)
VALUES
(value1a, value2a),
(value1b, value2b);
π§ͺ Example: Adding New Users
Single user
INSERT INTO users (name, email, country)
VALUES ('Alice', 'alice@example.com', 'USA');
Multiple users
INSERT INTO users (name, email, country)
VALUES
('Bob', 'bob@example.com', 'UK'),
('Lena', 'lena@example.com', 'Canada');
β οΈ Tips & Common Pitfalls
- π Always specify column names to avoid order mismatch issues
- β Data types must match the column definitions (e.g., no text in numeric fields)
- π« Missing
NOT NULL
fields without default values will cause errors - π Use
RETURNING *
(PostgreSQL) to get the inserted data back
π Recap
INSERT INTO
adds data to a table- You can insert one or many rows at once
- Always match value types to column types
- Naming columns explicitly is a best practice