SQL Learning
SQL INSERT Statement
SQL Learning
SQL INSERT Statement
Learn how to use INSERT INTO to add new data to a table. Includes single row, multi-row, and column-specific examples.
What Does the INSERT Statement Do?
The INSERT
statement is used to add new rows to a table. You can insert one row at a time, or multiple rows at once. It's a go-to command when loading user info, uploading content, or storing interaction data.
Basic Syntax
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example: Add a User to TokTuk
INSERT INTO Users (user_id, username, email)
VALUES (101, 'daisy', 'daisy@example.com');
Insert Multiple Rows
Insert several records at once by separating row values with commas:
INSERT INTO Videos (video_id, title, views)
VALUES
(1, 'Welcome to TokTuk', 100),
(2, 'How to SQL', 250),
(3, 'Cat Compilation', 999);
Insert Without Specifying Columns
If you're inserting values for every column in order, you can omit the column list:
INSERT INTO Users
VALUES (102, 'kai', 'kai@toktuk.io');
⚠️ Caution: This only works if you're providing values for *all* columns, in the correct order.
Best Practices for INSERT
- Always name your columns to avoid surprises when schema changes
- Use
DEFAULT
values where appropriate (e.g. created_at) - Use transactions when inserting multiple related rows
- Validate user input to avoid inserting bad data
- Watch out for constraint violations (e.g. UNIQUE, NOT NULL)