6.MySQL database structure design

The purpose of the data structure design:
  a sample table: Record the selected course information, student number + name of each student-centered curriculum key

  1, reduce data redundancy, the same unnecessary data exists in multiple places
    , such as tables, student's name appears several times with birthday information, course credits are redundant
  2, try to avoid appearing in data maintenance update, insert, and delete abnormal

  • Insert exception: If the table is an entity with another entity exists, as the table need to insert a course, but no students choose this course, then insert a separate course name and course of their information is being given.
  • Update anomalies: If you change a single attribute of an entity in the table, the need for multi-line update. As shown in Table needs to be updated math credits for this course will be updated simultaneously two rows, with the choice of courses students increases, will cause more updated records.
  • Delete Exception: If the table to delete an entity will lead to the disappearance of other entities. As shown in Table, delete the mathematics curriculum will delete multiple records

  3, save data storage space
  4, to improve the efficiency of the query


 Database structure design steps:

  • Requirements Analysis: a comprehensive understanding of storage requirements of product design, data processing requirements, data security and integrity
  • Logical Design: the logical relationship between the logical storage structure design data, data entities, data redundancy and data maintenance to address anomalies
  • Physical design: The design of the database table structure for the characteristics of the used
  • Maintenance optimization: optimize the indexing, storage structure according to the actual situation

Database design paradigm:

  1. The first paradigm: All fields in a database table having only a single attribute for each column by the basic data types are configured, designed tables are simple two-dimensional table.
  2. The second paradigm: only requires a table having a natural key, can not exist in dependence on the part of non-primary key column is the primary key. As shown in Table primary key student number + course name, credits and student number but no direct dependencies, so only a dependency on course name, does not satisfy the second paradigm. Review: a list of individual student information and course information table table, course selection table only need to focus on student number and course name information can be
  3. The third paradigm: Each non-primary property is neither dependent on nor transferred portion dependent on the natural key, i.e. the elimination of non-primary key attributes of the primary transfer dependence on the basis of the second paradigm.

As follows: Student Information table

   College student number can determine where the student, then the student information had any connection with the School of Information dependencies, which violates the third normal form. Students will then retain only the information in the table name of college, College of Information to create a separate table, storage College of Information, the primary key for the college name. In this case the two tables to meet the needs of the third paradigm.

Anti paradigm:
  
the use of space for time, in order to consider the performance and efficiency of reading of the appropriate requirements of database design paradigm is in violation, allowing a small amount of data redundancy
disadvantages design paradigm:
  Advantages: the data redundancy can be minimized , normalization of the update faster than the inverse normalization, normalization tables are generally smaller than the inverse normalization
  disadvantages: the need for the query associated with multiple tables, index optimization more difficult
anti paradigm design disadvantages:
  advantages: the table can be reduced the association, it can better optimize the index
  drawback: there is abnormal data redundancy and data maintenance, modification of data requires more costs


 Physical design:

  According to the characteristics of the selected relational database model to the logical storage structure design
  : Defining database, table and field naming conventions: Follow readability, expressive, long name of the principle of
  selecting the right storage engine: According to the engine applicability scene select the appropriate storage engine according to the
  selection of the appropriate data type field in a table: the column may be selected when a plurality of types of data, priority should be given numeric type, followed by a binary type or a date, and finally the character type. For the same level of data types, you should prefer a smaller footprint data type
Compare Real type:

  • FLOAT 4 bytes, not the exact type
  • DOUBLE 8 bytes, not the exact type
  • DECIMAL every four bytes of digital memory 9, representing a decimal bytes. Such as: DECIMAL (18,9) requires nine bytes of storage

VARCHAR and CHAR type:
  VARCHAR and CHAR storage space is not by character byte
  storage characteristics VARCHAR types:

  • varchar for storing variable-length strings, only the storage space occupied by the necessary
  • The maximum length of the column is less than 255 occupies only one extra byte string length for recording
  • The maximum length of the column is greater than 255 bytes will have to take up two extra string length for recording

VARCHAR length selection:
  use the smallest possible meet the length requirements
  varchar (5) and varchar (200) for the database is to allocate memory based on the actual data length, but in memory using MySQL character width is fixed, the column width too consumes more memory.
VARCHAR application scenarios:

  • The maximum length of the string is much larger than the average column length
  • String columns are rarely updated
  • Using a multi-byte character set is stored string

CHAR types of storage features:

  • CHAR type is fixed length
  • String is stored in a column of type CHAR will delete trailing spaces
  • The maximum width 255 of type CHAR

CHAR type of application scenarios:

  • CHAR type suitable length approximate value is stored
  • Adapted to store a short string of type CHAR
  • String CHAR type suitable column stores updated frequently

Date data type:

DATA TIME:

  In YYYY-MM-DD HH: MM: store SS [fraction] date and time format. DATATIME type when regardless of zone, 8 bytes of storage. Time 1000-01-01 00:00:00 to 9999-12-31 23:59:59

datetime = YYYY-MM-DD HH:MM:SS
datetime(6) = YYYY-MM-DD HH:MM:SS.fraction

TIMESTAMP:

  Depending on the designated time zone, it can be automatically modified when the value of the line data modification timestamp column date and Time:  date type is used to save the date between 1000-01-01 9999-12-31   Time type for storing time data format HH: the MM: the SS DATE types of advantages:




  • Ratio of the number of bytes occupied by the string, datetime, int less storage, use date type requires only 3 bytes
  • Use Date type may also be calculated by using a function of time between the date Date

Note the date and time to store the data:

  • Do not use string data type to store the date and time
  • May be utilized date datetime type is generally smaller than the type of date and time of storage space occupied by the string search filters is performed to compare
  • Datetime type has a rich handler, you can easily calculate the date of type period

 references:

  • High Performance MySQL Third Edition
  • Go On The MySQL database schema: https: //coding.imooc.com/class/chapter/49.html#Anchor

Guess you like

Origin www.cnblogs.com/zhangbLearn/p/12096866.html