MySQL INSERT INTO Statement

MySQL Tutorial

MySQL INSERT INTO Statement

The INSERT INTO statement in MySQL is used to add new records (rows) to a table. It is one of the most commonly used SQL commands for storing data in a database.


📌 Basic Syntax

There are two ways to use the INSERT INTO statement:


 1. Insert values into all columns


✔ Use this only when you provide values for every column in the correct order.


 2. Insert values into specific columns


✔ This method is preferred because it is flexible and avoids errors.


🧪 Example Table

Assume a table named students:

id name age city

🏷 Example 1: Insert All Column Values



🏷 Example 2: Insert into Selected Columns


If the id column is AUTO_INCREMENT, MySQL will automatically assign it.


📌 Insert Multiple Rows in One Query


✔ This method is faster than running separate insert statements.


🔠 Insert with NULL Value

If a column allows NULL, you can insert null like:



🔧 Insert with Default Values

If a table has default values:


Here, age will take its default or null value.


🎯 Example with AUTO_INCREMENT

Table structure:


Insert:

🟢 MySQL automatically assigns the next available id.


🧠 Best Practices

✔ Always specify column names
✔ Use proper data types
✔ Use multiple row insert for performance


🏁 Summary

Feature Example
Simple insert INSERT INTO table VALUES (...);
Insert in selected columns INSERT INTO table (col1, col2) VALUES (...);
Insert multiple rows INSERT INTO table VALUES (...), (...);
Insert NULL VALUES ('Name', NULL, 'City')
AUTO_INCREMENT support No need to specify id

✔ The INSERT INTO statement is essential for adding new data to a MySQL table.

You may also like...