Today’s tutorial is useful for all Magento 2 developers who want to learn exactly how to create a custom database table in Magento 2.
Today, we will teach you guys how to use InstallSchema in Magento 2 to create a custom database table. Now, before we start this tutorial, you will need to first create a custom module.
We have already written a step-by-step tutorial on How to Create a Custom Module in Magento 2 as well.
So, if you don’t know to create a custom module, please refer to our custom module tutorial.
Also, if you also want to add your data into the custom database table, then you’ll also need to create an InstallData.php file in your custom module. Below, we will show you how with an easy-to-follow process.
Steps to Create a Custom Database Table in Magento 2
Step 1
First of all, we will need to create a setup folder in app/code/Test/Helloworld/Setup in order to create the InstallSchema file.
Step 2
Once the Setup folder is created, we can now create the InstallSchema.php file at app/code/Test/Helloworld/Setup/InstallSchema.php.
Step 3
Now, copy the below code in the InstallSchema.php file to create a custom table in Magento 2.
<?php namespace Test\Helloworld\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\DB\Ddl\Table; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $tableName = $installer->getTable('test_helloworld'); // Get test_helloworld table // Check if the table already exists if ($installer->getConnection()->isTableExists($tableName) != true) { $table = $installer->getConnection() ->newTable($tableName) ->addColumn( 'id', Table::TYPE_INTEGER, null, [ 'identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true ], 'ID' ) ->addColumn( 'name', Table::TYPE_TEXT, null, [ 'nullable' => false, 'default' => '' ], 'Name' ) ->addColumn( 'description', Table::TYPE_TEXT, null, [ 'nullable' => false, 'default' => '' ], 'Description' ) ->addColumn( 'created_at', Table::TYPE_DATETIME, null, [ 'nullable' => false ], 'Created At' ) ->addColumn( 'status', Table::TYPE_SMALLINT, null, [ 'nullable' => false, 'default' => '0' ], 'Status' ) ->setComment('Test Helloworld Table') ->setOption('type', 'InnoDB') ->setOption('charset', 'utf8'); $installer->getConnection()->createTable($table); } $installer->endSetup(); } }
And it’s done!
Now, open your terminal in the Magento’s root folder and execute the following two commands:
php bin/magento setup:upgrade php bin/magento cache:flush
As you’ll run both these commands, your new custom database table will be created with the name test_helloworld.
Also read: How to Rename a Table Name Using Declarative Schema in Magento 2.3?
Final Words…
With this, we have come to the end of this tutorial. We hope you found this tutorial helpful for creating your own custom database table in Magento 2.
In case you’ve any questions, please share in the comments below. And if you need our professional help, feel free to contact us.