Java Database design 14 tips

Source: Internet
Author: User

Java Database Design - a trick

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
Generally, an entity cannot have no primary key and no 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
A base table differs from an intermediate table, a temporary table, because it has the following four attributes:
(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.Paradigm Standard
The relationship between the base 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 Rose2002, you specify two types of columns: data columns and computed columns. Columns such as amounts are referred to as computed columns, and columns such as unit price and quantity are referred to as data columns.

Table 1 table structure of the commodity table
Product name commodity model Unit Price quantity amount
TV 29 "2,500 40 100,000
  
5.to understand three paradigms in a popular way
A popular understanding of the three paradigms is of great benefit to database design. In the database design, in order to better apply three paradigms, it must be understood in a popular way
Three paradigms (popular understanding is sufficient understanding, 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 field redundancy, that is, any field cannot be derived from another field, it requires no redundancy in the field.

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.be good at identifying and correctly dealing with many-to-many relationships
This relationship should be eliminated if there is a many-to-many relationship between the two entities. 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".

7.PRIMARY KeyPKthe method of taking value
PK is an inter-table connection tool for programmers, which can be a string of numbers with no 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. But the 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 data redundancy
The repetition of the primary key and the foreign key in multiple tables is not data redundancy, and the concept must be clear, in fact many people are 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--rFigure no standard answer
The E--r diagram of information system has no standard answer, because its design and drawing is not unique, as long as it covers the business scope and function content of the system requirement, it 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.

.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?

One by one .intermediate tables, reports, and temporary tables
The intermediate table is the table that holds the statistics, which is designed for the data warehouse, the output report, or the query results, and sometimes it has no primary key and foreign key (except for the Data Warehouse). Temporary tables are designed by programmers to store temporary records that are used by individuals. 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.

.integrity constraints are represented in three ways
Domain Integrity: Use Check to implement constraints, and in the database design tool, when defining a field's range of values, there is a check button that defines the field's domain value.
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.

.the way to prevent database design patching is"Three less principles"
(1) The smaller the number of 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 patching-style design;

(2) The fewer fields in a table combine primary keys, the better. Because of the role of the primary key, one is to build the primary key index, the second is to do as a sub-table foreign key, so the combination of the number of primary key fields is less, not only saves the running time, but also saves the index storage space;

(3) The smaller the number of 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 "three less" principle is to prevent readers from using patching technology, constantly adding and removing the database, making the enterprise database become a random design of the database table "garbage heap", or database table "clump", and finally caused the database of basic tables, code tables, intermediate tables, temporary tables disorganized, countless, Cause the information system of enterprises and institutions 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.

.ways to improve the efficiency of database operation
Under the condition of the given system hardware and system software, the way to improve the operation 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 segmentation is done by dividing the table's records horizontally into two tables with a value of the primary key PK for that table as the line. 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.

Java Database design 14 tips

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.