Reading list Switch to dark mode

    How to create a new database table using InstallSchema in Magento 2.

    Updated 22 February 2024

    Here we will learn, How to create a database table by InstallSchema in Magento 2. In Magento 2.3.x and later versions, magento also provided the functionality to create a new table using Declarative Schema. To know more about this, you can check our another blog by clicking here.

    We start by defining the app/code/Webkul/CreateTable/Setup/InstallSchema.php

    <?php
    namespace Webkul\CreateTable\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
    {
        /**
         * Installs DB schema for a module
         *
         * @param SchemaSetupInterface $setup
         * @param ModuleContextInterface $context
         * @return void
         */
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $installer = $setup;
    
            $installer->startSetup();
    
            $table = $installer->getConnection()
                ->newTable($installer->getTable('my_database_table'))
                ->addColumn(
                    'entity_id',
                    Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'nullable' => false, 'primary' => true],
                    'ID'
                )
                ->addColumn(
                    'user_name',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'User Name'
                )
                ->addColumn(
                    'email',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'User Email'
                )
                ->addColumn(
                    'value',
                    Table::TYPE_DECIMAL,
                    '12,4',
                    [],
                    'Value'
                )
                ->setComment('About Your Table');
            $installer->getConnection()->createTable($table);
    
            $installer->endSetup();
        }
    }

    The install method is all that is required in above code. Within this method, we would add any relevant code we might have to create the tables and columns we need.

    The addColumn method is the used to create column. It takes five parameters,
    from column name, data type, column length, array of additional options,
    and description.
    Accepted column data types are:
    TYPE_BOOLEAN, TYPE_SMALLINT, TYPE_INTEGER, TYPE_BIGINT, TYPE_FLOAT, TYPE_NUMERIC, TYPE_DECIMAL, TYPE_DATE, TYPE_TIMESTAMP, TYPE_DATETIME, TYPE_TEXT, TYPE_BLOB, TYPE_VARBINARY

    Note: The module’s module.xml file must have setup_version to create database table using InstallSchema. To know steps to create module in Magento2,  you can check our another blog here.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Now Open the Terminal/cmd and from the Magento’s root directory run the below command
    php bin/magento setup:upgrade

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home