Relational Database
1. What is a relational database?
Relational database: is a database built on a relational model (mathematical model).
Relational Model: A so-called model built on relationships.
The relational model includes three aspects: 1. Data structure: the problem of data storage, two-dimensional table (with rows and columns) 2. Operation instruction set: all SQL statements 3. Integrity constraints: data constraints in the table (the relationship between fields and fields) between tables), constraints between tables (foreign keys)
2. Design of relational database?
Relational database: Analyze from the data requirements to be stored. If it is a type of data (entity), it should be designed as a two-dimensional table. The two-dimensional table is composed of a table header (field name: the name used to specify the data) and a data part ( actual stored data unit)
2D Table: Rows and Columns
header | field name 1 | field name 2 |
data unit | data 1 | data 2 |
Dealing with real cases: analyzing a teaching system, the lecturer is responsible for teaching, teaching students, teaching students in the classroom
- Find out which entities exist in the system: Lecturer table, Student table, Class table
- Find out the data information that should exist in the entity Lecturer: Name, Gender, Age, Salary Student: Name, Gender, Student ID, Subject Class: Class Name, Classroom ID
Relational database: maintain the relationship between entities and between entities
Inter-entity links: each student has name, gender, student ID, subject information
Student table:
Name | gender | student ID | Subject | age |
Chen Ming | male | 1001 | PHP | 20 |
Chen Jingjing | Female | 1002 | PHP | |
Wang Wei | 1003 | UI |
All the fields in the second row describe the student (internal connection), and the second column can only put gender (internal constraints)
One of the characteristics of relational databases : if there is no value (data) in a corresponding field in the table, but the system still needs to allocate space, relational databases waste space.
Entity to entity connection : each student must belong to a certain class, and each class must have multiple students (one-to-many)
Class Schedule:
class name | classroom number |
PHP0810 | B205 |
UI0810 | A203 |
Solution to the association problem between two tables: add a class field to the student table to implement the class (must be able to uniquely find a class information)
Name | gender | student ID | Subject | age | class name |
Chen Ming | male | 1001 | PHP | 20 | PHP0810 |
Chen Jingjing | Female | 1002 | PHP | PHP0810 | |
Wang Wei | 1003 | UI | UI0810 |
Association relationship between student entity and class entity: relationship between entity and entity