Once your table is created, the next step is inserting data into it. This chapter covers different ways to add records to a MySQL table.


✅ Basic Syntax

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

🎓 Example: Insert One Row

For the students table created earlier:

INSERT INTO students (first_name, last_name, email, birthdate)
VALUES ('John', 'Doe', 'john.doe@example.com', '2002-06-01');

💡 id will auto-increment, and is_active will default to TRUE.


🔁 Insert Multiple Rows

INSERT INTO students (first_name, last_name, email, birthdate)
VALUES 
('Alice', 'Smith', 'alice@example.com', '2003-02-15'),
('Bob', 'Brown', 'bob@example.com', '2001-12-25');

🔒 Insert with Explicit Columns

You can omit optional columns (like is_active) if they have default values.

INSERT INTO students (first_name, last_name, email)
VALUES ('Jane', 'Miller', 'jane.miller@example.com');

🚨 Handling Duplicate Entries

Use INSERT IGNORE to skip duplicate entries (for UNIQUE columns):

INSERT IGNORE INTO students (email) VALUES ('john.doe@example.com');

Or use REPLACE to overwrite the row:

REPLACE INTO students (id, first_name, last_name, email)
VALUES (1, 'Johnny', 'Doe', 'johnny.doe@example.com');

Or use ON DUPLICATE KEY UPDATE:

INSERT INTO students (id, email)
VALUES (1, 'johnny.doe@example.com')
ON DUPLICATE KEY UPDATE email = VALUES(email);

📊 View Inserted Data

SELECT * FROM students;

❗ Insert with NULL Values

If a column allows NULLs:

INSERT INTO students (first_name, last_name, email, birthdate)
VALUES ('Mark', 'Lee', NULL, NULL);

📝 Best Practices

  • Always match column order with the value order
  • Use parameterized queries in applications to prevent SQL injection
  • Avoid inserting into AUTO_INCREMENT columns unless necessary
  • Validate data before inserting (e.g., date formats, unique emails)

🎯 Summary

TaskCommand
Insert one rowINSERT INTO table_name (...) VALUES (...);
Insert multiple rowsUse multiple (...) sets separated by commas
Ignore duplicatesINSERT IGNORE
Update if duplicateON DUPLICATE KEY UPDATE

Once your table is created, the next step is inserting data into it. This chapter covers different ways to add records to a MySQL table.


✅ Basic Syntax

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

🎓 Example: Insert One Row

For the students table created earlier:

INSERT INTO students (first_name, last_name, email, birthdate)
VALUES ('John', 'Doe', 'john.doe@example.com', '2002-06-01');

💡 id will auto-increment, and is_active will default to TRUE.


🔁 Insert Multiple Rows

INSERT INTO students (first_name, last_name, email, birthdate)
VALUES 
('Alice', 'Smith', 'alice@example.com', '2003-02-15'),
('Bob', 'Brown', 'bob@example.com', '2001-12-25');

🔒 Insert with Explicit Columns

You can omit optional columns (like is_active) if they have default values.

INSERT INTO students (first_name, last_name, email)
VALUES ('Jane', 'Miller', 'jane.miller@example.com');

🚨 Handling Duplicate Entries

Use INSERT IGNORE to skip duplicate entries (for UNIQUE columns):

INSERT IGNORE INTO students (email) VALUES ('john.doe@example.com');

Or use REPLACE to overwrite the row:

REPLACE INTO students (id, first_name, last_name, email)
VALUES (1, 'Johnny', 'Doe', 'johnny.doe@example.com');

Or use ON DUPLICATE KEY UPDATE:

INSERT INTO students (id, email)
VALUES (1, 'johnny.doe@example.com')
ON DUPLICATE KEY UPDATE email = VALUES(email);

📊 View Inserted Data

SELECT * FROM students;

❗ Insert with NULL Values

If a column allows NULLs:

INSERT INTO students (first_name, last_name, email, birthdate)
VALUES ('Mark', 'Lee', NULL, NULL);

📝 Best Practices

  • Always match column order with the value order
  • Use parameterized queries in applications to prevent SQL injection
  • Avoid inserting into AUTO_INCREMENT columns unless necessary
  • Validate data before inserting (e.g., date formats, unique emails)

🎯 Summary

TaskCommand
Insert one rowINSERT INTO table_name (...) VALUES (...);
Insert multiple rowsUse multiple (...) sets separated by commas
Ignore duplicatesINSERT IGNORE
Update if duplicateON DUPLICATE KEY UPDATE