A database table to save the Magento 2 store data can be created using two methods. One of the methods to create a new database table in Magento 2.3 is with the db_schema.xml file.
The post shows how to create schema file to create new database table in Magento 2.3.
Apart from the below code, you can refer DB schema structure for Magento 2.
Method to Create Schema File To Create New Database Table in Magento 2.3:
Create db_schema.xml file at app/code/Vendor/Extension/etc/ directory
<?xml version="1.0"?> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="mt_distance_shipping_warehouse" resource="default" engine="innodb" comment="Distance Shipping Warehouse Table"> <column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true" comment="ID"/> <column xsi:type="varchar" name="longitude" comment="Longitude" nullable="false"/> <column xsi:type="varchar" name="latitude" comment="Latitude" nullable="false"/> <column xsi:type="varchar" name="street" comment="Street" nullable="false" /> <column xsi:type="varchar" name="city" comment="City" nullable="false" /> <column xsi:type="varchar" name="state" comment="State" nullable="false" /> <column xsi:type="varchar" name="country" comment="Country" nullable="false" /> <column xsi:type="varchar" name="zipcode" comment="Zipcode" nullable="false" /> <column xsi:type="boolean" name="status" comment="Status" nullable="false" /> <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/> <column xsi:type="timestamp" name="updated_at" on_update="true" nullable="false" default="CURRENT_TIMESTAMP" comment="Updated At"/> <constraint xsi:type="primary" referenceId="PRIMARY"> <column name="id"/> </constraint> </table> </schema>
That’s it.
The next step is to create a new database table in Magento 2.3.
Do share the solution with fellow developers of Magento community via social media.
Thanks.