---
title: "How to add/remove column and other DB operations to table in Magento 2.3 using declarative schema?"
url: "https://dolphinwebsolution.com/how-to-add-remove-column-and-other-db-operations-to-table-in-magento-2-3-using-declarative-schema"
date: "2022-01-13T11:31:08+05:30"
modified: "2023-08-25T08:51:27+05:30"
author:
  name: "Surbhi Gandhi"
categories:
  - "Magento"
word_count: 1204
reading_time: "7 min read"
summary: "Today I will show you how to add/remove a column from an existing table in Magento 2.3 using declarative schema.

Before Magento 2.3, developers were required to PHP script for installing and upd..."
description: "In Magento 2, Showing a shading filled edge alongside the item\'s size rather than plain text most certainly works on the experience of the store clients. In ..."
keywords: "declarative schema,remove column,db operations,existing table,db_schema, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to create Category Page Layout in Magento."
    url: "https://dolphinwebsolution.com/how-to-create-category-page-layout-in-magento"
  - title: "How to Get all Options of a Custom Attribute in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-get-all-options-of-a-custom-attribute-in-magento-2"
  - title: "How to Add a Custom field at Product Form Magento 2"
    url: "https://dolphinwebsolution.com/how-to-add-a-custom-field-at-product-form-page-magento-2"
---

# How to add/remove column and other DB operations to table in Magento 2.3 using declarative schema?

_Published: January 13, 2022_  
_Author: Surbhi Gandhi_  

![](https://dolphinwebsolution.com/wp-content/uploads/2022/01/remove-column-and-other-DB-operations-to-table-in-Magento-2.3-using-declarative-schema_-1024x536.png)

Today I will show you how to add/remove a column from an existing table in Magento 2.3 using declarative schema.

Before Magento 2.3, developers were required to PHP script for installing and updating data in the database.

Various scripts were required for:

1. Installing and updating the schema.
2. Installing and updating data.
3. Managing other operations when Magento was installed or upgraded.

Now, Magento has introduced a new declarative schema feature with its latest Magento 2.3 version.

Read This: [How to Display swatches instead of labeled product options on Checkout in Magento 2](https://dolphinwebsolution.com/how-to-display-swatches-instead-of-labeled-product-options-on-checkout-in-magento-2)

## What is Declarative Schema and Why should we use it ?

1. **Declarative Schema** files declare what the database structure should be, and Magento determines the differences between the current table structure and what it should be.
2. The **declarative schema** allows the developers to decide the final states of a particular database and then the system adjusts to those changes automatically; this saves the developers from performing a plethora of redundant functions.

## What are the operation perform with Declarative Schema ?

1. Create a table
2. Drop a table
3. Rename a table
4. Add a column to the table
5. Drop a column to the table
6. Change column type
7. Rename a column
8. Add an Index
9. Create a foreign key
10. Drop a foreign key
11. Recreate a foreign key

#### Step 1: Create a new module.

so create it’s required files and give module name **Dolphin\_DeclarativeSchema**.
*File Path:* app/code/Dolphin/DeclarativeSchema/etc/module.xml

```
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Dolphin_DeclarativeSchema" setup_version="0.1.0"></module>
</config>
```

*File Path:* app/code/Dolphin/DeclarativeSchema/registration.php

```
<?php

use MagentoFrameworkComponentComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Dolphin_DeclarativeSchema',
    __DIR__
);
```

Now we see one by one some of database operation with db\_schema.

#### Step 2: Create a new table using db\_schema.xml.

*File Path:* app/code/Dolphin/DeclarativeSchema/etc/db\_schema.xml

```
<?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="dolphin_declaration_schema_new" resource="default" engine="innodb" comment="Dolphin DeclarativeSchema">
      <column xsi:type="int" name="id" padding="10" unsigned="false" nullable="false" identity="true" comment="ID" />
      <column xsi:type="varchar" name="firstname" nullable="false" length="30" comment="Firstname" />
      <column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" disabled="true"/>
      <column xsi:type="varchar" name="email" nullable="false" length="45" comment="Email" />
      <column xsi:type="varchar" name="content" nullable="false" length="255" comment="Content" />
      <constraint xsi:type="primary" referenceId="PRIMARY">
         <column name="id" />
      </constraint>
   </table>
</schema>
```

#### Step 3: Add a column to the table using db\_schema.xml.

In above table, we have, id, firstname, lastname, email and content columns, and I want to add **mobile\_number** column in my existing table.
So I just edit my db\_schema.xml file and added below line inside *<table></table>* tags.

```
<column xsi:type="varchar" name="mobile_number" nullable="false" length="20" comment="Mobile Number" />
```

whenever you edit this file you need to run below command in terminal to whishlist new column in DB.

**php bin/magento setup:db-declaration:generate-whitelist –module-name=Dolphin\_DeclarativeSchema**

then ,
**php bin/magento setup:upgrade**

#### Step 4: Drop a column to the table using db\_schema.xml.

In above table, If you want to remove lastname column from my existing table **dolphin\_declaration\_schema\_new**, So you just have to add **disabled=”true”** attribute in lastname column like this..

```
<column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" disabled="true"/>
```

It’s not enough to write just *disabled=”true”* , You must first generate a *db\_schema\_whitelist.json* file to remove a column from the table by using below command.

**php bin/magento setup:db-declaration:generate-whitelist –module-name=Dolphin\_DeclarativeSchema**
then ,
**php bin/magento setup:upgrade**

After running above commands if you will check your database table column list in your MySQL, you can see lastname column is successfully removed and mobile\_number is successfully added there.

> NOTE : When dropping a table, do not remove it from the db\_schema\_whitelist.json file, otherwise it will not be dropped.

#### Step 5: Rename a table using db\_schema.xml.

```
<?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="dolphin_declaration_schema_new_tbl" onCreate="migrateDataFromAnotherTable(dolphin_declaration_schema_new)" resource="default" engine="innodb" comment="Dolphin DeclarativeSchema New">
          <column xsi:type="int" name="id" padding="10" unsigned="false" nullable="false" identity="true" comment="ID" />
          <column xsi:type="varchar" name="firstname" nullable="false" length="30" comment="Firstname" />
          <column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" disabled="true"/>
          <column xsi:type="varchar" name="email" nullable="false" length="45" comment="Email" />
          <column xsi:type="varchar" name="content" nullable="false" length="255" comment="Content" />
          <column xsi:type="varchar" name="mobile_number" nullable="false" length="20" comment="Mobile Number" />
          <constraint xsi:type="primary" referenceId="PRIMARY">
             <column name="id" />
          </constraint>
   </table>
</schema>
```

You can see **migrateDatafromAnotherTable**() function in which you can pass your old database table name as parameter value and set the new table name through the table tag name attribute as shown below.

***onCreate=”migrateDataFromAnotherTable(dolphin\_declaration\_schema\_new)***

Before running the upgrade command you need to add your schema to *db\_schema\_whitelist.json* file by running the following command :

**php bin/magento setup:db-declaration:generate-whitelist –module-name=Dolphin\_DeclarativeSchema**

then ,
**php bin/magento setup:upgrade**

#### Step 6: Create a foreign key using db\_schema.xml.

```
<?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="dolphin_declaration_schema_new" resource="default" engine="innodb" comment="Dolphin DeclarativeSchema">
          <column xsi:type="int" name="id" padding="10" unsigned="false" nullable="false" identity="true" comment="ID" />
          <column xsi:type="varchar" name="firstname" nullable="false" length="30" comment="Firstname" />
          <column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" disabled="true"/>
          <column xsi:type="varchar" name="email" nullable="false" length="45" comment="Email" />
          <column xsi:type="varchar" name="content" nullable="false" length="255" comment="Content" />
          <column xsi:type="varchar" name="mobile_number" nullable="false" length="20" comment="Mobile Number" />
          <constraint xsi:type="primary" referenceId="PRIMARY">
             <column name="id" />
          </constraint>
       <constraint xsi:type="foreign" referenceId="ADD_REF_FOR_CUSTOM_TABLE" table="custom_table"
              column="referrence_id" referenceTable="reference_custom_table" referenceColumn="reference_column"
              onDelete="CASCADE"/>
    </table>
</schema>
```

NOTE: Foreign keys can only be added to tables when both tables were created using a declarative schema (db\_schema.xml).

here we create a reference type as **foreign** in the constraint node, which indicates the foreign key for the *custom\_table* from the reference table called *reference\_custom\_table*.That’s it. Happy Coding with magento2!! ? Feel free to comment if you have any issue.


---

_View the original post at: [https://dolphinwebsolution.com/how-to-add-remove-column-and-other-db-operations-to-table-in-magento-2-3-using-declarative-schema](https://dolphinwebsolution.com/how-to-add-remove-column-and-other-db-operations-to-table-in-magento-2-3-using-declarative-schema)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:14:51 UTC_  
