MySQL database design specification 1, the database naming specification uses 26 English letters (case-sensitive) and 0-9 natural number (often do not need) with underscore ' _ ' composition; The name is concise and clear (the length cannot exceed 30 characters); For example: User, stat, log, can also Wifi_user, Wifi_stat, Wifi_log to the database to add a prefix; Unless the backup database can add 0-9 of the natural number: user_db_20151210; 2, the database table name naming specification uses 26 English letters (case-sensitive) and 0-9 natural numbers (often do not need) with the underscore ' _ ' composition; The name is concise and clear, and multiple words are separated by the underscore ' _ '; For example: User_login, User_profile, User_detail, User_role, User_role_relation, User_role_right, User_role_right_relat The ion table prefix ' User_ ' can effectively show the tables of the same relationship together; 3, the database table field name specification uses 26 English letters (case-sensitive) and 0-9 of the natural number (often do not need) with an underscore ' _ ' composition; The name is concise and clear, and multiple words are separated by the underscore ' _ '; For example: User_login table field user_id, user_name, Pass_word, Eamil, TickIT, status, Mobile, add_time; There must be a self-increment primary key in each table, and the Add_time (default system time) table has the same name as the associated field names between tables; 4, the Database table field type specification with as few storage space as possible to save the data of a field; For example, if you can use int instead of varchar, char, and varchar (16), do not use varchar (256); The IP address preferably uses the int type; Fixed-length types are best used with char, for example: postal code; Do not use Smallint,int if you can use tinyint; It is best to give each field a default value, preferably not null;5, the database table index specification is concise and clear, for example: User_login table user_name The index of the field should be User_name_index unique index; Create a primary key index for each table; Create a reasonable index for each table; Please be careful when building composite indexes. 6, simple familiar with the first paradigm of database paradigm (1NF): The field values are atomic and can no longer be divided (all relational database systems are satisfied with the first normal form); For example: Last Name field, where the last name and first name are a whole, if you distinguish the first and last name then you must set up two separate fields; Second Normal (2NF): A table must have a primary key, that is, each row of data can be uniquely differentiated; Note: First paradigm must be satisfied first; Third paradigm (3NF): A table cannot contain information about non-critical fields in other related tables, that is, the data table cannot have a sink field; Note: The second paradigm must be satisfied first; Note: Often we do not adhere to the third paradigm in the design table, because the reasonable sink residual field will give us to reduce the join query; For example: The album table will be added to the image of the click Number field, in the album Picture table will also add the image of the click Number field; MySQL Database design principles 1, core principles do not operate in the database; CPU computing must be moved to the business layer; Number of control columns (field few but good, number of fields recommended within 20); Balance paradigm and redundancy (efficiency first; often sacrificing paradigm) rejecting 3B (rejecting large SQL statements: Big SQL, rejecting large things: big transaction, rejecting Mass: big batch); 2, the field class principle with a good value type (with the appropriate field type to save space); Convert characters into numbers (the best conversions that can be transformed, as well as saving space and improving query performance); Avoid using null fields (Null fields are difficult to query for optimizations, NULL field indexes require extra space, invalid composite index for null fields); Less text type (use varchar instead of the text field as much as possible); 3. Index class principle reasonable use index (improve query, slow update, index must not be more better); Character fields must be prefixedIndex Do not perform column operations in the index; InnoDB the primary key is recommended to use the self-increment column (the primary key to establish the clustered index, the primary key should not be modified, the string should not be the master key) (Understand the InnoDB index preservation structure to know); No foreign keys (guaranteed by the program); 4, the SQL class principle SQL statement as simple as possible (a SQL can only be in a CPU operation, large statement to split small statements, reduce lock time, a large SQL can block the entire library); A simple transaction; Avoid using TRIG/FUNC (triggers, functions are not replaced by client programs); No SELECT * (Consumes Cpu,io, memory, bandwidth, this program is not extensible); or overwrite to in (or efficiency is n level); or rewrite to union (MySQL index merge is mentally retarded); Select ID from t where phone = ' 159′or name = ' John '; = = Select id from t where phone= ' 159′union select ID from t where name= ' Jonh ' Avoid negative to%; Use COUNT (*) with caution; Limit efficient paging (the greater the limit, the less efficient); Use UNION ALL instead of union (union has a de-heavy cost); Use less connection join; Use GROUP by; Please use the same type comparison; Break up batch update; 5. Performance analysis tool show profile; Mysqlsla; Mysqldumpslow; Explain Show slow log; Show Processlist;
Principles of Database design
1. The relationship between the original document and the entity can be a pair of one or one-to-many, many-to-many relationships. In general, they are one-to-one relationships: a single original document corresponds to and corresponds to only an entity. In special cases, they may be one-to-many or many-to-one relationships, that is, a single original document corresponds to multiple entities, or multiple original documents corresponding to an entity. The entities here can be understood as basic tables. Clear this correspondence, to our design input interface is very good. Example 1〗: An employee biographical data, in the Human Resources information System, the corresponding three basic tables: Employee basic Situation table, social relations table, work Resume table. This is a typical example of "a single original document corresponding to multiple entities". 2. Primary key and foreign key in general, an entity cannot have no primary key or foreign key. In a e-r diagram, an entity in the leaf area can define a primary key or not define a primary key (because it has no descendants), but must have a foreign key (because it has a father). The design of primary key and foreign key plays an important role in the design of global database. When the design of the global database was completed, an American database design expert said: "Key, Everywhere is the key, in addition to the key, nothing", this is his database design experience, but also reflects his information system core (data model) of the highly abstract ideas. Because: The primary key is the height abstraction of the entity, and the primary key is paired with the foreign key, representing the connection between the entities. 3. The nature of the basic table differs from the intermediate table and the temporary table because it has the following four characteristics: (1) atomicity. The fields in the base table are non-biodegradable. (2) Primitive nature. The records in the base table are the records of the original data (the underlying data). (3) Deductive nature. All the output data can be derived from the base table and the data in the Code table. (4) stability. The structure of the base table is relatively stable, and the records in the table are stored for a long time. After you understand the nature of the base table, you can differentiate the base table from the intermediate and temporal tables when you design the database. 4. The relationship between the standard basic table and its fields should satisfy the third paradigm as much as possible. However, the design of the database that satisfies the third paradigm is often not the best design. In order to improve the efficiency of database operation, it is often necessary to reduce the standard of normalization: to increase redundancy appropriately and to achieve the purpose of space-changing time. Example 2〗: There is a basic table for storing goods, as shown in table 1. The existence of the "Amount" field indicates that the design of the table does not satisfy the third paradigm, since "amount" can be obtained by multiplying the "unit price" by "quantity", stating that "amount" is a redundant field. However, increasing the "amount" of this redundant field can increase the speed of query statistics, which is the practice of space-changing time. In Rose 2002, it is stipulated that there are two types of columns: data columns and computed columns. "Amount" thisColumns are called "Computed columns", and columns such as "Unit Price" and "quantity" are referred to as "data columns." Table 1 table structure of commodity table commodity name commodity model price quantity amount TV 29 inch 2,500 40 100,000 5. A popular understanding of the three paradigms to understand the three paradigms in a popular way is of great benefit to database design. In the database design, in order to better apply three paradigms, it is necessary to understand the three paradigms (popular understanding is enough to understand, not the most scientific and accurate understanding): The first paradigm: 1NF is an atomic constraint on attributes, requiring attributes to be atomic and non-decomposed; The second paradigm: 2NF is a unique constraint on records, requiring records to have a unique identity, that is, the uniqueness of the entity; the third paradigm: 3NF is a constraint on the redundancy of the field, that is, any field cannot be derived from another field, it requires that the field is not redundant. There is no redundant database design to do. However, a database without redundancy is not necessarily the best database, and sometimes in order to improve operational efficiency, it is necessary to lower the paradigm standard and properly retain redundant data. The practice is to adhere to the third paradigm when designing the conceptual data model, and to lower the standard of normalization into the design of the physical data model. Lowering the paradigm is adding fields, allowing redundancy. 6. To be good at identifying and correctly dealing with many-to-many relationships if there are many-to-many relationships between two entities, this relationship should be eliminated. The solution is to add a third entity between the two. In this way, the original a many-to-many relationship, now becomes two one-to-many relationship. To properly assign the original two entity's attributes to three entities. The third entity here is essentially a more complex relationship that corresponds to a basic table. Generally speaking, the database design tool does not recognize many-to-many relationships, but it can handle many-to-many relationships. Example 3: In "Library information System", "book" is an entity, "reader" is also an entity. The relationship between these two entities is a typical many-to-many relationship: A book can be borrowed by multiple readers at different times, and a reader can borrow more books. To do this, add a third entity between the two, the entity is named "borrowed book", its properties are: Borrow time, borrow also sign (0 means borrowing, 1 means return), in addition, it should also have two foreign keys ("books" The primary Key, "reader" of the primary key), so that it can be connected with the "book" and "Reader". Gaze: Book 1 and the entity named "borrowing books" n Reader 1 and the entity named "borrowing book" N 7. The method of the primary key PK is an inter-table connection tool for programmers, which can be a digital string without physical meaning, which is implemented automatically by the program. It can also be a combination of field names or field names that have a physical meaning. NoThe former is better than the latter. When PK is a combination of field names, the number of suggested fields should not be too many, more not only the index occupies a large space, and the speed is slow. 8. Correct understanding of the data redundancy primary key and the foreign key in the multi-table repetition, does not belong to the data redundancy, this concept must be clear, in fact many people still unclear. The duplication of non-key fields is the data redundancy! And is a kind of low-level redundancy, that is, repetitive redundancy. Advanced redundancy is not a recurring occurrence of a field, but a derivation of a field. Example 4〗: "Unit price, quantity, amount" of three fields in a commodity, "amount" is derived from "unit price" multiplied by "quantity", it is redundant, and it is a kind of advanced redundancy. The purpose of redundancy is to improve processing speed. Only low-level redundancy increases the inconsistency of the data, because the same data can be entered multiple times, from different times, places, and roles. Therefore, we advocate advanced redundancy (derived redundancy) against low-level redundancy (repetitive redundancy). 9. E--r diagram There is no standard answer to the E--r map of the information system, because its design and painting is not unique, as long as it covers the system requirements of the business scope and functional content, is feasible. Conversely, to modify the E--r diagram. Although it does not have the only standard answer, it does not mean that it can be arbitrarily designed. The standard of good E-r chart is: The structure is clear, the association is concise, the number of entities is moderate, the attribute allocation is reasonable, there is no low level redundancy. 10. View technology is useful in database design unlike basic tables, code tables, and intermediate tables, a view is a virtual table that relies on a real table of data sources. View is a window for programmers to use the database, is a form of synthesis of the base table data, is a method of data processing, is a means of user data privacy. In order to perform complex processing, increase computation speed and save storage space, the definition depth of the view should not exceed three layers. If the three-tier view is still not enough, you should define a temporary table on the view and then define the view on the temporary table. With this iterative definition, the depth of the view is not restricted. The role of views is more important for certain information systems related to national political, economic, technical, military and security interests. Once the basic tables of these systems have been physically designed, a first-level view is immediately established on the base table, which has the same number and structure as the number and structure of the base table. It also stipulates that all programmers are only allowed to operate on the view. Only the database administrator, with the "security key" shared by multiple people, can operate directly on the base table. Let the reader think: why is this? 11. Intermediate tables, reports, and staging tables are tables that hold statistics, which are designed for data warehouses, output reports, or query results, sometimes without primary keys and foreign keys (except data warehouses). Temporary tables are designed by programmers and storedTemporary records, for personal use. The base table and the intermediate table are maintained by the DBA, and the temporary tables are automatically maintained by the programmer themselves using the program. 12. Integrity constraints are manifested in three aspects of domain integrity: Check to implement constraints, in the database design tool, the field value range is defined, there is a check button to define the value of the field of the city. Referential integrity: The use of PK, FK, table-level triggers to achieve. User-defined integrity: It is a business rule that is implemented with stored procedures and triggers. 13. The way to prevent database design patching is "Three less Principles" (1) the fewer tables in a database the better. Only the number of tables, can explain the system e--r diagram few but good, remove the redundant entities, formed a high degree of abstraction of the objective world, the system of data integration, to prevent the patch-style design; (2) The fewer fields in a table combine primary keys, the better. Because the primary key function, one is to construct the primary key index, and the second is the foreign key of the sub-table, so the number of fields of the combined primary key is less, not only saves the running time, but also saves the index storage space; (3) The fewer fields in a table the better. Only the number of fields is small, to show that there is no duplication of data in the system, and there is little data redundancy, and more importantly, to urge readers to learn "column to row", so that the child table is prevented from pulling into the main table, leaving a lot of spare fields in the main table. The so-called "column-to-row" is to pull some of the contents of the main table and create a separate child table. This method is very simple, some people are not accustomed to, do not adopt, do not execute. The practical principle of database design is to find the right balance between data redundancy and processing speed. "Three little" is a whole concept, a comprehensive view, can not isolate a certain principle. The principle is relative, not absolute. The "more than three" principle is certainly wrong. Imagine: If the same functionality is covered by the system, the e--r graph of 100 entities (1000 properties) is certainly much better than the E--r diagram of 200 entities (2000 properties). Advocating the principle of "three little" is called the reader to learn to use the database design technology for system data integration. The steps of data integration are to integrate the file system into the application database, integrate the application database into a subject database, and integrate the subject database into a global consolidated database. The higher the degree of integration, the more data sharing, the less information island phenomenon, the whole enterprise information System of the global E-r diagram of the number of entities, the number of primary keys, the number of attributes will be less. The purpose of advocating the principle of "three little" is to prevent readers from using patching technology, constantly adding and deleting databases, making the enterprise database become the "garbage heap" of arbitrary design database table, or "clump" of database table, and finally causeThe basic tables, code tables, intermediate tables, temporary tables in the database are disorganized, countless (that is, the dynamic creation of tables and increase the number of tables), resulting in information systems can not be maintained and paralyzed. "More than three" principle anyone can do, the principle is "patching method" design database crooked Science said. "Three less" principle is the principle of few but good, it requires a high degree of database design skills and art, not anyone can do, because the principle is to eliminate the "patching method" design database theory. 14. Ways to improve the efficiency of database operation in a given system hardware and system software conditions, the way to improve the operational efficiency of the database system is: (1) in the database physical design, reduce the paradigm, increase redundancy, less use of triggers, multi-use stored procedures. (2) When the calculation is very complex, and the number of records is very large (for example, 10 million), the complex calculation is first outside the database, in the file system in the C + + language after the processing is completed, the final storage is appended to the table. This is the experience of telecom billing system design. (3) If a table is found to have too many records, such as more than 10 million, the table is split horizontally. Horizontal splitting is done by dividing the table's records horizontally into two tables with a value of the table's primary key PK (that is, the table maintains table rows that are too large to manually split into two views of the two-table union to be transparent to a program). If you find that there are too many fields for a table, such as more than 80, split the table vertically and break the original table into two tables. (4) The database management system DBMS system optimization, that is, the optimization of various system parameters, such as the number of buffers. (5) When using the data-oriented SQL language for programming, the optimization algorithm should be taken as far as possible. In a word, in order to improve the efficiency of database operation, we must optimize the database system level, the database design level and the program implementation level, and work hard at the same time on three levels. The above 14 skills, is a lot of people in a large number of database analysis and design practice, gradually summed up. The use of these experiences, readers can not help hard sets, rote memorization, and to digest understanding, pragmatic, flexible grasp. and gradually achieve: in the application of development, in the development of the application. Reprinted from: Http://www.javaeye.com/topic/281611=================================denormalization How to explain in database? Give me an example. 2008-04-01 21:16 Dictionary: Reverse normalization, blocking normalization is what we usually call inverse normalization. For example, set two primary keys in a table. For example, the relationship between two tables is a many-to-many relationship. And so it's all against the standard paradigm.Standardizing or inverse normalization is all designed to improve database performance. Beginners should try to standardize well.
SQL Design Specification