{"id":1546,"date":"2020-03-14T10:09:52","date_gmt":"2020-03-14T10:09:52","guid":{"rendered":"https:\/\/www.magetop.com\/blog\/?p=1546"},"modified":"2021-07-22T02:57:35","modified_gmt":"2021-07-22T02:57:35","slug":"magento-2-how-to-create-database-table","status":"publish","type":"post","link":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/","title":{"rendered":"Magento 2: How To Create Database Table"},"content":{"rendered":"\n\n\n<p>In the previous article, we learned <strong>How To Create A Simple Custom Module<\/strong>. Following the previous tutorial, we will learn about <strong>How To Create Database Table in Magento 2<\/strong>.<\/p>\n\n\n\n<p>This is a very basic tutorial but it is very important. You will apply it a lot. Let&#8217;s start!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Module File Structure<\/h2>\n\n\n\n<p>We updated our module file structure looks as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"399\" height=\"445\" data-attachment-id=\"1547\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/magetop-helloworld-file-structure\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/magetop-helloworld-file-structure.png?fit=399%2C445&amp;ssl=1\" data-orig-size=\"399,445\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"magetop-helloworld-file-structure\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/magetop-helloworld-file-structure.png?fit=269%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/magetop-helloworld-file-structure.png?fit=399%2C445&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/magetop-helloworld-file-structure.png?resize=399%2C445&#038;ssl=1\" alt=\"magetop helloworld file struture\" class=\"wp-image-1547\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/magetop-helloworld-file-structure.png?w=399&amp;ssl=1 399w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/magetop-helloworld-file-structure.png?resize=269%2C300&amp;ssl=1 269w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Create file module.xml and registration.php<\/h2>\n\n\n\n<p>The job of creating these 2 files I have detailed instructions and explanations in the previous article. You can read it <a href=\"https:\/\/www.magetop.com\/blog\/how-to-create-new-custom-module-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"here (opens in a new tab)\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create file InstallSchema.php<\/h2>\n\n\n\n<p>You create this file according to the path: Magetop\/Helloworld\/Setup\/InstallSchema.php.<\/p>\n\n\n\n<p>We will create a table name magetop_blog with fields: id, title, description, created_at, status.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&lt;?php\n\nnamespace Magetop\\Helloworld\\Setup;\n\nuse Magento\\Framework\\DB\\Ddl\\Table;\nuse Magento\\Framework\\Setup\\InstallSchemaInterface;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\n\nclass InstallSchema implements InstallSchemaInterface\n{\n    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $installer = $setup;\n        $installer-&gt;startSetup();\n        $tableName = $installer-&gt;getTable('magetop_blog');\n        \/\/Check for the existence of the table\n        if ($installer-&gt;getConnection()-&gt;isTableExists($tableName) != true) {\n            $table = $installer-&gt;getConnection()\n                -&gt;newTable($tableName)\n                -&gt;addColumn(\n                    'id',\n                    Table::TYPE_INTEGER,\n                    null,\n                    &#x5B;\n                        'identity' =&gt; true,\n                        'unsigned' =&gt; true,\n                        'nullable' =&gt; false,\n                        'primary' =&gt; true\n                    ],\n                    'ID'\n                )\n                -&gt;addColumn(\n                    'title',\n                    Table::TYPE_TEXT,\n                    null,\n                    &#x5B;'nullable' =&gt; false, 'default' =&gt; ''],\n                    'Title'\n                )\n                -&gt;addColumn(\n                    'description',\n                    Table::TYPE_TEXT,\n                    null,\n                    &#x5B;'nullable' =&gt; false, 'default' =&gt; ''],\n                    'Description'\n                )\n                -&gt;addColumn(\n                    'created_at',\n                    Table::TYPE_DATETIME,\n                    null,\n                    &#x5B;'nullable' =&gt; false],\n                    'Created At'\n                )\n                -&gt;addColumn(\n                    'status',\n                    Table::TYPE_SMALLINT,\n                    null,\n                    &#x5B;'nullable' =&gt; false, 'default' =&gt; '0'],\n                    'Status'\n                )\n                \/\/Set comment for magetop_blog table\n                -&gt;setComment('Magetop Blog Table')\n                \/\/Set option for magetop_blog table\n                -&gt;setOption('type', 'InnoDB')\n                -&gt;setOption('charset', 'utf8');\n            $installer-&gt;getConnection()-&gt;createTable($table);\n        }\n        $installer-&gt;endSetup();\n    }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Create file InstallData.php<\/h2>\n\n\n\n<p>Next, we will insert data for the magetop_blog table.<\/p>\n\n\n\n<p>You create this file according to the path:Magetop\/Helloworld\/Setup\/InstallData.php.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&lt;?php\n\nnamespace Magetop\\Helloworld\\Setup;\n\nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n\nclass InstallData implements InstallDataInterface\n{\n\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $setup-&gt;startSetup();\n\n        $tableName = $setup-&gt;getTable('magetop_blog');\n        \/\/Check for the existence of the table\n        if ($setup-&gt;getConnection()-&gt;isTableExists($tableName) == true) {\n            $data = &#x5B;\n                &#x5B;\n                    'title' =&gt; 'How to Speed Up Magento 2 Website',\n                    'description' =&gt; 'Speeding up your Magento 2 website is very important, it affects user experience. Customers will feel satisfied when your site responds quickly',\n                    'created_at' =&gt; date('Y-m-d H:i:s'),\n                    'status' =&gt; 1,\n                ],\n                &#x5B;\n                    'title' =&gt; 'Optimize SEO for Magento Website',\n                    'description' =&gt; 'One of the important reasons why many people choose Magento 2 for their website is the ability to create SEO friendly',\n                    'created_at' =&gt; date('Y-m-d H:i:s'),\n                    'status' =&gt; 1,\n                ],\n                &#x5B;\n                    'title' =&gt; 'Top 10 eCommerce Websites',\n                    'description' =&gt; 'These are the websites of famous e-commerce corporations in the world. With very large revenue contributing to the world economy',\n                    'created_at' =&gt; date('Y-m-d H:i:s'),\n                    'status' =&gt; 0,\n                ],\n            ];\n            foreach ($data as $item) {\n                \/\/Insert data\n                $setup-&gt;getConnection()-&gt;insert($tableName, $item);\n            }\n        }\n        $setup-&gt;endSetup();\n    }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Increase the version in module.xml<\/h2>\n\n\n\n<p>If you have just created this module and you have not declared it on the database yet, you do not need to increase the version in module.xml<\/p>\n\n\n\n<p>If you have already declared this module on the database, please increase the version in module.xml<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1041\" height=\"366\" data-attachment-id=\"1549\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/increase-version\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?fit=1041%2C366&amp;ssl=1\" data-orig-size=\"1041,366\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"increase-version\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?fit=300%2C105&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?fit=800%2C281&amp;ssl=1\" src=\"https:\/\/i1.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?fit=800%2C281&amp;ssl=1\" alt=\"increase version\" class=\"wp-image-1549\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?w=1041&amp;ssl=1 1041w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?resize=300%2C105&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?resize=1024%2C360&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/increase-version.png?resize=768%2C270&amp;ssl=1 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Run the command<\/h2>\n\n\n\n<p>You need to run 2 commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento setup:upgrade<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"418\" data-attachment-id=\"1550\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/run-setup-upgrade\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?fit=978%2C511&amp;ssl=1\" data-orig-size=\"978,511\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"run-setup-upgrade\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?fit=300%2C157&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?fit=800%2C418&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?resize=800%2C418&#038;ssl=1\" alt=\"run setup upgrade\" class=\"wp-image-1550\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?w=978&amp;ssl=1 978w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?resize=300%2C157&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?resize=768%2C401&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-upgrade.png?resize=390%2C205&amp;ssl=1 390w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento setup:db-schema:upgrade<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"419\" data-attachment-id=\"1551\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/run-setup-schema-upgrade\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?fit=978%2C512&amp;ssl=1\" data-orig-size=\"978,512\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"run-setup-schema-upgrade\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?fit=300%2C157&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?fit=800%2C419&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?resize=800%2C419&#038;ssl=1\" alt=\"run setup schema upgrade\" class=\"wp-image-1551\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?w=978&amp;ssl=1 978w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?resize=300%2C157&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?resize=768%2C402&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/run-setup-schema-upgrade.png?resize=390%2C205&amp;ssl=1 390w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Check in the database<\/h2>\n\n\n\n<p>Please check on the database and You will see your accomplishment.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1182\" height=\"933\" data-attachment-id=\"1552\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/check-in-database-1\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?fit=1182%2C933&amp;ssl=1\" data-orig-size=\"1182,933\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"check-in-database-1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?fit=300%2C237&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?fit=800%2C631&amp;ssl=1\" src=\"https:\/\/i2.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?fit=800%2C631&amp;ssl=1\" alt=\"check in database 1\" class=\"wp-image-1552\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?w=1182&amp;ssl=1 1182w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?resize=300%2C237&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?resize=1024%2C808&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database-1.png?resize=768%2C606&amp;ssl=1 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1223\" height=\"737\" data-attachment-id=\"1553\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/check-in-database\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?fit=1223%2C737&amp;ssl=1\" data-orig-size=\"1223,737\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"check-in-database\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?fit=300%2C181&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?fit=800%2C482&amp;ssl=1\" src=\"https:\/\/i2.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?fit=800%2C482&amp;ssl=1\" alt=\"check in database\" class=\"wp-image-1553\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?w=1223&amp;ssl=1 1223w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?resize=300%2C181&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?resize=1024%2C617&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-in-database.png?resize=768%2C463&amp;ssl=1 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Upgrade table in the database<\/h2>\n\n\n\n<p>Next, I will guide you to add fields to the created table and create a new table in UpgradeSchema.php.<\/p>\n\n\n\n<p>You create this file according to the path: Magetop\/Helloworld\/Setup\/InstallData.php.<\/p>\n\n\n\n<p>In the following code, I will implement two field image, category_id and create a new table magetop_blog_categories.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&lt;?php\n\nnamespace Magetop\\Helloworld\\Setup;\n\nuse Magento\\Framework\\DB\\Ddl\\Table;\nuse Magento\\Framework\\Setup\\UpgradeSchemaInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\n\nclass UpgradeSchema implements UpgradeSchemaInterface\n{\n    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $setup-&gt;startSetup();\n        \/\/Add new fields to the created table\n        if (version_compare($context-&gt;getVersion(), '1.0.2') &lt; 0) {\n            $table = $setup-&gt;getTable('magetop_blog');\n            \/\/Check for the existence of the table\n            if ($setup-&gt;getConnection()-&gt;isTableExists($table) == true) {\n                \/\/ Declare data\n                $columns = &#x5B;\n                    'image' =&gt; &#x5B;\n                        'type' =&gt; Table::TYPE_TEXT,\n                        &#x5B;'nullable' =&gt; true],\n                        'comment' =&gt; 'Image',\n                    ],\n                    'category_id' =&gt; &#x5B;\n                        'type' =&gt; Table::TYPE_INTEGER,\n                        &#x5B;'nullable' =&gt; false, 'default' =&gt; 0],\n                        'comment' =&gt; 'Category ID',\n                    ],\n                ];\n                $connection = $setup-&gt;getConnection();\n                foreach ($columns as $name =&gt; $definition) {\n                    $connection-&gt;addColumn($table, $name, $definition);\n                }\n            }\n        }\n        \/\/Create a new table\n        if (version_compare($context-&gt;getVersion(), '1.0.2') &lt; 0) {\n            $categories = $setup-&gt;getTable('magetop_blog_categories');\n            \/\/Check for the existence of the table\n            if ($setup-&gt;getConnection()-&gt;isTableExists($categories) != true) {\n                $tableCategories = $setup-&gt;getConnection()\n                    -&gt;newTable($categories)\n                    -&gt;addColumn(\n                        'cat_id',\n                        Table::TYPE_INTEGER,\n                        null,\n                        &#x5B;'identity' =&gt; true, 'unsigned' =&gt; true, 'nullable' =&gt; false, 'primary' =&gt; true],\n                        'Category Id'\n                    )\n                    -&gt;addColumn(\n                        'status',\n                        Table::TYPE_SMALLINT,\n                        null,\n                        &#x5B;'nullable' =&gt; false, 'default' =&gt; 1],\n                        'Status'\n                    )\n                    -&gt;addColumn(\n                        'cat_title',\n                        Table::TYPE_TEXT,\n                        null,\n                        &#x5B;'nullable' =&gt; false, 'default' =&gt; ''],\n                        'Category Title'\n                    )\n                    -&gt;addColumn(\n                        'created_at',\n                        Table::TYPE_TIMESTAMP,\n                        null,\n                        &#x5B;'nullable' =&gt; false],\n                        'Created At'\n                    )\n                    \/\/Set comment for magetop_blog table\n                    -&gt;setComment('Magetop Blog Categories')\n                    \/\/Set option for magetop_blog table\n                    -&gt;setOption('type', 'InnoDB')\n                    -&gt;setOption('charset', 'utf8');\n                $setup-&gt;getConnection()-&gt;createTable($tableCategories);\n            }\n        }\n        $setup-&gt;endSetup();\n    }\n}\n<\/pre><\/div>\n\n\n<p>We will insert data for the magetop_blog_categories table.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&lt;?php\n\nnamespace Magetop\\Helloworld\\Setup;\n\nuse Magento\\Framework\\Setup\\UpgradeDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n\nclass UpgradeData implements UpgradeDataInterface\n{\n\n    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $setup-&gt;startSetup();\n\n        if (version_compare($context-&gt;getVersion(), '1.0.2') &lt; 0) {\n            $tableName = $setup-&gt;getTable('magetop_blog_categories');\n            \/\/Check for the existence of the table\n            if ($setup-&gt;getConnection()-&gt;isTableExists($tableName) == true) {\n                $data = &#x5B;\n                    &#x5B;\n                        'cat_title' =&gt; 'News',\n                        'status' =&gt; 1,\n                        'created_at' =&gt; date('Y-m-d H:i:s'),\n                    ],\n                    &#x5B;\n                        'cat_title' =&gt; 'Tutorials',\n                        'status' =&gt; 0,\n                        'created_at' =&gt; date('Y-m-d H:i:s'),\n                    ],\n                    &#x5B;\n                        'cat_title' =&gt; 'Uncategorized',\n                        'status' =&gt; 0,\n                        'created_at' =&gt; date('Y-m-d H:i:s'),\n                    ]\n                ];\n                foreach ($data as $item) {\n                    \/\/Insert data\n                    $setup-&gt;getConnection()-&gt;insert($tableName, $item);\n                }\n            }\n        }\n        $setup-&gt;endSetup();\n    }\n}\n<\/pre><\/div>\n\n\n<p>Do not forget to increase the version in module.xml!.<\/p>\n\n\n\n<p>Finally, run the two commands above and enjoy the result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1192\" height=\"929\" data-attachment-id=\"1554\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/check-magetop-blog-categories-table\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?fit=1192%2C929&amp;ssl=1\" data-orig-size=\"1192,929\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"check-magetop-blog-categories-table\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?fit=300%2C234&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?fit=800%2C623&amp;ssl=1\" src=\"https:\/\/i1.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?fit=800%2C623&amp;ssl=1\" alt=\"check magetop blog categories table\" class=\"wp-image-1554\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?w=1192&amp;ssl=1 1192w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?resize=300%2C234&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?resize=1024%2C798&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-table.png?resize=768%2C599&amp;ssl=1 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1226\" height=\"559\" data-attachment-id=\"1555\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/check-new-field-in-magetop-blog-table\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?fit=1226%2C559&amp;ssl=1\" data-orig-size=\"1226,559\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"check-new-field-in-magetop-blog-table\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?fit=300%2C137&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?fit=800%2C365&amp;ssl=1\" src=\"https:\/\/i1.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?fit=800%2C365&amp;ssl=1\" alt=\"check new field in magetop blog table\" class=\"wp-image-1555\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?w=1226&amp;ssl=1 1226w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?resize=300%2C137&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?resize=1024%2C467&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-new-field-in-magetop-blog-table.png?resize=768%2C350&amp;ssl=1 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1099\" height=\"735\" data-attachment-id=\"1556\" data-permalink=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/check-magetop-blog-categories-in-database\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?fit=1099%2C735&amp;ssl=1\" data-orig-size=\"1099,735\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"check-magetop-blog-categories-in-database\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?fit=300%2C201&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?fit=800%2C535&amp;ssl=1\" src=\"https:\/\/i1.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?fit=800%2C535&amp;ssl=1\" alt=\"check magetop blog categories in database\" class=\"wp-image-1556\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?w=1099&amp;ssl=1 1099w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?resize=300%2C201&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?resize=1024%2C685&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/check-magetop-blog-categories-in-database.png?resize=768%2C514&amp;ssl=1 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>I hope through this series you can create your own complete module. Good luck!<\/p>\n\n\n\n<p>In addition to Magento 2: How To Create Database Table, you can read the articles <a href=\"https:\/\/www.magetop.com\/blog\/how-to-use-model-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Use Model In Magento 2<\/a>.<\/p>\n\n\n\n<p>Follow us for the more helpful article!<\/p>\n\n\n\n<p>We hope this is a useful series for you.<\/p>\n\n\n\n<p>Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous article, we learned How To Create A Simple Custom Module. Following the previous tutorial, we will learn about How To Create Database Table in Magento 2. This is a very basic tutorial but it is very important. You will apply it a lot. Let&#8217;s start!<\/p>\n","protected":false},"author":106,"featured_media":1558,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[321,320,319,315,317,318,316,107],"class_list":["post-1546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-2-tutorials","tag-create-custom-database-table-in-magento-2","tag-how-to-add-a-new-table-to-a-database","tag-how-to-create-custom-database-table","tag-how-to-create-database-table","tag-how-to-create-database-table-in-magento-2","tag-magento-2-create-custom-database-table","tag-magento-2-how-to-create-database-table","tag-magento-2-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Magento 2: How To Create Database Table - Magetop Blog<\/title>\n<meta name=\"description\" content=\"Following the previous tutorial, we will learn about How To Create Database Table in Magento 2. This is a very basic tutorial but it is very important...\" \/>\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.magetop.com\/blog\/magento-2-how-to-create-database-table\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Magento 2: How To Create Database Table - Magetop Blog\" \/>\n<meta property=\"og:description\" content=\"Following the previous tutorial, we will learn about How To Create Database Table in Magento 2. This is a very basic tutorial but it is very important...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/\" \/>\n<meta property=\"og:site_name\" content=\"Magetop Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/MagetopStore\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-14T10:09:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-22T02:57:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i2.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"455\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Aaron LX\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@MagetopStore\" \/>\n<meta name=\"twitter:site\" content=\"@MagetopStore\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aaron LX\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/\"},\"author\":{\"name\":\"Aaron LX\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7\"},\"headline\":\"Magento 2: How To Create Database Table\",\"datePublished\":\"2020-03-14T10:09:52+00:00\",\"dateModified\":\"2021-07-22T02:57:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/\"},\"wordCount\":369,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1\",\"keywords\":[\"create custom database table in magento 2\",\"How to Add a New Table to a Database\",\"how to create custom database table\",\"How To Create Database Table\",\"How To Create Database Table in Magento 2\",\"magento 2 create custom database table\",\"Magento 2 How To Create Database Table\",\"magento 2 tutorials\"],\"articleSection\":[\"Magento 2 Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/\",\"url\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/\",\"name\":\"Magento 2: How To Create Database Table - Magetop Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1\",\"datePublished\":\"2020-03-14T10:09:52+00:00\",\"dateModified\":\"2021-07-22T02:57:35+00:00\",\"description\":\"Following the previous tutorial, we will learn about How To Create Database Table in Magento 2. This is a very basic tutorial but it is very important...\",\"breadcrumb\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1\",\"width\":750,\"height\":455,\"caption\":\"How To Create Database Table in Magento 2\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.magetop.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Magento 2: How To Create Database Table\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#website\",\"url\":\"https:\/\/www.magetop.com\/blog\/\",\"name\":\"Magetop Blog\",\"description\":\"Exploring Magento Tips, Tricks, and Trends\",\"publisher\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.magetop.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#organization\",\"name\":\"Magetop.com\",\"url\":\"https:\/\/www.magetop.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1\",\"width\":475,\"height\":475,\"caption\":\"Magetop.com\"},\"image\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/MagetopStore\",\"https:\/\/x.com\/MagetopStore\",\"https:\/\/www.linkedin.com\/company\/magetop\",\"https:\/\/www.pinterest.com\/magetop\",\"https:\/\/www.youtube.com\/channel\/UCXoiJsz88OfPmwa8QpUkwOA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7\",\"name\":\"Aaron LX\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1\",\"caption\":\"Aaron LX\"},\"description\":\"Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.\",\"url\":\"https:\/\/www.magetop.com\/blog\/author\/aaron-lx\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Magento 2: How To Create Database Table - Magetop Blog","description":"Following the previous tutorial, we will learn about How To Create Database Table in Magento 2. This is a very basic tutorial but it is very important...","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.magetop.com\/blog\/magento-2-how-to-create-database-table\/","og_locale":"en_US","og_type":"article","og_title":"Magento 2: How To Create Database Table - Magetop Blog","og_description":"Following the previous tutorial, we will learn about How To Create Database Table in Magento 2. This is a very basic tutorial but it is very important...","og_url":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/","og_site_name":"Magetop Blog","article_publisher":"https:\/\/www.facebook.com\/MagetopStore","article_published_time":"2020-03-14T10:09:52+00:00","article_modified_time":"2021-07-22T02:57:35+00:00","og_image":[{"width":750,"height":455,"url":"https:\/\/i2.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1","type":"image\/png"}],"author":"Aaron LX","twitter_card":"summary_large_image","twitter_creator":"@MagetopStore","twitter_site":"@MagetopStore","twitter_misc":{"Written by":"Aaron LX","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#article","isPartOf":{"@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/"},"author":{"name":"Aaron LX","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7"},"headline":"Magento 2: How To Create Database Table","datePublished":"2020-03-14T10:09:52+00:00","dateModified":"2021-07-22T02:57:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/"},"wordCount":369,"commentCount":0,"publisher":{"@id":"https:\/\/www.magetop.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1","keywords":["create custom database table in magento 2","How to Add a New Table to a Database","how to create custom database table","How To Create Database Table","How To Create Database Table in Magento 2","magento 2 create custom database table","Magento 2 How To Create Database Table","magento 2 tutorials"],"articleSection":["Magento 2 Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/","url":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/","name":"Magento 2: How To Create Database Table - Magetop Blog","isPartOf":{"@id":"https:\/\/www.magetop.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage"},"image":{"@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1","datePublished":"2020-03-14T10:09:52+00:00","dateModified":"2021-07-22T02:57:35+00:00","description":"Following the previous tutorial, we will learn about How To Create Database Table in Magento 2. This is a very basic tutorial but it is very important...","breadcrumb":{"@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#primaryimage","url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1","width":750,"height":455,"caption":"How To Create Database Table in Magento 2"},{"@type":"BreadcrumbList","@id":"https:\/\/www.magetop.com\/blog\/magento-2-how-to-create-database-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.magetop.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Magento 2: How To Create Database Table"}]},{"@type":"WebSite","@id":"https:\/\/www.magetop.com\/blog\/#website","url":"https:\/\/www.magetop.com\/blog\/","name":"Magetop Blog","description":"Exploring Magento Tips, Tricks, and Trends","publisher":{"@id":"https:\/\/www.magetop.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.magetop.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.magetop.com\/blog\/#organization","name":"Magetop.com","url":"https:\/\/www.magetop.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1","width":475,"height":475,"caption":"Magetop.com"},"image":{"@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/MagetopStore","https:\/\/x.com\/MagetopStore","https:\/\/www.linkedin.com\/company\/magetop","https:\/\/www.pinterest.com\/magetop","https:\/\/www.youtube.com\/channel\/UCXoiJsz88OfPmwa8QpUkwOA"]},{"@type":"Person","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7","name":"Aaron LX","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1","caption":"Aaron LX"},"description":"Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.","url":"https:\/\/www.magetop.com\/blog\/author\/aaron-lx\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/03\/How-To-Create-Database-Table-in-Magento-2.png?fit=750%2C455&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/paOdw5-oW","jetpack-related-posts":[{"id":1640,"url":"https:\/\/www.magetop.com\/blog\/how-to-use-model-in-magento-2\/","url_meta":{"origin":1546,"position":0},"title":"How To Use Model In Magento 2","author":"Aaron LX","date":"April 1, 2020","format":false,"excerpt":"In this tutorial, we will learn about How To Use Model in Magento 2. This is a basic tutorial followed by How To Create Database Table in Magento 2. You should see the previous article to understand this lesson.","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"Magento 2 How To Use Model","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Model.png?fit=750%2C445&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Model.png?fit=750%2C445&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Model.png?fit=750%2C445&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Model.png?fit=750%2C445&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":1402,"url":"https:\/\/www.magetop.com\/blog\/magento-2-extension-tutorial\/","url_meta":{"origin":1546,"position":1},"title":"Magento 2 Extension Tutorial","author":"Aaron LX","date":"February 15, 2020","format":false,"excerpt":"Magento 2 is an open-source platform that allows you can create a module for yourself. And those modules can extend functions for the Magento store. And in this article, I will introduce to Magento 2 Extension Tutorial.","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"Magetop Extension Tutorial","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/02\/Extension-Tutorial.jpg?fit=750%2C455&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/02\/Extension-Tutorial.jpg?fit=750%2C455&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/02\/Extension-Tutorial.jpg?fit=750%2C455&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/02\/Extension-Tutorial.jpg?fit=750%2C455&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":1696,"url":"https:\/\/www.magetop.com\/blog\/how-to-custom-admin-grid-in-magento\/","url_meta":{"origin":1546,"position":2},"title":"How To Custom Admin Grid In Magento 2","author":"Aaron LX","date":"April 2, 2020","format":false,"excerpt":"In the previous lesson, I introduced How To Create Admin Grid In Magento 2. You should read that lesson to be able to do How To Custom Admin Grid In Magento 2 lesson.","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"Magento 2 How To Custom Admin Grid","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Custom-Admin-Grid.png?fit=750%2C445&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Custom-Admin-Grid.png?fit=750%2C445&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Custom-Admin-Grid.png?fit=750%2C445&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Custom-Admin-Grid.png?fit=750%2C445&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":94,"url":"https:\/\/www.magetop.com\/blog\/how-to-create-new-custom-module-in-magento-2\/","url_meta":{"origin":1546,"position":3},"title":"How To Create New Custom Module in Magento 2","author":"Adam Roger","date":"March 23, 2019","format":false,"excerpt":"In this tutorial, we will teach you how to create new custom module in Magento 2 to add customized functionality and give you more control over the store. We\u2019re going to build a very simple module in Magento 2. When finished, the module\u2019s output will say \u201cHello world!\u201d in the\u2026","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"how to create new custom module in magento 2","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2019\/03\/create-custom-magento-2-module.png?fit=679%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2019\/03\/create-custom-magento-2-module.png?fit=679%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2019\/03\/create-custom-magento-2-module.png?fit=679%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":5605,"url":"https:\/\/www.magetop.com\/blog\/import-and-export-database-in-mysql-or-mariadb\/","url_meta":{"origin":1546,"position":4},"title":"How To Import And Export Database In MySQL Or MariaDB","author":"Aaron LX","date":"June 24, 2021","format":false,"excerpt":"In this post, I will guide you on\u00a0How To Import And Export Database In MySQL Or MariaDB. Use data dumps to back up and restore your information.","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"How To Import And Export Database In MySQL Or MariaDB","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/06\/How-To-Import-And-Export-Database-In-MySQL-Or-MariaDB.png?fit=1200%2C711&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/06\/How-To-Import-And-Export-Database-In-MySQL-Or-MariaDB.png?fit=1200%2C711&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/06\/How-To-Import-And-Export-Database-In-MySQL-Or-MariaDB.png?fit=1200%2C711&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/06\/How-To-Import-And-Export-Database-In-MySQL-Or-MariaDB.png?fit=1200%2C711&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/06\/How-To-Import-And-Export-Database-In-MySQL-Or-MariaDB.png?fit=1200%2C711&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":1651,"url":"https:\/\/www.magetop.com\/blog\/how-to-use-event-in-magento-2\/","url_meta":{"origin":1546,"position":5},"title":"How To Use Event In Magento 2","author":"Aaron LX","date":"April 1, 2020","format":false,"excerpt":"In the previous article, we learned How To Use Helper And Setting In Magento 2. Following the previous tutorial, we will learn about How To Use Event in Magento 2. This is a very basic tutorial but it is very important.","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"Magento 2 How To Use Event","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Event.png?fit=750%2C445&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Event.png?fit=750%2C445&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Event.png?fit=750%2C445&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2020\/04\/Magento-2-How-To-Use-Event.png?fit=750%2C445&ssl=1&resize=700%2C400 2x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts\/1546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/users\/106"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":4,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"predecessor-version":[{"id":1758,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts\/1546\/revisions\/1758"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/media\/1558"}],"wp:attachment":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}