OpenCartA free open-source CMS for online stores, written in PHP with a MySQL/MariaDB database. Everything that is not part of the core is added through extensions. More in the glossary → database tables consist of several repeating patterns: the main entity table (product, category), its translations (_description with language_id), store binding (_to_store), and page layout binding (_to_layout). Once you understand these patterns, you can quickly read any part of the schema. In 3.0.5.1 there are 134 tables, in 4.1.0.4 there are 159, and some key tables in 4.1 have changed structure.
Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ. For where the code lives for the models that work with these tables, see the articles OpenCart model and $this->db and OpenCart file structure.
Where is the schema described, and how do the versions differ overall?
The source of truth for the schema is different in each branch:
- OpenCart 3.0: the
install/file with ready-madeopencart.sql CREATE TABLEand demo data. - OpenCart 4.1: a PHP array in
system/(functionhelper/ db_schema.php oc_db_schema()). The installer and upgrader build tables from it, and the demo data is stored separately ininstall/.opencart.sql
All tables have a prefix from the DB_PREFIX constant in config.php; by default it is oc_, but on a specific server it may be different, so in module code always write DB_PREFIX . "product". For configs, see the article config.php in OpenCart.
| Release | Tables | Engine | Core table encoding |
|---|---|---|---|
| 3.0.4.1 | 136 | 134 MyISAM, 2 InnoDB | utf8 / utf8_general_ci |
| 3.0.5.1 | 134 | 132 MyISAM, 2 InnoDB | utf8mb4 / utf8mb4_unicode_ci |
| 4.1.0.4 | 159 | InnoDB | utf8mb4 / utf8mb4_unicode_ci |
Between 3.0.4.1 and 3.0.5.1, two tables disappeared: order_shipment and shipping_courier. An important consequence of the engine: in 3.0, MyISAM has neither foreign keys nor transactions. In 4.1, db_schema.php describes relationships in the foreign section, but the installer does not create foreign keys, so integrity between tables in both branches is maintained only by model code. If you delete a product with a direct SQL query, you will leave "orphans" in ten related tables.
How are the tables grouped?
OpenCart table names speak for themselves, and they are convenient to read in groups:
| Group | Key tables |
|---|---|
| Catalog | product, product_description, product_to_category, product_image, product_option, product_option_value, product_attribute, product_discount, category, category_description, category_path, manufacturer |
| Attributes, options, filters | attribute, attribute_group, option, option_value, filter, filter_group and their _description |
| Orders | order, order_product, order_option, order_total, order_history, order_status |
| Customers | customer, customer_group, address, customer_transaction, customer_reward |
| Localisation | language, currency, country, zone, geo_zone, tax_class, tax_rate |
| Stores and design | store, layout, layout_route, layout_module, module, theme, seo_url |
| System | setting, extension, extension_install, event, modification, session, cart, user, user_group |
A separate note on several tables that modules work with most often:
setting— core and module settings:store_id,code,key,value,serialized. Arrays are stored as JSON withserialized = 1. For details, see the article setting model and the setting table.category_path— a ready-made categoryA section of the catalog that contains products. It may have subcategories. More in the glossary → tree: for each category, rows with all its ancestors (path_id) and the level (level). Through it, the core finds all subcategories without recursion.cart— the database cart, bound tosession_idandcustomer_id.eventandmodification— registered events and OCMOD modifications of modules.
Which patterns repeat across the entire schema?
Translations: _description and language_id
Everything shown to the customer as text is moved into tables with the suffix _description and the primary key (id сутності, language_id). For example, product_description has PRIMARY KEY (product_id, language_id) and the fields name, description, tag, meta_title, meta_description, meta_keyword. Therefore, every query for names has the filter language_id, and a module that adds a product must insert a row for each language. In 4.1 this pattern was extended to countries and regions: country_description and zone_description appeared.
Stores: _to_store and store_id 0
The tables product_to_store, category_to_store, information_to_store, manufacturer_to_store determine in which store an entity is visible. A product without a row in product_to_store for the current store will not be found on the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary →. The main store has store_id = 0, and in the table store it is absent: there are only additional stores there, while the settings of the main store are in setting with store_id = 0.
Page layouts: _to_layout
product_to_layout, category_to_layout and others define which layout a specific page uses, and layout_module defines which modules are displayed in the positions of that layout.
Example
SELECT p.product_id, p.model, p.price, pd.name
FROM oc_product p
JOIN oc_product_description pd ON (pd.product_id = p.product_id AND pd.language_id = 1)
JOIN oc_product_to_store p2s ON (p2s.product_id = p.product_id AND p2s.store_id = 0)
WHERE p.status = 1
ORDER BY pd.name
LIMIT 20;
The value language_id = 1 here is for illustration only: language ids depend on the installation. In code, take it from $this, and the store from config_store_id.
What changed in the OpenCart 4.1 schema?
Most 4.1 tables match 3.0 by name, but there are differences in key tables that break direct SQL queries from modules:
| What | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| Product codes | columns sku, upc, ean, jan, isbn, mpn in product | table product_code (product_id, code, value) and the reference identifier (SKU, UPC, EAN, JAN, etc.) |
| Product variants | none | columns master_id, variant, override in product |
| Special prices | separate product_special | product_discount rows with special = 1 and the column type |
| Product views | column product.viewed | tables product_viewed and product_report |
| Recurring payments | recurring, order_recurring, product_recurring | subscription, subscription_plan, product_subscription and others |
| Gift vouchers | voucher, voucher_theme, order_voucher | not in the core schema |
| SEO URLs | seo_url.query = product_id=48 | seo_url.key = product_id, value = 48, plus sort_order |
| Payment method in order | order.payment_method varchar(128) and payment_code | order.payment_method text with JSON, without payment_code |
| Customer | has salt, cart, wishlist, address_id, fax | these columns are absent |
This is what seo_url looks like in 3.0:
install/ · OpenCart 3.0.5.1 · lines 3699–3708
CREATE TABLE `oc_seo_url` (
`seo_url_id` int(11) NOT NULL AUTO_INCREMENT,
`store_id` int(11) NOT NULL,
`language_id` int(11) NOT NULL,
`query` varchar(255) NOT NULL,
`keyword` varchar(255) NOT NULL,
PRIMARY KEY (`seo_url_id`),
KEY `query` (`query`),
KEY `keyword` (`keyword`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
In 4.1, instead of one query row there is a pair key (varchar(64)) and value (varchar(255)), and keyword has been expanded to varchar(768). A 3.0 module query of the form WHERE query = 'product_id=48' must be rewritten for 4.1 as WHERE `key` = 'product_id' AND `value` = '48'. For the table itself, see the article seo_url in OpenCart.
New table groups in 4.1: blog (article, topic with descriptions and bindings), tasks cron, startup, antispam, gdpr, tokens and authorization (customer_token, customer_authorize, user_token, user_authorize, user_login), reports (product_report, download_report, marketing_report).
Which indexes exist, and where are they missing?
Core indexes are minimal, and it is worth knowing this before writing your own queries. For comparison of several tables:
| Table | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
order | primary key only | additionally email and order_status_id |
customer | primary key only | additionally email |
product_description | name | name |
product_to_category | category_id | category_id |
order_product | order_id | order_id |
The practical conclusion: in 3.0, selecting orders by status or customers by email on a large database performs a full table scan. If a module regularly makes such queries, check the plan via EXPLAIN and, if necessary, suggest that the store owner add an index. Changing core tables from the install() module without warning is not advisable: it affects all other modules and future upgrades.
How to work with the schema safely from a module?
- Read through core models where possible. Models already account for language, store, customer groupA segment of buyers (retail, wholesale) with different prices and terms. More in the glossary →, and status, while direct SQL easily skips one of these conditions.
- Write through core models where possible.
editProduct()ordeleteProduct()update and clean up a dozen related tables; a manualDELETE FROM productwon't do this, and there are no foreign keys to act as a safety net. - Keep your own data in your own tables with the module prefix, on InnoDB, and do not add columns to core tables. For how to design them, see the article custom module tables in OpenCart.
- Check the schema against the version. Before a direct query, open
install/(3.0) oropencart.sql system/(4.1) and verify that the column exists there.helper/ db_schema.php - Always filter by
language_idandstore_idin tables where they exist, otherwise you will get duplicate rows.
Summary
- 3.0.4.1 has 136 tables, 3.0.5.1 has 134, and 4.1.0.4 has 159; the prefix is taken from
DB_PREFIX. - The 3.0 schema is described in
install/, and the 4.1 schema is inopencart.sql system/.helper/ db_schema.php - Patterns:
_descriptionwithlanguage_id,_to_storewithstore_id(the main store is 0, and it is absent fromstore),_to_layoutfor page layouts. - In 4.1, product codes, specials, SEO URLs, subscriptions, and the payment methodAn extension such as card or cash payment. More in the glossary → format have changed; there are no vouchers in the core schema.
- There are no foreign keys in either branch, so write data through core models.
Tables for products, orders, and customers, working with language_id and store_id, and safe and slow queries are covered separately; see the articles in the Database section.