[Database Principle] Conceptual structure, logical structure design case

Conceptual structural design steps in macro.

According to the case data flow diagram DFD and data dictionary DD:

  • Build a local ER model
  • Combine and optimize multiple local ER models
  • Get the global ER model

Partial ER model design.

Taking the relationship between teachers and students in the school as an example, analyzing the data structure involved and the relationship between multiple data structures, we can get the semantic agreement between entities as shown below:

  • A student can take multiple courses, and a course can be taken by multiple students, so the course selection relationship between the course entity and the student entity is a 1:n relationship;
  • A department can have multiple students, and a student can only belong to one department, so the belonging relationship between the student entity and the department entity is an n:1 relationship;
  • A department can offer multiple courses, but a course can only be offered by one department, so the opening relationship between the department entity and the course entity is a 1:n relationship;
  • A teacher can teach multiple courses, and a course can also be taught by multiple teachers. Therefore, the relationship between the course entity and the teacher entity is an m:n relationship;
  • A department can hire multiple teachers, but a teacher can only be hired by one department, so the employment relationship between the department entity and the teacher entity is a 1:n relationship;

In the above-mentioned reasonable semantic convention, the involved data structure is transformed into the entity in the ER diagram, and the involved relationship is transformed into the relationship in the ER diagram. The partial ER diagram obtained is as follows:
Insert picture description here
Insert picture description here

ER diagram conflicts.

1. Attribute conflict.

  • [ Value range conflict ] The attribute type, value range, or value set are different. Taking the student ID as an example, it can be defined as a numeric type or a character type; another example is age, which can be the year of birth. The month implies that the age can also be expressed as a numerical value;
  • [ Value Unit Conflict ] For example, the weight of students can be measured in Kg or g.

Attribute conflicts belong to the user’s business agreement, and they can be resolved after negotiation with the user. For example, the student ID of the student is unified as a character type, because it may include A, B, and C, and the weight is uniformly measured in the unit of T because the student is not willing See that your weight is a big number.

2. Naming conflict.

The naming conflicts may occur between entity names, attribute names, and contact names. The most common conflicts between attribute names are mostly the semantic ambiguity of the same name or the same name. I don’t know how the language is learned. of.

  • [ Same name with different meanings ] originated from the profoundness of Chinese culture, such as the attribute "unit", which in some cases represents a person's organization; in other application environments, it also represents a benchmark for measuring certain attributes of an object;
  • [ Synonyms with synonyms ] Once again derived from the profoundness of Chinese culture, for example, my mobile phone number is 12345678999, it can be called "contact", it can also be called "mobile phone number", it can also be called " Pickup certificate".

Naming conflicts are similar to attribute conflicts and need to be resolved after consultation with the user.

3. Structural conflict.

  • The same object has different abstractions in different applications. It may be both an entity and an attribute. For example, a department as an entity representing a certain major in a school can also be one of the attributes of a student entity.
    When resolving such conflicts, it is necessary to unify the abstraction of the object, or transform the entity into an attribute, or transform the attribute into an entity.
  • The same entity has different composition properties in different application scenarios.
    The solution is to unify the attribute set as the union of the attribute sets in each local ER graph.
  • The same connection presents different types in different application scenarios. It may be a 1:n connection in a partial ER diagram, and a 1:1 connection in another ER diagram.
    When resolving such conflicts, the connection needs to be adjusted according to semantics.

Incorporate local ER models.

First of all, we found that there is a phenomenon of [ synonymous ] entities in the partial ER diagram of student selection and the partial ER diagram of teacher's class. The [ department ] entity of the former and the [ unit ] entity of the latter actually represent the college’s A certain profession, and the two entities still have conflicts of attribute composition. The attributes [ name ] and [ unit name ] are also synonymous with different names of attributes, and need to be unified and the attribute set is merged.
Observe again and find that there is a structural conflict between the [ course ] entities in the two partial graphs, and their attribute sets are inconsistent, requiring a union operation.
After resolving the above conflicts, the preliminary ER diagram obtained is as follows:
Insert picture description here

ER diagram redundancy.

Redundancy is divided into data redundancy and entity connection redundancy.

  • [ Data Redundancy ] Data that can be derived from basic data, such as the individual scores and average scores of the students taking each course. The latter is the redundant data that can be derived from the former.
  • [ Relationship Redundancy ] Connections that can be derived from other connections. For example, Alipay provides shared bicycles, shared bicycles to facilitate humans, and Alipay serves humans. The third connection is the redundant connection that can be derived from the first two connections. It's not advertising, or I can't think of a good example.

The method of eliminating redundancy is based on analysis, which is the meaning of the previous database standardization theory.

Optimize the preliminary ER diagram.

There are redundant data and redundant connections in the preliminary ER diagram given above. For example, the attribute of teacher number appears once in the teacher entity, and once again in the course entity (think carefully and find the course entity, and There is no necessary connection between teacher numbers. Different teachers teach the same course every year, and each teacher teaches different courses); the average grade attribute of students can be obtained by processing the grade attribute of the elective relationship. [ Teacher teaches courses ] and [ Teacher belongs to department ] The two connections can deduce the connection [ Department offers courses ], so the third one is a redundant connection.
After optimization, the basic ER diagram obtained is shown below, and the design of the logical structure can be started at this point.
Insert picture description here

Relationship mode conversion.

The previous one obtained the basic ER diagram of the educational administration management system, and the subsequent logical design of the relational database is to obtain a set of relational patterns, so converting the ER diagram into a relational model is to convert entities, attributes and connections into relational patterns. The principles for conversion are as follows:

  • [ Entity-Relationship Mode ] The attribute of the entity is the attribute of the relationship, and the main code of the entity is the main code of the relationship.
  • [ Contact-Relationship Mode ] The attribute of the relationship is the attribute of the contact itself and the main code of the entities involved in the contact. The main code of the relationship needs to be determined according to the type of contact:
    The main code of the 1:1 relationship can be related The main code of any one of the entities;
    The main code of the 1:n relationship is the main code of the entity at the end of the n;
    The main code of the m:n relationship is the combination of the main codes of each entity.
  • [ Special case ] For multiple connections composed of three or more entities, the specific method of conversion is to combine the main codes of all entities to become the main code of the multiple relationship mode. The attribute set of the multiple relationship mode is the main code set of all entities And the attribute collection of the relationship itself.

Insert picture description here
Looking back at the basic ER diagram obtained earlier, we transform the entities in it:

  • Student ( student number , name, gender, age)
  • Course ( course number , course name)
  • Department ( department number , department name, telephone)
  • Teacher ( teacher number , name, gender, job title)

Subsequent conversion of the links:

  • Elective ( student number, course number , grade)
  • Lecture ( Teacher ID, Course ID )
  • Belongs to ( teacher number , department number)
  • Possess ( student number , department number)

The transformation of the above relationship model is based on the global ER model. Therefore, the above relationship model meets 3NF. If there are more stringent requirements in the future, it can continue to be standardized to BCNF and 4NF.

Combine relationship patterns.

There are some relational patterns that can be merged in the set of relational patterns obtained by transforming the global ER diagram above. The principle is to merge those relationship patterns that have the same main code , such as [ student ] and [ own ], [ teacher ] and [ belonging ], and the merged relationship model is as follows:

  • Student ( student number , name, gender, age, department number)
  • Teacher ( teacher number , name, gender, title, department number)

Finally, the relationship model set of the entire educational administration management system is as follows:

  • Teacher ( teacher number , name, gender, title, department number)
  • Student ( student number , name, gender, age, department number)
  • Course ( course number , course name)
  • Department ( department number , department name, telephone)
  • Elective ( student number, course number , grade)
  • Lecture ( Teacher ID, Course ID )

Some Examples.

E1.

There are the following information in a library management system.
Books: book number, book title, quantity, location
Borrower: library card number, name, unit
Publishing house: publishing house name, zip code, address, telephone, E-mail.
The agreement:
anyone can borrow a variety of books, any A book can be borrowed by multiple people. When borrowing and returning a book, register the corresponding borrowing date and return date; a publishing house can publish multiple books, and the same book is only published by one publishing house. The name is unique.
According to the above situation, complete the following design.
(1) Design the ER diagram of the system.
(2) Convert the ER diagram into a relational model.
(3) Point out the master code of each relation mode after conversion.

[ Problem solving ideas ]

  • First draw the ER representation that represents each entity. The entities are represented by rectangles and the attributes are represented by ellipses.
  • Then, according to the semantic convention in the question, draw the ER of the link, which means that anyone can borrow multiple books, and any kind of book can be borrowed by multiple people, so the borrowing link between the book and the borrower entity is n:m According to the semantic agreement, it has its own attributes for borrowing and returning dates. The publishing relationship between publishers and books is 1:n, and has no attributes.

Therefore, the obtained ER diagram is as follows:
Insert picture description here
According to the obtained ER, the relationship model is constructed.

  • The first is the relationship model corresponding to the three entities:
    book ( book number, book title, quantity, location)
    publishing house ( publishing house name , zip code, telephone, address, E-Mail)
    borrower ( library card number , name, unit )
  • Then there is the corresponding relationship model:
    Borrowing ( library card number, book number , borrowing date, return date)
    publication ( book number , publisher name)

For specific conversion rules, please refer to [Relationship Mode Conversion] . Afterwards, we will merge and optimize the obtained relationship mode set. It is found that [ Book ] and [ Publishing ] have the same master code, so merge, and finally get the relationship mode. The collection looks like this:

  • Books ( book number, title, quantity, location, publisher name)
  • Publisher ( publisher name , zip code, telephone, address, E-Mail)
  • Borrower ( library card number , name, unit)
  • Borrowing ( library card number, book number, borrowing date, return date)

E2.

Class scheduling is an important process in the teaching process, which includes the following entities.
Course entity: course (cid, cname, chour, ctype). Among them, cid uniquely identifies each course, cname is the name of the course, chour is the course hours, and ctype is the course category (0 means elective course, 1 means compulsory course).
Classroom entity: classroom (crid, crname, crbuilding). Among them, crid is used to identify each classroom, crbuilding is the building of the classroom, and crname is the name of the classroom.
Teacher entity: teacher (tid, tname). Among them, tid uniquely identifies each teacher, and tname is the teacher's name.
The relationship between the entities is: each teacher can teach multiple courses, a course can be taught by multiple teachers, a classroom can host multiple courses, and a course can be arranged in multiple classrooms. When the course is arranged in the designated classroom, the date of arrangement (cdata) and the course of the day (carrange) should be indicated.
Please answer the following questions based on the above requirements.
(1) Design the ER diagram of the system.
(2) Convert the ER diagram into a relational model and indicate the main code.
(3) According to the relational model, use SQL to create course entities, requiring SQL statements to include master code constraints and non-empty constraints, and the type and length of each attribute are optional.

Question Solution
Insert picture description here

  • 关系模式
    course(cid,cname,chour,ctype)
    classroom(crid,crname,crbuilding)
    teacher(tid,tname)
    teach(tid,cid)
    arrangement(cid,crid,cdate,carrage)
  • Create course entity
CREATE TABLE course
(cid	CHAR(8) PRIMARY KEY,
cname	VARCHAR(20) NOT NULL,
chour	INT NOT NULL,
ctype	INT NOT NULL)

E3.

The library management system is a kind of common information management system. After analyzing the library management system, the preliminary entity information obtained is as follows.
Book: book (bookid, bookname, num). Among them, bookid is used to identify each book, bookname is the name of the book, and num is the number of books.
Borrowing user: bookuser (tid, username, age). Among them, tid is used to identify each borrowing user, username is the name of the borrowing user, and age is the age of the borrowing user.
The relationship between the book entity and the borrowing user entity is: a borrowing user can borrow multiple books, and at the same time, a book can be borrowed by multiple borrowing users. The borrowing process generates attributes such as borrowing date (borrow_time) and returning date (return_time).
Please answer the following questions based on the above requirements.
(1) Design the ER diagram of the system.
(2) Convert the ER diagram into a relational model and indicate the main code.
(3) According to the relational model, use SQL to create the borrowing user entity, requiring the SQL statement to include the master code constraint and the non-empty constraint.

Question Solution
Insert picture description here

  • 关系模式
    book(bookid,bookname,num)
    bookuser(tid,username,age)
    borrow(bookid,tid,borrow_time,return_time)
  • Create user entity
CREATE TABLE bookuser
(
	tid CHAR(8) PRIMARY KEY,
	username VARCHAR(20) NOT NULL,
	age INT
)

Guess you like

Origin blog.csdn.net/weixin_44246009/article/details/108423701