MySQL INSERT INTO Statement

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.

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *