[Notes] python MySQL database design (basic)

[Notes] python MySQL database design (basic) 

Some basic conceptual design of databases and other notes

 

  • Relational database built on the basis of ER model, we need to design planning product manager, extracted model and the relationship to develop a table structure, which is the first step in the beginning of the project
  • There are many design software database in development, such as the commonly used power designer, db desinger, these software can visually see the relationships between entities and entity
  • Database design, may be done by specialized personnel database design, it could be done by the development team members, the general manager to lead the project team members to complete

 

Three paradigms

  • After research and a summary of use in question, for the design of the database made a number of specifications, which are called paradigm (Normal Form)
  • Currently there are eight kinds of discernable paradigm, the paradigm can generally be subject to 3
  • first paradigm (1NF) : emphasized that the atomic columns , i.e., columns not be subdivided into several other column, the column can not be divided .

    Consider this table: [contact] (name, gender, telephone) if the actual scenario, there is a contact home phone and business phone, then this table structure design is not reached 1NF. We just need to comply with the 1NF column (telephone) split, namely: [contact] (name, gender, home phone, business phone). 1NF good discrimination, but 2NF and 3NF it is easy to confuse.

  • a second paradigm (2NF) : First 1NF, further comprising two parts, one table must have a primary key ; the second is not contained in the primary key must be entirely dependent on the primary key , but can not rely only on a part of the primary key.

    Consider an Order Details: OrderDetail [] (OrderID, ProductID, UnitPrice, Discount , Quantity, ProductName). Because we know that the order can be ordered in a variety of products, a OrderID (order number) is not uniquely distinguish each record, it just is not enough to become a OrderID primary key, ProductID empathy. Primary key should be (OrderID, ProductID). Apparent from the Discount (discounts), Quantity (amount) is completely dependent (dependent) to primary key (OderID, ProductID), and UnitPrice, ProductName only depends on the ProductID, i.e. dependent only on the part of the primary key. So OrderDetail table does not meet the 2NF. The design does not meet the 2NF prone redundant data.

    [] Table can OrderDetail split into [OrderDetail] (OrderID, ProductID, Discount, Quantity ) , and [Product] (ProductID, UnitPrice, ProductName) to eliminate the original Orders table UnitPrice, ProductName repeated situation.

  • third normal form (3NF) : First, 2NF, additional non-primary key column must be directly dependent on the primary key, can not rely on the presence of transfer . I.e. not exist: A case of non-primary key column depends on the non-primary key columns B, non-primary key column B is dependent on the primary key.

    Consider an order form] [Order (OrderID, OrderDate, CustomerID, CustomerName , CustomerAddr, CustomerCity) primary key is (OrderID) . Wherein OrderDate, CustomerID, CustomerName, CustomerAddr, CustomerCity other non-primary key column are fully dependent on the primary key (OrderID), so the compliance 2NF. But the problem is CustomerName, CustomerAddr, CustomerCity is directly dependent on the CustomerID (non-primary key columns), rather than directly dependent on the primary key, which is passed through only dependent on the primary key, so do not meet 3NF . By resolution of [] Order [] Order (OrderID, OrderDate, CustomerID) and [] Customer (CustomerID, CustomerName, CustomerAddr, CustomerCity ) to achieve 3NF. * Concept (2NF), and a third paradigm (3NF) a second paradigm can easily be confused, distinguish them key point is, 2NF: non-primary key column is entirely dependent on the primary key, or dependent on the part of the primary key; 3NF: non-primary key column It is directly dependent on the primary key, or depend directly on the non-primary key column.

 

ER model

  • E represents entry, entity, design entity defined as a class, specify what the object is described, an entity is converted to a table in the database
  • Relationship R represents a relationship, the relationship between the description of the corresponding rule two entities, including the type of relationship comprises one, one-to-many
  • A data relationship is also required by a field stored in a table

 

1. The entity A to the entity B is 1 to 1:

I.e., a record A table record corresponding to a table B, create a primary key field, is stored in another table of Table A or Table B.

Example: a city has a team, a team corresponding to a city.

2. The entity A to the entity B is more than one pair :

A table that is a table of data corresponding to a plurality of data B, A needs to be the primary key table as a foreign key B of table. Create a field, the primary key values ​​stored in Table A Table B.

Example: a class corresponding to a plurality of students.

3. The entity B to entity A many-

Table C requires an intermediate their relationship to the corresponding (requires a table to map their relationship). In this case, A, B table does not need other foreign keys. Just have their own primary key on the line, just as a separate table. I.e. new C a table, the table has only two fields, one for the primary key value A is stored, a primary key value for B is stored.

For example: the relationship between students and elective courses, a student can select multiple electives, and each elective and can be more than students can choose.

 

Tombstone

  • For important data, do not want to physically remove, once removed, data can not be retrieved
  • Delete Program: Set isDelete column is bit, showing a tombstone, the default value is 0
  • For non-critical data can be physically deleted
  • The importance of the data, to determine the actual development

Example:

  • Design two tables: Table class, students watch
  • Class table classes
    • id
    • name
    • isdelete
  • Students watch students
    • id
    • name
    • birthday
    • gender
    • clsid
    • isdelete

 

 

--------end----------

Published 50 original articles · won praise 10 · views 6610

Guess you like

Origin blog.csdn.net/qq_23996069/article/details/104419787