1. 什么是数据库?/ What is a Database? 我们的数字宝藏库
有没有过这种经历?你花了一整个周末在《我的世界》里搭的梦幻城堡,下次打开游戏的时候,所有的房子、箱子里的钻石剑、甚至你养的小猫都不见了?你急得快要哭出来的时候,妈妈告诉你“存档坏啦”——其实那个帮你记住所有游戏进度的小盒子,就是最最简单的数据库呀! Have you ever had this experience? You spent a whole weekend building a dream castle in Minecraft, but when you opened the game next time, all the houses, diamond swords in your chest, and even your little cat were gone? You were so anxious that you were about to cry, and your mom told you "the save file is broken" — actually, that little box that remembers all your game progress is the simplest database!
数据库可不只是记游戏进度哦,它的用处大着呢。你有没有见过班主任手里那个记着全班所有同学信息的本子?每个人对应一行:姓名、学号、家长电话、家庭住址,甚至还有你上次考试考了多少分。要是班主任想找所有数学考满分的同学,不用一页一页翻,只要扫一眼就能找出来——这个本子,就是现实版的数据库。 The database is not just for saving game progress! Have you ever seen the notebook that the homeroom teacher holds, which records all the information of the whole class? Each person corresponds to a row: name, student ID, parent's phone number, home address, and even your last exam score. If the teacher wants to find all students who got full marks in math, she doesn't need to flip through the pages one by one, she can find them at a glance — this notebook is a real-life version of a database.
简单来说,数据库(Database)就是一个长期存储在计算机里的、有组织的、可共享的数据集合。你可以把它想象成一个超级智能的收纳箱:你往里面放东西的时候,它会自动帮你按类别分好;你要找东西的时候,只要告诉它你要什么,它马上就能给你递过来;就算全班几十个人同时要找东西,它也能应付得来,不会乱,也不会把东西弄丢。 Simply put, a database is a long-term stored, organized, and shareable collection of data in a computer. You can think of it as a super smart storage box: when you put things in it, it will automatically sort them by category for you; when you want to find something, just tell it what you want, and it will hand it to you right away; even if dozens of people in the whole class are looking for things at the same time, it can handle it without getting messy or losing things.
数据库的三个超能力 / Three Superpowers of Databases
数据库之所以这么有用,是因为它有三个普通人(或者普通收纳箱)没有的超能力: Databases are so useful because they have three superpowers that ordinary people (or ordinary storage boxes) don't have:
1. 永远不会忘事的“超强记忆” / Never-Forget Super Memory
你有没有过把作业记在本子上,结果本子丢了,或者睡了一觉就忘了写什么的经历?人的记忆是不可靠的,但是数据库的记忆是100%靠谱的:只要你把数据存进去,就算你关了电脑、手机没电、甚至过了十年,只要你需要,它就能马上给你找出来。 Have you ever written your homework in a notebook, but lost the notebook, or forgot what you wrote after a night's sleep? Human memory is unreliable, but database memory is 100% reliable: as long as you store data in it, even if you turn off the computer, your phone runs out of power, or even after ten years, it can find it for you right away as long as you need it.
比如你去年在游戏里抽到的稀有皮肤,就算你一年没玩那个游戏,下次登录的时候,皮肤还是会在你的账号里,因为游戏数据库一直帮你记着。要是靠你脑子记,肯定早就忘了自己抽过什么皮肤啦。 For example, the rare skin you drew in the game last year, even if you haven't played that game for a year, the skin will still be in your account when you log in next time, because the game database has been remembering it for you. If you rely on your memory, you would have forgotten what skins you drew long ago.
2. 一秒找到的“超级搜索” / One-Second Super Search
如果你的玩具都乱塞在一个大箱子里,你要找一个小小的乐高积木,可能要翻十分钟才能找到;但如果你的乐高是按颜色、型号分格子放的,你要找红色的2x4积木,只要几秒钟就能拿到。数据库就是那个帮你把数据分好格子的超级管理员,不管你要找什么,它都能在几毫秒里给你结果。 If all your toys are stuffed in a big box, you may take ten minutes to find a small LEGO brick; but if your LEGO is sorted by color and model into different grids, you can get the red 2x4 brick in just a few seconds. The database is the super administrator that helps you sort the data into grids, no matter what you want to find, it can give you the result in a few milliseconds.
比如学校图书馆有几十万本书,要是靠人工翻卡片找《哈利波特》,可能要找半小时;但用图书馆的数据库,你只要在电脑上输入书名,1秒钟就能告诉你这本书在哪个书架、哪一层、有没有被借走,超级方便。 For example, the school library has hundreds of thousands of books, if you rely on manual card search to find "Harry Potter", it may take half an hour; but using the library database, you only need to enter the book name on the computer, and it will tell you which shelf, which floor the book is on, and whether it is borrowed in 1 second, which is super convenient.
3. 不打架的“共享魔法” / No-Fighting Sharing Magic
如果你只有一本班级通讯录,你要看,同桌也要看,你们可能会抢起来,容易把本子撕坏,还可能把电话记混。但数据库是电子版的,全班几百个人同时查都不会乱:每个人查的时候,数据库都会给你看正确的信息,不会因为你看了,别人的信息就没了,也不会两个人同时改信息的时候弄混。 If you only have one class contact list, you want to look at it, and your deskmate also wants to look at it, you may fight over it, easily tear the notebook, and may mix up the phone numbers. But the database is electronic, hundreds of people in the whole class can query it at the same time without getting messy: when everyone queries, the database will show you the correct information, your viewing will not make other people's information disappear, and it will not mix up when two people modify the information at the same time.
比如你玩《王者荣耀》的时候,全国有几百万玩家同时在线,每个人都在查自己的好友列表、皮肤、战绩,数据库都能稳稳地应付,不会乱,也不会把你的皮肤弄到别人的账号里。 For example, when you play "Honor of Kings", there are millions of players online at the same time, everyone is checking their friend list, skins, and record, the database can handle it steadily, will not get messy, and will not transfer your skin to other people's accounts.
数据库的小历史 / Little History of Databases
数据库可不是最近才有的发明哦,它的历史比爸爸妈妈的年纪还大,从人类开始记录信息的时候,数据库的雏形就出现啦。 Databases are not a recent invention! Their history is older than your parents' age. The prototype of databases appeared when humans started recording information.
最早最早的数据库,是原始人的结绳记事。原始人没有纸和笔,打了几头羊、采了多少果子,就在绳子上打几个结,下次要算数量的时候,只要数绳子上的结就行。这个结绳,就是最最早的“数据库”,用来记录数据,方便以后查。 The earliest database was the knotted rope record-keeping of primitive people. Primitive people had no paper or pens, when they hunted a few sheep, picked a lot of fruits, they would tie a few knots on the rope, when they wanted to count the quantity next time, they just needed to count the knots on the rope. This knotted rope is the earliest "database", used to record data for future查询.
后来到了秦朝,政府开始用户籍册记录每家每户有多少人、多少田、要交多少税,这个户籍册也是数据库,而且是全国通用的,各个郡县的官员都能查,不会乱。那时候的户籍册是写在竹简上的,要是全国的人口数据都存在竹简上,能堆满好几个仓库呢,比现在的电脑数据库可重多啦。 Later, in the Qin Dynasty, the government began to use household registration books to record how many people, how much land, and how much tax each family had to pay. This household registration book is also a database, and it is used nationwide, officials in various prefectures and counties can query it without getting messy. At that time, the household registration book was written on bamboo slips, if the national population data was stored on bamboo slips, it could fill several warehouses, which is much heavier than the current computer database.
等到计算机发明之后,最早的时候人们把数据存在一个个单独的文件里,比如把班级通讯录存在一个叫“contact.txt”的文件里,游戏存档存在一个叫“save.dat”的文件里。要是你想找某个同学的电话,就得自己打开文件翻,要是全班有几千个同学,找起来要花好几分钟,特别麻烦。 After the invention of computers, people stored data in separate files at the earliest, for example, storing the class contact list in a file called "contact.txt", and game saves in a file called "save.dat". If you want to find a certain student's phone number, you have to open the file and look for it yourself, if there are thousands of students in the whole class, it will take several minutes to find it, which is very troublesome.
1960年代,IBM的工程师查尔斯·巴赫曼(Charles Bachman)发明了第一代数据库管理系统(DBMS),就像给所有数据找了个超级管理员。你不用自己翻文件,只要告诉管理员“我要找学号是123的同学的电话”,它马上就能给你找出来,还不会把数据弄丢。巴赫曼因为这个发明,还得了计算机界的最高奖——图灵奖呢! In the 1960s, Charles Bachman, an engineer at IBM, invented the first generation of Database Management System (DBMS), which is like finding a super administrator for all data. You don't need to flip through files by yourself, just tell the administrator "I want to find the phone number of the student with student ID 123", and it will find it for you right away, and will not lose the data. Bachman won the highest award in the computer field — the Turing Award for this invention!
到了1970年代,IBM的科学家埃德加·科德(Edgar Codd)又提出了关系型数据库模型,这个模型到现在还是最常用的数据库。关系型数据库就是把数据存在一个个表格里,每一行是一条记录,每一列是一个属性,就像我们之前说的班级通讯录,特别清楚,特别好用。 In the 1970s, Edgar Codd, a scientist at IBM, proposed the relational database model, which is still the most commonly used database now. The relational database stores