Magento 2 offers a mechanism to create database tables, modify existing ones, and add data into them.
You can create a new database table by InstallSchema in Magento 2.
Create table in Magento 2 to store and retrieve the data. Not only that, but you can also perform various operations such as insert, update and delete on the table data.
Moreover, it can be used for checking the login credentials.
Check the below steps to programmatically create table in Magento 2. You can use this method when creating a custom module in Magento 2.
Steps to Create Table in Magento 2:
- Create InstallSchema.php at app\code\Meetanshi\Extension\Setup
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071<?phpnamespace Meetanshi\Extension\Setup;use Magento\Framework\Setup\InstallSchemaInterface;use Magento\Framework\Setup\ModuleContextInterface;use Magento\Framework\Setup\SchemaSetupInterface;use Magento\Setup\Exception;class InstallSchema implements InstallSchemaInterface{/*** {@inheritdoc}** @SuppressWarnings(PHPMD.ExcessiveMethodLength)*/public function install(SchemaSetupInterface $setup, ModuleContextInterface $context){$installer = $setup;$installer->startSetup();try {$table = $installer->getConnection()->newTable($installer->getTable(‘extension’))->addColumn(‘id’,\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,null,[‘identity’ => true, ‘nullable’ => false, ‘primary’ => true],‘Record Id’)->addColumn(‘name’,\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,255,[‘nullable’ => false],‘Name’)->addColumn(’email’,\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,‘255’,[‘nullable’ => false],‘Email’)->addColumn(‘telephone’,\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,‘255’,[‘nullable’ => false],‘Telephone’)->addColumn(‘created_at’,\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,null,[‘nullable’ => false, ‘default’ => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],‘Created At’)->addColumn(‘update_time’,\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,null,[‘nullable’ => false, ‘default’ => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],‘Updated At’)->setComment(‘Data Table’);$installer->getConnection()->createTable($table);$installer->endSetup();} catch (Exception $err) {\Magento\Framework\App\ObjectManager::getInstance()->get(‘Psr\Log\LoggerInterface’)->info($err->getMessage());}}}
- Once the extension is created, all the commands are being run. We are creating InstallSchema.php file after executing the extension. Therefore, we have to delete the existing record.To do that, go to table setup_module and search Meetanshi_Extension in the column module and delete the record.Now, run the commands given below.
123php bin/magento setup:upgradephp bin/magento setup:static–content:deploy –fphp bin/magento cache:flush
After running these commands, the table ‘extension’ will appear in the database.
Done!
If you have a question regarding this post, feel free to ask in the Comments section below.
I would be happy to help you.
Do share the post with Magento Community via social media.
Thank you.
[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]]]>