MySQL in MySQL is best learned by connecting the rule to an order-management database. Start with the smallest SQL statement, observe the output, and then add one realistic constraint so the concept becomes practical.
The key habit for this lesson is to watch table rows, constraints, and indexes as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.
The INSERT INTO statement adds new rows to a table. You can specify the column list explicitly (recommended) or rely on column order. Always list columns explicitly to make your code resilient to future schema changes.
-- Insert a single row (explicit column list)
INSERT INTO customers (first_name, last_name, email, phone, active)
VALUES ('Alice', 'Johnson', 'alice@example.com', '555-0101', 1);
-- Insert using SET syntax (alternative, column = value pairs)
INSERT INTO customers SET
first_name = 'Bob',
last_name = 'Smith',
email = 'bob@example.com',
active = 1;
-- Insert multiple rows in a single statement (much faster than multiple INSERTs)
INSERT INTO customers (first_name, last_name, email, active)
VALUES
('Carol', 'White', 'carol@example.com', 1),
('Dave', 'Brown', 'dave@example.com', 1),
('Eve', 'Davis', 'eve@example.com', 0),
('Frank', 'Wilson', 'frank@example.com', 1);
-- Get the AUTO_INCREMENT ID of the last inserted row
SELECT LAST_INSERT_ID();
You can insert rows from the result of a SELECT query. This is useful for copying data between tables, creating summary tables, or archiving records.
-- Copy active customers to an archive table
INSERT INTO customers_archive (first_name, last_name, email, active)
SELECT first_name, last_name, email, active
FROM customers
WHERE active = 1;
-- INSERT IGNORE: skip rows that would cause a duplicate key error
-- (useful when importing data that may have duplicates)
INSERT IGNORE INTO customers (first_name, last_name, email, active)
VALUES ('Alice', 'Johnson', 'alice@example.com', 1);
-- If alice@example.com already exists (UNIQUE key), this row is silently skipped
-- Insert products into the shop
INSERT INTO products (name, description, price, stock, category)
VALUES
('Laptop Pro 15', 'High-performance laptop', 1299.99, 50, 'Electronics'),
('Wireless Mouse', 'Ergonomic wireless mouse', 29.99, 200, 'Electronics'),
('USB-C Hub', '7-in-1 USB-C hub', 49.99, 150, 'Electronics'),
('Desk Lamp', 'LED adjustable desk lamp', 39.99, 100, 'Office'),
('Notebook A5', 'Premium lined notebook', 9.99, 500, 'Stationery');
This statement inserts a row if no duplicate key exists, or updates the existing row if a duplicate is found. It's an efficient "upsert" pattern that avoids a separate SELECT check.
-- Assume email has a UNIQUE constraint on customers
-- If the email already exists, update the name and active status instead
INSERT INTO customers (first_name, last_name, email, active)
VALUES ('Alice', 'Johnson-Updated', 'alice@example.com', 1)
ON DUPLICATE KEY UPDATE
first_name = VALUES(first_name),
last_name = VALUES(last_name),
active = VALUES(active);
-- MySQL 8.0+ syntax using aliases (cleaner)
INSERT INTO customers (first_name, last_name, email, active)
VALUES ('Alice', 'Johnson-Updated', 'alice@example.com', 1) AS new_row
ON DUPLICATE KEY UPDATE
first_name = new_row.first_name,
last_name = new_row.last_name,
active = new_row.active;
-- Track stock changes: increment stock on duplicate product name
INSERT INTO products (name, price, stock, category)
VALUES ('Wireless Mouse', 29.99, 50, 'Electronics')
ON DUPLICATE KEY UPDATE
stock = stock + VALUES(stock);
Use MySQL when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real MySQL task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.
A reliable practice flow is: create the smallest working SQL statement, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with result set and EXPLAIN output. If the result surprises you, reduce the code until the behavior is visible again.
The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why MySQL is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.
Copying the syntax before understanding the behavior.
Write the expected behavior first, then make the example prove it.
Practicing only the perfect input.
Also test missing, repeated, empty, or boundary input before considering the lesson complete.
Looking only at the final output.
Trace table rows, constraints, and indexes through each important step.
Use it when the problem matches the behavior shown in the example and when the result can be verified through result set and EXPLAIN output.
Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.
Trace table rows, constraints, and indexes, predict the result, run the example, and compare your prediction with the actual output.
Explore 500+ free tutorials across 20+ languages and frameworks.