「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":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章