relational-database and MySQL overview
Introduction
Whether it is e-commerce orders, personal blog content or enterprise core business data, relational-database has always been the cornerstone of persistent storage for modern applications. Among many relational-databases, MySQL occupies an absolutely dominant position in web development and Python projects by virtue of its advantages of open source, free, high performance, and mature ecology. This article will start from scratch and use an easy-to-understand method to guide you to master the core concepts of relational-database and the basic operations of MySQL. It will also demonstrate how to use Python to connect to the database through actual code, laying a solid foundation for the subsequent database development journey.
1. relational-database core foundation
1.1 What is relational-database?
relational-database is built on the relational model. The core idea is to organize data into two-dimensional tables (tables), and tables are connected through "relationships". You can think of it like a structured Excel workbook: each worksheet is a table, each row represents a complete record, and each column represents a field.
More specifically, it has the following key characteristics:
- Table storage data: Each table corresponds to a type of object in the real world, such as "employee table" and "department table".
- Clear rows and columns: Each row is a complete record (such as "Zhang San, the employee"), and each column is an attribute of the object (such as "Zhang San's mailbox").
- Unique identification: Each table has a primary key to ensure that records are not duplicated and can be accurately positioned.
- Inter-table association: Concatenate data from different tables through foreign keys to reduce redundant storage.
- Data Constraints: Ensure the accuracy and completeness of data through rules (such as "Email cannot be empty" "Salary must be greater than zero").
1.2 Reliable guarantee: ACID characteristics
relational-database is capable of handling scenarios such as bank transfers and order payments that require extremely high data consistency. The core reason is that it strictly follows the four major characteristics of ACID:
1.3 When to choose relational style? When to choose non-relational?
To help you make a choice based on actual needs, here is a simple comparison:
2. Popular choice: MySQL Quick Start
2.1 Why choose MySQL?
- Open source and free: The Community version is fully sufficient for individuals or small teams and complies with the GPL agreement.
- Cross-platform: Windows, Linux, macOS can be easily deployed.
- Rich Ecology: Python, Java, Go and other mainstream languages have mature drivers, complete documentation and community support.
- Excellent performance: The default InnoDB engine supports transactions, row-level locks and high concurrency, with excellent read and write performance.
2.2 Common storage engines
MySQL supports multiple storage engines. Newbies only need to remember: InnoDB is preferred in most scenarios.
The characteristics of different engines are compared as follows:
3. Local environment-setup
The following only introduces the quick installation method of Community Edition. For production environments, it is recommended to further refer to official documents to strengthen security configuration.
3.1 Windows installation (MySQL Installer recommended)
- Visit MySQL官网下载页面
- Download
mysql-installer-community(No need to register an Oracle account, just click "No thanks, just start my download." below) - Run the installer and select Server only (or manually select components through Custom)
- Follow the prompts to set the root password and port (default 3306)
- After the installation is complete, open the command line and enter
mysql -u root -p, enter the password to verify whether it is successful
3.2 Ubuntu/Debian installation (start with one line of commands)
3.3 Basic connection commands
4. Basic operations of database and tables
Next, we create a company management system database to practice, and gradually master the database creation, table creation and table structure modification.
4.1 Database operations
4.2 Table operations (create department tables and employee tables)
First create the related department table, then create the employee table and establish a foreign key association:
5. Basic SQL: data addition, deletion, modification and query
5.1 Insert data (INSERT)
5.2 Query data (SELECT)
6. Python quickly connects to MySQL
There are two mainstream ways for Python to operate MySQL: Native PyMySQL (lightweight and intuitive, suitable for quick docking) and SQLAlchemy ORM (object-oriented, suitable for complex projects). Here we first introduce the most basic usage of PyMySQL.
6.1 Native PyMySQL (first choice for getting started)
Install dependencies first:
Sample code (contains secure context manager and parameterized queries):
Related tutorials
Summarize
This article starts from the core concept of relational-database, introduces the characteristics, installation and basic operations of MySQL, and finally demonstrates how to interact with the database through Python code. After mastering these contents, you already have the basics of Python database development.
If you need in-depth study in the future, you can continue to pay attention to these directions:
- Complex SQL queries and subqueries
- Advanced usage of SQLAlchemy ORM
- Database performance optimization and index design
- Database security, backup and recovery

