Skip to Content
《Head First SQL》 (Chinese Edition), 2nd Edition
book

《Head First SQL》 (Chinese Edition), 2nd Edition

by Kimberly Fessel
June 2026
740 pages
13h 7m
Chinese
O'Reilly Media, Inc.
Content preview from 《Head First SQL》 (Chinese Edition), 2nd Edition

第 8 章. 主键 与数据关系:周密的表设计

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

Image

致所有架构师和设计师:你们一直都在不假思索地构建表。这倒也没什么问题,它们确实能正常工作。你们可以在这些表上执行所有的 CRUD 操作,并编写任意复杂的 SELECT 语句。但随着数据量的增加,你们会逐渐发现 一些问题,让你不禁后悔当初 ,以便让WHERE子句更简洁。又或者,你的单张表已经不够用了,需要对“布局”稍作调整。随着数据日益复杂,关键在于让你的表 原子化 ,并将数据划分为 多个相关表。谁知道呢?也许你最优的蓝图最终会演变成整座摩天大楼的规划方案。继续阅读,一探究竟。

你知道圣何塞属于哪个州吗?

Image
Image

犯错在所难免,但幸运的是,艾比在第 5 章学到了UPDATE语句。她可以利用它将“CA”添加为state

Image

城市混淆

如果艾比遵循了最佳实践,她本应 在执行 UPDATE 语句前,先通过 SELECT 查询测试其WHERE子句 。现在她执行了一条SELECT 语句,查看哪些内容发生了变化。

Image
Image

艾比编写WHERE子句时,本以为城市名称只会匹配一行,但事实证明这是错误的,她无意中将哥斯达黎加的圣何塞标记为位于加利福尼亚州。

当然,她本可以使用带有WHERE AND子句来同时匹配city country,例如:

WHERE city = ‘San Jose’
 AND country = ‘USA’

但如果同一个国家/地区内恰好有两个同名城市(例如位于美国的俄勒冈州波特兰市和缅因州波特兰市),这种做法可能会引发问题。

你完全正确,这被称为 主键 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Overcome a Power Deficit

How to Overcome a Power Deficit

Cyril Bouquet, Jean-Louis Barsoux
Relational Power Is the New Currency of Hybrid Work

Relational Power Is the New Currency of Hybrid Work

Lebene Soga, Yemisi Bolade-Ogunfodun, Nazrul Islam, Joseph Amankwah-Amoah

Publisher Resources

ISBN: 0642572408114