[Practical exercise] paradigm of basic knowledge and principles Series 02- database design and development of database

django web development, has developed static pages basically finished, to develop dynamic web pages (user and the site can input and output interaction), you need to show layers (static pages) -> logical layer (function) -> data layers (database) interact.


And how in the database, database tables, fields, relationships are clear and straightforward case, django create the database is very simple.

However, if the system requirements gathering, analysis, and design a database of the entire database table, fields, relationships, it is very difficult. (Called database design / development)


In database design / development phase stage, we need to follow three paradigm to design the database.


1, database design three paradigms:

First paradigm , data can not be separated.

# Can be understood as a field in which there is only one number, for example, a field can not only store the user name and cell phone number, if you want to keep, you open the two fields exist.

The second paradigm , each table needs to have a unique primary key.

# Because the relational database is required by the primary key to find, such as the name of the same name which may have more than Joe Smith, it can not be used as a primary key, use ××× number.

Third paradigm , which may not have data tables for the non-primary key are dependent.

# For example a table inside ××× number, name, phone number, province of residence, so four fields residence City, No. ××× determined, we will be able to determine the name and phone number, called "name" and "phone number "The only dependence. The "province of residence", "city living" These two fields "××× number" is actually not necessarily dependencies. It would have been no problem, but obviously "living city" is a "province of residence" There are dependent, for example, certainly in Guangzhou City, Guangdong Province under the premise of talent selection. The "province of residence" is not a primary key, so called non-primary key data have dependencies. Therefore, violation of third normal form, the solution is to be the "province of residence", "city living" separate split to another piece of "living surface."


2, data relationships:

As I mentioned before, a relational database, there is a pile of two-dimensional tables relationship, the relationship usually has three cases.

One: direct use of a table, the other fields depend on the primary key;

# Such as a school, a student's name will correspond

Many, many-to: reference a foreign key in a multi inside

# For example, assume that a teacher can only teach a lesson, but a lesson can be more teachers to teach. Then the relationship is many-to-teacher and curriculum, curriculum and teacher relationship is one to many.

In the database design which should be added where the foreign key in a multi-person table (table teacher) inside, citing a person (curriculum) table inside the primary key (Course ID)

# Additional assumptions can be made, for example, a teacher can teach many courses, and a course can only be a teacher in the class, then the relationship between the teacher and the curriculum becomes many, and many-to-curriculum and teacher relationship changed.

Database design, it becomes a multi-person table where the (curriculum) to add foreign key references to a person (teacher meter) primary key (may be a teacher ID, employee job number and the like)

Many to many: this situation can not establish a direct relationship between the two tables, we need to establish a middle table out of thin air, as the connection.

# For example a student can more than elective courses, a course can give more students to take, compared to many to many relationship, which can not be solved by the introduction of a foreign key in the sides.

You must create a middle table, for example, is called "enrollment form", create a primary key (such as serial number / ID), the primary key and foreign key references the primary key table of students, curriculum. The student and associate programs.


3, database table design

We look at django website system development requirements: [practical exercise] Python + Django web development series 02-Django complete development environment deploy https://blog.51cto.com/14423403/2418370

We have students, teachers, curriculum these three categories of objects, first analyze the needs and relationships.

We assume that the system requires that each student can be up to two elective courses, each teacher can teach two courses, each course can only be a teacher in the class. Then such a demand under:

1) the relationship between the student and the course is: many to many;

2) the relationship between the teacher and the curriculum are: one to many;

3) the relationship between students and teachers is: There is no direct relationship, are linked through a course;

Therefore, at least four tables, student tables, table teacher, curriculum, transcript (students and curriculum of the middle table), while the curriculum (and more) which references the teacher table (a) primary key foreign key.


And then design field four tables, think about what information they need to meet our system requirements. (Not null representation can not be null)

Student Table
student ID sno Primary key not null
Full name come off
null
gender Sseksh
null
username susername
null
age sage
null
Where the Department
sdept
null


Teacher table
Number of teachers TNO Primary key not null
Name of Teacher tname
null
Teachers Username tusername
null
job title ttitle
null


Class Schedule
Course No.

Who are

Primary key not null
course name cname
null
credit ccredit
null
class time ctime
null
class locations cplace
null
No. teacher TNO Foreign key not null


Results table
Course No. Who are Foreign key not null
student ID sno Foreign key not null
Achievement cscore
null


Thus, the design / development of the database is complete. Three inspection in accordance with the paradigm of what relationship, if there is a breach, we need to split up again field a violation paradigm, and then depending on whether relationship point of view need to increase the middle of the table. (These are just a very simple case, the actual production system database design / development than this much difficulty, behind the back of the database django web development still go back to these database tables)

Guess you like

Origin blog.51cto.com/14423403/2418782