Chapter 3. Django Models and Database

Relational Database

Relational Database
Tag:

Before jumping into Django Models, it is beneficial to review the key points of a relational database.

A database that Django officially supports is a relational database, which is the most commonly used database type. A relational database organizes data into columns and rows.

Key terms in Relational Database

There are five key terms in the relational database you need to understand.

1. Table

Tables are sets of data organized in rows and columns. A relational database consists of multiple tables. In the main figure example, a database consists of three tables – Employee table, Personal Info table, and Division table.

2. Field

Fields are a crucial part of a relational database storing data. Each field determines a data type that can store a specific type of data. We'll explain the data field types used in Django later.

3. Record

A record is a set of values that are assigned to each field or column in a data table. In the example of the main figure, a record consists of data values that cover each field in the Employee table.

4. Relationship

A relationship defines a connection between two tables. There are three types of relationships.

  • One-to-Many Relationship: This is the most frequently used relationship in the relational database. In this relationship, each record in one table can correspond to many records in another table, but not the other way around. For example, each division of a company consists of several employees, and each employee belongs to only one division.

Relational-Database

  • One-to-One Relationship: In this relationship, each record in one table corresponds to one record in another table. In the main figure example, the relationship between the Employee table and the Personal Info table is one-to-one relationship. One-to-one relationship tables can be combined into one table, but in practice, there are reasons for separating tables from the data management point of view.

Relational-Database

  • Many-to-Many Relationship: In this relationship, each record in one table can correspond to many records in another table, and vice versa. For example, an employee can be enrolled in multiple digital learning courses, and each digital learning course can have multiple employees enrolled. To create a many-to-many relationship between two tables, another table called an intermediary join table (associative entity) is created to join the two tables.

Relational-Database

5. Primary Key

Primary key is defined as a field or column of a table that uniquely identifies each record in the table. The main role of the primary key is to define relationships with other tables in a relational database.

Before jumping into Django Models, it is beneficial to review the key points of a relational database.

A database that Django officially supports is a relational database, which is the most commonly used database type. A relational database organizes data into columns and rows.

Key terms in Relational Database

There are five key terms in the relational database you need to understand.

1. Table

Tables are sets of data organized in rows and columns. A relational database consists of multiple tables. In the main figure example, a database consists of three tables – Employee table, Personal Info table, and Division table.

2. Field

Fields are a crucial part of a relational database storing data. Each field determines a data type that can store a specific type of data. We'll explain the data field types used in Django later.

3. Record

A record is a set of values that are assigned to each field or column in a data table. In the example of the main figure, a record consists of data values that cover each field in the Employee table.

4. Relationship

A relationship defines a connection between two tables. There are three types of relationships.

  • One-to-Many Relationship: This is the most frequently used relationship in the relational database. In this relationship, each record in one table can correspond to many records in another table, but not the other way around. For example, each division of a company consists of several employees, and each employee belongs to only one division.

Relational-Database

  • One-to-One Relationship: In this relationship, each record in one table corresponds to one record in another table. In the main figure example, the relationship between the Employee table and the Personal Info table is one-to-one relationship. One-to-one relationship tables can be combined into one table, but in practice, there are reasons for separating tables from the data management point of view.

Relational-Database

  • Many-to-Many Relationship: In this relationship, each record in one table can correspond to many records in another table, and vice versa. For example, an employee can be enrolled in multiple digital learning courses, and each digital learning course can have multiple employees enrolled. To create a many-to-many relationship between two tables, another table called an intermediary join table (associative entity) is created to join the two tables.

Relational-Database

5. Primary Key

Primary key is defined as a field or column of a table that uniquely identifies each record in the table. The main role of the primary key is to define relationships with other tables in a relational database.

Tag: