「SQL数据分析系列」2. 创建和使用数据库

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"写在前面:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家好,我是强哥,一个热爱分享的技术狂。目前已有 12 年大数据与AI相关项目经验, 10 年推荐系统研究及实践经验。平时喜欢读书、暴走和写作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"业余时间专注于输出大数据、AI等相关文章,目前已经输出了40万字的推荐系统系列精品文章,今年 6 月底会出版「构建企业级推荐系统:算法、工程实现与案例分析」一书。如果这些文章能够帮助你快速入门,实现职场升职加薪,我将不胜欢喜。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想要获得更多免费学习资料或内推信息,一定要看到文章最后喔。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"内推信息","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你正在看相关的招聘信息,请加我微信:liuq4360,我这里有很多内推资源等着你,欢迎投递简历。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"免费学习资料","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想获得更多免费的学习资料,请关注同名公众号【数据与智能】,输入“资料”即可!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"学习交流群","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想找到组织,和大家一起学习成长,交流经验,也可以加入我们的学习成长群。群里有老司机带你飞,另有小哥哥、小姐姐等你来勾搭!加小姐姐微信:epsila,她会带你入群。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"第二章创建和使用数据库","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本章内容包括创建第一个数据库以及本书中示例所用的表和相关数据,你还将了解各种数据类型以及如何在创建表时使用它们。因为本书中的示例是针对MySQL数据库执行的,所以本章会偏向使用MySQL的特性和语法,但大多数概念对其他的数据库服务器也适用。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"创建MySQL数据库","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想使用本书中示例所用的数据,你有两个选择:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 下载并安装MySQL server 8.0版(或更高版本),然后从https://dev.mysql.com/doc/index-other.html下载Sakila示例数据库;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 跳转到https://www.katacoda.com/mysql-db-sandbox/scenarios/mysql-sandbox访问MySQL沙箱,在MySQL实例中加载Sakila示例数据库。要使用这种方法,你必须新建一个Katacoda帐户(免费的),然后单击Start Scenario按钮。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你选择第二种方法,那么一旦启动场景,就会安装并启动MySQL服务器,然后加载Sakila模式和数据。启动以及加载完毕之后,会出现一个标准的mysql>提示符,然后就可以开始查询示例数据库了。这当然是最简单的选择,我认为大多数读者都会选择这个方法。如果你觉得使用第二种方法很不错并且打算使用这种方法,那么可以跳到下一节了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你希望拥有自己的数据副本,并且希望所做的任何更改都是永久性的,或者如果你就是想在自己的计算机上安装MySQL服务器,那么你可能偏向于第一个选择。你也可以选择使用托管在Amazon Web Services或Google Cloud等环境中的MySQL服务器。无论你是在本机下载还是在云端托管,你都需要自己执行安装和配置,这里不予赘述。一旦数据库可用(假设你已经下载并且安装和配置完毕),就需要按照以下几个步骤加载Sakila示例数据库。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,启动mysql命令行客户端并输入密码,然后执行以下步骤:","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"前往https://dev.mysql.com/doc/index-other.html下载Example Databases下的“sakila database”文件。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"将文件放在本地目录中,例如C:\\temp\\sakila-db(为下面两个步骤做准备)。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"键入source c:\\temp\\sakila db\\sakila-schema.sql,然后按Enter键。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"键入source c:\\temp\\sakila db\\sakila-data.sql,然后按Enter键。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"现在你应该有一个可用数据库了,其中包括了本书中示例所需的所有数据。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"注意:Sakila示例数据库由MySQL提供,并通过New BSD获得许可。Sakila包含一个虚构的电影租赁公司的数据,幷包含诸如store, inventory, film, customer,和payment等表。虽然实际上的电影租赁商店基本上已经是过去时了,但是你可以想象一下,通过忽略staff和address表,将store重命名为streaming_service,然后就可以把这个电影租赁公司当成电影流媒体公司了。但是,这本书中的例子并不会变,还是以电影租赁公司为例。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"使用mysql命令行工具","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除非是使用临时数据库会话(上一节中的第二个选择),否则需要启动mysql命令行工具才能与数据库交互。为此,你需要打开Windows或Unix shell并执行mysql程序。例如,如果你使用root帐户登录,需要执行以下操作:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql -u root -p;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然后你需要输入密码,登陆成功之后你会看到提示符mysql>。要查看所有可用的数据库,可以使用以下命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> show databases;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"--------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Database","attrs":{}},{"type":"text","text":" |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"--------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| information_schema |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| mysql |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| performance_schema |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| sakila |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| sys |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"--------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"5 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.01 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于你将使用Sakila数据库,因此需要通过use命令指定要使用的数据库:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"use","attrs":{}},{"type":"text","text":" sakila;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Database","attrs":{}},{"type":"text","text":" changed","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"无论何时调用mysql命令行工具,都可以指定要使用的用户名和数据库,如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql -u root -p sakila;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这样就不用在登录之后再键入use sakila;使用数据库了。现在你已经建立了会话并指定了数据库,接下来就可以使用SQL语句并查看结果了。例如,如果你想知道当前日期和时间,可以使用以下查询:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" now();","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| now() |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 2019-04-04 20:44:26 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.01 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函数now()是一个内置的MySQL函数,返回当前日期和时间。如你所见,mysql命令行工具将查询结果格式化为以+、-和|字符为边界的矩形。结果显示结束之后(在本例中,只有一行结果),mysql命令行工具显示返回了多少行,以及SQL语句执行所用的时间。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"关于从句缺失","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于某些数据库服务器,使用查询语句的时候必须包括from子句,并且至少要指明一个表名,否则无法正常查询,像广泛使用的Oracle数据库就是这样的。对于只需要调用函数的情况,Oracle提供了一个名为dual的表,该表由一个名为dummy的列组成,该列包含一行数据。为了与Oracle数据库兼容,MySQL也提供了dual表。因此,用于前面查询当前日期和时间的语句可以写成:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" now()  ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" dual;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| now() | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 2019-04-04 20:44:26 | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.01 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你不使用Oracle并且也不需要与它兼容,那么可以完全忽略dual表,只使用不带有from子句的select语句即可。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用mysql命令行工具后,只需键入quit;或exit;即可返回Unix或Windows command shell。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"MySQL数据类型","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般来说,所有流行的数据库服务器都能够存储相同类型的数据,比如字符串、日期和数字。它们的不同之处通常在于一些特殊的数据类型,比如XML和JSON文档或空间数据。由于这是一本关于SQL的入门书,而且你使用的数据列中98%都是简单的数据类型,因此本章仅介绍字符型、日期型和数值型的数据类型。第十八章将探讨如何使用SQL查询JSON文档。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"字符型数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字符数据可以存储为固定长度或可变长度的字符串,其不同之处在于:固定长度的字符串用空格向右填充,使得占用的字节数相同,而可变长度的字符串不需要向右填充,并且字节数是可变的。定义字符列的时候,必须指定该列能够存放字符串的最大长度。例如,如果要存储长度不超过20个字符的字符串,可以使用以下定义之一:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"char(20) ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"/* fixed-length */","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"varchar(20) ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"/* variable-length */","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"char列的最大长度当前为255字节,而varchar列的最大长度可达65535字节。如果你要存储更长的字符串(如电子邮件、XML文档等),那么就需要使用文本类型(mediumtext和longtext),后面会介绍这些内容。一般来说,当要存储在列中的所有字符串是等长时(如州名的缩写),应该使用char类型。当要存储在列中的字符串长度不同时,使用varchar类型。char和varchar在所有主流数据库服务器中的使用方法都类似。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:使用Oracle数据库的时候,varchar的使用方式不同。Oracle用户在定义可变长度的字符列时应使用varchar2类型","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"字符集","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于拉丁语系的语言(如英语)字符数足够少,因此只需要一个字节来存储每个字符。其他语言(如日语和韩语)则包含大量字符,每个字符需要多个字节来存储,因此这种字符集被称为多字节字符集。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL可以使用各种字符集(包括单字节和多字节)存储数据。要查看服务器所支持的字符集,可以使用show命令,如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> SHOW CHARACTER ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SET","attrs":{}},{"type":"text","text":";","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"----------+---------------------------------+---------------------+--------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| Charset | Description | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Default","attrs":{}},{"type":"text","text":" collation | Maxlen |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"----------+---------------------------------+---------------------+--------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ascii | US ASCII | ascii_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| binary | Binary pseudo charset | binary | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp1250 | Windows Central European | cp1250_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp1256 | Windows Arabic | cp1256_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp1257 | Windows Baltic | cp1257_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp850 | DOS West European | cp850_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp852 | DOS Central European | cp852_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp866 | DOS Russian | cp866_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cp932 | SJIS ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"for","attrs":{}},{"type":"text","text":" Windows Japanese | cp932_japanese_ci | 2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| dec8 | DEC West European | dec8_swedish_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| eucjpms | UJIS ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"for","attrs":{}},{"type":"text","text":" Windows Japanese | eucjpms_japanese_ci | 3 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| euckr | EUC-KR Korean | euckr_korean_ci | 2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| greek | ISO 8859-7 Greek | greek_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| hp8 | HP West European | hp8_english_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| latin1 | cp1252 West European | latin1_swedish_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| macce | Mac Central European | macce_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| macroman | Mac West European | macroman_general_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| tis620 | TIS620 Thai | tis620_thai_ci | 1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| utf8mb4 | UTF-8 Unicode | utf8mb4_0900_ai_ci | 4 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"----------+---------------------------------+---------------------+--------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"41 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.04 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果第四列maxlen中的值大于1,则表明该字符集是多字节字符集。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在MySQL服务器的早期版本中,latin1字符集被视作默认字符集,但版本8默认使用utf8mb4字符集。你可以选择为数据库中的每个字符列使用不同的字符集,甚至可以在同一个表中存储不同的字符集数据。要在定义列时选择非默认字符集,只需在类型定义后加上一个系统支持的字符集,如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"varchar(20) character ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" latin1","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL中,还可以为整个数据库设置默认字符集:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"create","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"database","attrs":{}},{"type":"text","text":" european_sales character ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" latin1;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对一本介绍性的书来说,介绍这些关于字符集的内容已经够多了,但是实际上关于国际化的主题中包含的内容更多。如果你要处理多个不熟悉的字符集,那么可能需要学习一本专业书籍,比如Jukka Korpela的Unicode Explained: Internationalize Documents, Programs, and Web Sites (O’Reilly)","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"文本数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果需要存储可能超过varchar列的限制的数据(64KB以上),则需要使用文本类型。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下表(2-1)显示了可用的文本类型及其最大长度:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1c/1ce4ffe33c21df3f3c24724684cd79d4.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 使用文本类型时,应注意以下几点:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 如果装载到文本列中的数据超过该类型的最大大小,数据将被截断。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 将数据装载到文本列中时,不会删除尾部的空格。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 使用文本列进行排序或分组时,仅使用前1024个字节,但必要时可放宽该限制。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 这些不同的文本类型是MySQL独有的。对于大字符数据,SQLServer只有一种文本类型(text类型),而DB2和Oracle使用一种称为clob的数据类型(Character Large Object)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 既然MySQL允许varchar列最多容纳65535字节(在版本4中限制为255字节),那么一般不需要特别使用tinytext或text类型了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你要创建一个用于存储自由格式数据的列,例如一个notes列来存储有关客户与公司的客服部门交互的数据,那么varchar可能就足够了。但是,如果要存储文档,则应选择mediumtext或longtext类型。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:Oracle数据库中char列最多容纳2000字节,varchar2列最多容纳4000字节。对于较大的文档,可以使用clob类型。对于char和varchar数据,SQL Server最多可以处理8000个字节,但是在定义为varchar(max)的列中最多可以存储2GB的数据。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"数值型数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"尽管使用叫“numeric”的数值数据类型似乎是合理的,但实际上存在好几种不同的数值数据类型,它们反映了使用数字的各种方式,如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"某列需要指示客户订单是否已发送","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 这种类型的列可以被设为Boolean,将包含一个0或1,分别表示false和true。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"系统为交易表生成的主键","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 这列数据通常从1开始,每次自增1,最后的数字可能会非常大。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"顾客电子购物篮的商品编号","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 这类列的值应该是一个正整数,范围从1到200(假设购物狂的购物篮能容纳最多的物品数量是200,当然也可能更大或更小)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"电路板钻床的位置数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 高精度的科学或制造数据通常要求精确到小数点后8位。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了处理这些类型的数据(以及更多类型的数据),MySQL提供几种不同的数值数据类型。最常用的是用来存储整数即integers的类型。在指定类型的时候,还可以指定数据是unsigned的,以向服务器指明存储这列的所有数据都将大于或等于零。下表(2-2)展示了用于存储整数的五种不同数据类型:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/54/5450ae71699abf100e7a27800c3f5033.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当你使用其中一个整数类型创建列时,MySQL将为存储数据分配适当大小的空间,范围从tinyint的一个字节到bigint的八个字节。因此在选择数据类型的时候,确保能够容纳你所预期的最大数据即可,以免白白浪费存储空间。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于浮点数(如3.1415927),可以从下表(2-3)所示的数字类型中选择:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b5/b549ebb707780f74c67362dfad63ebc0.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"使用浮点类型时,可以指定精度precision(小数点左侧到右侧允许的总位数)和有效位scale(小数点右侧允许的位数),不过这并不是必须的。上面两个值在上表(2-3)中表示为p和s。注意:为浮点类型的列指定精度和有效位的时候,如果位数超过该列定义的小数位数和精度,则存储在该列中的数据将被四舍五入。例如,定义为float(4,2)的列总共会存储四位数字,两位在小数点左侧,两位在小数点右侧。因此,这样的列可以很好地存储27.44和8.19这两个数字,但是存储17.8675将被四舍五入到17.87,存储178.375则会产生错误。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"与整数类型一样,浮点列可以被定义为无符号的(unsigned),但这只能防止列中存放负数,而无法改变列所能存储数据的范围。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"时间数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了字符串和数字,你肯定还会使用有关日期和时间的信息。这种类型的数据称为时间型数据,下面是数据库中有关时间型数据的一些例子:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 预计未来发生的特定事件的日期(例如运送客户订单);","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 客户订单的实际发货日期;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 用户修改表中某行的日期和时间;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 员工的出生日期;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 与数据仓库的yearly_sales表中,每行对应的年份;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 在汽车装配线上完成流水线所需的时间。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL能为上述所有情况提供合适的数据类型。MySQL支持的时间数据类型如下表(2-4)所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/30/305e6fc406eaa005b58eb3f6e37eeb6b.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d3/d3fb2bf8cecb2a5c3141315168e91d02.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 虽然数据库服务器能够以各种方式存储时间数据,但格式字符串(上表2-4的第二列)的目的是指定检索时如何显示数据,以及插入或更新时间列时需要提供日期字符串的格式。因此,如果要使用默认格式为YYYY-MM-DD的date列存储日期2020年3月23日则要使用字符串'2020-03-23'。第七章全面探讨了时间数据是如何构造和显示的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"datetime、timestamp和time类型还允许小数点后6位(微秒)的小数秒,使用这些数据类型之一定义列时,可以提供0到6之间的值;例如,指定datetime(2)将允许时间值包含百分之一秒。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"注意:每个数据库服务器针对时间列允许的日期范围各不相同。Oracle数据库接受从公元前4712年到公元9999年的日期,而SQL Server只能处理从公元1753年到公元9999年的日期(除非用的是SQL Server 2008的datetime2数据类型,它允许从公元1年到公元9999年的日期)。MySQL介于Oracle和SQL Server之间,可以存储从公元1000年到公元9999年的日期。虽然对于大多数处理当前和未来事件的系统来说,这些差别可能并不重要,但存放历史时期的时候就请务必记住这些差别了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面介绍如何使用各种时间类型来实现前面的例子:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 用于保存客户订单的预期未来发货日期和员工出生日期的列将使用date类型,因为未来发货时间精确到秒是不现实的,此外也没有必要知道一个人出生的具体时间。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 保存客户订单实际发货时间信息的列可以用datetime类型,因为不仅要记录发货日期,还要记录发货时间。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 记录用户修改表中某行的日期和时间,可以用使用timestamp类型。timestamp类型与datetime类型(包括年、月、日、小时、分钟、秒)保存的信息相同,但是当在表中新增一行或修改一行时,MySQL服务器将自动为timestamp列增加当前日期/时间信息。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 仅包含年份数据的列将使用year类型。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 保存完成任务所需时长数据的列将使用time类型。对于这种类型的数据,无需存储日期信息,因为你只关心完成任务所需的时/分/秒数。该类信息可以使用两个datetime列来存储(一个用于存放任务开始日期/时间,另一个用于存放任务完成日期/时间),两者的差值就是时长,但是很显然,使用单个time列会更简单。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第七章会探讨如何使用这些时间数据类型。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"创建表","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在你已经了解了MySQL数据库中可能存储的数据类型,所以是时候看看如何在表定义中使用这些类型了。让我们先定义一个表来保存个人信息。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"第一步:设计","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"开始设计表格之前,最好是头脑风暴一下,以确定需要包括的信息。以下是我在思考了一会儿之后想出的用于描述个人信息的项:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 姓名","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 眼睛颜色","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 出生日期","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 地址","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 喜爱的食物","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当然这些信息并不全面,但是这里已经够用了。下一步是分配列名和数据类型。下表(2-6)是我的初始设计:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/95/955d8f3c42c8a5c0ba0281b552336669.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"name、address和favorite_foods列的类型为varchar,允许任何格式的数据条目。eye_color列限制两个字符只应为BR、BL或GR中的一个。birth_date列的类型为date,因为不需要包括具体的时间信息。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"第二步:完善","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在第一章中,我们介绍过规范化的概念——规范化是确保数据库设计中没有重复(外键除外)或复合列的过程。再次查看person表中的列时,会发现以下问题:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• name列实际上是一个由名字和姓氏组成的复合对象。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 由于多个人可以拥有相同的姓名、眼睛颜色、出生日期等,因此person表中没有保证唯一性的列。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• address列也是一个复合对象,由街道、城市、州/省、国家和邮政编码组成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• favorite_foods列是一个包含零个、一个或多个独立条目的列表,所以最好为这些数据创建一个单独的表,然后在其中包含person表的外键,这样就可以知道某个特定食物归属的人员了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"考虑到以上问题,下表(2-7)给出了person表的规范化版本:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b1/b139881316d8654b4043d0e0bfa606b0.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"既然person表有主键(person_id)来保证唯一性,那么下一步就是构建一个favorite_food表,其中包含指向person表的外键,如下表(2-8)所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/22/222be70841c6bd1c738b9671eaddb0b5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"person_id和food列构成favorite_food表的主键,person_id列也是person表的外键。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"这些设计就够了吗?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"将favorite_foods列从person表中移出绝对是个好主意,但这样就够了吗?例如,如果一个人把pasta列为最喜欢的食物,而另一个人却把spaghetti列为最喜欢的食物,它们指的都是意大利面,那该怎么办?它们是一样的吗?为了防止此问题发生,你可以让人们从列表中选择他们最喜欢的食物,在这种情况下,应该创建一个包含food_id和food_name列的food表,然后修改favorite_food包含food表的外键。虽然这种设计是完全规范化的,但若你只是想存储用户输入的值,那么可以保持原有的表设计。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"第三步:构建SQL语句","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在已经完成了这两个表的设计,它们包括关于人员信息以及他们喜欢的食物的信息,下一步是生成SQL语句来在数据库中创建表。下面是创建person表的语句:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CREATE","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" person","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" (person_id SMALLINT UNSIGNED, ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"fname VARCHAR(20),","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" lname VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"eye_color CHAR(2), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"birth_date DATE, street VARCHAR(30), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"city VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"state VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"country VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"postal_code VARCHAR(20),","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CONSTRAINT","attrs":{}},{"type":"text","text":" pk_person ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"PRIMARY","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"KEY","attrs":{}},{"type":"text","text":" (person_id) );","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了最后一项,这条语句中的其他所有内容应该都很好理解。在定义表时,需要告诉数据库服务器哪些列将用作表的主键,可以通过在表上创建约束(constraint)实现这一点。可以向表定义中添加多种类型的表约束。上述语句的约束是主键约束,它在person_id列上创建,并命名为pk_person。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有关约束,对于person表而言,还有另一种类型的约束也很有用。在前面的表(2-6)中,第三列只接受特定的值(比如eye_color列的“BR”和“BL”),此时可以给它增加一个检查约束,用以限制该列存放的值。MySQL允许将检查约束附加到列定义,如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"eye_color CHAR(2) ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CHECK","attrs":{}},{"type":"text","text":" (eye_color ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"IN","attrs":{}},{"type":"text","text":" ('BR','BL','GR')),","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虽然检查约束在大多数数据库服务器上可以预期运行,但对MySQL服务器来说,它虽允许定义检查约束,但并不强制执行。其实MySQL确实有提供另一种名为enum的字符数据类型,它将检查约束合并到数据类型定义中。下面是用这种方法定义eye_color列的语句:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"eye_color ENUM('BR','BL','GR'),","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是person表的重定义,其中eye_color列以enum作为其数据类型:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CREATE","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(person_id SMALLINT UNSIGNED,  fname VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" lname VARCHAR(20),  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"eye_color ENUM('BR','BL','GR'), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" birth_date DATE, ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" street VARCHAR(30), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" city VARCHAR(20),  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"state VARCHAR(20),  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"country VARCHAR(20),  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"postal_code VARCHAR(20),  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CONSTRAINT","attrs":{}},{"type":"text","text":" pk_person ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"PRIMARY","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"KEY","attrs":{}},{"type":"text","text":" (person_id)  );","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在本章后面会介绍向列中添加违反检查约束(或者在MySQL中,违反其枚举值)的数据会发生什么事情。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在可以使用mysql命令行工具运行create table语句了。如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CREATE","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" person ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> (person_id SMALLINT UNSIGNED,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> fname VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> lname VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> eye_color ENUM('BR','BL','GR'), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> birth_date DATE, ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> street VARCHAR(30), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> city VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> state VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> country VARCHAR(20),","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> postal_code VARCHAR(20), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CONSTRAINT","attrs":{}},{"type":"text","text":" pk_person ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"PRIMARY","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"KEY","attrs":{}},{"type":"text","text":" (person_id) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> );","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 0 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" affected (0.37 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在处理完create table语句之后,MySQL服务器返回消息“Query OK,0 rows affected”,表示该语句没有语法错误。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果想确认person表的存在,可以使用describe命令(简称desc)查看表定义:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"desc","attrs":{}},{"type":"text","text":" person;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+----------------------+------+-----+---------+-------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| Field | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Type","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Null","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Key","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Default","attrs":{}},{"type":"text","text":" | Extra |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+----------------------+------+-----+---------+-------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| person_id | smallint(5) unsigned | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | PRI | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| fname | varchar(20) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| lname | varchar(20) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| eye_color | enum('BR','BL','GR') | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| birth_date | date | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| street | varchar(30) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| city | varchar(20) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| state | varchar(20) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| country | varchar(20) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| postal_code | varchar(20) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+----------------------+------+-----+---------+-------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"10 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.00 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"describe输出的第一列和第二列的含义很好理解,第三列显示将数据插入表中时是否可以省略特定列。现在我暂时不想深入探讨这个话题(简要介绍请参见下面的“什么是空值”),后面在第四章中,我们会全面探讨这个问题。第四列显示这列是否是键值(主键或外键),这个例子中,person_id列被标记为主键。第五列显示在向表中插入数据时,如果忽略某列,是否会用默认值填充该列。第六列(Extra)显示该列附加的说明信息。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"什么是空值?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在某些情况下,插入数据的时候,可能无法为表中的某列提供具体的值。例如,在新增有关新客户订单数据时,还不能确定ship_date列。在本例中,该列被设置为null(注意,我并不是说它等于“null”这个字符串),这表示没有值。null被用于无法赋值的各种情况,例如:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 不适用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 未知","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 空集","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在设计表时,可以指定哪些列允许为null(默认),哪些列不允许为null(通过在类型定义后添加关键字not null来指定)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在已经创建完person表,下一步是创建favorite_food表:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CREATE","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" favorite_food ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> (person_id SMALLINT UNSIGNED, ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> food VARCHAR(20),","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CONSTRAINT","attrs":{}},{"type":"text","text":" pk_favorite_food ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"PRIMARY","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"KEY","attrs":{}},{"type":"text","text":" (person_id, food), ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CONSTRAINT","attrs":{}},{"type":"text","text":" fk_fav_food_person_id ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FOREIGN","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"KEY","attrs":{}},{"type":"text","text":" (person_id)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"REFERENCES","attrs":{}},{"type":"text","text":" person (person_id) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> );","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 0 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" affected (0.10 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述语句与person表的create table语句非常相似,但是也有不同之处,如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 由于一个人可以有多种喜爱的食物(这也是创建此表的主要原因),因此要保证表中数据的唯一性,仅仅靠person_id列是不行的。故而该表有两列主键:person_id和food。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• favorite_food表包含另一类型的约束,称为外键约束,它限制favorite_food表中person_id列的值只能来自person表。加了这个约束之后,如果person表中还没有person_id为27的行,那么在favorite_food表中添加一行表示person_id为27并且喜欢的食物比萨的数据是不可能的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"注意:如果在第一次创建表时忘记创建外键约束,可以稍后通过alter table语句添加","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"执行完create table语句后,使用describe命令显示以下结果:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"desc","attrs":{}},{"type":"text","text":" favorite_food;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+----------------------+------+-----+---------+-------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| Field | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Type","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Null","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Key","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Default","attrs":{}},{"type":"text","text":" | Extra |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+----------------------+------+-----+---------+-------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| person_id | smallint(5) unsigned | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | PRI | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| food | varchar(20) | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | PRI | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+----------------------+------+-----+---------+-------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.00 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在我们已经将表创建完毕,接下来就要向其中添加一些数据了。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"操作与修改表","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"准备好person和favorite_food表后,可以开始研究四个SQL数据语句(insert、update、delete和select)了。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"插入数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于person和favorite_food表中还没有任何数据,因此在四种数据语句中,我们先研究insert语句。insert语句有三个主要组成部分:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 要添加数据的表的名称","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 要使用的列的名称","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 用于插入列的值","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实并不需要为表中的每一列提供数据(除非表中的所有列都定义为not null)。在某些情况下,初始insert语句中可能并不包含某列的值,这部分将在之后通过update语句获得更新。还有一些情况下,某列的值可能始终为null(例如在发货前取消的客户订单,ship_date列就不用再赋值了)。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"生成数字型主键数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在将数据插入person表之前,最好先讨论一下数字型主键的生成机制。除了随机选择数字外,还可以选择以下方式:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 查看表中当前主键的最大值,在此基础之上加1;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 让数据库服务器自动生成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虽然第一种方式看起来似乎很有效,但在多用户环境中它可能会出现问题,因为两个用户可能同时访问表并生成两个相同的值作为主键。实际上,如今市面上所有的数据库服务器都提供了一种安全、健壮的方法来生成数字型主键。在一些服务器中,比如Oracle数据库就使用了一个单独的模式(schema)对象,称为序列号(sequence);而在MySQL中,为主键列启用自动递增(auto-increment)功能即可。通常,在创建表的时候就应该执行该操作。现在再介绍一下另一种方案语句——alter table,它的功能是修改现有表的定义:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ALTER","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" person ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MODIFY","attrs":{}},{"type":"text","text":" person_id SMALLINT UNSIGNED AUTO_INCREMENT;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:如果在数据库中运行这些语句,则应首先禁用favorite_food表上的外键约束,在完成表的重定义之后,再启用约束。语句如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" foreign_key_checks=0; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ALTER","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" person  ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MODIFY","attrs":{}},{"type":"text","text":" person_id SMALLINT UNSIGNED AUTO_INCREMENT; ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" foreign_key_checks=1;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"该语句实质上重新定义了person表中的person_id列。现在再使用describe命令,可以看到person_id的Extra列下列出的自增特性:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"DESC","attrs":{}},{"type":"text","text":" person;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+------------------------+------+-----+---------+-----------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| Field | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Type","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Null","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Key","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Default","attrs":{}},{"type":"text","text":" | Extra |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+------------------------+------+-----+---------+-----------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| person_id | smallint(5) unsigned | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | PRI | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | auto_increment |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| . | | | | | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| . | | | | | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| . | | | | | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在向person表中插入数据时,只需为person_id列提供一个null值,MySQL将自动向该列提供下一个可用的主键数字(默认情况下,MySQL是从1开始递增的)。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"insert","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"语句","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在一切就绪,所以可以向表中添加一些数据了。下面的语句在person表中为William Turner创建一行数据:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INSERT","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INTO","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> (person_id, fname, lname, eye_color, birth_date)  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"VALUES","attrs":{}},{"type":"text","text":" (","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"null","attrs":{}},{"type":"text","text":", 'William','Turner', 'BR', '1972-05-27'); ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.22 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"运行结果“Query OK, 1 row affected”表示该语句语法正确,并且有一行被添加到了数据库中(因为它是insert语句)。可以通过select语句查看刚刚添加到表中的这条数据:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" person_id, fname, lname, birth_date  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" person; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| person_id | fname | lname | birth_date | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"| 1 | William | Turner | 1972-05-27 | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.06 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 如你所见,MySQL服务器为主键生成的值为1。因为person表中只有一行,所以我省略了查询条件以获取表中的所有行。但是,如果表中有多行,则可以添加where子句进行过滤,以指定需要获取的数据。检索person_id为1的行:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" person_id, fname, lname, birth_date    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id = 1; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| person_id | fname | lname | birth_date | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"| 1 | William | Turner | 1972-05-27 |  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.00 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 该查询指定了特定主键值,其实还可以指定表中的任意列,比如下面的查询就是用于查找lname列为Turner的行:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" person_id, fname, lname, birth_date  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" person ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" lname = 'Turner'; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"| person_id | fname | lname | birth_date | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 1 | William | Turner | 1972-05-27 | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.00 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 注意关于insert语句还有几点值得一提:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 没有为任何地址列提供值。因为这些列允许空值,所以没关系。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 为birth_date列提供的值是字符串。只要符合表2-4所示的格式,MySQL就会将字符串转换为日期类型。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"• 列名和提供的值必须在数字和类型上对应。如果表有七列但是插入的时候只提供六个值,或者提供的值无法转换为相应列的相应数据类型,则会产生错误。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"William Turner还提供了他最喜欢的三种食物的信息,因此还需要三条insert语句来存储他的食物偏好:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INSERT","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INTO","attrs":{}},{"type":"text","text":" favorite_food (person_id, food)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"VALUES","attrs":{}},{"type":"text","text":" (1, 'pizza');","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.01 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INSERT","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INTO","attrs":{}},{"type":"text","text":" favorite_food (person_id, food) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"VALUES","attrs":{}},{"type":"text","text":" (1, 'cookies');","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.00 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INSERT","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INTO","attrs":{}},{"type":"text","text":" favorite_food (person_id, food) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"VALUES","attrs":{}},{"type":"text","text":" (1, 'nachos');","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.01 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面的查询使用order by子句按字母顺序排列William最喜欢的食物:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" food  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" favorite_food ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id = 1  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ORDER","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"BY","attrs":{}},{"type":"text","text":" food; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"| food | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"| cookies | | nachos | | pizza | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"---------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.02 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"order by子句告诉服务器如何对查询返回的数据进行排序。如果不用order by子句,就不能保证表中的数据获取的顺序。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"William一个人可能太孤单,所以你可以再次执行insert语句将Susan Smith添加到person表:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INSERT","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INTO","attrs":{}},{"type":"text","text":" person ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> (person_id, fname, lname, eye_color, birth_date, ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> street, city, state, country, postal_code)  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"VALUES","attrs":{}},{"type":"text","text":" (","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"null","attrs":{}},{"type":"text","text":", 'Susan','Smith', 'BL', '1975-11-02', ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> '23 Maple St.', 'Arlington', 'VA', 'USA', '20220');  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.01 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于Susan提供了她的地址,上面的insert语句比之前插入William数据用到的语句多五列。如果再次查询该表,可以看到Susan所在行的主键值被赋值为2:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" person_id, fname, lname, birth_date  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" person;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+","attrs":{}},{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | person_id | fname | lname | birth_date |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+","attrs":{}},{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | 1 | William | Turner | 1972-05-27 || 2 | Susan | Smith | 1975-11-02 |  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-----------+---------+--------+------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.00 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"可以获取XML格式的数据吗?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你使用XML数据,就会很高兴地看到大多数数据库服务器提供了一种从查询结果中生成XML输出的简单方法。例如,对于MySQL,可以在调用mysql工具时使用--xml选项,如此一来,所有的输出都将自动使用xml格式化。下面演示如何获取XML文档格式的favorite_food数据:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C:\\database> mysql -u lrngsql -p ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"--xml bank","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Enter password: xxxxxx","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Welcome ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"to","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"the","attrs":{}},{"type":"text","text":" MySQL Monitor...  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" * ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" favorite_food;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"   ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 1   ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"cookies ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 1 ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  nachos","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"   ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  1   ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"pizza ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.00 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用SQL Server,不需要配置命令行工具,只需在每个查询的末尾添加for xml子句,如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SELECT","attrs":{}},{"type":"text","text":" * ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" favorite_food  ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FOR","attrs":{}},{"type":"text","text":" XML AUTO, ELEMENTS","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"更新数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当William Turner的数据被添加到表中时,insert语句中并没有提供地址列的数据。下面演示如何通过update语句更新这些列的数据:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"UPDATE","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SET","attrs":{}},{"type":"text","text":" street = '1225 Tremont St.', ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> city = 'Boston', ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> state = 'MA',","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  -> country = 'USA',  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> postal_code = '02138' ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id = 1;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.04 sec) ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Rows","attrs":{}},{"type":"text","text":" matched: 1 Changed: 1 Warnings: 0","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服务器响应了两行消息:“Rows matched: 1”项表示where子句中的条件与表中的一行匹配,“Changed: 1”项表示表中的一行数据已被修改。由于where子句指定了William所在行的主键,所以修改的数据肯定与你所预期的一样。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据where子句中的条件,还可以使用单个语句修改多行数据。例如,考虑如下where子句:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id < 10","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因为William和Susan的person_id值都小于10,所以它们的两行都将被修改。如果省略where子句,那么update语句将修改表中的每一行数据。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"删除数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在看来William和Susan相处得不太好,所以他们两人中得有一人离开。既然William是第一个来的,那么我们可以删除Susan的数据,使用delete语句如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"DELETE","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FROM","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id = 2; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.01 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同样,主键用于定位我们感兴趣的行,因此表中只会有一行数据被删除。与update语句一样,我们也可以根据where子句中的条件删除多行数据,如果省略where子句,则将删除表中所有行。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"导致错误的语句情况","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到目前为止,本章中演示的所有SQL数据语句都是符合标准格式并且能够正常运行的。然而,根据person和favorite_food表的表定义,在插入或修改数据时可能会出现很多运行错误。本节主要展示可能遇到的一些常见错误情况以及MySQL服务器是如何响应它们的。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"主键不唯一","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于表定义创建了主键约束,所以MySQL确保不会将重复的主键值插入表中。下一条语句忽略person_id列的自增特性,并在person表中创建person_id为1的另一行数据:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INSERT","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INTO","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> (person_id, fname, lname, eye_color, birth_date) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"VALUES","attrs":{}},{"type":"text","text":" (1, 'Charles','Fulton', 'GR', '1968-01-15'); ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ERROR 1062 (23000): Duplicate ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"entry","attrs":{}},{"type":"text","text":" '1' ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"for","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"key","attrs":{}},{"type":"text","text":" 'PRIMARY'","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于当前的方案对象来说,你完全可以创建两个具有相同姓名、地址、出生日期等条目的数据行,但是注意前提是它们的主键不同,也即person_id列的值不同。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"不存在的外键","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"favorite_food表的表定义在person_id列上创建了外键约束,此约束确保favorite_food表中所输入person_id列的值都在person表中存在。下面演示违背该约束创建新行的情况:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INSERT","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"INTO","attrs":{}},{"type":"text","text":" favorite_food (person_id, food)  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"VALUES","attrs":{}},{"type":"text","text":" (999, 'lasagna'); ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ERROR 1452 (23000): Cannot ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"add","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"or","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"update","attrs":{}},{"type":"text","text":" a ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"child","attrs":{}},{"type":"text","text":" row: a ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"foreign","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"key","attrs":{}},{"type":"text","text":" constraint fails ('sakila'.'favorite_food', ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CONSTRAINT","attrs":{}},{"type":"text","text":" 'fk_fav_food_person_id' ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FOREIGN","attrs":{}},{"type":"text","text":" KEY ('person_id') ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"REFERENCES","attrs":{}},{"type":"text","text":" 'person' ('person_id'))","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在这种情况下,因为favorite_food表的某些数据依赖于person表,所以可以将favorite_food表视为子表,将person表视为父表。如果要在两个表中都输入数据,则应该先在父表person中创建一行数据,才能在favorite_food中插入新数据。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:只有使用InnoDB存储引擎创建表时,才会强制执行外键约束。我们将在第十二章讨论MySQL的存储引擎。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"非法列值","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"person表中的eye_color列仅限于:“BR”表示棕色,“BL”表示蓝色,“GR”表示绿色。如果你试图将该列的值设置为任何其他值,那么你将会收到如下响应:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"UPDATE","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SET","attrs":{}},{"type":"text","text":" eye_color = 'ZZ' ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id = 1; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ERROR 1265 (01000): ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Data","attrs":{}},{"type":"text","text":" truncated ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"for","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"column","attrs":{}},{"type":"text","text":" 'eye_color' ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"at","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" 1","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这个错误消息有点让人迷糊,但你大致能知道服务器对于为eye_color列提供的值并不满意。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"无效的日期转换","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果构造用于产生date列的字符串,而该字符串与预期格式不匹配,则会产生又一个错误。以下示例使用的日期格式与默认的YYYY-MM-DD日期格式不匹配:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"UPDATE","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SET","attrs":{}},{"type":"text","text":" birth_date = 'DEC-21-1980' ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id = 1; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ERROR 1292 (22007): Incorrect date value: 'DEC-21-1980' ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"for","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"column","attrs":{}},{"type":"text","text":" 'birth_date' ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"at","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" 1","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般来说,最好显式指定格式字符串,而不是依赖默认格式。下面是语句的另一个版本,它使用str_to_date函数指定要使用的字符串格式:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"UPDATE","attrs":{}},{"type":"text","text":" person  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"SET","attrs":{}},{"type":"text","text":" birth_date = str_to_date('DEC-21-1980' , '%b-%d-%Y') ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" -> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"WHERE","attrs":{}},{"type":"text","text":" person_id = 1; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 1 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"row","attrs":{}},{"type":"text","text":" affected (0.12 sec) ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Rows","attrs":{}},{"type":"text","text":" matched: 1 Changed: 1 Warnings: 0","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不仅数据库服务器很高兴,威廉也很高兴(我们刚刚让他年轻了8岁,而且还不需要昂贵的整容手术!)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:在本章的前面介绍各种时态数据类型时,我展示过日期格式字符串,如YYYY-MM-DD。虽然许多数据库服务器使用这种格式,但MySQL使用%Y指定四位数字的年份。以下是在MySQL中将字符串转换为datetime时可能需要的其他一些格式:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%a The short weekday name, such as Sun, Mon, ...","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" %b The short month name, such as Jan, Feb, ... ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%c The numeric month (0..12)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" %d The numeric day of the month (00..31) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%f The number of microseconds (000000..999999) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%H The hour of the day, in 24-hour format (00..23) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%h The hour of the day, in 12-hour format (01..12)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" %i The minutes within the hour (00..59)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" %j The day of year (001..366) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%M The full month name (January..December) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%m The numeric month ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%p AM or PM %s The number of seconds (00..59)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" %W The full weekday name (Sunday..Saturday) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%w The numeric day of the week (0=Sunday..6=Saturday) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"%Y The four-digit year","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Sakila数据库","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在本书的其余部分,大多数示例将会用到名叫Sakila的示例数据库,该数据库由使用MySQL的好心人提供。这个数据库是一个DVD租赁连锁公司的模型,虽然有点过时,但只要发挥你的想象力,就可以把它当作一家视频流媒体公司。该数据库中包括的表格有customer、film、actor、payment、rental和category。当你按照本章开头介绍的步骤加载MySQL服务器并生成示例数据时,应该就已经创建了数据库的整个方案和示例数据。有关表字段以及相互关系的图示,请参见附录A。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下表(2-9)显示了Sakila中使用的一些表,以及每个表的简单定义:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f9/f94b3dbe3cadfbe43176c98e22c8b7b3.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"你可以随意使用这些表,还能新增自己的表以扩展业务功能。如果你修改了数据库之后,还想使用原来完好无损的数据库的话,就可以删除数据库,然后下载文件重新创建示例数据库。如果你使用的是临时会话,那么会话关闭时,你所做的所有更改都将丢失,因此你可能需要保留所做更改的脚本(代码),以便下一次重新执行它们。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果要查看数据库中可用的表,可以使用show tables命令,如下所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> show ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"tables","attrs":{}},{"type":"text","text":"; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"----------------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":" ","attrs":{}},{"type":"text","text":"| Tables_in_sakila |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"----------------------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| actor | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| actor_info | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| address |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"category","attrs":{}},{"type":"text","text":" | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| city | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| country | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| customer |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | customer_list | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| film | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| film_actor |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | film_category | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| film_list | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| film_text |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | inventory |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | language | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| nicer_but_slower_film_list |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | payment | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| rental | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| sales_by_film_category |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | sales_by_store | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| staff | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| staff_list |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"store","attrs":{}},{"type":"text","text":" | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"----------------------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"23 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"in","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"set","attrs":{}},{"type":"text","text":" (0.02 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了Sakila方案中的23个表之外,该列表还包括本章中创建的两个表:person和favorite_food,我们在后面的章节中不会使用它们了,所以可以通过使用下面的命令来删除它们:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"DROP","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" favorite_food; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 0 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" affected (0.56 sec) ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"DROP","attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TABLE","attrs":{}},{"type":"text","text":" person; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Query","attrs":{}},{"type":"text","text":" OK, 0 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"rows","attrs":{}},{"type":"text","text":" affected (0.05 sec)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果要查看表中的列,可以使用describe命令。下面是customer表的describe输出:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mysql> ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"desc","attrs":{}},{"type":"text","text":" customer; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+--------------+------+-----+-------------+----------------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| Field | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Type","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Null","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Key","attrs":{}},{"type":"text","text":" | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Default","attrs":{}},{"type":"text","text":" | Extra | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"+","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+--------------+------+-----+-------------+----------------------+ ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| customer_id | smallint(5) | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | PRI | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | auto_increment |  unsigned  | store_id | tinyint(3) | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | MUL | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"|  unsigned  | first_name | varchar(45) | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| last_name | varchar(45) | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | MUL | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| email | varchar(50) | YES | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| address_id | smallint(5) | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | MUL | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"unsigned  | active | tinyint(1) | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | | 1 | | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| create_date | datetime | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NO","attrs":{}},{"type":"text","text":" | | ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NULL","attrs":{}},{"type":"text","text":" | |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" | last_update | timestamp | YES | | CURRENT_ | DEFAULT_GENERATED on  TIMESTAMP ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"update","attrs":{}},{"type":"text","text":" CURRENT_  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TIMESTAMP |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"  +","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"-------------+--------------+------+-----+-------------+----------------------+","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你对示例数据库越熟悉,就越能理解示例,从而更好地理解后面章节中介绍的概念。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章