In this blog, we’re going to show you how to use the db_schema.xml file to create a new database table.

Once you Create file db_schema.xml in this directory app/code/VName/MName/etc. We will create a table named  table_name”.

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
     <table name="table_name">
       <column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" comment="Entity Id"/>
       <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
       <column xsi:type="timestamp" name="time_occurred" comment="Time of event"/>
       <column xsi:type="int" name="status" nullable="false" default="0" comment="status"/>
       <column xsi:type="mediumtext" name="description" nullable="true" comment="Description"/>
       <constraint xsi:type="primary" referenceId="PRIMARY">
        <column name="id"/>
        </constraint>
     </table>
</schema>

After will running the below command :

  • php bin/magento setup:upgrade

As you run the upgrade command, a new database table will be created in your database.

If you have any questions let me know in the comment section.

Thank you!

CynoInfotech was founded in 2019, in Ahmedabad, Gujrat. CynoInfotech is a Magento module development company that grow up over this year with outsourcing clients.