{"id":88,"date":"2018-04-18T12:00:38","date_gmt":"2018-04-18T12:00:38","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/04\/18\/create-upgrade-database-in-magento-2\/"},"modified":"2025-03-17T06:42:06","modified_gmt":"2025-03-17T06:42:06","slug":"create-upgrade-database-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-upgrade-database-in-magento-2\/","title":{"rendered":"How to Create and Upgrade Database in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Managing database is essential for any platform and Magento 2 E-commerce store is no exception! One may want to add a new database or update the existing ones. Add new rows or columns or delete particular data from the database and manage the database easily in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have come up with a process to<em><strong>&nbsp;create and upgrade&nbsp;<\/strong><\/em><em><strong>database<\/strong><\/em><em><strong>&nbsp;in Magento 2&nbsp;<\/strong><\/em>the easy way. Hope it is helpful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to create a database and upgrade in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2 provides some classes in the directory&nbsp;<strong>app\/code\/[Namespace]\/[ModuleName]\/Setup<\/strong><strong>&nbsp;<\/strong>to create or upgrade database.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>InstallSchema \u2013 setup database structure<\/li>\n\n\n\n<li>InstallData \u2013 initial the data for database table.<\/li>\n\n\n\n<li>UpgradeSchema \u2013 upgraded database structure<\/li>\n\n\n\n<li>UpgradeData \u2013 upgraded (add\/remove) data from table.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Functions of these classes will be run when we install or upgrade module by this command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento setup:upgrade<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Elaborating each step,<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Install Schema Create file:&nbsp;<strong>app\/code\/Meetanshi\/HelloWorld\/Setup\/InstallSchema.php<\/strong><\/p>\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 Magestore\\DataExample\\Setup;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\InstallSchemaInterface;\nclass InstallSchema implements InstallSchemaInterface\n{\n    public function install(SchemaSetupInterface $setup,ModuleContextInterface $context)\n    {\n        $installer = $setup;\n        $installer->startSetup();\n        $table = $installer->getConnection()->newTable($installer->getTable('data_example'))\n            ->addColumn('example_id',\\Magento\\Framework\\Db\\Ddl\\Table::TYPE_INTEGER,null,\n                ['identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true],\n                'Example Id')\n            ->addColumn('title',\\Magento\\Framework\\Db\\Ddl\\Table::TYPE_TEXT,255,\n                ['nullable' => false],\n                'Example Title')\n            ->addColumn('content',\\Magento\\Framework\\Db\\Ddl\\Table::TYPE_TEXT,'2M',\n                [],\n                'Example Content')\n            ->addColumn('created_at',\\Magento\\Framework\\Db\\Ddl\\Table::TYPE_TIMESTAMP,null,\n                ['nullable'=>false,'default'=>\\Magento\\Framework\\Db\\Ddl\\Table::TIMESTAMP_INIT],\n                'Created At');\n        $installer->getConnection()->createTable($table);\n        $installer->endSetup();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this file, we created a table with name \u201cdata_example\u201d&nbsp;with four columns:&nbsp;<strong><em>example_id, title, content, created_at<\/em><\/strong>&nbsp;with data types are&nbsp;<em>integer, varchar, text, timestamp<\/em><br>You can review all data types in this file:&nbsp;<strong><strong>\\Magento\\Framework\\Db\\Ddl\\Table.php<\/strong><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. Install Data File:&nbsp;<strong>app\/code\/Meetanshi\/HelloWorld\/Setup\/InstallData.php<\/strong><\/p>\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 Meetanshi\\HelloWorld\\Setup;\nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nclass InstallData implements InstallDataInterface\n{\n    protected $_exampleFactory;\n    public function __construct(\\Meetanshi\\HelloWorld\\Model\\ExampleFactory $exampleFactory)\n    {\n        $this->_exampleFactory = $exampleFactory;\n    }\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $data = ['title' => \"First example title\",'content' => \"First Example content\"];\n        $example = $this->_exampleFactory->create();\n        $example->addData($data)->save();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, &nbsp;we insert a row with value for \u201ctitle\u201d and \u201ccontent\u201d columns to data_example table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. Upgrade Schema File:&nbsp;<strong>app\/code\/ Meetanshi\/HelloWorld\/Setup\/UpgradeSchema.php<\/strong><\/p>\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 Meetanshi\\HelloWorld\\Setup;\nuse Magento\\Framework\\Setup\\UpgradeSchemaInterface;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nclass UpgradeSchema implements UpgradeSchemaInterface\n{\n    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $installer = $setup;\n        $installer->startSetup();\n        if (version_compare($context->getVersion(), '1.0.1', '&lt;')) \n        {\n            $installer->getConnection()->dropColumn($installer->getTable('data_example'), 'created_at');\n        }\n        $installer->endSetup();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, note that we have to check module version in upgrade function. In this example, we upgraded module from version 1.0.0 to 1.0.1. Drop created_at column from the data_example table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. Upgrade Data File:&nbsp;<strong>app\/code\/ Meetanshi\/HelloWorld\/Setup\/UpgradeData.php<\/strong><\/p>\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 Meetanshi\\HelloWorld\\Setup;\nuse Magento\\Framework\\Setup\\UpgradeDataInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nclass UpgradeData implements UpgradeDataInterface\n{\n    protected $_exampleFactory;\n    public function __construct(\\Meetanshi\\HelloWorld\\Model\\ExampleFactory $exampleFactory)\n    {\n        $this->_exampleFactory = $exampleFactory;\n    }\n    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        if (version_compare($context->getVersion(), '1.0.1', '&lt;')) \n        {\n            $data = ['title' => \"The second example title\", 'content' => \"The second example content\"];\n            $example = $this->_exampleFactory->create();\n            $example > addData($dalogta)->save();\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Play with your database and control it easily in your Magento 2 store:) You may also love to read our blog post on&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/change-database-name-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Change Database Name in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me know if you have any doubts in the Comments below and I\u2019ll help you out to solve it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Rate my article with 5 stars if you find it useful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing database is essential for any platform and Magento 2 E-commerce store is no exception! One may want to add a new database or update&#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-88","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/88","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=88"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":9402,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/88\/revisions\/9402"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}