Logical Database Design-Polymorphic Association

Source: Internet
Author: User
Tags crosstab
Polymorphic Correlation

First, what is polymorphic correlation.

Let's say we have an address table, where the address may be in user or in orders.

  

Above, just for example, there are a lot of practical examples, such as we want to design a content management system (CMS), our CMS has an article table, a software table. Also asked to support comments, then the ID of our comment table is to refer to the article table or reference software table it?

For the disadvantages of the above example, it seems that there is a suspicion that the pattern of this polymorphic association is going soft in the book. Shortcomings do not say, mainly query trouble, second, can not support foreign key constraints.

Solution Solutions

Cross table

For situations where a foreign key is referenced as multiple tables, you can create a crosstab table. Make address no longer dependent on orders or users.

  

Advantages:

Referential integrity support.

Disadvantages:

If we want a given address that can only appear once in a crosstab, the composite primary key above has already been done. If you want an address to appear more than once in a crosstab, you can cancel the composite primary key. However, it is not possible to ensure that an address is not present in more than one cross-table, which needs to be implemented in our program code.

In fact, the Polymorphic Association is related to the entity-attribute-value that was mentioned in the previous article.

If the previous use of the class table design, there is no problem at all, the above address, can also rely on inversion. For example, add a addressid in the order table, and add a ADDRESSID solution to the user table.

Assume that the method of class table inheritance is used. Direct the address with a foreign key to the base table is OK there is no such problem. This article in the book is a bit confusing. Now that I've finished, forget it, whatever.

Logical Database Design-Polymorphic Association

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.