Magento, Magento 2

How to create a new custom database table in Magento 2?

Create a new custom database table in Magento 2

In this technical blog, we will learn about how to create a new database table by InstallSchema in Magento 2. Before create InstallSchema file. You need to create a custom module for that. You can create a custom module from here.

If you want to add any data into table then,  you need to create InstallData.php file into Setup Folder in your custom module. Except InstallSchema and InstallData, There are many files available in Setup Folder. For exaple, UpgradeSchema, UpgradeData, Recurring, RecurringData etc.

You may also like this :

Now, Let’s start to create InstallSchema file to create new table in database. Before creating that file you need to create Setup Folder at app/code/RH/Helloworld/Setup
After creating folders you need to create InstallSchema.php file in app/code/RH/Helloworld/Setup/InstallSchema.php and paste this below code.

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\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('rh_helloworld'); // Get rh_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('RH Helloworld Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');

                $installer->getConnection()->createTable($table);
        }
        $installer->endSetup();
    }
}

Now, Open terminal in folder root of magento and run both commands

php bin/magento setup:upgrade
php bin/magento cache:flush

 

Finally, It’s done. You can check that rh_helloworld table created successfully.

I hope this blog is easy to understand about how to create a new custom database table in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂

Tagged ,