{"id":13758,"date":"2023-12-29T01:00:17","date_gmt":"2023-12-29T08:00:17","guid":{"rendered":"https:\/\/www.mysqltutorial.org\/?page_id=13758"},"modified":"2023-12-29T01:00:18","modified_gmt":"2023-12-29T08:00:18","slug":"mysql-insert","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/","title":{"rendered":"MySQL INSERT"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>INSERT<\/code> statement to insert one or more rows into a table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the MySQL INSERT statement<\/h2>\n\n\n\n<p>The <code>INSERT<\/code> statement allows you to insert one or more rows into a table.&nbsp;The following illustrates the syntax of the <code>INSERT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">INSERT INTO table_name(column1, column2,...) \nVALUES (value1, value2,...);<\/code><\/span><\/pre>\n\n\n<p>In this syntax,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the table name and a list of comma-separated columns inside parentheses after the <code>INSERT INTO<\/code> clause.<\/li>\n\n\n\n<li>Then, put a comma-separated list of values of the corresponding columns inside the parentheses following the <code>VALUES<\/code> keyword.<\/li>\n<\/ul>\n\n\n\n<p>When using the <code>INSERT<\/code> statement, you need to ensure that the number of columns matches the number of values. <\/p>\n\n\n\n<p>Additionally, you need to specify that the positions of columns correspond precisely to the positions of their corresponding values.<\/p>\n\n\n\n<p>To <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">insert multiple rows<\/a> into a table using a single <code>INSERT<\/code> statement, you use the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> <span class=\"hljs-keyword\">table<\/span>(column1, column2,...) \n<span class=\"hljs-keyword\">VALUES<\/span> \n  (value1, value2,...), \n  (value1, value2,...), \n  ...\n  (value1, value2,...);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, rows are separated by commas in the <code>VALUES<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL INSERT statement examples<\/h2>\n\n\n\n<p>Let&#8217;s <a title=\"MySQL create new table\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a new table<\/a> called <code>tasks<\/code> for practicing the <code>INSERT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> tasks;\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> tasks (\n  task_id <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>, \n  title <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>, \n  start_date <span class=\"hljs-built_in\">DATE<\/span>, \n  due_date <span class=\"hljs-built_in\">DATE<\/span>, \n  <span class=\"hljs-keyword\">priority<\/span> <span class=\"hljs-built_in\">TINYINT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">3<\/span>, \n  description <span class=\"hljs-built_in\">TEXT<\/span>\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">1) Basic MySQL INSERT statement example<\/h3>\n\n\n\n<p>The following statement inserts a new row into the <code>tasks<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> tasks(title, <span class=\"hljs-keyword\">priority<\/span>) \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Learn MySQL INSERT Statement'<\/span>, <span class=\"hljs-number\">1<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">1 row(s) affected<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output indicates that the statement inserted one row into the <code>tasks<\/code> table successfully.<\/p>\n\n\n\n<p>In this example, we specified values for the <code>title<\/code> and <code>priority<\/code> columns only. For other columns, MySQL uses the default values.<\/p>\n\n\n\n<p>The <code>task_id<\/code> column is an <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-auto_increment\/\">AUTO_INCREMENT<\/a><\/code>\u00a0column, meaning that MySQL generates a sequential integer whenever a row is inserted into the table.<\/p>\n\n\n\n<p>The <code>start_date<\/code>, <code>due_date<\/code>, and <code>description<\/code> columns use <code>NULL<\/code> as the default value. Therefore, MySQL insert <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-null\/\">NULL<\/a><\/code> into these columns if you don&#8217;t specify their values in the <code>INSERT<\/code> statement.<\/p>\n\n\n\n<p>The following retrieves data from the <code>tasks<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> tasks;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+---------+------------------------------+------------+----------+----------+-------------+\n| task_id | title                        | start_date | due_date | priority | description |\n+---------+------------------------------+------------+----------+----------+-------------+\n|       <span class=\"hljs-number\">1<\/span> | Learn MySQL INSERT Statement | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>     |        <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n+---------+------------------------------+------------+----------+----------+-------------+\n<span class=\"hljs-number\">1<\/span> row in set (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">2) Inserting rows using default value example<\/h3>\n\n\n\n<p>If you want to insert a default value into a column, you have two ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ignore both the column name and value in the <code>INSERT<\/code> statement.<\/li>\n\n\n\n<li>Specify the column name in the <code>INSERT INTO<\/code> clause and use the <code>DEFAULT<\/code> keyword in the <code>VALUES<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>The following example demonstrates the second way:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> tasks(title, <span class=\"hljs-keyword\">priority<\/span>) \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Understanding DEFAULT keyword'<\/span>, <span class=\"hljs-keyword\">DEFAULT<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we specified the value for <code>title<\/code> and <code>priority<\/code> columns.<\/p>\n\n\n\n<p>Because the default value for the column <code>priority<\/code> is 3 as declared in the table definition, the statement inserts number 3 into the <code>priority<\/code> column.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">priority TINYINT NOT NULL DEFAULT 3<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement returns the contents of the <code>tasks<\/code> table after the insert:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> tasks;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+---------+-------------------------------+------------+----------+----------+-------------+\n| task_id | title                         | start_date | due_date | priority | description |\n+---------+-------------------------------+------------+----------+----------+-------------+\n|       <span class=\"hljs-number\">1<\/span> | Learn MySQL INSERT Statement  | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>     |        <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">2<\/span> | Understanding <span class=\"hljs-keyword\">DEFAULT<\/span> keyword | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>     |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n+---------+-------------------------------+------------+----------+----------+-------------+\n<span class=\"hljs-number\">2<\/span> rows in set (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3) Inserting dates into the table example<\/h3>\n\n\n\n<p>To insert a literal date value into a column, you use the following format:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">'YYYY-MM-DD'<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this format:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>YYYY<\/code> represents a four-digit year e.g., 2018.<\/li>\n\n\n\n<li><code>MM<\/code> represents a two-digit month e.g., 01, 02, and 12.<\/li>\n\n\n\n<li><code>DD<\/code> represents a two-digit day e.g., 01, 02, 30.<\/li>\n<\/ul>\n\n\n\n<p>The following statement inserts a new row to the <code>tasks<\/code> table with the start and due date values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> tasks(title, start_date, due_date) \n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Insert date into table'<\/span>, <span class=\"hljs-string\">'2018-01-09'<\/span>, <span class=\"hljs-string\">'2018-09-15'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+---------+-------------------------------+------------+------------+----------+-------------+\n| task_id | title                         | start_date | due_date   | priority | description |\n+---------+-------------------------------+------------+------------+----------+-------------+\n|       <span class=\"hljs-number\">1<\/span> | Learn MySQL INSERT Statement  | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>       |        <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">2<\/span> | Understanding <span class=\"hljs-keyword\">DEFAULT<\/span> keyword | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>       |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">3<\/span> | Insert date into table        | <span class=\"hljs-number\">2018<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-09<\/span> | <span class=\"hljs-number\">2018<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-15<\/span> |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n+---------+-------------------------------+------------+------------+----------+-------------+\n<span class=\"hljs-number\">3<\/span> rows in set (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It is possible to use expressions in the <code>VALUES<\/code> clause. For example, the following statement adds a new task using the current date for the start date and due date columns:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> tasks(title, start_date, due_date) \n<span class=\"hljs-keyword\">VALUES<\/span> \n  (\n    <span class=\"hljs-string\">'Use current date for the task'<\/span>, \n    <span class=\"hljs-keyword\">CURRENT_DATE<\/span>(), \n    <span class=\"hljs-keyword\">CURRENT_DATE<\/span>()\n  );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we used the <code>CURRENT_DATE()<\/code> function as the values for the <code>start_date<\/code> and <code>due_date<\/code> columns. Note that the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-curdate\/\">CURRENT_DATE()<\/a><\/code> function is a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/\">date function<\/a> that returns the current system date.<\/p>\n\n\n\n<p>Here are the contents of the <code>tasks<\/code> table after insert:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+---------+-------------------------------+------------+------------+----------+-------------+\n| task_id | title                         | start_date | due_date   | priority | description |\n+---------+-------------------------------+------------+------------+----------+-------------+\n|       <span class=\"hljs-number\">1<\/span> | Learn MySQL INSERT Statement  | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>       |        <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">2<\/span> | Understanding <span class=\"hljs-keyword\">DEFAULT<\/span> keyword | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>       |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">3<\/span> | Insert date into table        | <span class=\"hljs-number\">2018<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-09<\/span> | <span class=\"hljs-number\">2018<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-15<\/span> |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">4<\/span> | <span class=\"hljs-keyword\">Use<\/span> <span class=\"hljs-title\">current<\/span> <span class=\"hljs-title\">date<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">task<\/span> | 2023-12-28 | 2023-12-28 |        3 | <span class=\"hljs-title\">NULL<\/span>        |\n+---------+-------------------------------+------------+------------+----------+-------------+\n4 <span class=\"hljs-title\">rows<\/span> <span class=\"hljs-title\">in<\/span> <span class=\"hljs-title\">set<\/span> (0.00 <span class=\"hljs-title\">sec<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">4) Inserting multiple rows example<\/h3>\n\n\n\n<p>The following statement inserts three rows into the <code>tasks<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> tasks(title, <span class=\"hljs-keyword\">priority<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>\n\t(<span class=\"hljs-string\">'My first task'<\/span>, <span class=\"hljs-number\">1<\/span>),\n\t(<span class=\"hljs-string\">'It is the second task'<\/span>,<span class=\"hljs-number\">2<\/span>),\n\t(<span class=\"hljs-string\">'This is the third task of the week'<\/span>,<span class=\"hljs-number\">3<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, each row of data is specified as a list of values in the <code>VALUES<\/code> clause.<\/p>\n\n\n\n<p>MySQL returns the following message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">3 row(s) affected Records: 3  Duplicates: 0  Warnings: 0<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It means that the three rows have been inserted successfully with no duplicates or warnings.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> tasks;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The table <code>tasks<\/code> has the following data:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+---------+------------------------------------+------------+------------+----------+-------------+\n| task_id | title                              | start_date | due_date   | priority | description |\n+---------+------------------------------------+------------+------------+----------+-------------+\n|       <span class=\"hljs-number\">1<\/span> | Learn MySQL INSERT Statement       | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>       |        <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">2<\/span> | Understanding <span class=\"hljs-keyword\">DEFAULT<\/span> keyword      | <span class=\"hljs-keyword\">NULL<\/span>       | <span class=\"hljs-keyword\">NULL<\/span>       |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">3<\/span> | Insert date into table             | <span class=\"hljs-number\">2018<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-09<\/span> | <span class=\"hljs-number\">2018<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-15<\/span> |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-keyword\">NULL<\/span>        |\n|       <span class=\"hljs-number\">4<\/span> | <span class=\"hljs-keyword\">Use<\/span> <span class=\"hljs-title\">current<\/span> <span class=\"hljs-title\">date<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">task<\/span>      | 2023-12-28 | 2023-12-28 |        3 | <span class=\"hljs-title\">NULL<\/span>        |\n|       5 | <span class=\"hljs-title\">My<\/span> <span class=\"hljs-title\">first<\/span> <span class=\"hljs-title\">task<\/span>                      | <span class=\"hljs-title\">NULL<\/span>       | <span class=\"hljs-title\">NULL<\/span>       |        1 | <span class=\"hljs-title\">NULL<\/span>        |\n|       6 | <span class=\"hljs-title\">It<\/span> <span class=\"hljs-title\">is<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">second<\/span> <span class=\"hljs-title\">task<\/span>              | <span class=\"hljs-title\">NULL<\/span>       | <span class=\"hljs-title\">NULL<\/span>       |        2 | <span class=\"hljs-title\">NULL<\/span>        |\n|       7 | <span class=\"hljs-title\">This<\/span> <span class=\"hljs-title\">is<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">third<\/span> <span class=\"hljs-title\">task<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">week<\/span> | <span class=\"hljs-title\">NULL<\/span>       | <span class=\"hljs-title\">NULL<\/span>       |        3 | <span class=\"hljs-title\">NULL<\/span>        |\n+---------+------------------------------------+------------+------------+----------+-------------+\n7 <span class=\"hljs-title\">rows<\/span> <span class=\"hljs-title\">in<\/span> <span class=\"hljs-title\">set<\/span> (0.00 <span class=\"hljs-title\">sec<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">5) Dealing with auto-increment column<\/h3>\n\n\n\n<p>Suppose you have a table that has an auto-increment column:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">CREATE TABLE t(\n   id INT AUTO_INCREMENT PRIMARY KEY\n);<\/code><\/span><\/pre>\n\n\n<p>The following statement insert a new row into the <code>t<\/code> table, which uses the generated value:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">INSERT INTO t VALUES();<\/code><\/span><\/pre>\n\n\n<p>In this statement, we don&#8217;t specify any column after the table name and any values inside the VALUES() clause.<\/p>\n\n\n\n<p>Here&#8217;s the contents of the t table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT * FROM t;<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+\n| id |\n+----+\n|  <span class=\"hljs-number\">1<\/span> |\n+----+\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>INSERT<\/code> statement to insert one or more rows into a table.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"13758\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\"\n\t\t\t\tdata-post-title=\"MySQL INSERT\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"13758\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\"\n\t\t\t\tdata-post-title=\"MySQL INSERT\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the MySQL INSERT statement to insert one or more rows into a table. Introduction to the MySQL INSERT statement The INSERT statement allows you to insert one or more rows into a table.&nbsp;The following illustrates the syntax of the INSERT statement: In this syntax, When [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":52,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-13758","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL INSERT<\/title>\n<meta name=\"description\" content=\"This tutorial shows you step by step how to use various forms of the MySQL INSERT statement to insert one or more rows into a table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL INSERT\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you step by step how to use various forms of the MySQL INSERT statement to insert one or more rows into a table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T08:00:18+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-insert\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-insert\\\/\",\"name\":\"MySQL INSERT\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2023-12-29T08:00:17+00:00\",\"dateModified\":\"2023-12-29T08:00:18+00:00\",\"description\":\"This tutorial shows you step by step how to use various forms of the MySQL INSERT statement to insert one or more rows into a table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-insert\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-insert\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-insert\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL INSERT\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL INSERT","description":"This tutorial shows you step by step how to use various forms of the MySQL INSERT statement to insert one or more rows into a table.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/","og_locale":"en_US","og_type":"article","og_title":"MySQL INSERT","og_description":"This tutorial shows you step by step how to use various forms of the MySQL INSERT statement to insert one or more rows into a table.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T08:00:18+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/","name":"MySQL INSERT","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2023-12-29T08:00:17+00:00","dateModified":"2023-12-29T08:00:18+00:00","description":"This tutorial shows you step by step how to use various forms of the MySQL INSERT statement to insert one or more rows into a table.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL INSERT"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=13758"}],"version-history":[{"count":2,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13758\/revisions"}],"predecessor-version":[{"id":13760,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13758\/revisions\/13760"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=13758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}