---
description: Insert INTO command is an important function in Database. It is used to insert new row in a table.The INSERT command can also be used to insert data from one table into another.
title: "MySQL INSERT INTO Query: How to add Row in Table (Example)"
image: https://www.guru99.com/images/mysql-insert-into-query.png
---

[Skip to content](#main)

**⚡ Smart Summary**

MySQL INSERT INTO Query adds new rows to a table, either by naming the target columns explicitly or by supplying a value for every column. Correct quoting of strings, numbers, and dates determines whether the stored data survives intact.

- 📥 **Core Syntax:** INSERT INTO \`table\_name\`(column\_1,column\_2) VALUES (value\_1,value\_2) creates one new row in the named table.
- 🔤 **Quoting Rule:** String and date values require single quotes; numeric values are supplied without quotes.
- 0️⃣ **Leading Zero Trap:** An unquoted phone number is read as a number, so the leading zero is silently dropped.
- 🔀 **Column Order:** Column order inside the query is irrelevant, provided each value maps to its matching column.
- 🕳️ **Omitted Columns:** Columns left out of the statement receive NULL, or their declared default value.
- 📑 **Table to Table:** INSERT INTO … SELECT copies rows between tables when the column structures align.

Read More

![](https://www.guru99.com/images/mysql-insert-into-query.png)

## What is INSERT INTO?

**INSERT INTO** is used to store data in the tables. The INSERT command creates a new row in the table to store data. The data is usually supplied by application programs that run on top of the database.

## Basic syntax

Let’s look at the basic syntax of the INSERT INTO MySQL command:

```
INSERT INTO `table_name`(column_1,column_2,...) VALUES (value_1,value_2,...);
```

**HERE**

- INSERT INTO \`table\_name\` is the command that tells MySQL server to add a new row into a table named \`table\_name.\`
- (column\_1,column\_2,…) specifies the columns to be updated in the new MySQL row
- VALUES (value\_1,value\_2,…) specifies the values to be added into the new row

When supplying the data values to be inserted into the new table, the following should be considered:

- String data types – all the string values should be enclosed in single quotes.
- Numeric data types – all numeric values should be supplied directly without enclosing them in single or double quotes.
- Date data types – enclose date values in single quotes in the format ‘YYYY-MM-DD’.

### Inserting Multiple Rows at Once

A single INSERT statement can carry several rows. Separate each set of values with a comma. One statement is faster than many, because the index updates once.

```
INSERT INTO `categories`(`category_name`,`remarks`) VALUES
('Documentary', 'Factual films'),
('Musical', 'Song driven films');
```

## MySQL INSERT INTO Query Example

Suppose that we have the following list of new library members that need to be added to the database.

| Full names | Date of Birth | gender | Physical address | Postal address | Contact number | Email Address |
| --- | --- | --- | --- | --- | --- | --- |
| Leonard Hofstadter |  | Male | Woodcrest |  | 0845738767 | |
| Sheldon Cooper |  | Male | Woodcrest |  | 0976736763 | |
| Rajesh Koothrappali |  | Male | Woodcrest |  | 0938867763 | |
| Leslie Winkle | 14/02/1984 | Male | Woodcrest |  | 0987636553 | |
| Howard Wolowitz | 24/08/1981 | Male | SouthPark | P.O. Box 4563 | 0987786553 | lwolowitz@email.me |

Let’s INSERT the data one row at a time, starting with Leonard Hofstadter. We will treat the contact number as a numeric data type and not enclose the number in single quotes.

```
INSERT INTO `members` (`full_names`,`gender`,`physical_address`,`contact_number`) VALUES ('Leonard Hofstadter','Male','Woodcrest',0845738767);
```

Executing the above script drops the 0 from Leonard’s contact number, because the value is treated as numeric and a leading zero is not significant.

To avoid such problems, the value must be enclosed in single quotes as shown below.

```
INSERT INTO `members` (`full_names`,`gender`,`physical_address`,`contact_number`) VALUES ('Sheldon Cooper','Male','Woodcrest', '0976736763');
```

**⚠️ Tip:** Store phone numbers, postal codes and similar identifiers in a text column such as VARCHAR. Numeric columns cannot keep a leading zero.

**Don't Miss:**

- [MySQL AUTO\_INCREMENT with Examples](https://www.guru99.com/auto-increment.html "MySQL AUTO_INCREMENT with Examples")
- [What is ER Modeling? Learn with Example](https://www.guru99.com/er-modeling.html)
- [11 Best SQL Tools & Software (2026)](https://www.guru99.com/sql-tools.html)
- [9 BEST Online SQL Compiler and Editors (2026)](https://www.guru99.com/best-online-sql-compiler-editors.html)

### Changing the Column Order

**Changing the order of the columns has no effect on the INSERT query in MySQL, as long as the correct values have been mapped to the correct columns.** The query below demonstrates this point.

```
INSERT INTO `members` (`contact_number`,`gender`,`full_names`,`physical_address`) VALUES ('0938867763','Male','Rajesh Koothrappali','Woodcrest');
```

The above queries skipped the date of birth column. By default, MySQL inserts NULL values into columns that are omitted from the INSERT query.

Let’s now insert the record for Leslie, which has the date of birth supplied. The date value must be enclosed in single quotes using the format ‘YYYY-MM-DD’.

```
INSERT INTO `members` (`full_names`,`date_of_birth`,`gender`,`physical_address`,`contact_number`) VALUES ('Leslie Winkle','1984-02-14','Male','Woodcrest', '0987636553');
```

### Omitting the Column List

All of the above queries named the columns and mapped them to values. If we supply values for ALL the columns in the table, the column list can be omitted from the MySQL insert query.

```
INSERT INTO `members` VALUES (9,'Howard Wolowitz','Male','1981-08-24',
'SouthPark','P.O. Box 4563', '0987786553', 'lwolowitz@email.me');
```

Let’s now use the [SELECT statement](https://www.guru99.com/select-statement.html) to view all the rows in the members table.

```
SELECT * FROM `members`;
```

| membership\_ number | full\_ names | gender | date\_of\_ birth | physical\_address | postal\_ address | contct\_ number | email |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 1 | Janet Jones | Female | 21-07-1980 | First Street Plot No 4 | Private Bag | 0759 253 542 | janetjones@yagoo.cm |
| 2 | Janet Smith Jones | Female | 23-06-1980 | Melrose 123 | NULL | NULL | jj@fstreet.com |
| 3 | Robert Phil | Male | 12-07-1989 | 3rd Street 34 | NULL | 12345 | rm@tstreet.com |
| 4 | Gloria Williams | Female | 14-02-1984 | 2nd Street 23 | NULL | NULL | NULL |
| 5 | Leonard Hofstadter | Male | NULL | Woodcrest | NULL | 845738767 | NULL |
| 6 | Sheldon Cooper | Male | NULL | Woodcrest | NULL | 0976736763 | NULL |
| 7 | Rajesh Koothrappali | Male | NULL | Woodcrest | NULL | 0938867763 | NULL |
| 8 | Leslie Winkle | Male | 14-02-1984 | Woodcrest | NULL | 0987636553 | NULL |
| 9 | Howard Wolowitz | Male | 24-08-1981 | SouthPark | P.O. Box 4563 | 0987786553 | lwolowitz@email.me |

Notice that Leonard Hofstadter’s contact number has dropped its leading zero, exactly as predicted. The quoted numbers retain it.

## Inserting into a Table from another Table

The INSERT command can also be used to insert data into a table from another table. The basic syntax is as shown below.

```
INSERT INTO table_1 SELECT * FROM table_2;
```

We will now create a dummy table for movie categories, called categories\_archive. The script below [creates the table](https://www.guru99.com/how-to-create-a-database.html).

```
CREATE TABLE `categories_archive` ( `category_id` int(11) AUTO_INCREMENT, `category_name` varchar(150) DEFAULT NULL, `remarks` varchar(500) DEFAULT NULL, PRIMARY KEY (`category_id`))
```

Execute the above script to create the table. Let’s now insert all the rows from the categories table into the categories archive table.

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

The table structures have to be the same for that script to work. A more robust script maps the column names in the destination table to the ones in the table containing the data, as shown below.

```
INSERT INTO `categories_archive`(category_id,category_name,remarks) SELECT category_id,category_name,remarks FROM `categories`;
```

Executing the SELECT query below gives the following results.

```
SELECT * FROM `categories_archive`;
```

| category\_id | category\_name | remarks |
| --- | --- | --- |
| 1 | Comedy | Movies with humour |
| 2 | Romantic | Love stories |
| 3 | Epic | Story acient movies |
| 4 | Horror | NULL |
| 5 | Science Fiction | NULL |
| 6 | Thriller | NULL |
| 7 | Action | NULL |
| 8 | Romantic Comedy | NULL |
| 9 | Cartoons | NULL |
| 10 | Cartoons | NULL |

## PHP Example: Insert into MySQL Table

The mysqli\_query function is used to execute [SQL](https://www.guru99.com/sql.html) queries from PHP. It runs Insert, Select, Update and Delete statements alike, and has the following syntax.

```
mysqli_query($db_handle,$query);
```

HERE,

- “mysqli\_query(…)” is the function that executes the SQL queries.
- “$query” is the SQL query to be executed.
- “$db\_handle” is the server connection link returned by mysqli\_connect.

### Example

```
$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" . '<br>';
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
```

## FAQs

⚠️ What happens if an INSERT INTO query duplicates a primary key?

MySQL rejects it with a duplicate entry error. Use INSERT IGNORE to skip the row silently, or INSERT … ON DUPLICATE KEY UPDATE to update the existing row instead.

🔑 How do I get the ID of the row I just inserted?

Run SELECT LAST\_INSERT\_ID() straight after the insert, or call mysqli\_insert\_id($conn) in PHP. It returns the AUTO\_INCREMENT key generated for the latest row on your own connection.

🛡️ How do I stop SQL injection in an INSERT statement?

Never concatenate user input into the query string. Use prepared statements with bound parameters, so values travel separately from the [SQL](https://www.guru99.com/sql.html) and cannot execute as commands.

🤖 Can AI tools generate INSERT INTO statements from sample data?

Yes. AI assistants turn spreadsheets or plain descriptions into INSERT statements, and [MySQL Workbench](https://www.guru99.com/introduction-to-mysql-workbench.html) can run them. Check column order and quoting, because a model may guess the schema.

🧪 Is AI-generated test data safe to insert into a database?

Insert it into a development copy first. AI-generated rows may break foreign keys, exceed column lengths, or invent invalid dates. Run the batch in a transaction so it can be rolled back.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/mysql-insert-into-query.png","url":"https://www.guru99.com/images/mysql-insert-into-query.png","width":"700","height":"250","caption":"MySQL INSERT INTO Query","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/insert-into.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/sql","name":"SQL"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/insert-into.html","name":"MySQL INSERT INTO Query: How to add Row in Table (Example)"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/insert-into.html#webpage","url":"https://www.guru99.com/insert-into.html","name":"MySQL INSERT INTO Query: How to add Row in Table (Example)","dateModified":"2026-10-07T16:13:39+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/mysql-insert-into-query.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/insert-into.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"SQL","headline":"MySQL INSERT INTO Query: How to add Row in Table (Example)","description":"Insert INTO command is an important function in Database. It is used to insert new row in a table.The INSERT command can also be used to insert data from one table into another.","keywords":"sql","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-07T16:13:39+05:30","image":{"@id":"https://www.guru99.com/images/mysql-insert-into-query.png"},"copyrightYear":"2026","name":"MySQL INSERT INTO Query: How to add Row in Table (Example)","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What happens if an INSERT INTO query duplicates a primary key?","acceptedAnswer":{"@type":"Answer","text":"MySQL rejects it with a duplicate entry error. Use INSERT IGNORE to skip the row silently, or INSERT ... ON DUPLICATE KEY UPDATE to update the existing row instead."}},{"@type":"Question","name":"How do I get the ID of the row I just inserted?","acceptedAnswer":{"@type":"Answer","text":"Run SELECT LAST_INSERT_ID() straight after the insert, or call mysqli_insert_id($conn) in PHP. It returns the AUTO_INCREMENT key generated for the latest row on your own connection."}},{"@type":"Question","name":"How do I stop SQL injection in an INSERT statement?","acceptedAnswer":{"@type":"Answer","text":"Never concatenate user input into the query string. Use prepared statements with bound parameters, so values travel separately from the SQL and cannot execute as commands."}},{"@type":"Question","name":"Can AI tools generate INSERT INTO statements from sample data?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI assistants turn spreadsheets or plain descriptions into INSERT statements, and MySQL Workbench can run them. Check column order and quoting, because a model may guess the schema."}},{"@type":"Question","name":"Is AI-generated test data safe to insert into a database?","acceptedAnswer":{"@type":"Answer","text":"Insert it into a development copy first. AI-generated rows may break foreign keys, exceed column lengths, or invent invalid dates. Run the batch in a transaction so it can be rolled back."}}]}],"@id":"https://www.guru99.com/insert-into.html#schema-1143775","isPartOf":{"@id":"https://www.guru99.com/insert-into.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/insert-into.html#webpage"}}]}
```
