【Teach me how to fish】What is a database? What is a relational database?

Yuxian: CSDN content partner, CSDN rising star mentor, rising star creator in the full stack field, 51CTO (Top celebrity + expert blogger), github open source enthusiast (go-zero source code secondary development, game back-end architecture https: //github.com/Peakchen)

What is a database?

A database is a system for storing and managing data. It is an organized collection of data that can be accessed and processed through various operations and queries. Databases provide a structured way to store and manage data so that it can be organized, accessed and updated efficiently.

What is a relational database?

A relational database is a database system based on a relational model. In a relational database, data is organized in the form of tables, which consist of rows and columns. Each table represents an entity or concept, each row represents a record, and each column represents an attribute. Relational databases use Structured Query Language (SQL) to define and manipulate data.

The principle of relational database is explained in detail:

Relational databases follow a set of principles:

  1. Data is organized in tabular form: data is stored in tables, each table consists of columns and rows. Each column defines an attribute, and each row represents a record.

  2. Establish relationships between tables: By establishing foreign key relationships, different tables can be associated to achieve data consistency and integrity.

  3. Data uniqueness and integrity: Relational databases define various constraints, such as primary keys, unique keys, foreign keys, etc., to ensure the uniqueness and integrity of data.

  4. ACID transaction support: Relational databases provide transaction support for ACID (atomicity, consistency, isolation, and durability) attributes to ensure data consistency and reliability.

Flow chart of the underlying architecture of a relational database:

+------------------------------------------------+
|                  Client Application           |
+------------------------------------------------+
                       |
                       |
                       v
+------------------------------------------------+
|                  Database Management System   |
|                      (DBMS)                    |
+------------------------------------------------+
                       |
                       |
                       v
+------------------------------------------------+
|                   Storage Engine               |
+------------------------------------------------+

In the underlying architecture of a relational database, client applications interact with a database management system (DBMS). DBMS is responsible for parsing and executing SQL statements, as well as managing data storage and access. DBMS interacts with the underlying storage engine (Storage Engine), which is responsible for actual data storage and retrieval operations.

Explanation of usage scenarios of relational database:

Relational databases are suitable for many different application scenarios, including:

  1. Enterprise applications: Relational databases are widely used in enterprise-level applications, such as customer relationship management (CRM), human resource management (HRM), and supply chain management (SCM).

  2. Financial system: Relational databases are widely used in the financial field to store and manage large amounts of transaction data, account information and customer data.

  3. Online transaction processing (OLTP) system: Relational database is suitable for online transaction processing systems that require frequent read and write operations, such as e-commerce websites and banking systems.

  4. Data analysis and reporting: Relational databases can be used to store and process large amounts of data and support complex queries and report generation.

A code sample implementation of a relational database:

The following is a simple code example using the MySQL relational database to create tables, insert data, and perform query operations:

import mysql.connector

# 连接到MySQL数据库
cnx = mysql.connector.connect(user='username', password='password',
                              host='localhost', database='mydatabase')

# 创建表格
cursor = cnx.cursor()
create_table_query = '''
CREATE TABLE employees (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100),
  age INT,
  department VARCHAR(100)
)
'''
cursor.execute(create_table_query)

# 插入数据
insert_data_query = '''
INSERT INTO employees (name, age, department)
VALUES (%s, %s, %s)
'''
data = [
    ('John Doe', 30, 'Sales'),
    ('Jane Smith', 35, 'Marketing'),
    ('Bob Johnson', 40, 'Finance')
]
cursor.executemany(insert_data_query, data)
cnx.commit()

# 执行查询操作
select_query = 'SELECT * FROM employees'
cursor.execute(select_query)
result = cursor.fetchall()
for row in result:
    print(row)

# 关闭数据库连接
cursor.close()
cnx.close()

This example uses Python's mysql.connectorlibrary to connect to a MySQL database and creates a table named "employees". Then it inserts some data and performs a simple SELECT query, printing out the results.

Links to literature on relational databases:

The following are some links to literature materials about relational databases for further study and understanding:

  1. MySQL official documentation↗
  2. PostgreSQL Official Documentation↗
  3. Oracle Database Documentation↗
  4. SQL Tutorial-W3Schools ↗
  5. Relational Database - Wikipedia ↗

Products currently using relational databases:

Currently, there are many relational database products in use on the market. Some common relational database products include:

  1. MySQL
  2. PostgreSQL
  3. Oracle Database
  4. Microsoft SQL Server
  5. SQLite
  6. IBM Db2
  7. MariaDB

These database products are widely used in a variety of application areas and have different features and applicability. Specific relational database products should be selected based on project requirements and business scenarios.

Guess you like

Origin blog.csdn.net/feng1790291543/article/details/132741267