Back to tree

🔍 SQL:跟数据库"说话"的语言🔍 SQL: Talking to Databases

上一片里,💻 小电脑把借书单搬进带标签的柜子(数据库)。柜子有了,可怎么跟它说话?这一片只学一句查询。 Last leaf, the 💻 little computer shelved the borrow list into a labeled cabinet (a database). Now that we have the cabinet — how do we talk to it? This leaf teaches the one-line query.

第 1 步:一句话查数据Step 1: Ask in one sentence

先看这句口令长什么样——三个词就够。First, what does the magic sentence look like — three words are enough.

SELECT 语句卡:SELECT / FROM / WHERE
SELECT 说查什么(姓名),FROM 说去哪张表(借书表),WHERE 说加什么条件。💻 小电脑照着念,柜子就吐出结果。 SELECT says what to get (names), FROM says which table (borrow), WHERE adds the filter. The little computer reads it out, and the cabinet answers.

整表查出来了,可我只想要借书多的人怎么办?The whole table is out — but what if I only want the heavy readers?

第 2 步:WHERE 留下想要的行Step 2: WHERE keeps the rows you want

WHERE 过滤:4 行留下 2 行
WHERE 借书数 > 3 像一把筛子:8 行留下 3 行,借得少的人被筛掉。记住四件套:查 SELECT、增 INSERT、改 UPDATE、删 DELETE。 WHERE books > 3 works like a sieve: 8 rows in, 3 stay — light readers fall through. Remember the four moves: SELECT reads, INSERT adds, UPDATE changes, DELETE removes.

查改删都会了,可有人一条语句删掉整个库怎么办?Reading and writing work — but what if one line wipes out the whole library?

第 3 步:删库跑路的教训Step 3: The lesson of the deleted library

删库警示:DROP DATABASE,要权限,先备份
DROP DATABASE 一按,整个库就没了——这就是"删库跑路"的梗。所以删改都要权限,动手之前先备份。 One DROP DATABASE and the whole library is gone — that's the famous joke. So writes need permission, and always back up first.

🎮 你来当查询员(30 秒)🎮 Your turn: query clerk (30 seconds)

句式学会了。去查询台帮小电脑挑 SQL:4 关全过,还要数对行数、认出删库语句。Pattern learned. Head to the query desk and pick the right SQL for the little computer: clear 4 rounds, count the rows, and spot the library-deleting line.

一句话记住它:SQL 是跟数据库说话的语言;SELECT 查什么、FROM 去哪找、WHERE 留哪些;删库之前先问权限和备份。 Remember it in one line: SQL is how you talk to a database — SELECT what, FROM where, WHERE which rows; ask for permission and a backup before any DROP.
SELECT…FROM…WHERE:查什么、去哪找、留哪些SELECT…FROM…WHERE: what to get, where to look, which rows stay 四件套:查 SELECT、增 INSERT、改 UPDATE、删 DELETEFour moves: SELECT reads, INSERT adds, UPDATE changes, DELETE removes 删库跑路:DROP 很危险,要权限、先备份The deleted-library joke: DROP is dangerous — permission plus backup first
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。 Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.