Skip to content
TheCoachSMB
  • Follow Us:
Email: info@gmail.com
Call: +123-456-7890
TheCoachSMB

We Make It Happen

  • Blog
    • Magento2
    • HTML5
    • PHP Tutorial
  • Our Courses
  • Services
    • Magento 2 Installation Service
    • Magento Development Services
    • Support & Maintenance Services
    • Migration Services
    • Magento 2 Upgrade Service
    • Magento Security Patches
    • Magento Site Audit
    • Magento Speed & Performance
    • Magento Extension Development
    • Magento Consulting Services
    • SEO Services
    • Designing Services
  • Book A Call
  • Profile
  • About Us

    Lorem Ipsum is simply dummy text of the printing and typesetting industry.

    Contact Us
    Contact Info

    684 West College St. Sun City, United States America, 064781.

    (+55) 654 - 545 - 1235

    info@gmail.com

  • Get In Touch
  • Follow Us:
Email: info@gmail.com
Call: +123-456-7890
TheCoachSMB

We Make It Happen

  • Get In Touch
  • Blog
    • Magento2
    • HTML5
    • PHP Tutorial
  • Our Courses
  • Services
    • Magento 2 Installation Service
    • Magento Development Services
    • Support & Maintenance Services
    • Migration Services
    • Magento 2 Upgrade Service
    • Magento Security Patches
    • Magento Site Audit
    • Magento Speed & Performance
    • Magento Extension Development
    • Magento Consulting Services
    • SEO Services
    • Designing Services
  • Book A Call
  • Profile

How to Add an Extra Column in Your Existing Magento 2 Table?

  • Home
  • How to Add an Extra Column in Your Existing Magento 2 Table?
  • Sonal Motghare-Balpande Sonal Motghare-Balpande
  • Dec, Sat, 2021
  • Magento 2
How to Add an Extra Column in Your Existing Magento 2 Table?

Table of Contents

  • To Add A Column To Existing Database Table In Magento 2
    • Method 1: Using UpgradeSchema:
    • Method 2: USING DB SCHEMA IN MAGENTO 2
      • Step 1: Create a db_schema.xml file
      • Step 2: Add the schema to db_whitelist_schema.json file
      • Step 3: Run setup upgrade command
  • Conclusion

While working with Magento 2, sometimes you need to add an extra column to your already existing Magento 2 table. Today, we are going to provide you with an optimal solution for the same.

Magento 2 allows a developer to create, upgrade, or delete a table in the database. In this article, you will learn how to add a column to existing database table in Magento 2.

You can use the below code for the same. For example, adding an order delivery date column to the order table in the database.

To Add A Column To Existing Database Table In Magento 2

There are two ways of doing this –

  1. Using UpgradeSchema (Used in Magento 2.3 earliar version)
  2. Using DB Schema

Method 1: Using UpgradeSchema:

This method is used earliar Magento 2.3.  In this method, we will use the UpgradeSchema.php file for adding extra columns in existing Magento 2 tables and this file should be under the setup folder of the module. So let’s get started.

The below-given example adds an extra column named ‘delivery_date’ under the table name ‘sales_order’.

Note:

Increase the setup_version in the app/code/Vendor/Module/etc/module.xml file and mention the same in below file.

Let’s create UpgradeSchema.php in the path app/code/Vendor/Module/Setup

<?php 
namespace Vendor\Module\Setup; 
use Magento\Framework\Setup\UpgradeSchemaInterface; 
use Magento\Framework\Setup\ModuleContextInterface; 
use Magento\Framework\Setup\SchemaSetupInterface; 
use Magento\Framework\DB\Ddl\Table; 

class UpgradeSchema implements UpgradeSchemaInterface { 
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { 
         if (version_compare($context–>getVersion(), ‘1.0.1’) < 0) { 
            $connection = $setup–>getConnection();
            $connection–>addColumn(
                $setup–>getTable(‘sales_order’),
                ‘delivery_date’,
                [
                    ‘type’ => Table::TYPE_TEXT,
                    ‘length’ => 255,
                    ‘nullable’ => true,
                    ‘default’ => ”,
                    ‘comment’ => ‘Add New Filed’
                ]
            );
        }
    }
}

After saving files, you need to run

php bin/magento s:up

Now check your database and you will be able to find a new column ‘delivery_date’ in ‘sales_order’ table.

Method 2: USING DB SCHEMA IN MAGENTO 2

In the previous version of Magento, this was done by using a UpgradeSchema.php file.

But, in the Magento 2.3 version or later version, we can add a new column to an existing table using the db_schema.xml file.

Here’s an easy and step-by-step process for adding a new column to an existing table using db_schema.xml file in Magento 2.

Let’s say, we have table abc with columns id, title, email, description. And now we want to add column short_desctiption in this table ‘abc’.

Step 1: Create a db_schema.xml file

The first step of the process is to create a db_schema.xml file in the app/code/Vendor/Module/etc/ directory

app/code/Vendor/Module/etc/db_schema.xml

and Content for this file is..

<?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="abc" resource="default" engine="innodb" comment="ABC is the table name">
      <column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="ID" />
      <column xsi:type="varchar" name="title" nullable="false" length="25" comment="Title" />
      <column xsi:type="varchar" name="email" nullable="false" length="25" comment="Email" />
      <column xsi:type="varchar" name="description" nullable="false" length="255" comment="Descrition" />
      <column xsi:type="varchar" name="short_description" nullable="false" length="255" comment="Short Description" />
      <constraint xsi:type="primary" referenceId="PRIMARY">
         <column name="id" />
      </constraint>
   </table>
</schema>

As you can see, we’ve added a new column short_description in the abc table.

Step 2: Add the schema to db_whitelist_schema.json file

Now, before executing the upgrade command, you’ll have to add the schema to db_whitelist_schema.json file. This provides a history of all tables, columns, and keys added with declarative schema. It is required to allow drop operations. It can be generated manually or created automatically with the following command:

php bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Module

Once you execute the above command, the db_whitelist_schema.json file will be created in the /Vendor/Module/etc directory.

Step 3: Run setup upgrade command

Lastly, you need to run the setup upgrade command given below after the db_schema_whitelist_json file has been successfully generated.

php bin/magento s:up && php bin/magento se:s:d -f

Now, check the output and you’ll see that a new column will be created in your database table.

Conclusion

So, this is how easy it is to add a new column to an existing database table using db schema in Magento 2.

We hope that you found this tutorial helpful. If you’ve any queries, please share them in the comments below.

Related Posts:

  • Events and Observers in Magento2
  • HTML5
  • PHP: How to install intl extension on CentOS
  • Full tutorial to Learn CSS Step By Step
  • Magento 2 Useful Commands List 2022
  • Install Magento 2 on Ubuntu 21.04 [Complete Guide]
How to add new column to an existing table in Magento 2.3 using declarative schema?
Comments (4)
Sonal Motghare-Balpande

Hello Readers, My passion is to guide people by sharing the knowledge I have. If I can contribute even little to the people life, its very big achievement for me. Thank You, Sonal

One thought on “How to Add an Extra Column in Your Existing Magento 2 Table?”

  1. Annanymous says:
    October 26, 2023 at 1:20 AM

    For the db_schema method is it required to write column of existing table (id, title, email, description) along with new column (short_description).

    Reply
    1. Sonal Motghare-Balpande says:
      October 26, 2023 at 2:20 PM

      Yes

      Reply
  2. syam s gopal says:
    April 13, 2022 at 3:45 PM

    now..how to add datas into those new columns…
    hope you will help me…

    Reply
    1. Sonal Motghare-Balpande says:
      April 13, 2022 at 4:26 PM

      Follow the step of adding data from this video – https://youtu.be/3zjYfxmZUpM

      Reply

Leave a Reply to syam s gopal Cancel reply

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

Search
Recent Posts

  • Install/debug Magento2 delivery date extensionNovember 18, 2024
  • Install Magento 2.4.6 on Ubuntu 22.04 [Complete Guide]October 16, 2024
  • Install Magento 2.4.7 on Ubuntu 22.04 [Complete Guide]October 16, 2024
Categories

  • Accounting
  • Agency
  • Business
  • Consultant
  • Finance
  • Investment
  • Magento 2
  • Masonry
Calendar
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Nov    
Tags

6 Steps to Install Magento 2 on XAMPP Windows Using Composer Agency Business composer install magento 2 download xampp for windows Graphics how to install magento how to install magento2 in localhost how to install magento2 in localhost ubuntu how to install magento2 in localhost wamp how to install magento 2 in windows 10 How To Install Magento2 in Windows on localhost using Xampp how to install magento 2 on xampp How to install Magento 2 using Composer in windows How to install Magento 2.4 in localhost XAMPP how to install magento 2.4 on xampp install magento 2 in windows10 install magento 2 using composer install magento 2 using composer windows install magento 2 windows xampp install Magento 2 with Sample Data install magento2.4 Install Magento 2.4 xampp windows 10 install magento 2.4.3 install magento2.4.3 install xampp on windows 10 Latest magento2 magento 2 composer install magento 2 install magento2 install magento 2 installation magento 2 installation steps magento 2 installation using composer magento 2 system requirement magento2.4 magento2.4.3 magento download magento for windows magento installation on xampp Magento with windows magneto2 Multisite thecoachsmb xampp download

LATEST BUSINESS IDEAS

Subscribe to Our Newsletter
& Stay Update

Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.

Thank you to visit our site and doing business with us.

We give our 100% to help you and to grow together.

About Us

Ready To Start Work With Us?

Felis consequat magnis est fames sagittis ultrices placerat sodales porttitor quisque.

Get a Quote
Contact Us
  • Courses
  • Blog
  • Magento2 Tutorial
  • Shop
Quick Links
  • Appointment
  • Price Plans
  • Investment Strategy
  • Financial Advices
  • Strategy Growth
  • Services
  • Business Planning
Links
  • Privacy Policy
  • Terms and Conditions
  • My Account
  • Contact Us
  • About Us
Latest Posts
  • Install/debug Magento2 delivery date extensionNovember 18, 2024
  • Install Magento 2.4.6 on Ubuntu 22.04 [Complete Guide]October 16, 2024
  • Install Magento 2.4.7 on Ubuntu 22.04 [Complete Guide]October 16, 2024
Open Hours

Our support available to help you.

  • Monday-Friday: 10am to 10pm
  • Email: support@thecoachsmb.com
  • Call: +91 7020500374
Opening Hours
Week Days 10:00 - 17:00
Saturday 10:00 - 15:00
Sunday Day Off
Contact us
Copyright © 2025 TheCoachSMB
🚀 Let’s Connect on LinkedIn! 🚀

Want to level up your skills and knowledge? 🚀
Follow us on LinkedIn to get:
✅ Expert insights on business growth
✅ Daily tips to sharpen your skills
✅ Exclusive updates from The Coach SMB

Let's grow together!

Follow me on LinkedIn

No thanks, I’m not interested!

WhatsApp

WhatsApp

Skype

Skype

Hide