当前位置:网站首页>Chapter 2 build CRM project development environment (database design)

Chapter 2 build CRM project development environment (database design)

2022-07-07 17:53:00 Make a light

2.1CRM Database design

2.1.1 Database design principles

We can consider the database design in combination with the project prototype , Marketing activities , user , These need to be persistent , So it needs to be designed as a table . There is usually a certain relationship between tables . Look at the attributes on each creation form .

1、 All tables come from requirements :

    Noun , Especially business-related terms . Clarify the relationship between nouns .

    Conceptual nouns are designed into tables 、 Descriptive nouns are designed as fields .

    Some obscure nouns , Need to be abstract , Such as data dictionary table and data dictionary type table

2、 Field :

       1> Primary key : There are many fields in the table , If a group of fields can uniquely identify a record , You can design this group of fields as primary keys . In general , A field is used as the primary key , And fields without business semantics are used as primary keys . Of course , In some cases, business-related fields are used as primary keys .

      How to generate primary key : Determine the type of the primary key field and the length of the primary key field .

a、 Self increasing : Adopt the primary key generation mechanism provided by database .

         Mysql:auto_increment

         Oracle:sequence

        Due to the common database , Such as Oracle、DB2、SQLServer、MySql etc. , They all provide an easy-to-use primary key generation mechanism (Auto-Increase Field or Sequence). We can use the primary key generation mechanism provided by the database , use generator-class=native How to generate primary key .
        But here's the thing , The primary key generation mechanism provided by some databases may not be the best in efficiency , Massive concurrency insert Data synchronization may cause interlocking between tables .
         Primary key generation mechanism provided by database , Often by saving the current primary key state in an internal table ( For example, for a self increasing primary key , The current maximum value and increment are maintained in this internal table ), After that, the maximum value will be read every time the data is inserted , Then add the increment as the primary key of the new record , Then update the new maximum value back to the internal table , such , once Insert Operation may cause multiple table read and write operations inside the database , At the same time, it is accompanied by data locking and unlocking operation , This has a big impact on performance . therefore , For concurrency Insert A more demanding system , Recommend uuid.hex As a primary key generation mechanism .

b、assigned: External programmers are responsible for generating .

UUID,

hi/lo,

Twitter-Snowflake The background of the algorithm is quite simple , In order to satisfy the Twitter Requests for tens of thousands of messages per second , Each message must be assigned a unique id, these id We need some general order ( Convenient for client sorting ), And in a distributed system, different machines produce id Must be different .

c、 Shared primary key :

d、 Combined the primary key :

     2>、 Foreign keys : Foreign keys are used to represent the relationship between tables . Some run without foreign keys , Is to ensure the integrity of data through procedures .

     3>、 Use of date data char type

 

1,crm The table structure :
  tbl_user   User table

  tbl_dic_type   Data dictionary type table
  tbl_dic_value  Data dictionary values

  tbl_activity   Market activity table
  tbl_activity_remark  Notes to market activities

  tbl_clue       Clue table
  tbl_clue_remark   Clue remarks table

  tbl_clue_activity_relation  Table of the relationship between clues and market activities

  tbl_customer   Customer list
  tbl_customer_remark  Customer remarks table

  tbl_contacts   Contact list
  tbl_contacts_remark Contact notes table

  tbl_contacts_activity_relation Table of the relationship between contacts and marketing activities

  tbl_tran       Deal sheet
  tbl_tran_remark  Transaction notes table
  tbl_tran_history  Transaction history table

  tbl_task   Task list

  1) Primary key field : In the database table , If a group of fields can uniquely identify a record , You can design them as the primary key fields of the table .
              It is recommended to use a field as the primary key , And it is recommended to use fields without business meaning as primary keys , such as :id etc. .

          The type and length of the primary key field are determined by the generation method of the primary key value :
                   How to generate primary key values :
               1) Self increasing : With the help of the database's own primary key generation mechanism
                       Numerical type The length is determined by the amount of data

                   Low operating efficiency
                   High development efficiency
               2)assighed: Programmers manually generate primary key values , The only one is not empty , Algorithm .
                       hi/low: Numerical type The length depends on the amount of data
                   UUID: character string The length is 32 position
               3) Shared primary key : Determined by the type and length of another table
                       tbl_person         tbl_card
                   id     name        id     name
                   1001   zs          1001    card1
                   1002   ls
               4) Combined the primary key : Determined by the type and length of multiple fields
   2) Foreign key field : Used to determine the relationship between tables .
              1) One to many : A watch (A) A record in can correspond to another table (B) Multiple records in ;
                    The other table (B) A record in can only correspond to one table (A) One of the records .
            A(1)---------B(n)
             Parent table          Sub table
            tbl_student                    tbl_class
            id      name class_id          id     name
            1001    zs    111              111    class1
            1002    ls    111              222    class2
            1003    ww    222
            1004    zl    

             When adding data , First add the parent table record , Add sub table records ;
             When deleting data , Delete the sub table record first , Then delete the parent table record ;
             When querying data , Association query may be performed :
            // Check the names of all students surnamed Zhang id,name And your class name
            select s.id,s.name,c.name as className
            from tbl_student s
            join tbl_class c on s.class_id=c.id// If the foreign key cannot be empty
            where s.name like 'z%'

             Internal connection : Query all qualified data , And the results are required to have corresponding records in both tables
             The left outer join : Query all qualified data in the table on the left , Even if there is no corresponding record in the table on the right
                     
                * If the foreign key cannot be empty , Internal connections are preferred ;
             If the foreign key can be empty ,
                               -- Suppose you only need to query those records that have corresponding records in another table , Use internal connections
                       -- If you need to query all the qualified records in the table on the left , Use left outer connection .
        2) one-on-one : A watch (A) A record in can only correspond to another table (B) One of the records ;
                  The other table (B) A record in can only correspond to one table (A) One of the records .
            tbl_person         tbl_card
            id     name        id     name
            1001   zs          1001    card1
               a) Shared primary key :( Not recommended )
                 Add data : First add the generated table , The table records generated later
             Delete data : Table records generated after deletion , Then delete the table record generated first
             Query data : No connection query is required
                       // Inquire about zhangsan Driver's license information   1001
                   select *
                   from tbl_card
                   where id='1001'
                       b) The only foreign key :
                 tbl_person             tbl_card
             id     name            id     name     person_id( Uniqueness constraint )
             1001   zs              111    card1    1001
             1002   ls              222    card2    1002
             1003   ww              333    card3    1003
             * One to one is a special one to many .
             * The operation is exactly the same as one to many .
         3) Many to many : A watch (A) A record in can correspond to another table (B) Multiple records in ;
                   The other table (B) A record in can also correspond to a table (A) Multiple records in .
               tbl_student                    tbl_course
               id     name                    id     name   
               1001   zs                      111    java   
               1002   ls                      222    mysql  
                       tbl_student_course_relation
                    student_id     course_id
                1001            111
                1001            222
                1002            111
                1002            222
               When adding data , First add the parent table record (tbl_student,tbl_course), Add sub table (tbl_student_course_relation) Record ;
               When deleting data , Delete the sub table record first (tbl_student_course_relation), Then delete the parent table record (tbl_student,tbl_course)
               When querying data , Association query may be performed :
                  // Check the names of all students surnamed Zhang id,name, And the... Of the selected course name
              select s.id,s.name,c.name as courseName
              from tbl_student s
                          join tbl_student_course_relation scr on s.id=scr.student_id
                          join tbl_course c on scr.course_id=c.id
              where s.name like 'z%'
  3) Give fields about date and time :
     Are handled as strings :
     char(10)   yyyy-MM-dd
     char(19)   yyyy-MM-dd HH:mm:ss

2, establish crm Database instance of :
  hold sql Script import database instance :

3, Build development environment :
  1) Create project :crm-project
              Set up JDK.
    Create a project :crm
              Complete directory structure :
  2) add to jar package

  

原网站

版权声明
本文为[Make a light]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071523277416.html