How to create schema file to create new database table Magento 2.3?

From Magento 2.3, You want to create a new database table or upgrade an existing table, you can create it with the db_schema.xml file. New database table creation/updating concept in Magento 2.3 is called as declarative schema.

From Magento 2.3 You don’t need to create InstallSchema.php and UpgradeSchema.php

Now you can create a database table using a new way by creating db schema XML file.

Location for db_schema.xml file,

You can create your custom table by below XML code,

I have taken many different types of the field for creating the new database table. You can take the only required field to create a database table.

<table>…</table> : “Used for create table in Magento 2.3+”
<column>…</column> : “Used for create different column of the table”
You can set int, text, varchar, timestamp, date, datetime, smallint, float, decimal, double etc.
<constraint>…</constraint> : “Used for set of constraint, Like primary key, foreign key, unique key.”

You can set primary key usig constraint tag

Where name is your primary key field in the above column tag.

Run Magento 2 Upgrade command to create/update table in Magento 2.3

Now you can check a new table will be generated in a database using db_schema.xml file.


2 Replies to “How to create schema file to create new database table Magento 2.3?”

  1. Wow!

    Thanks for update. its really good article, in the previous version we have to create install schema and upgrade. This will make to light for developers to create tables for custom requirements.

    thank you rakesh.

  2. Hello, Rakesh sir again awesome content I wish a blog to compare between both 2.2.x schema and 2.3.x schema.
    like why this new style of creating a schema is adopted by Magento guys
    thanks again, master.

Leave a Reply

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