How to add a new table to magento database

How to add a new table to magento database

August 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add a new table to magento database

Adding a New Table to Your Magento Database

Hello there! Welcome back to our Magento development series at Laravel Company. Today, we’re going to walk you through the process of adding a new table to your Magento database for custom CMS pages.

Understanding the Requirement

You’re using Magento 1.4.2.0 and need to create a new table for storing data related to some custom CMS pages. This data will then be fetched and displayed within those pages. As you’re new to Magento, we’ll break down this process into simple, manageable steps.

Step 1: Planning Your Table Structure

Before jumping into the code, let’s plan your table structure:

  • Table Name: custom_page_content
  • Columns:
    • id (INT, PRIMARY KEY, AUTO_INCREMENT)
    • page_id (INT, FOREIGN KEY referencing cms_page table)
    • content_type (VARCHAR)
    • content_data (TEXT)

Step 2: Creating the Database Schema File

In your Magento root directory, navigate to app/code/core/Mage/Install/etc. Here, you’ll find a file called schema-1.4.2.0.xml.

cd app/code/core/Mage/Install/etc

Copy this file and save it as schema-custom.xml in the same folder.

Step 3: Defining Your New Table

Open your new schema file (schema-custom.xml) in a text editor or IDE. You’ll see some existing <table> tags within the <setup> section. At the end of these, add the following code to define your custom_page_content table:

<table name="custom_page_content">
    <column name="id" type="int(10) unsigned" nullable="false" primary_key="true" default="nextval('custom_page_content_id_seq')">
        <constraints>
            <primary key referenceId="PRIMARY"/>
        </constraints>
    </column>
    <column name="page_id" type="int(10) unsigned" nullable="false">
        <constraints>
            <foreign key referenceId="CMS_PAGE_CONTENT_CMS_PAGE_ID_FK"/>
        </constraints>
    </column>
    <column name="content_type" type="varchar(255)" nullable="true"/>
    <column name="content_data" type="text" nullable="true"/>
</table>

Step 4: Defining the Foreign Key

To create a foreign key relationship with the cms_page table, add this code right after the closing tag of your new custom_page_content table definition:

<constraint referenceId="CMS_PAGE_CONTENT_CMS_PAGE_ID_FK" table="cms_page">
    <column name="page_id"/>
</constraint>

Step 5: Updating Your Database

Now, let’s run the installer to create your new table.

cd /path/to/your/magento/root
php bin/magento install --recreate-previous

Replace /path/to/your/magento/root with the actual path of your Magento installation directory. This command will reinstall Magento, recreating all tables including your new custom_page_content table.

Step 6: Inserting Data and Retrieving It

To insert data into your new table, you can use SQL queries or create a custom model. For simplicity, let’s use an SQL query:

INSERT INTO custom_page_content (page_id, content_type, content_data)
VALUES ('123', 'json', '{"key":"value"}');

To fetch data and display it in your CMS pages, you’ll need to create a custom block. You can find detailed guides on creating custom blocks at Laravel Company.

Step 7: Conclusion

Congratulations! You’ve successfully added a new table to your Magento database and learned how to store and retrieve data from it for use in custom CMS pages.

Remember, while this example uses Magento 1.4.2.0, the principles remain the same across most versions of Magento. Always refer to official Magento documentation or consider upgrading to later versions like Magento 2 for better performance and security features.

Happy coding! If you have any other questions about Magento development, feel free to explore our other tutorials at Laravel Company.

Magento 1 Database

Our network