MySQL users can easily be confused by the seemingly infinite differences between MySQL and PostgreSQL. The good news is that for someone familiar with MySQL, they have the basics of databases covered and can explore some of the treasures to be discovered in the new PostgreSQL environment.

One of the interesting facets that you need to explore that has no corresponding counterpart in the MySQLverse is table inheritance. If you are a big fan of data normalization, this feature will catch your attention. You can start with a really minimalistic parent table and build upon it.

Example

In the following example, pretend that an imaginary company has products, inventory for that product, and warehouses where that product inventory is held. Tables for products, inventory, and warehouses are created. The inherits keyword is new for someone from the MySQL universe. In the example below, inventory inherits product information from the product table. And the warehouse table inherits product and inventory information from those tables, respectively.

Examining the definitions of the tables, note that the inventory table informs us that this table inherits from the product table and has at least one child table. The warehouse table tells us that it inherits from the product and the inventory tables.

The next step is to add some test data.

Each table has its own data. All the product data is in the product table.

And the inventory data is in the inventory table.

If you are in a situation where you need to exclude some users from accessing specific tables, you could use inheritance in place of column permissions or views to segregate the data.

Data propagation

But be careful. If we insert a new record into the product table, the senior parent table, the data does not flow downward to the inventory or warehouse tables.

However, the data does not flow logically downward to the warehouse table.

Likewise, inserting into the inventory (middle table) does not propagate the data to the parent or its child table.

If we create a second child table from the inventory table and add data to that new child table, the data will flow up into inventory and product. But none of that data makes it to the warehouse table. So you need to be careful of where you are adding data and aware that siblings and child tables may be deprived of the data you need.

Conclusion

Inheritance can be a useful way to normalize data. It may also be handy in securing data. However, you need to be aware that the data does not propagate by itself through the family tree in the way you expect.

Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community in a single distribution, designed and tested to work together.

 

Download Percona Distribution for PostgreSQL Today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments