IT tutorials
 
Database
 

SQL Server 2012 : Data Design Patterns (part 2) - Many-to-Many Pattern

11/26/2013 1:51:19 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Many-to-Many Pattern

In a many-to-many relationship, both sides may relate to multiple tuples (rows) on the other side of the relationship. The many-to-many relationship is common in reality, as shown in the following examples:

  • The classic example is members and groups. A member may belong to multiple groups, and a group may have multiple members.
  • In a typical sales system, an order may contain multiple products, and each product may be sold on multiple orders.
  • In the AdventureWorks2012 sample database, a product may qualify for several special offers, and each special offer may have several qualified products.

In a conceptual diagram, the many-to-many relationship can be diagramed by signifying multiple cardinality at each side of the relationship, as shown in Figure 3.

Figure 3 The many-to-many logical model shows multiple tuples on both ends of the relationship.

7.6

Many-to-many relationships are nearly always optional. For example, the many products-to-many special offers relationship is optional because the product and the special offer are each valid without the other.

The one-to-one and the one-to-many relationship can typically be constructed from items within an organization that users can describe and understand. That's not always the case with many-to-many relationships.

To implement a many-to-many relationship in SQL DDL, a third table, called an associative table (sometimes called a junction table) is used, which artificially creates two one-to-many relationships between the two entities (see Figure 4).

Figure 4 The many-to-many implementation adds an associative table to create artificial one-to-many relationships for both tables.

7.7

Figure 5 shows the associative entity with data to illustrate how it has a foreign key to each of the two many-to-many primary entities. This enables each primary entity to assume a one-to-many relationship with the other entity.

Figure 5 In the associative entity (SpecialOfferProduct), each special offer can be represented multiple times, which creates an artificial one-specialoffer-to-many-product relationship. Likewise, each product can be listed multiple times in the associative entity, creating a one-product-to-many-special relationship.

7.8

In some cases the subject-matter experts can readily recognize the associated table:

  • In the case of the many orders to many products example, the associative entity is the order details entity.
  • A class may have many students, and each student may attend many classes. The associative entity would be recognized as the registration entity.

In other cases an organization might understand that the relationship is a many-to-many relationship, but there's no term to describe the relationship. In this case, the associative entity is still required to resolve the many-to-many relationship — just don't discuss it with the subject-matter experts.

Typically, additional facts and attributes describe the many-to-many relationship. These attributes belong in the associative entity. For example:

  • In the case of the many orders to many products example, the associative entity (order details entity) would include the quantity and sales price attributes.
  • In the members and groups example, the member_groups associative entity might include the datejoined and status attributes.

When designing attributes for associative entities, it's extremely critical that every attribute actually describe only the many-to-many relationship and not one of the primary entities. For example, including a product name describes the product entity and not the many orders to many products relationship.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us