---Advertisement---

MySQL INSERT INTO Query: A Complete Guide with Examples Best 2025

By Manisha

Updated On:

---Advertisement---
MySQL INSERT INTO Query

👉What is INSERT INTO?

MySQL INSERT INTO Query: The INSERT INTO statement is used to add new records to a MySQL table. It creates a new row and populates it with the specified values. This command is commonly used in applications to store data dynamically.

sql

INSERT INTO `table_name` (column_1, column_2, …) 

VALUES (value_1, value_2, …);

  • INSERT INTO table_name – Tells MySQL to add a new row to the specified table.
  • (column_1, column_2, …) – Specifies the columns where data will be inserted.
  • VALUES (value_1, value_2, …) – Defines the values to be stored in the respective columns.
  • String values should be enclosed in single quotes (‘value’).
  • Numeric values should not be enclosed in quotes.
  • Date values should be enclosed in single quotes (‘YYYY-MM-DD’).

👉MySQL INSERT INTO Example

MySQL INSERT INTO Query: Consider a members table that stores details about users. Here’s how to insert a new record:

sql

INSERT INTO `members` (`full_names`, `gender`, `physical_address`, `contact_number`) 

VALUES (‘Leonard Hofstadter’, ‘Male’, ‘Woodcrest’, ‘0845738767’);

👉 Note: If a contact number starts with 0, enclose it in quotes to prevent MySQL from dropping the leading zero.

You can change the order of columns in an INSERT statement as long as the values match:

sql

INSERT INTO `members` (`contact_number`, `gender`, `full_names`, `physical_address`) 

VALUES (‘0938867763’, ‘Male’, ‘Rajesh Koothrappali’, ‘Woodcrest’);

If a column is omitted, MySQL inserts NULL by default.


👉Inserting Data from Another Table

MySQL INSERT INTO Query: To copy all records from categories into categories_archive:

sql

INSERT INTO `categories_archive` SELECT * FROM `categories`;

For better control, specify column names:

sql

INSERT INTO `categories_archive` (category_id, category_name, remarks) 

SELECT category_id, category_name, remarks FROM `categories`;


👉PHP Example: Inserting Data into MySQL

MySQL INSERT INTO Query:Using mysqli_query(), you can insert data into a table via PHP:

php

$servername = “localhost”;

$username = “alex”;

$password = “yPXuPT”;

$dbname = “afmznf”;

// Create connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn) {

    die(“Connection failed: ” . mysqli_connect_error());

}

$sql = “INSERT INTO addkeyworddata(link, keyword) VALUES (‘https://www.guru99.com/’, ‘1000’)”;

if (mysqli_query($conn, $sql)) {

    echo “New record created successfully”;

} else {

    echo “Error: ” . $sql . “<br>” . mysqli_error($conn);

}


👉Summary

✅ INSERT INTO is used to add new rows to a table.
✅ Use single quotes for string and date values.
✅ Numeric values do not need quotes.
✅ You can insert data from one table into another.

DATABASE IN MYSQL

Download My SQL

Leave a Comment

Index