DB schema method in Magento2

DB schema method is used to create tables or columns inside a table.

create dbschema.xml inside app/code/vendor/module/etc/

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
   <table name="declarative_table">
       <column xsi:type="int" name="id_column" padding="10" unsigned="true" nullable="false" comment="Entity Id"/>
       <column xsi:type="text" name="severity" padding="10" unsigned="true" nullable="false" comment="Severity code"/>
       <column xsi:type="timestamp" name="time_occurred" padding="10" comment="Time of event"/>
       <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id_column"/>
       </constraint>
    </table>
</schema>

It will create a table named declarative_table.

Thank you.

Leave a comment

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