Inserting Data

Site Admin · 11 Sep 2026 · 2 views

The INSERT Statement

You add rows to a table with INSERT. The column list after the table name is optional, but providing it is safer when column order changes.

INSERT INTO customers (name, email, city) VALUES (
  'Priya Sharma', 'priya@example.com', 'Mumbai'
);

INSERT INTO customers (name, email, city) VALUES
  ('Rahul Patel', 'rahul@example.com', 'Pune'),
  ('Anjali Rao', 'anjali@example.com', 'Hyderabad');

Selecting What You Inserted

SELECT * FROM customers;

Inserting From Another Table

INSERT INTO vip_customers (name, email)
  SELECT name, email FROM customers WHERE city = 'Mumbai';

Ignoring Duplicates

INSERT IGNORE INTO customers (name, email) VALUES ('Priya Sharma', 'priya@example.com');

With a UNIQUE column, INSERT IGNORE silently skips conflicting rows instead of raising an error.

Handling Duplicate Keys With an Update

INSERT INTO customers (name, email, city) VALUES ('Priya', 'priya@example.com', 'Delhi')
ON DUPLICATE KEY UPDATE city = VALUES(city);

Key Points

  • List columns explicitly to make INSERT robust.
  • Multiple rows can be inserted in one statement.
  • Use INSERT ... SELECT to copy data between tables.
  • INSERT IGNORE and ON DUPLICATE KEY UPDATE manage unique conflicts.
Share this post:

Comments (0)

Please login or register to comment.