Application Example of one-to-one relational database mode

Source: Internet
Author: User

In relational databases, one-to-many relationships are widely used, and many-to-many relationships are also used, while one-to-one relationships are rarely used, this article shows an example of a one-to-one relationship.

First, we have a database:

This is a simple commercial database, and now there is a demand: users are required to upload multiple images to demonstrate goods, shops, markets, and brands. How should we design it?

The first thing that comes to mind is to append an affiliated table to each table for special image storage. First, create a market photo table:

Next is the shop photo table:

You will immediately find that there is a lot of redundancy, and if you have added an online shop table, you have to add an online shop photo table for it. If you are not satisfied with the information recorded in the photo, to add an equal-width thumbnail field, you have to modify all the photo tables, which is undoubtedly painful.

In this case, we can consider using a picture as a single table for Unified Storage. Then, we can create a new ing table to store the ing of each table to form a many-to-many relationship:

In this way, we can store all kinds of information in the photo table without worrying about repeated efforts.

But I have to think about the relationship: A brand can have multiple photos, and a picture can be used by multiple brands.

This is not in line with our intention. Photos cannot be stolen by other brands, so the solution is to set the photos in the ing table as the primary key so that they will not be repeated, the relationship between the ing table and the photo table will automatically become one-to-one:

At this time, the relationship between the image and the brand becomes zero or one-to-many. The reason is zero or one, because the photos in the photo table do not always correspond to the brand table.

Other ing tables should be modified according to this method to meet our design requirements.

Overview after completion:

Download the sample database:


App_data200905280212

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.