{"id":1335,"date":"2020-10-31T15:55:22","date_gmt":"2020-10-31T15:55:22","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/10\/31\/magento-2-create-table\/"},"modified":"2025-07-17T17:15:14","modified_gmt":"2025-07-17T11:45:14","slug":"magento-2-create-table","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/magento-2-create-table\/","title":{"rendered":"How to Create Table in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 offers a mechanism to create database tables, modify existing ones, and add data into them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can create a new database table by InstallSchema in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create table in Magento 2&nbsp;to store and retrieve the data. Not only that, but you can also perform various operations such as insert, update and delete on the table data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moreover, it can be used for checking the login credentials.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the below steps to programmatically create table in Magento 2. You can use this method when creating a custom module in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Create Table in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong><em>InstallSchema.php<\/em><\/strong>&nbsp;at&nbsp;<strong><em><strong><em>app\\code\\Meetanshi\\Extension\\Setup<\/em><\/strong><\/em><\/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\n\nnamespace Meetanshi\\Extension\\Setup;\n\nuse Magento\\Framework\\Setup\\InstallSchemaInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\nuse Magento\\Setup\\Exception;\n\nclass InstallSchema implements InstallSchemaInterface\n{\n    \/**\n     * {@inheritdoc}\n     *\n     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)\n     *\/\n    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $installer = $setup;\n\n        $installer->startSetup();\n        try {\n            $table = $installer->getConnection()->newTable(\n                $installer->getTable('extension')\n            )->addColumn(\n                'id',\n                \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_INTEGER,\n                null,\n                ['identity' => true, 'nullable' => false, 'primary' => true],\n                'Record Id'\n            )->addColumn(\n                'name',\n                \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TEXT,\n                255,\n                ['nullable' => false],\n                'Name'\n            )->addColumn(\n                'email',\n                \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TEXT,\n                '255',\n                ['nullable' => false],\n                'Email'\n            )->addColumn(\n                'telephone',\n                \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TEXT,\n                '255',\n                ['nullable' => false],\n                'Telephone'\n            )->addColumn(\n                'created_at',\n                \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TIMESTAMP,\n                null,\n                ['nullable' => false, 'default' => \\Magento\\Framework\\DB\\Ddl\\Table::TIMESTAMP_INIT],\n                'Created At'\n            )->addColumn(\n                'update_time',\n                \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TIMESTAMP,\n                null,\n                ['nullable' => false, 'default' => \\Magento\\Framework\\DB\\Ddl\\Table::TIMESTAMP_INIT_UPDATE],\n                'Updated At'\n            )->setComment(\n                'Data Table'\n            );\n\n            $installer->getConnection()->createTable($table);\n            $installer->endSetup();\n        } catch (Exception $err) {\n            \\Magento\\Framework\\App\\ObjectManager::getInstance()->get('Psr\\Log\\LoggerInterface')->info($err->getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Once the extension is created, all the commands are being run. We are creating InstallSchema.php file after executing the extension. Therefore, we have to delete the existing record.To do that, go to table&nbsp;<em><strong>setup_module&nbsp;<\/strong><\/em>and search&nbsp;<strong><em>Meetanshi_Extension&nbsp;<\/em><\/strong>in the column module and delete the record.Now, run the commands given below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento setup:upgrade\nphp bin\/magento setup:static-content:deploy -f\nphp bin\/magento cache:flush<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After running these commands, the table \u2018extension\u2019 will appear in the database.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-Create-Table-for-Magento-Extension-Development-1-min-1024x274.jpg\" alt=\"How to Create Table for Magento Extension Development\" class=\"wp-image-11203\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/add-new-column-in-magento-2-quote-table-order-table\/\" data-type=\"link\" data-id=\"https:\/\/meetanshi.com\/blog\/add-new-column-in-magento-2-quote-table-order-table\/\">How to Add New Column in Magento 2 Quote Table &amp; Order Table?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/create-custom-form-in-magento-2\/\">How to Create Custom Form in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/create-model-file-to-perform-crud-operation-in-magento-2\/\">How to Create Model File to Perform CRUD Operation in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/add-column-to-existing-database-table-in-magento-2\/\" data-type=\"link\" data-id=\"https:\/\/meetanshi.com\/blog\/add-column-to-existing-database-table-in-magento-2\/\">How To Add A Column To Existing Database Table In Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 offers a mechanism to create database tables, modify existing ones, and add data into them. You can create a new database table by&#8230;<\/p>\n","protected":false},"author":13,"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-1335","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1335","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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1335"}],"version-history":[{"count":8,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1335\/revisions"}],"predecessor-version":[{"id":17841,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1335\/revisions\/17841"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}