{"id":900,"date":"2020-03-24T14:04:46","date_gmt":"2020-03-24T14:04:46","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/24\/add-column-to-existing-database-table-in-magento-2\/"},"modified":"2025-06-16T13:08:51","modified_gmt":"2025-06-16T07:38:51","slug":"add-column-to-existing-database-table-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-column-to-existing-database-table-in-magento-2\/","title":{"rendered":"How To Add A Column To Existing Database Table In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 allows a developer to create, upgrade, or delete a table in the database. Earlier I talked about how to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-upgrade-database-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">create and upgrade database in Magento 2<\/a>. However, there were queries regarding the programmatic method to&nbsp;<em><strong>add a column to existing database table in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the below code for the same. For example, adding an order delivery date column to the <a href=\"https:\/\/meetanshi.com\/blog\/add-column-to-sales-order-history-page-magento-2\/\" data-type=\"link\" data-id=\"https:\/\/meetanshi.com\/blog\/add-column-to-sales-order-history-page-magento-2\/\">order table in the database<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While development tasks, whenever you need to add a custom column in the existing database table, bookmark this post for reference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Add A Column To Existing Database Table In Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Setup;\nuse Magento\\Framework\\Setup\\UpgradeSchemaInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\nuse Magento\\Framework\\DB\\Ddl\\Table;\n \nclass UpgradeSchema implements UpgradeSchemaInterface\n{\n    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)\n    {\n        if (version_compare($context->getVersion(), '1.0.1') &lt; 0) {\n            $connection = $setup->getConnection();\n            $connection->addColumn(\n                $setup->getTable('sales_order'),\n                'delivery_date',\n                [\n                    'type' => Table::TYPE_TEXT,\n                    'length' => 255,\n                    'nullable' => true,\n                    'default' => '',\n                    'comment' => 'Add New Filed'\n                ]\n            );\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, you can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/update-column-datatype-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">change column datatype in a table in Magento 2<\/a>&nbsp;using the&nbsp;<code>changeColumn<\/code>&nbsp;function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution via social media among the Magento community.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 allows a developer to create, upgrade, or delete a table in the database. Earlier I talked about how to&nbsp;create and upgrade database in&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-900","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=900"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":16882,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/900\/revisions\/16882"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}