MySQL — Insert
Basic INSERT
INSERT INTO users (name, email, age)
VALUES ('John Doe', 'john@example.com', 25);
Insert Multiple Rows
INSERT INTO users (name, email, age) VALUES
('John Doe', 'john@example.com', 25),
('Jane Smith', 'jane@example.com', 30),
('Bob Johnson', 'bob@example.com', 35);
Insert with NULL
INSERT INTO users (name, email, age)
VALUES ('John Doe', 'john@example.com', NULL);
INSERT Examples
-- Insert single row
INSERT INTO products (name, price, stock)
VALUES ('Laptop', 999.99, 50);
-- Insert multiple rows
INSERT INTO products (name, price, stock) VALUES
('Phone', 699.99, 100),
('Tablet', 499.99, 75),
('Watch', 299.99, 200);
-- Insert with default values
INSERT INTO users (name, email)
VALUES ('John Doe', 'john@example.com');
-- Insert from another table
INSERT INTO archived_users
SELECT * FROM users WHERE created_at < '2023-01-01';
INSERT with AUTO_INCREMENT
-- Let auto_increment handle id
INSERT INTO users (name, email, age)
VALUES ('John Doe', 'john@example.com', 25);
-- Get last inserted id
SELECT LAST_INSERT_ID();
Mini Practice
Write MySQL code that:
- Inserts a single row
- Inserts multiple rows
- Inserts with default values
- Inserts from another table
Up Next
Next: Learn about Selecting Data.
Related Topics
Frequently Asked Questions about Insert
What is Insert in MySQL?
Insert is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Insert?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Insert.
Why is Insert important in MySQL?
Insert is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.