How should we design a database (3) (continued) (1)

Source: Internet
Author: User

After writing the previous blog "how to design a database (iii)", I deeply felt that I was not clearly written. It took a lot of space to describe the problem, however, the solution to the problem and its advantages and disadvantages were taken over by us, so we wrote this blog to apologize.

Download Sample Code: Click here to download

The sample code is described below

First, let's review the problems described in "how to design a database (3:

Now there is a system. We assume it is a course selection system.

The system should be sold by school. The course selection logic of each school is the same, while the data in the table is common but also different. For example, the basic structure of the Teacher table is as follows:

Now we sell the system to school. In addition to the username and password, the Teacher table in school A also stores the FirstName and LastName of the instructor. The table structure changes as follows:

School B also buys our system. Their Teacher table should not be FirstName or LastName, but should store the instructor's employee ID "Number". The table structure is as follows:

Okay, now we have a problem: how can we solve this difference?

There are roughly three solutions

1. Adding redundancy to tables

2. Add redundant tables

3. model inheritance

First thought:Redundancy is added to the table, which has been analyzed in the previous article. The advantages and disadvantages are described here.

The advantage is simplicity: simple thinking and easy implementation

The disadvantage is that maintenance is difficult: ① if our system sells many schools, it is a nightmare for system maintenance personnel. ② violation of open and closed: every time you add a field, you have to modify the existing Model.

The second approach:Add redundant tables

As Xu shaoxia said in the previous article, we can also design it like this and use a redundant table to store different fields ,:

FirstName, LastName, and Number correspond to the three rows in the extended table.

The advantage is that the idea is simple and the implementation is more complicated than the first one, but not complicated.

Disadvantages: ① Join.

② Open or closed: Modify the existing Model for each field added.

Xu shaoxia also said the disadvantage in his message: it is hard to query data. It can only be used in areas with low read/write pressure .)

The third approach:Model inheritance

This method is suitable for ORM or Code First ORM.

Here, the recommended EF5EF5 environment is. NET 4.5, But. NET 4 is also harmless)

 
 
  1. public class Identifier  
  2.     {  
  3.         [Key]  
  4.         public int ID { get; set; }  
  5.     }  
  6.  
  7.     public interface Contact  
  8.     {  
  9.         string Phone { get; set; }  
  10.         string Email { get; set; }  
  11.     }  
  12.  
  13.    public class TeacherBase : Identifier, Contact  
  14.     {  
  15.         [StringLength(50)]  
  16.         public string UserName { get; set; }  
  17.  
  18.         [StringLength(50)]  
  19.         public string Pwd { get; set; }  
  20.  
  21.         [StringLength(50)]  
  22.         public string Phone { get; set; }  
  23.  
  24.         [StringLength(50)]  
  25.         public string Email { get; set; }  
  26.     } 

This is our basic Teacher table.

Then our system is sold to school A. The Teacher table of school A has two different fields: FirstName and LastName. The Model should be written in this way.

 
 
  1. namespace Model.A  
  2. {  
  3.     public class Teacher : TeacherBase  
  4.     {  
  5.         [StringLength(50)]  
  6.         public string FirstName { get; set; }  
  7.  
  8.         [StringLength(50)]  
  9.         public string LastName { get; set; }  
  10.     }  

Generated database

Then the system was sold to B. B's school system is deployed independently. The Difference Field in the Teacher table of school B is Number:

 
 
  1. namespace Model.B  
  2. {  
  3.     public class Teacher : TeacherBase  
  4.     {  
  5.         [StringLength(50)]  
  6.         public string Number { get; set; }  
  7.     }  

Generated database


Related Article

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.