How to Create Table in Magento 2
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
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.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
9 Comments
I am developing CRUD module but i am getting error in phtml template.how phtml form code working and how it is saved in database.
Hello Ramu,
Please follow the below blog:
https://meetanshi.com/blog/create-model-file-to-perform-crud-operation-in-magento-2/
Thank You
im running on ver 2.4.2, i have done step by step, but it doesn’t create in DB, althought I run commands your recommand. please help me ?
Hello,
You just need to follow the above steps.
As it is working properly from our end.
Thank you
Getting this error while upgrading
Fatal error: Namespace declaration statement has to be the very first statement
or after any declare call in the script in C:\xampp\htdocs\projects\app\code\Reg
istration\RegisterForm\Setup\InstallSchema.php on line 3
Hello Aarthi,
There’s an issue in your namespace.
Thank You.
extension table is not created
Hello Dinesh,
Please do follow this to solve your query.
Thank you.