How to create custom table in Magento2 Module?

  • author-img Nidhi Arora
  • 4 years
  • 712 views
How to create custom table in magento2 module

This post will helpful to create a custom mysql table in magento2 module. For creating custom table in magento2 we will use InstallSchema.php and for adding an extra column in magento 2 tables we will use UpgradeSchema.php . Magento2, mysql file should be inside Setup folder under module.

Following is the example of custom table creation. I am going to create a custom table called custom_table.

Use Below code to create a custom table. Create a file called InstallSchema.php in setup folder of module i.e. (app/code/vendor/modulename/Setup/installschema.php). And Use following Code.

?php
namespace vendor\modulename\Setup;
use Magento\Framework\DB\Ddl\Table as Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
 * Class InstallSchema
 * @package Eecom\Reservation\Setup
 */
class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /**
         * insert new table
         */
        $installer = $setup;
        /**
         * Create table 'custom_table'
         */
        $table = $installer->getConnection()->newTable($installer->getTable('custom_table'))
            ->addColumn(
                'id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'ID'
            )
            ->addColumn(
                'customer_id',
                Table::TYPE_INTEGER,
                null,
                ['unsigned' => true, 'nullable' => false],
                'Customer ID'
            )
               ->addColumn(
                'customer_email',
                Table::TYPE_TEXT,
                255,
                [],
                'Customer Email'
            )
            ->setComment('Custom  Table');

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

In above code you can change tableName according to your need and can add more columns.

If you are having any doubts, feel free to contact us at [email protected] . However, if you do have any additional tips or method on this “How to”, you can suggest them in the comment section below.

Stay tuned for more “How to” blog on Magento 2. Till then Happy Magento 2 Coding!

Download Blog

ENQUIRY

Ready to Get started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    1250 Pittsford Victor Road Ste 310
    Pittsford, NY 14534

    Development Center

    Plot no. 10, Rajiv Gandhi
    Chandigarh Technology Park