数据库 Overview
什么数据库是, 为什么 apps 需要 one, and 如何 relational 对比 NoSQL 系统 differ.
你将学到
- 什么数据库是 and 为什么每个真实 application 需要 one
- difference 之间数据库, DBMS, and 查询 language
- Relational (SQL) 对比非-relational (NoSQL) 数据库 at glance
- 常见数据库系统: PostgreSQL, MySQL, SQLite, MongoDB, Redis
概念讲解
什么是数据库?
A database is an organized collection of data stored so that it can be accessed, managed, and updated efficiently. Unlike a plain file or a spreadsheet, a database is designed to handle concurrent access from many users at once, enforce rules about what data is allowed, and recover safely from crashes. Almost every real-world application — from a blog to a banking system — stores its data in a database.
Database vs DBMS vs Query Language
These three terms are often confused. The database is the data itself. The database management system (DBMS) is the software that stores and retrieves that data (e.g. PostgreSQL, MySQL). The query language (e.g. SQL) is the syntax you use to ask the DBMS to do things. When people say "I use PostgreSQL," they mean they use the PostgreSQL DBMS to manage their database, and they write SQL to interact with it.
Relational vs NoSQL
Relational databases (PostgreSQL, MySQL, SQLite, SQL Server, Oracle) store data in tables with rows and columns, enforce a strict schema, and use SQL. They excel at complex queries, joins, and transactional integrity. NoSQL databases (MongoDB, Redis, Cassandra, DynamoDB) trade strict schema for flexibility — they store documents, key-value pairs, or wide columns, and are often chosen for horizontal scale and unstructured or rapidly changing data.
Popular Database Systems
- PostgreSQL — powerful open-source RDBMS with rich feature set
- MySQL — widely used open-source RDBMS, common in web stacks
- SQLite — lightweight file-based RDBMS, perfect for mobile and prototyping
- MongoDB — document-oriented NoSQL storing JSON-like documents
- Redis — in-memory key-value store, extremely fast for caching
Choosing the right database depends on your data shape, query patterns, scale requirements, and consistency needs.
示例代码
-- Create a simple table to store users
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);
-- Insert a row
INSERT INTO users (id, name, email)
VALUES (1, 'Ada Lovelace', '[email protected]');
-- Query the data
SELECT * FROM users;
这个是 完整 mini-示例: 创建表, insert one 行, and 读取它 后面. 每个 relational 数据库 supports 这些 three 操作.
动手试一试
- SQL Formatter
格式 and beautify SQL 查询 as 你编写它们.
- JSON 到 SQL
Convert JSON 数据到 SQL insert 语句.
常见错误
错误写法
Using spreadsheet as 数据库
正确做法
Spreadsheets lack concurrency, constraints, and 查询 power. Use SQLite for 零-配置开始 — 它是 单个 file 但真实数据库.
错误写法
Confusing 数据库使用 DBMS
正确做法
数据库是 数据; DBMS (e.g. PostgreSQL) 是 software 那个 manages 它. 你编写 SQL 到 talk 到 DBMS.
练习 数据库 Overview
4 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。