{"id":3930,"date":"2014-10-31T00:58:41","date_gmt":"2014-10-31T08:58:41","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=3930"},"modified":"2024-01-12T02:57:47","modified_gmt":"2024-01-12T09:57:47","slug":"mysql-add-column","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/","title":{"rendered":"How to Add Columns to a Table Using MySQL ADD COLUMN Statement"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to add a column to a table using MySQL <code>ADD COLUMN<\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL ADD COLUMN statement<\/h2>\n\n\n\n<p>To add a new column\u00a0to\u00a0an existing table, you use the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-alter-table\/\">ALTER TABLE<\/a> &#8230; <code>ADD COLUMN<\/code> statement as follows:<\/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\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> new_column_name data_type \n&#91;<span class=\"hljs-keyword\">FIRST<\/span> | <span class=\"hljs-keyword\">AFTER<\/span> existing_column];<\/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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, provide the table name to which you want to add a new column after the <code>ALTER TABLE<\/code> clause.<\/li>\n\n\n\n<li>Second, define the new column and its attributes after the <code>ADD COLUMN<\/code> clause. Note that <code>COLUMN<\/code> keyword is optional so you can omit it.<\/li>\n\n\n\n<li>Third, specify the position of the new column in the table.<\/li>\n<\/ul>\n\n\n\n<p>When adding a new column to a table, you can specify its position within the table. You can use the keyword <code>FIRST<\/code> if you want the new column to be positioned as the first column in the table. <\/p>\n\n\n\n<p>Alternatively, you can use the <code>AFTER existing_column<\/code> clause to specify that you want to add a new column after an existing column.<\/p>\n\n\n\n<p>If you do not explicitly specify the position of the new column, the statement will automatically add it as the last column in the table.<\/p>\n\n\n\n<p>To add two or more columns to a table at the same time, you use multiple <code>ADD COLUMN<\/code> clauses like this:<\/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\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> &#91;<span class=\"hljs-keyword\">COLUMN<\/span>] new_column_name data_type &#91;<span class=\"hljs-keyword\">FIRST<\/span>|<span class=\"hljs-keyword\">AFTER<\/span> existing_column],\n<span class=\"hljs-keyword\">ADD<\/span> &#91;<span class=\"hljs-keyword\">COLUMN<\/span>] new_column_name data_type &#91;<span class=\"hljs-keyword\">FIRST<\/span>|<span class=\"hljs-keyword\">AFTER<\/span> existing_column],\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<h2 class=\"wp-block-heading\">MySQL ADD COLUMN examples<\/h2>\n\n\n\n<p>Let&#8217;s look at examples of adding one or more columns to a table.<\/p>\n\n\n\n<p>We&#8217;ll  <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> called <code>vendors<\/code> with two columns <code>id<\/code> and <code>name<\/code>:<\/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\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> vendors (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,l\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>)\n);<\/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<h3 class=\"wp-block-heading\">1) Adding one column example<\/h3>\n\n\n\n<p>First, add a new column <code>phone<\/code> to the <code>vendors<\/code> table:<\/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\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> vendors\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> phone <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">15<\/span>) <span class=\"hljs-keyword\">AFTER<\/span> <span class=\"hljs-keyword\">name<\/span>;<\/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>Because we specify the position of the <code>phone<\/code> column explicitly after the <code>name<\/code> column, the statement places the <code>phone<\/code> column after the <code>name<\/code> column.<\/p>\n\n\n\n<p>Second, view the columns list of the vendor table using the <code>DESC<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">DESC vendors;<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+-------+--------------+------+-----+---------+----------------+\n| Field | Type         | <span class=\"hljs-keyword\">Null<\/span> | Key | <span class=\"hljs-keyword\">Default<\/span> | Extra          |\n+-------+--------------+------+-----+---------+----------------+\n| id    | int          | NO   | PRI | <span class=\"hljs-keyword\">NULL<\/span>    | auto_increment |\n| name  | varchar(<span class=\"hljs-number\">255<\/span>) | YES  |     | <span class=\"hljs-keyword\">NULL<\/span>    |                |\n| phone | varchar(<span class=\"hljs-number\">15<\/span>)  | YES  |     | <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-5\"><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) Adding a column as the last column<\/h3>\n\n\n\n<p>First, add a new column <code>vendor_group<\/code> to the <code>vendors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> vendors\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> vendor_group <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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 statement, we don&#8217;t specify the new column&#8217;s&nbsp;position so it adds the&nbsp;<code>vendor_group<\/code> column as the last column of the <code>vendors<\/code> table.<\/p>\n\n\n\n<p>Second, view the <code>vendors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">DESC vendors;<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+--------------+--------------+------+-----+---------+----------------+\n| Field        | Type         | <span class=\"hljs-keyword\">Null<\/span> | Key | <span class=\"hljs-keyword\">Default<\/span> | Extra          |\n+--------------+--------------+------+-----+---------+----------------+\n| id           | int          | NO   | PRI | <span class=\"hljs-keyword\">NULL<\/span>    | auto_increment |\n| name         | varchar(<span class=\"hljs-number\">255<\/span>) | YES  |     | <span class=\"hljs-keyword\">NULL<\/span>    |                |\n| phone        | varchar(<span class=\"hljs-number\">15<\/span>)  | YES  |     | <span class=\"hljs-keyword\">NULL<\/span>    |                |\n| vendor_group | int          | NO   |     | <span class=\"hljs-keyword\">NULL<\/span>    |                |\n+--------------+--------------+------+-----+---------+----------------+\n<span class=\"hljs-number\">4<\/span> rows in set (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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) Adding two columns example<\/h3>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert some rows<\/a> into the <code>vendors<\/code> table.<\/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\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> vendors(<span class=\"hljs-keyword\">name<\/span>,phone,vendor_group)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'IBM'<\/span>,<span class=\"hljs-string\">'(408)-298-2987'<\/span>,<span class=\"hljs-number\">1<\/span>),\n      (<span class=\"hljs-string\">'Microsoft'<\/span>,<span class=\"hljs-string\">'(408)-298-2988'<\/span>,<span class=\"hljs-number\">1<\/span>);<\/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>Second, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query the data<\/a> of the <code>vendors<\/code> table:<\/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> \n  <span class=\"hljs-keyword\">id<\/span>, \n  <span class=\"hljs-keyword\">name<\/span>, \n  phone, \n  vendor_group \n<span class=\"hljs-keyword\">FROM<\/span> \n  vendors;<\/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=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+-----------+----------------+--------------+\n| id | name      | phone          | vendor_group |\n+----+-----------+----------------+--------------+\n|  <span class=\"hljs-number\">1<\/span> | IBM       | (<span class=\"hljs-number\">408<\/span>)<span class=\"hljs-number\">-298<\/span><span class=\"hljs-number\">-2987<\/span> |            <span class=\"hljs-number\">1<\/span> |\n|  <span class=\"hljs-number\">2<\/span> | Microsoft | (<span class=\"hljs-number\">408<\/span>)<span class=\"hljs-number\">-298<\/span><span class=\"hljs-number\">-2988<\/span> |            <span class=\"hljs-number\">1<\/span> |\n+----+-----------+----------------+--------------+\n<span class=\"hljs-number\">2<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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<p>Third, add two more columns <code>email<\/code> and <code>hourly_rate<\/code> to the <code>vendors<\/code> table using two <code>ADD<\/code> clauses:<\/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\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> vendors\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> email <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> hourly_rate <span class=\"hljs-built_in\">decimal<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>;<\/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>Note that both <code>email<\/code> and <code>hourly_rate<\/code> columns are <code>NOT NULL<\/code>. However, the <code>vendors<\/code> table already has data. In this case, MySQL will use default values for these new columns.<\/p>\n\n\n\n<p>Finally, retrieve data from the <code>vendors<\/code> table:<\/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\">SELECT<\/span> \n  <span class=\"hljs-keyword\">id<\/span>, \n  <span class=\"hljs-keyword\">name<\/span>, \n  phone, \n  vendor_group, \n  email, \n  hourly_rate \n<span class=\"hljs-keyword\">FROM<\/span> \n  vendors;<\/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=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+-----------+----------------+--------------+-------+-------------+\n| id | name      | phone          | vendor_group | email | hourly_rate |\n+----+-----------+----------------+--------------+-------+-------------+\n|  <span class=\"hljs-number\">1<\/span> | IBM       | (<span class=\"hljs-number\">408<\/span>)<span class=\"hljs-number\">-298<\/span><span class=\"hljs-number\">-2987<\/span> |            <span class=\"hljs-number\">1<\/span> |       |        <span class=\"hljs-number\">0.00<\/span> |\n|  <span class=\"hljs-number\">2<\/span> | Microsoft | (<span class=\"hljs-number\">408<\/span>)<span class=\"hljs-number\">-298<\/span><span class=\"hljs-number\">-2988<\/span> |            <span class=\"hljs-number\">1<\/span> |       |        <span class=\"hljs-number\">0.00<\/span> |\n+----+-----------+----------------+--------------+-------+-------------+\n<span class=\"hljs-number\">2<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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<p>The output indicates that the <code>email<\/code> column is populated with blank, not the <code>NULL<\/code> values. And the <code>hourly_rate<\/code> column is filled with <code>0.00<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4) Adding a column that already exists<\/h3>\n\n\n\n<p>If you add a column that already exists in the table, MySQL will issue an error. For example, if you execute the following statement:<\/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\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> vendors\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> vendor_group <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>;<\/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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Error Code: 1060. Duplicate column name 'vendor_group'<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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>For the table with a few columns, it is easy to see which columns are already there. However, it becomes more difficult for a big table with hundreds of columns.<\/p>\n\n\n\n<p>In this case, you want to check whether a column exists in a table before adding it.<\/p>\n\n\n\n<p>However, there is no statement like <code>ADD COLUMN IF NOT EXISTS<\/code> available. Fortunately, you can get this information from the <code>columns<\/code> table of the <code>information_schema<\/code> database as the following query:<\/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\">SELECT<\/span> \n    <span class=\"hljs-keyword\">IF<\/span>(<span class=\"hljs-keyword\">count<\/span>(*) = <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'Exist'<\/span>,<span class=\"hljs-string\">'Not Exist'<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">result<\/span>\n<span class=\"hljs-keyword\">FROM<\/span>\n    information_schema.columns\n<span class=\"hljs-keyword\">WHERE<\/span>\n    table_schema = <span class=\"hljs-string\">'classicmodels'<\/span>\n        <span class=\"hljs-keyword\">AND<\/span> table_name = <span class=\"hljs-string\">'vendors'<\/span>\n        <span class=\"hljs-keyword\">AND<\/span> column_name = <span class=\"hljs-string\">'phone'<\/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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+-----------+\n| result    |\n+-----------+\n| Not Exist |\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-17\"><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<p>In the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-where\/\">WHERE clause<\/a>, we passed three arguments: table schema or database, table name, and column name. We used the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-if-function\/\">IF function<\/a> to check whether the column exists or not.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5) Adding an auto-increment column<\/h3>\n\n\n\n<p>MySQL allows a table to have up to one <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-auto_increment\/\">auto-increment<\/a> column and that column must be defined as a key. For example:<\/p>\n\n\n\n<p>First, create a new table called <code>contacts<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">CREATE TABLE contacts(\n   name VARCHAR(<span class=\"hljs-number\">255<\/span>) NOT <span class=\"hljs-keyword\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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>Second, insert some rows into <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">INSERT INTO contacts(name) \nVALUES \n   (<span class=\"hljs-string\">'John'<\/span>), \n   (<span class=\"hljs-string\">'Jane'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><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<p>Third, retrieve the data from the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT * FROM contacts;<\/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\">+------+\r\n| name |\r\n+------+\r\n| John |\r\n| Jane |\r\n+------+\r\n<span class=\"hljs-number\">2<\/span> rows <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<p>Fourth, add <code>id<\/code> column as the auto-increment primary key column to the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ALTER TABLE contacts \nADD COLUMN id INT AUTO_INCREMENT \nPRIMARY KEY;<\/code><\/span><\/pre>\n\n\n<p>Fifth, show the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">DESC contacts;<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+-------+--------------+------+-----+---------+----------------+\r\n| Field | Type         | <span class=\"hljs-keyword\">Null<\/span> | Key | <span class=\"hljs-keyword\">Default<\/span> | Extra          |\r\n+-------+--------------+------+-----+---------+----------------+\r\n| name  | varchar(<span class=\"hljs-number\">255<\/span>) | NO   |     | <span class=\"hljs-keyword\">NULL<\/span>    |                |\r\n| id    | int          | NO   | PRI | <span class=\"hljs-keyword\">NULL<\/span>    | auto_increment |\r\n+-------+--------------+------+-----+---------+----------------+\r\n<span class=\"hljs-number\">2<\/span> rows in set (<span class=\"hljs-number\">0.03<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><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>Finally, retrieve the data from the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT * FROM contacts;<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+------+----+\r\n| name | id |\r\n+------+----+\r\n| John |  <span class=\"hljs-number\">1<\/span> |\r\n| Jane |  <span class=\"hljs-number\">2<\/span> |\r\n+------+----+\r\n<span class=\"hljs-number\">2<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><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<p>The output indicates that MySQL automatically generates value for the <code>id<\/code> column.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use MySQL <code>ADD COLUMN<\/code> clause in the <code>ALTER TABLE<\/code> statement to add one or more columns to 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=\"3930\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/\"\n\t\t\t\tdata-post-title=\"How to Add Columns to a Table Using MySQL ADD COLUMN Statement\"\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=\"3930\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/\"\n\t\t\t\tdata-post-title=\"How to Add Columns to a Table Using MySQL ADD COLUMN Statement\"\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>This tutorial shows you how to use MySQL ADD COLUMN statement to add one ore more columns to a table.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":40,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3930","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>How to Add Columns to a Table using MySQL ADD COLUMN<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use MySQL ADD COLUMN statement to add one or more columns to 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-add-column\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Add Columns to a Table using MySQL ADD COLUMN\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use MySQL ADD COLUMN statement to add one or more columns to a table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-12T09:57:47+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-add-column\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-add-column\\\/\",\"name\":\"How to Add Columns to a Table using MySQL ADD COLUMN\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2014-10-31T08:58:41+00:00\",\"dateModified\":\"2024-01-12T09:57:47+00:00\",\"description\":\"This tutorial shows you how to use MySQL ADD COLUMN statement to add one or more columns to a table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-add-column\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-add-column\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-add-column\\\/#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\":\"How to Add Columns to a Table Using MySQL ADD COLUMN Statement\"}]},{\"@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":"How to Add Columns to a Table using MySQL ADD COLUMN","description":"This tutorial shows you how to use MySQL ADD COLUMN statement to add one or more columns to 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-add-column\/","og_locale":"en_US","og_type":"article","og_title":"How to Add Columns to a Table using MySQL ADD COLUMN","og_description":"This tutorial shows you how to use MySQL ADD COLUMN statement to add one or more columns to a table.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-12T09:57:47+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-add-column\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/","name":"How to Add Columns to a Table using MySQL ADD COLUMN","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2014-10-31T08:58:41+00:00","dateModified":"2024-01-12T09:57:47+00:00","description":"This tutorial shows you how to use MySQL ADD COLUMN statement to add one or more columns to a table.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/#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":"How to Add Columns to a Table Using MySQL ADD COLUMN Statement"}]},{"@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\/3930","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=3930"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3930\/revisions"}],"predecessor-version":[{"id":14544,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3930\/revisions\/14544"}],"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=3930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}