There are seven forms of Database Design with many-to-many relationships ).

Source: Internet
Author: User
Several forms of Database Design with many-to-many relationships
Many-to-many relationships require at least three tables. We call one table a master table and the other a relational table, the other is called a dictionary table or sub-table (the Sub-table has few records and is basically stable, for example, the Forum name; the sub-table has many contents and the content changes, for example ).
According to the database addition, deletion, query, and modification operations, you can use inner join or select * from primary table where ID in (select primary table ID from relational table) for multiple link searches)

1. Role appointment

Features: The foreign key combinations of relational tables do not have repeated records. Generally, a relational table does not require a time field or a primary key. One table is a dictionary table.
Interface features: displays the master table, and uses checkbox or multiple select statements to set the multi-choice relationship.
For example, you can appoint a moderator (User table-relational table-Forum name table), role permission control, and so on. If you are a member of five Forum versions, you can create a five-row Link Table Record, the two Foreign keys of the relational table are associated with primary keys.
Add link: If no combination record exists, insert it.
Delete link: If a combination record exists, delete it.

2. Set Group type

Features: similar to the role appointment type, the foreign key combination of the relational table does not have repeated records, and the relational table generally does not require the time field and primary key. The difference is that the primary and secondary tables are not dictionary tables, and they may be quite unfixed.
Interface features: displays the master table, and replaces the simple checkbox or multiple select statements with search, or adds one by one.
For example, a song album (album table-relational table-song table ). Mobile phone group (group table-relational table-mobile phone table ). User circle (circle table-relational table-User table ). Article tag (article table-relational table-label table)
Add link: Same moderator appointment type.
Delete link: Same moderator appointment type.

3. ledger

Features: a relational table can have repeated records. A relational table generally has time fields, primary keys, and text fields to indicate the cause of each link (consumption ).
Interface features: displays the relationship table and uses radio or drop-down to set the single-choice relationship.
For example, a cash consumption statement or an order (User table-order table-consumption reason table) may be repeatedly consumed on the same thing for multiple times. Point change records also belong to this category.
Add relationship: whether there is a combination of records, insert, record time.
Delete link: delete a link based on the Link Table PK.

4. Comment reply type

Features: The same as the primary account relationship Table generally has a time field and a primary key. The difference is that the fields in the text font are used to describe the content of each link (comment reply ).
Interface features: reply text box.
For example, if you reply to a forum (User table-reply table-post table), you may post comments on different posts for multiple times.
Add relationship: whether there is a combination of records, insert, record time and text.
Delete link: delete a link based on the primary key of The Link Table (reply table.

5. Intra-site SMS

Features: The primary and secondary tables are the same, and the Relational Tables generally have time fields and primary keys. The emphasis is on the text fields in the relational tables to describe the content (Message) of each link) or other markup positions are used to indicate the time when the text is read.
Interface features: reply text box.
For example, for intra-site text messages (User table-SMS table-User table), users may send a group of messages or send a single message, with tags indicating the time when the text has been read.
Add relationship: whether there is a combination of records, insert, record time and text.
Delete link: delete a link based on the primary key of The Link Table (reply table.

6. User Friend

Features: The primary and secondary tables are the same, with the same set grouping type. The foreign key combinations of the relational tables do not have repeated records. The Relational Tables generally do not need time fields and primary keys.
Interface features: displays the master table in the same set grouping type, and replaces the simple checkbox or multiple select statements with search, or adds one by one.
For example, a file from a download site (File Table-relational table-file table) can be opened by a software tool. The software tool itself is also a file that can be downloaded. A user's friend is also a user (User table-friend relationship table-User table)
Add link: Same moderator appointment type.
Delete link: Same moderator appointment type.

7. Unknown attribute type

Features: at the initial stage of design, when some field types and names of the primary table are uncertain, the relational table is actually an extensible field of the primary table,
A [primary table] (ID ),
A [attribute name table] (attribute ID. attribute name ),
A [attribute value table] contains three fields:
Attribute Value (attribute value varchar (500 ))
Primary table ID
Property ID

In this way, the minimum redundancy can be achieved.
(Different from common many-to-many relationships: values are stored in varchar, because these values are generally not used for calculation ).

For example:

There is a kind of material in the database design of the military called "War Materials", that is, the materials are seized during war, and the military itself does not know the attributes of these materials.

For example, the seized chemicals include chemical names, general names, whether there is radiation, measurement units, packaging specifications, quantities, and so on, or whether the chemicals are other unknown things.
In this way, you can

Something strange. Attribute Set ["XX strange attribute name"] = "XX strange value ";
A metamorphosis. Attribute Set ["XX abnormal attribute name"] = "XX Abnormal Value ";

Storage in this way.

For example:

There are thousands of mobile phone models. In addition to the common attributes, there are hundreds of different attributes. The attribute names and value types are different. Some mobile phones have this attribute, while others do not.
For such "polymorphism", we adopt the above design structure.
The effect is equivalent:

A strange mobile phone. Property set ["XX strange property name"] = "XX strange value ";
Abnormal mobile phone. Attribute Set ["XX abnormal property name"] = "XX Abnormal Value ";

Interface features: when setting the attributes of a row record in the master table, you need to list all possible attribute names, each corresponding to a text box.

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.