SQL 必知必會(第3版)

出版時間:2007-07-01  出版社:人民郵電出版社  作者:(英)Ben Forta  頁數(shù):204  譯者:鐘鳴,劉曉霞  
Tag標簽:無  

內容概要

  SQL 是目前使用最為廣泛的數(shù)據(jù)庫語言之一。本書沒有涉及理論,而是從實踐出發(fā),由淺入深地講解了廣大讀者所必需的SQL 知識,適用于各種主流數(shù)據(jù)庫。實例豐富,便于查閱。本書涉及不同平臺上數(shù)據(jù)的排序、過濾和分組,以及表、視圖、聯(lián)結、子查詢、游標、存儲過程和觸發(fā)器等內容,通過本書讀者可以系統(tǒng)地學習到SQL 的知識和方法?! ”緯⒅貙嵱眯?,操作性很強,適合于SQL 的初學者學習和廣大軟件開發(fā)及管理人員參考。

作者簡介

  Ben Forta是世界知名的技術作家,也是Adobe技術界最為知名的專家之一,目前擔任Adobe公司的高級技術推廣專家。他具有計算機行業(yè)20多年工作經驗,多年來撰寫了十幾本技術圖書,其中不少是世界暢銷書,已被翻譯為十幾種文字。除本書外,他撰寫的《正則表達式必知必會》也即將由人民郵電出版社出版。

書籍目錄

第1章 了解SQL第2章 檢索數(shù)據(jù)第3章 排序檢索數(shù)據(jù)第4章 過濾數(shù)據(jù)第5章 高級數(shù)據(jù)過濾第6章 用通配符進行過濾第7章 創(chuàng)建計算字段第8章 使用數(shù)據(jù)處理函數(shù)第9章 匯總數(shù)據(jù)第10章 分組數(shù)據(jù)第11章 使用子查詢第12章 聯(lián)結表第13章 創(chuàng)建高級聯(lián)結第14章 組合查詢第15章 插入數(shù)據(jù)第16章 更新和刪除數(shù)據(jù)第17章 創(chuàng)建和操縱表第18章 使用視圖第19章 使用存儲過程第20章 管理事務處理第21章 使用游標第22章 了解高級SQL特性附錄A 樣例表腳本A.1 樣例表A.2 獲得樣例表A.2.1 下載可供使用的Microsoft Access MDB文件A.2.2 下載DBMS SQL腳本附錄B 流行的應用系統(tǒng)B.1 使用Aqua Data StudioB.2 使用DB2B.3 使用Macromedia ColdFusionB.4 使用Microsoft AccessB.5 使用Microsoft ASPB.6 使用Microsoft ASP.NETB.7 使用Microsoft QueryB.8 使用Microsoft SQL ServerB.9 使用MySQLB.10 使用OracleB.11 使用PHPB.12 使用PostgreSQLB.13 使用Query ToolB.14 使用SybaseB.15 配置ODBC數(shù)據(jù)源附錄C SQL語句的語法C.1 ALTER TABLEC.2 COMMITC.3 CREATE INDEXC.4 CREATE PROCEDUREC.5 CREATE TABLEC.6 CREATE VIEWC.7 DELETEC.8 DROPC.9 INSERTC.10 INSERT SELECTC.11 ROLLBACKC.12 SELECTC.13 UPDATE附錄D SQL數(shù)據(jù)類型D.1 串數(shù)據(jù)類型D.2 數(shù)值數(shù)據(jù)類型D.3 日期和時間數(shù)據(jù)類型D.4 二進制數(shù)據(jù)類型附錄E SQL保留字索引

編輯推薦

  《SQL必知必會》(第3版)注重實用性,操作性很強,適合于SQL的初學者學習和廣大軟件開發(fā)及管理人員參考。

圖書封面

圖書標簽Tags

評論、評分、閱讀與下載


    SQL 必知必會(第3版) PDF格式下載


用戶評論 (總計17條)

 
 

  •     對于行家來說,這應該是本非常非?;A的書,但是這本書的目的也就是讓菜鳥能夠入門。知道那些數(shù)據(jù)庫的常用操作以及概念...通讀全書基本不累,理論上周末兩天就可以解決...話說本人不才,畢業(yè)N年從未學習過數(shù)據(jù)庫,現(xiàn)在算是了解了數(shù)據(jù)庫的基本操作語法,以及相關術語的出處,接下來對于那些數(shù)據(jù)庫操作的玩意兒應該有底了
      最后,說句受益匪淺
  •     失望。
      下個月項目要用到數(shù)據(jù)庫,翻了翻這本書。
      只能說是sql基本命令,各種select用法,對數(shù)據(jù)庫的原理和設計沒有任何介紹。屬于2天看完,1周忘掉的節(jié)奏。如果用到數(shù)據(jù)庫忘記sql語法,建議看看http://www.w3school.com.cn/算了,或者直接google。
      個人感覺,這本書用途不大。
  •     總體來說這是一本非?;A的數(shù)據(jù)庫方面的書。這本書幾乎涉及了SQL的全部內容。涉及的都不深入,比如存儲過程、事務處理、約束、游標這些高級內容就只是一筆帶過,作為一本初級入門書能夠大致了解一下高級主題為進一步深入了解做準備。
      在基礎語法方面做得很優(yōu)秀。SELECT查詢語句由淺入深講得很容易理解。由最簡單的檢索單個列,檢索多列,排序,WHERE子句過濾行到計算字段,使用函數(shù)以及匯總查詢和聯(lián)結多表查詢。這些方面講得透徹。
      比如,匯總查詢時,會提醒WHERE子句應位于GROUP BY子句之前,并且出現(xiàn)在SELECT后面的列名,要么是匯總字段,要么是GROUP BY子句的列名。
      不足之處是在插入和刪除講得簡略,似乎作者認為只要講好了查詢,這本書的工作就算是完成了。對于高級主題更是涉及很少。
      在使用這本書的時候有幾點需要注意:
      1、參考每個DBMS的聯(lián)機文檔,因為每個DBMS使用的SQL語法都不一致,在SQLServer 2008上能運行的SQL在MySQL上就不一定能運行。作者提的SQL也不是所有都能在每一個DBMS上運行。比如在ACCESS上是不支持CREATE VIEW操作的(解決的辦法是直接SELECT查詢保存查詢,并在另外一個查詢中引用此查詢名)。同時ACCESS不支持在定義數(shù)據(jù)表時使用Decimal數(shù)據(jù)類型。
      2、一定要對每一個SQL試驗一遍,甚至應該想出更多需要去查詢的信息。盡可能多地讓自己去想解決的方法和給自己出難題。如果覺得沒有,可以到論壇上CSDN上找別入無法解決的問題。
      ——————————————————————————————-————
      總體上,這本書是一本比較好的入門書。對于盡了解SQL是很有幫助的。
  •     很棒的書。
      老外的書總是那么有條理,由深入淺。你越看越想看。雖然后面的速度會慢些,內容難度有所提高。但是看下來后你會有對SQL的增刪改查足矣。
      這是一本基礎的書 ,不是sql基礎理論,而是一些常見應用的知識講解。
      值得一讀。
      講的“淺”,但是知識很實用 很基本的知識,也是使用中需要掌握的。由于現(xiàn)在的數(shù)據(jù)庫都有自己的方言,所以結合數(shù)據(jù)管理器的使用方法是另一說。你需要看其他的書來了解。但是基礎知識是通用的。
  •     記得是從候清榮那里借閱的,通過這本書可以輕松全面地掌握最常用的SQL,配套了示例腳本可以練習。通過本書可以快速入門。對我第一次找工作起了很大的作用。到甲方做項目經理三年了,別說程序,現(xiàn)在sql都寫得很少了,準備再郵購一本,抽空溫習一下。
  •     
        注1:該筆記為未完成版(最后修改日期為2009.3.25)
        注2:轉帖請注明出處(筆記作者:城市行走者)
      
      第0章 安裝mysql以及建庫、建表、導入數(shù)據(jù)
      第1章 了解SQL
      1.1數(shù)據(jù)庫基礎
      1.2 什么是SQL
      1.3 動手實踐
      第2章 檢索數(shù)據(jù)
      2.1 SELECT 語句
      2.2 檢索單個列
      2.3 檢索多個列
      2.4 檢索所有列
      第3章 排序檢索數(shù)據(jù)
      3.1 排序數(shù)據(jù)
      3.2 按多個列排序
      3.3 按列位置排序
      3.4 指定排序方向
      第4章 過濾數(shù)據(jù)
      4.1 使用WHERE子句
      4.2 WHERE子句操作符
      4.2.1 檢查單個值
      4.2.2 不匹配檢查
      4.2.3 范圍值檢查
      4.2.4 空值檢查
      第 5 章 高級數(shù)據(jù)過濾
      5.1 組合WHERE子句
      5.1.1 AND操作符 (與運算)
      5.1.2 OR操作符 (或運算)
      5.1.3 計算次序(操作符的優(yōu)先級)
      5.2 IN操作符
      5.3 NOT操作符
      第6章 用通配符進行過濾
      6.1 LIKE操作符
      6.1.1 百分號(%)通配符
      6.1.2 下劃線(_)通配符
      6.1.3 方括號([])通配符
      6.2 使用通配符的技巧
      附錄:MySQL正則表達式
      第7章 創(chuàng)建計算字段
      7.1 計算字段
      7.2 拼接字段
      7.3 執(zhí)行算術計算
      第8章 使用數(shù)據(jù)處理函數(shù)
      8.1 函數(shù)
      8.2 使用函數(shù)
      8.2.1 文本處理函數(shù)
      8.2.2 日期和時間處理函數(shù)
      8.2.3 數(shù)值處理函數(shù)
      第9章 匯總數(shù)據(jù)
      9.1 聚集函數(shù)
      9.2 聚集不同值
      9.3 組合聚集函數(shù)
      第10章 分組數(shù)據(jù)
      10.1 數(shù)據(jù)分組
      10.2 創(chuàng)建分組
      10.3 過濾分組
      10.4 分組和排序
      10.5 SELECT子句順序
      第11章 使用子查詢
      11.1 子查詢
      11.2 利用子查詢進行過濾
      11.3 作為計算字段使用子查詢
      第12章 聯(lián)結表(join table)
      12.1 聯(lián)結
      12.1.1 關系表
      12.2 創(chuàng)建聯(lián)結
      12.2.2 內部聯(lián)結
      12.2.3 聯(lián)結多個表
      第13章 創(chuàng)建高級聯(lián)結
      13.1 使用表別名
      13.2 使用不同類型的聯(lián)結
      13.2.1 自聯(lián)結
      13.2.2 自然聯(lián)結
      13.2.3 外部聯(lián)結
      13.3 使用帶聚集函數(shù)的聯(lián)結
      13.4 使用聯(lián)結和聯(lián)結條件
      第14章 組合查詢
      14.1 組合查詢
      14.2 創(chuàng)建組合查詢
      14.2.1 使用UNION
      14.2.2 UNION規(guī)則
      14.2.3 包含或取消重復的行
      14.2.4 對組合查詢排序
      第15章 插入數(shù)據(jù)
      15.1 插入數(shù)據(jù)
      15.1.1 插入完整的行
      15.1.2 插入部分行
      15.1.3 插入檢索出的數(shù)據(jù)
      15.2 從一個表復制到另一個表
      第16章 更新和刪除數(shù)據(jù)
      16.1 更新數(shù)據(jù)
      16.2 刪除數(shù)據(jù)
      16.3 更新和刪除的指導原則
      第17章 創(chuàng)建和操縱表
      17.1 創(chuàng)建表
      17.1.1 表創(chuàng)建基礎
      17.1.2 使用NULL值
      17.1.3 制定默認值
      17.2 更新表結構
      17.3 刪除表
      17.4 重命名表名
      第18章 使用視圖
      18.1 視圖
      18.1.1 為什使用視圖
      18.1.2 視圖的規(guī)則和限制
      18.2 創(chuàng)建視圖
      18.2.1 利用視圖簡化復雜的聯(lián)結
      18.2.2 用視圖重新格式化檢索出的數(shù)據(jù)
      18.2.3 用視圖過濾不想要的數(shù)據(jù)
      18.2.4 使用視圖與計算字段
      18.3 小結
      第19章 使用存儲過程
      附錄: Oracle 和 mysql 的一些簡單SQL操作命令對比
      
      第0章 安裝mysql以及建庫、建表、導入數(shù)據(jù)
      安裝mysql時,向導建立的管理員用戶和密碼
      root:lvhejia001;'
      
       一、連接mysql。
      
       1、首先在打開DOS窗口,再鍵入命令mysql -uroot -p, 回車后提示你輸密碼。
      2、直接利用開始菜單!開始----程序---mysql5.0
      
      二、建庫、建表、導入數(shù)據(jù)
      1、 顯示數(shù)據(jù)庫列表。
      例子:
      mysql> show tables;
      +-------------------+
      | Tables_in_example |
      +-------------------+
      | customers |
      | orderitems |
      | orders |
      | products |
      | vendors |
      +-------------------+
      5 rows in set (0.00 sec)
      剛開始時才兩個數(shù)據(jù)庫:mysql和test。 mysql庫很重要它里面有mysql的系統(tǒng)信息,
      我們改密碼和新增用戶,實際上就是用這個庫進行操作。
      
      2、 顯示庫中的數(shù)據(jù)表:
      語法:use 數(shù)據(jù)庫名; //打開庫,學過FOXBASE的一定不會陌生吧
       show 表名;
      
      3、 顯示數(shù)據(jù)表的結構:
      語法:describe 表名;
      
      4、 建庫:
      語法:create database 庫名;
      
      例子:
      mysql> create database example;
      Query OK, 1 row affected (0.00 sec)
      
      mysql> use example;
      Database changed
      
      mysql> show tables;
      Empty set (0.00 sec)
      
      
      
      5、 建表:
      語法:use 庫名;
       create table 表名(字段設定列表);
      
      使用Ben Forta提供的例子,直接復制粘貼。
      -------------------------
      -- Create Customers table
      -------------------------
      CREATE TABLE Customers
      (
       cust_id char(10) NOT NULL ,
       cust_name char(50) NOT NULL ,
       cust_address char(50) NULL ,
       cust_city char(50) NULL ,
       cust_state char(5) NULL ,
       cust_zip char(10) NULL ,
       cust_country char(50) NULL ,
       cust_contact char(50) NULL ,
       cust_email char(255) NULL
      );
      ……
      無錯誤提示,再檢查一下
      mysql> show tables;
      +-------------------+
      | Tables_in_example |
      +-------------------+
      | customers |
      | orderitems |
      | orders |
      | products |
      | vendors |
      +-------------------+
      5 rows in set (0.00 sec)
      
      6、導入數(shù)據(jù)
      還是使用Ben Forta提供的例子,直接復制粘貼。
      ---------------------------
      -- Populate Customers table
      ---------------------------
      INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
      VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com');
      …… ……
      
      無錯誤提示
      第1章 了解SQL
      1.1數(shù)據(jù)庫基礎
      1.1.1 數(shù)據(jù)庫(database):保存有組織的數(shù)據(jù)的倉庫(數(shù)據(jù)集合)。
      1.1.2 表(table):一種結構化的文件。
      1.1.3 列(column):表中的一個字段(field)。
      數(shù)據(jù)類型(data type):每列都有相應的數(shù)據(jù)類型,它限制或容許該列中存儲的數(shù)據(jù)。
      注意:雖然大多數(shù)的數(shù)據(jù)類型得到一致的支持,但許多更為高級的數(shù)據(jù)類型卻不是這樣。更糟的是,我們偶熱會發(fā)現(xiàn)相同的數(shù)據(jù)類型在不同的DBMS中具有不同的名稱。對此用戶毫無辦法,重要的是在創(chuàng)建表結構時要記住這些差異。
      1.1.4 行(row):表中的一個記錄(record)。
      1.1.5 主鍵(primary key):一列(或一組列),其值能夠唯一標識表中的每一列。
      作為主鍵的列,需要滿足以下條件:
      1、任意兩行都不具有相同的主鍵值;
      2、每一行都必須具有一個主鍵值(主鍵列不允許NULL值);
      3、主鍵列中的值不允許修改或更新;
      4、主鍵值不能重用(如果某行從表中刪除,它的主鍵不能賦給以后的新行)
      
      1.2 什么是SQL
      SQL(Structured Query Language):結構化查詢語言,是一種專門用來與數(shù)據(jù)庫通信的語言。
      注意:標準SQL有ANSI標準委員會管理,從而成為ANSI SQL。各個DBMS供應商通過增加語句或指令,對標準SQL進行擴展。
      
      1.3 動手實踐
      這里使用的DBMS為MySQL。
      第2章 檢索數(shù)據(jù)
      2.1 SELECT 語句
      關鍵字(keyword):作為SQL組成部分的保留字。
      附錄E ——SQL保留字(略)
      
      2.2 檢索單個列
      mysql> select prod_name
       -> from products;
      
      +---------------------+
      | prod_name |
      +---------------------+
      | Fish bean bag toy |
      | Bird bean bag toy |
      | Rabbit bean bag toy |
      | 8 inch teddy bear |
      | 12 inch teddy bear |
      | 18 inch teddy bear |
      | Raggedy Ann |
      | King doll |
      | Queen doll |
      +---------------------+
      9 rows in set (0.20 sec)
      
      注意:1、SQL語句可以分行。
       2、多條SQL語句必須以分號(;)分隔。
       3、SQL語句不區(qū)分大小寫。
      
      2.3 檢索多個列
      列名之間必須以逗號分隔。
      mysql> select prod_id, prod_name, prod_price
       -> from products;
      
      +---------+---------------------+------------+
      | prod_id | prod_name | prod_price |
      +---------+---------------------+------------+
      | BNBG01 | Fish bean bag toy | 3.49 |
      | BNBG02 | Bird bean bag toy | 3.49 |
      | BNBG03 | Rabbit bean bag toy | 3.49 |
      | BR01 | 8 inch teddy bear | 5.99 |
      | BR02 | 12 inch teddy bear | 8.99 |
      | BR03 | 18 inch teddy bear | 11.99 |
      | RGAN01 | Raggedy Ann | 4.99 |
      | RYL01 | King doll | 9.49 |
      | RYL02 | Queen doll | 9.49 |
      +---------+---------------------+------------+
      9 rows in set (0.00 sec)
      
      2.4 檢索所有列
      通配符——星號(*)
      mysql> select *
       -> from products;
      第3章 排序檢索數(shù)據(jù)
      3.1 排序數(shù)據(jù)
      mysql> select prod_name
       -> from products
       -> order by prod_name;
      +---------------------+
      | prod_name |
      +---------------------+
      | 12 inch teddy bear |
      | 18 inch teddy bear |
      | 8 inch teddy bear |
      | Bird bean bag toy |
      | Fish bean bag toy |
      | King doll |
      | Queen doll |
      | Rabbit bean bag toy |
      | Raggedy Ann |
      +---------------------+
      9 rows in set (0.16 sec)
      
      注意:order by子句必須是select語句中的最后一條子句。
      
      3.2 按多個列排序
      
      mysql> select prod_id, prod_price, prod_name
       -> from products
       -> order by prod_price, prod_name;
      
      +---------+------------+---------------------+
      | prod_id | prod_price | prod_name |
      +---------+------------+---------------------+
      | BNBG02 | 3.49 | Bird bean bag toy |
      | BNBG01 | 3.49 | Fish bean bag toy |
      | BNBG03 | 3.49 | Rabbit bean bag toy |
      | RGAN01 | 4.99 | Raggedy Ann |
      | BR01 | 5.99 | 8 inch teddy bear |
      | BR02 | 8.99 | 12 inch teddy bear |
      | RYL01 | 9.49 | King doll |
      | RYL02 | 9.49 | Queen doll |
      | BR03 | 11.99 | 18 inch teddy bear |
      +---------+------------+---------------------+
      9 rows in set (0.00 sec)
      
      3.3 按列位置排序
      mysql> select prod_id, prod_price, prod_name
       -> from products
       -> order by 2, 3;
      
      +---------+------------+---------------------+
      | prod_id | prod_price | prod_name |
      +---------+------------+---------------------+
      | BNBG02 | 3.49 | Bird bean bag toy |
      | BNBG01 | 3.49 | Fish bean bag toy |
      | BNBG03 | 3.49 | Rabbit bean bag toy |
      | RGAN01 | 4.99 | Raggedy Ann |
      | BR01 | 5.99 | 8 inch teddy bear |
      | BR02 | 8.99 | 12 inch teddy bear |
      | RYL01 | 9.49 | King doll |
      | RYL02 | 9.49 | Queen doll |
      | BR03 | 11.99 | 18 inch teddy bear |
      +---------+------------+---------------------+
      9 rows in set (0.00 sec)
      
      3.4 指定排序方向
      排序時默認為升序ASC(ASDENDING),如果需要降序,則通過關鍵字DESC(DESCENDING)。
      mysql> select prod_id, prod_price, prod_name
       -> from products
       -> order by prod_price desc;
      
      +---------+------------+---------------------+
      | prod_id | prod_price | prod_name |
      +---------+------------+---------------------+
      | BR03 | 11.99 | 18 inch teddy bear |
      | RYL01 | 9.49 | King doll |
      | RYL02 | 9.49 | Queen doll |
      | BR02 | 8.99 | 12 inch teddy bear |
      | BR01 | 5.99 | 8 inch teddy bear |
      | RGAN01 | 4.99 | Raggedy Ann |
      | BNBG01 | 3.49 | Fish bean bag toy |
      | BNBG02 | 3.49 | Bird bean bag toy |
      | BNBG03 | 3.49 | Rabbit bean bag toy |
      +---------+------------+---------------------+
      9 rows in set (0.00 sec)
      
      注意:DESC關鍵字之應用到直接位于其前面的列名。如果想對多個列進行排序,必須對每個列指定DESC關鍵字。
      第4章 過濾數(shù)據(jù)
      4.1 使用WHERE子句
      mysql> select prod_name, prod_price
       -> from products
       -> where prod_price = 3.49;
      
      +---------------------+------------+
      | prod_name | prod_price |
      +---------------------+------------+
      | Fish bean bag toy | 3.49 |
      | Bird bean bag toy | 3.49 |
      | Rabbit bean bag toy | 3.49 |
      +---------------------+------------+
      3 rows in set (0.00 sec)
      
      4.2 WHERE子句操作符
      
      操作符 說明
      =
      !=
      <>
      <
      <=
      >
      >=
      BETWEEN
      IS UNLL
      
      4.2.1 檢查單個值
      mysql> select prod_name, prod_price
       -> from products
       -> where prod_price < 10;
      
      +---------------------+------------+
      | prod_name | prod_price |
      +---------------------+------------+
      | Fish bean bag toy | 3.49 |
      | Bird bean bag toy | 3.49 |
      | Rabbit bean bag toy | 3.49 |
      | 8 inch teddy bear | 5.99 |
      | 12 inch teddy bear | 8.99 |
      | Raggedy Ann | 4.99 |
      | King doll | 9.49 |
      | Queen doll | 9.49 |
      +---------------------+------------+
      8 rows in set (0.00 sec)
      4.2.2 不匹配檢查
      mysql> select vend_id, prod_name
       -> from products
       -> where vend_id != 'DLL01';
      +---------+--------------------+
      | vend_id | prod_name |
      +---------+--------------------+
      | BRS01 | 8 inch teddy bear |
      | BRS01 | 12 inch teddy bear |
      | BRS01 | 18 inch teddy bear |
      | FNG01 | King doll |
      | FNG01 | Queen doll |
      +---------+--------------------+
      5 rows in set (0.00 sec)
      
      注意:!=和<>通常可以互換。但是,并非所有的DBMS都支持這兩種操作符。例如Microsoft Access支持<>而不支持!=。如有疑問,請參閱相應的DBMS文檔。
      4.2.3 范圍值檢查
      mysql> select prod_name, prod_price
       -> from products
       -> where prod_price between 5 and 10;
      
      +--------------------+------------+
      | prod_name | prod_price |
      +--------------------+------------+
      | 8 inch teddy bear | 5.99 |
      | 12 inch teddy bear | 8.99 |
      | King doll | 9.49 |
      | Queen doll | 9.49 |
      +--------------------+------------+
      4 rows in set (0.00 sec)
      4.2.4 空值檢查
      mysql> select vend_name, vend_state
       -> from vendors
       -> where vend_state is null;
      
      +----------------+------------+
      | vend_name | vend_state |
      +----------------+------------+
      | Fun and Games | NULL |
      | Jouets et ours | NULL |
      +----------------+------------+
      2 rows in set (0.00 sec)
      第 5 章 高級數(shù)據(jù)過濾
      5.1 組合WHERE子句
      操作符(operateor):用來聯(lián)接或改變WHERE子句中的子句的關鍵詞。也稱邏輯操作符(logical operator)。
      5.1.1 AND操作符 (與運算)
      mysql> select prod_id, prod_price, prod_name
       -> from products
       -> where vend_id = 'DLL01' and prod_price <= 4;
      
      +---------+------------+---------------------+
      | prod_id | prod_price | prod_name |
      +---------+------------+---------------------+
      | BNBG01 | 3.49 | Fish bean bag toy |
      | BNBG02 | 3.49 | Bird bean bag toy |
      | BNBG03 | 3.49 | Rabbit bean bag toy |
      +---------+------------+---------------------+
      3 rows in set (0.00 sec)
      5.1.2 OR操作符 (或運算)
      mysql> select vend_id, prod_name, prod_price
       -> from products
       -> where vend_id = 'DLL01' or vend_id = 'BRS01';
      +---------+---------------------+------------+
      | vend_id | prod_name | prod_price |
      +---------+---------------------+------------+
      | DLL01 | Fish bean bag toy | 3.49 |
      | DLL01 | Bird bean bag toy | 3.49 |
      | DLL01 | Rabbit bean bag toy | 3.49 |
      | BRS01 | 8 inch teddy bear | 5.99 |
      | BRS01 | 12 inch teddy bear | 8.99 |
      | BRS01 | 18 inch teddy bear | 11.99 |
      | DLL01 | Raggedy Ann | 4.99 |
      +---------+---------------------+------------+
      
      注意:事實上,許多DBMS在OR子句的第一個條件滿足的情況下,不再計算第二個條件(即,第一個條件滿足時,不管第二個條件是否滿足,相應的行都將被檢索出來)。
      5.1.3 計算次序(操作符的優(yōu)先級)
      以下列表顯示了操作符優(yōu)先級的由低到高的順序。排列在同一行的操作符具有相同的優(yōu)先級。
      
      :=
      ||, OR, XOR
      &&, AND
      NOT
      BETWEEN, CASE, WHEN, THEN, ELSE
      =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
      |
      &
      <<, >>
      -, +
      *, /, DIV, %, MOD
      ^
      - (一元減號), ~ (一元比特反轉)
      !
      BINARY, COLLATE
      
      注意:不要過分依賴默認的優(yōu)先級。任何時候使用具有AND和OR的操作符的WHERE子句時,都應該使用圓括號明確的分組操作符。使用圓括號沒有什么壞處,它能消除歧義。
      5.2 IN操作符
      mysql> select vend_id, prod_name, prod_price
       -> from products
       -> where vend_id = 'DLL01' or vend_id = 'BRS01';
      
      +---------+---------------------+------------+
      | vend_id | prod_name | prod_price |
      +---------+---------------------+------------+
      | DLL01 | Fish bean bag toy | 3.49 |
      | DLL01 | Bird bean bag toy | 3.49 |
      | DLL01 | Rabbit bean bag toy | 3.49 |
      | BRS01 | 8 inch teddy bear | 5.99 |
      | BRS01 | 12 inch teddy bear | 8.99 |
      | BRS01 | 18 inch teddy bear | 11.99 |
      | DLL01 | Raggedy Ann | 4.99 |
      +---------+---------------------+------------+
      7 rows in set (0.00 sec)
      
      注意:IN操作符可以完成OR操作符完全相同的功能。
      
      IN操作符最大的優(yōu)點是可以包含其他SELECT語句,使得能夠更動態(tài)的建立WHERE子句。詳細介紹在第11章。
      5.3 NOT操作符
      mysql> select vend_id, prod_name, prod_price
       -> from products
       -> where not vend_id = 'DLL01' ;
      
      +---------+--------------------+------------+
      | vend_id | prod_name | prod_price |
      +---------+--------------------+------------+
      | BRS01 | 8 inch teddy bear | 5.99 |
      | BRS01 | 12 inch teddy bear | 8.99 |
      | BRS01 | 18 inch teddy bear | 11.99 |
      | FNG01 | King doll | 9.49 |
      | FNG01 | Queen doll | 9.49 |
      +---------+--------------------+------------+
      5 rows in set (0.00 sec)
      
      注意:NOT操作符在復雜子句中用處更大,比如與IN操作符聯(lián)合使用——NOT IN。
      第6章 用通配符進行過濾
      6.1 LIKE操作符
      通配符(wildcard):用來匹配值(文本字段,即“串”)的一部分的特殊字符。
      搜索模式(search pattern):由字面值、通配符或者兩者組合構成的搜索條件。
      6.1.1 百分號(%)通配符
      百分號(%)表示任何字符出現(xiàn)任意次數(shù)。
      mysql> select prod_name
       -> from products
       -> where prod_name like 'fish%';
      
      +-------------------+
      | prod_name |
      +-------------------+
      | Fish bean bag toy |
      +-------------------+
      1 row in set (0.01 sec)
      
      注意:根據(jù)DBMS的不同及其配置,搜索可以是區(qū)分大小寫的!
      
      mysql> select prod_name
       -> from products
       -> where prod_name like 'f%y';
      
      +-------------------+
      | prod_name |
      +-------------------+
      | Fish bean bag toy |
      +-------------------+
      1 row in set (0.00 sec)
      注意:要特別小心后面所跟的空格。許多DBMS,包括Microsoft Access,都用空格來填補字段內容。
      6.1.2 下劃線(_)通配符
      下劃線(_)表示任意單個字符。
      mysql> select prod_name
       -> from products
       -> where prod_name like '_ inch teddy bear';
      
      +-------------------+
      | prod_name |
      +-------------------+
      | 8 inch teddy bear |
      +-------------------+
      1 row in set (0.00 sec)
      
      mysql> select prod_name
       -> from products
       -> where prod_name like '% inch teddy bear';
      
      +--------------------+
      | prod_name |
      +--------------------+
      | 8 inch teddy bear |
      | 12 inch teddy bear |
      | 18 inch teddy bear |
      +--------------------+
      3 rows in set (0.00 sec)
      6.1.3 方括號([])通配符
      方括號([])通配符:用來指定一個字符集。
      注意:并不是所有的SBMS都支持方括號([])通配符
      6.2 使用通配符的技巧
      除非絕對有必要,否則不要把它們用在搜索模式的開始處。把通配符置于搜索模式的開始處,搜索起來是最慢的。
      
      
      附錄:MySQL正則表達式
      
      正則表達式是為復雜搜索指定模式的強大方式。
      MySQL采用Henry Spencer的正則表達式實施,其目標是符合POSIX 1003.2。請參見附錄C:感謝。MySQL采用了擴展的版本,以支持在SQL語句中與REGEXP操作符一起使用的模式匹配操作。請參見3.3.4.7節(jié),“模式匹配”。
      在本附錄中,歸納了在MySQL中可用于REGEXP操作的特殊字符和結構,并給出了一些示例。本附錄未包含可在Henry Spencer的regex(7)手冊頁面中發(fā)現(xiàn)的所有細節(jié)。該手冊頁面包含在MySQL源碼分發(fā)版中,位于regex目錄下的regex.7文件中。
      正則表達式描述了一組字符串。最簡單的正則表達式是不含任何特殊字符的正則表達式。例如,正則表達式hello匹配hello。
      非平凡的正則表達式采用了特殊的特定結構,從而使得它們能夠與1個以上的字符串匹配。例如,正則表達式hello|word匹配字符串hello或字符串word。
      作為一個更為復雜的示例,正則表達式B[an]*s匹配下述字符串中的任何一個:Bananas,Baaaaas,Bs,以及以B開始、以s結束、并在其中包含任意數(shù)目a或n字符的任何其他字符串。
      對于REGEXP操作符,正則表達式可以使用任何下述特殊字符和結構:
      ? ^
      匹配字符串的開始部分。
      mysql> SELECT 'fo\nfo' REGEXP '^fo$'; -> 0
      mysql> SELECT 'fofo' REGEXP '^fo'; -> 1
      ? $
      匹配字符串的結束部分。
      mysql> SELECT 'fo\no' REGEXP '^fo\no$'; -> 1
      mysql> SELECT 'fo\no' REGEXP '^fo$'; -> 0
      ? .
      匹配任何字符(包括回車和新行)。
      mysql> SELECT 'fofo' REGEXP '^f.*$'; -> 1
      mysql> SELECT 'fo\r\nfo' REGEXP '^f.*$'; -> 1
      ? a*
      匹配0或多個a字符的任何序列。
      mysql> SELECT 'Ban' REGEXP '^Ba*n'; -> 1
      mysql> SELECT 'Baaan' REGEXP '^Ba*n'; -> 1
      mysql> SELECT 'Bn' REGEXP '^Ba*n'; -> 1
      ? a+
      匹配1個或多個a字符的任何序列。
      mysql> SELECT 'Ban' REGEXP '^Ba+n'; -> 1
      mysql> SELECT 'Bn' REGEXP '^Ba+n'; -> 0
      ? a?
      匹配0個或1個a字符。
      mysql> SELECT 'Bn' REGEXP '^Ba?n'; -> 1
      mysql> SELECT 'Ban' REGEXP '^Ba?n'; -> 1
      mysql> SELECT 'Baan' REGEXP '^Ba?n'; -> 0
      ? de|abc
      匹配序列de或abc。
      mysql> SELECT 'pi' REGEXP 'pi|apa'; -> 1
      mysql> SELECT 'axe' REGEXP 'pi|apa'; -> 0
      mysql> SELECT 'apa' REGEXP 'pi|apa'; -> 1
      mysql> SELECT 'apa' REGEXP '^(pi|apa)$'; -> 1
      mysql> SELECT 'pi' REGEXP '^(pi|apa)$'; -> 1
      mysql> SELECT 'pix' REGEXP '^(pi|apa)$'; -> 0
      ? (abc)*
      匹配序列abc的0個或多個實例。
      mysql> SELECT 'pi' REGEXP '^(pi)*$'; -> 1
      mysql> SELECT 'pip' REGEXP '^(pi)*$'; -> 0
      mysql> SELECT 'pipi' REGEXP '^(pi)*$'; -> 1
      ? {1}, {2,3}
      {n}或{m,n}符號提供了編寫正則表達式的更通用方式,能夠匹配模式的很多前述原子(或“部分”)。m和n均為整數(shù)。
      o a*
      可被寫入為a{0,}。
      o a+
      可被寫入為a{1,}。
      o a?
      可被寫入為a{0,1}。
      更準確地講,a{n}與a的n個實例準確匹配。a{n,}匹配a的n個或更多實例。a{m,n}匹配a的m~n個實例,包含m和n。
      m和n必須位于0~RE_DUP_MAX(默認為255)的范圍內,包含0和RE_DUP_MAX。如果同時給定了m和n,m必須小于或等于n。
      mysql> SELECT 'abcde' REGEXP 'a[bcd]{2}e'; -> 0
      mysql> SELECT 'abcde' REGEXP 'a[bcd]{3}e'; -> 1
      mysql> SELECT 'abcde' REGEXP 'a[bcd]{1,10}e'; -> 1
      ? [a-dX], [^a-dX]
      匹配任何是(或不是,如果使用^的話)a、b、c、d或X的字符。兩個其他字符之間的“-”字符構成一個范圍,與從第1個字符開始到第2個字符之間的所有字符匹配。例如,[0-9]匹配任何十進制數(shù)字 。要想包含文字字符“]”,它必須緊跟在開括號“[”之后。要想包含文字字符“-”,它必須首先或最后寫入。對于[]對內未定義任何特殊含義的任何字符,僅與其本身匹配。
      mysql> SELECT 'aXbc' REGEXP '[a-dXYZ]'; -> 1
      mysql> SELECT 'aXbc' REGEXP '^[a-dXYZ]$'; -> 0
      mysql> SELECT 'aXbc' REGEXP '^[a-dXYZ]+$'; -> 1
      mysql> SELECT 'aXbc' REGEXP '^[^a-dXYZ]+$'; -> 0
      mysql> SELECT 'gheis' REGEXP '^[^a-dXYZ]+$'; -> 1
      mysql> SELECT 'gheisa' REGEXP '^[^a-dXYZ]+$'; -> 0
      ? [.characters.]
      在括號表達式中(使用[和]),匹配用于校對元素的字符序列。字符為單個字符或諸如新行等字符名。在文件regexp/cname.h中,可找到字符名稱的完整列表。
      mysql> SELECT '~' REGEXP '[[.~.]]'; -> 1
      mysql> SELECT '~' REGEXP '[[.tilde.]]'; -> 1
      ? [=character_class=]
      在括號表達式中(使用[和]),[=character_class=]表示等同類。它與具有相同校對值的所有字符匹配,包括它本身,例如,如果o和(+)均是等同類的成員,那么[[=o=]]、[[=(+)=]]和[o(+)]是同義詞。等同類不得用作范圍的端點。
      ? [:character_class:]
      在括號表達式中(使用[和]),[:character_class:]表示與術語類的所有字符匹配的字符類。標準的類名稱是:
      alnum 文字數(shù)字字符
      alpha 文字字符
      blank 空白字符
      cntrl 控制字符
      digit 數(shù)字字符
      graph 圖形字符
      lower 小寫文字字符
      print 圖形或空格字符
      punct 標點字符
      space 空格、制表符、新行、和回車
      upper 大寫文字字符
      xdigit 十六進制數(shù)字字符
      它們代表在ctype(3)手冊頁面中定義的字符類。特定地區(qū)可能會提供其他類名。字符類不得用作范圍的端點。
      mysql> SELECT 'justalnums' REGEXP '[[:alnum:]]+'; -> 1
      mysql> SELECT '!!' REGEXP '[[:alnum:]]+'; -> 0
      ? [[:<:]], [[:>:]]
      這些標記表示word邊界。它們分別與word的開始和結束匹配。word是一系列字字符,其前面和后面均沒有字字符。字字符是alnum類中的字母數(shù)字字符或下劃線(_)。
      mysql> SELECT 'a word a' REGEXP '[[:<:]]word[[:>:]]'; -> 1
      mysql> SELECT 'a xword a' REGEXP '[[:<:]]word[[:>:]]'; -> 0
      要想在正則表達式中使用特殊字符的文字實例,應在其前面加上2個反斜杠“\”字符。MySQL解析程序負責解釋其中一個,正則表達式庫負責解釋另一個。例如,要想與包含特殊字符“+”的字符串“1+2”匹配,在下面的正則表達式中,只有最后一個是正確的:
      mysql> SELECT '1+2' REGEXP '1+2'; -> 0
      mysql> SELECT '1+2' REGEXP '1\+2'; -> 0
      mysql> SELECT '1+2' REGEXP '1\\+2'; -> 1
      
      這是MySQL參考手冊的翻譯版本,關于MySQL參考手冊,請訪問dev.mysql.com。原始參考手冊為英文版,與英文版參考手冊相比,本翻譯版可能不是最新的。
      
      第7章 創(chuàng)建計算字段
      7.1 計算字段
      計算字段并不是實際存在出數(shù)據(jù)庫中。計算字段是運行時在SELECT語句中創(chuàng)建的。
      字段(field)與列(column):數(shù)據(jù)庫中一般稱為列(column),而術語字段(field)通常用在計算字段的連接上。
      7.2 拼接字段
      拼接(concatenate):將值聯(lián)接到一起構成單個值。操作符根據(jù)不同的DBMS而有差異。
      Oracle、DB2、Sybase使用||。
      SQL Server和Sybase使用加號(+)。
      MySQL使用CONCAT函數(shù)(拼接函數(shù)concatenate)。
      
      mysql> select CONCAT(vend_name ,' (',vend_country,')')
       -> from vendors;
      
      +------------------------------------------+
      | concat(vend_name ,' (',vend_country,')') |
      +------------------------------------------+
      | Bear Emporium (USA) |
      | Bears R Us (USA) |
      | Doll House Inc. (USA) |
      | Fun and Games (England) |
      | Furball Inc. (USA) |
      | Jouets et ours (France) |
      +------------------------------------------+
      6 rows in set (0.00 sec)
      
      TRIM函數(shù):去掉空格(略)
      
      使用別名,關鍵詞為AS(也可省略)。
      mysql> select concat(vend_name ,' (',vend_country,')') AS vend_title
       -> from vendors;
      +-------------------------+
      | vend_title |
      +-------------------------+
      | Bear Emporium (USA) |
      | Bears R Us (USA) |
      | Doll House Inc. (USA) |
      | Fun and Games (England) |
      | Furball Inc. (USA) |
      | Jouets et ours (France) |
      +-------------------------+
      6 rows in set (0.00 sec)
      
      mysql> select concat(vend_name ,' (',vend_country,')') vend_title
       -> from vendors;
      +-------------------------+
      | vend_title |
      +-------------------------+
      | Bear Emporium (USA) |
      | Bears R Us (USA) |
      | Doll House Inc. (USA) |
      | Fun and Games (England) |
      | Furball Inc. (USA) |
      | Jouets et ours (France) |
      +-------------------------+
      6 rows in set (0.00 sec)
      
      注意:別名一般是一個單詞(可用下劃線連接多個單詞組成)。
      7.3 執(zhí)行算術計算
      SQL算術操作符
      +
      -
      *
      /
      
      
      第8章 使用數(shù)據(jù)處理函數(shù)
      8.1 函數(shù)
      與幾乎所有的DBMS都同等地支持SQL語句不同,每一個DBMS都有特定的函數(shù)。
      所以,SQL函數(shù)是不可移植的。
      可移植(portable):所編寫的代碼可以在多個系統(tǒng)上運行。
      
      注意:是否應該使用函數(shù)?如果你決定使用函數(shù),應該保證做好代碼注釋,以便以后你(或者其他人)能確切的知道所編寫SQL代碼的含義。
      8.2 使用函數(shù)
      8.2.1 文本處理函數(shù)
      函數(shù) 說明
      LEFT( )
      LENGTH( )
      LOWER( )
      LTRIM( )
      RTRIM( )
      RIGHT( )
      SOUNDEX( ) 語音匹配
      UPPER( )
      
      mysql> select upper( vend_name)
       -> from vendors;
      +-------------------+
      | upper( vend_name) |
      +-------------------+
      | BEAR EMPORIUM |
      | BEARS R US |
      | DOLL HOUSE INC. |
      | FUN AND GAMES |
      | FURBALL INC. |
      | JOUETS ET OURS |
      +-------------------+
      6 rows in set (0.00 sec)
      8.2.2 日期和時間處理函數(shù)
       Oracle mysql
      日期函數(shù) SQL> select sysdate from dual; mysql> select now();
      mysql> select sysdate();
      mysql> select curdate();
      mysql> select current_date;
      mysql> select curtime();
      mysql> select current_time;
      日期格式化 SQL> select to_char(sysdate,'yyyy-mm-dd') from dual;
      SQL> select to_char(sysdate,'hh24-mi-ss') from dual; mysql> select date_format(now(),'%Y-%m-%d');
      mysql> select time_format(now(),'%H-%i-%S');
      
      mysql> select order_date
       -> from orders;
      +---------------------+
      | order_date |
      +---------------------+
      | 2004-05-01 00:00:00 |
      | 2004-01-12 00:00:00 |
      | 2004-01-30 00:00:00 |
      | 2004-02-03 00:00:00 |
      | 2004-02-08 00:00:00 |
      +---------------------+
      5 rows in set (0.00 sec)
      
      mysql> select order_date
       -> from orders
       -> where month(order_date) = 02;
      +---------------------+
      | order_date |
      +---------------------+
      | 2004-02-03 00:00:00 |
      | 2004-02-08 00:00:00 |
      +---------------------+
      2 rows in set (0.00 sec)
      
      mysql> select order_date
       -> from orders
       -> where date_format(order_date, '%m') = 02;
      +---------------------+
      | order_date |
      +---------------------+
      | 2004-02-03 00:00:00 |
      | 2004-02-08 00:00:00 |
      +---------------------+
      2 rows in set (0.00 sec)
      8.2.3 數(shù)值處理函數(shù)
      在主要DBMS函數(shù)中,數(shù)值函數(shù)的統(tǒng)一性最高。
      函數(shù) 說明
      ABS( ) 絕對值
      COS( )
      EXP( ) 指數(shù)值
      PI( ) 圓周率
      SIN( )
      SQRT( ) 平方根
      TAN( )
      
      第9章 匯總數(shù)據(jù)
      9.1 聚集函數(shù)
      聚集函數(shù)(aggregate function):從多個輸入行中計算,并返回單個值的函數(shù)。
      
      SQL聚集函數(shù)
      AVG( )
      COUNT( )
      MAX( )
      MIN( )
      SUM( )
      
      COUNT( )函數(shù)有兩種使用方式:
      1、使用COUNT(*)對表中行的數(shù)據(jù)進行計數(shù),不管表的列中包含的是空值(NULL)還是非空值。
      2、使用COUNT(column)對特定列中具有值的行進行計數(shù),忽略空值(NULL)。
      9.2 聚集不同值
      DISTINCT參數(shù):只包含不同的值。
      9.3 組合聚集函數(shù)
      (略)
      第10章 分組數(shù)據(jù)
      10.1 數(shù)據(jù)分組
      分組允許把數(shù)據(jù)分為多個邏輯組,以便能對每個組進行聚集計算。
      10.2 創(chuàng)建分組
      分組在SELECT語句的GROUP BY子句中創(chuàng)建。
      mysql> select vend_id, count(*) num_prods
       -> from products
       -> group by vend_id;
      +---------+-----------+
      | vend_id | num_prods |
      +---------+-----------+
      | BRS01 | 3 |
      | DLL01 | 4 |
      | FNG01 | 2 |
      +---------+-----------+
      3 rows in set (0.00 sec)
      
      mysql> select vend_id
       -> from products;
      +---------+
      | vend_id |
      +---------+
      | BRS01 |
      | BRS01 |
      | BRS01 |
      | DLL01 |
      | DLL01 |
      | DLL01 |
      | DLL01 |
      | FNG01 |
      | FNG01 |
      +---------+
      9 rows in set (0.00 sec)
      
      mysql> select vend_id
       -> from products
       -> group by vend_id;
      +---------+
      | vend_id |
      +---------+
      | BRS01 |
      | DLL01 |
      | FNG01 |
      +---------+
      3 rows in set (0.00 sec)
      
      注意:除聚集語句外,SELECT語句中的每個列都必須在GROUP BY子句中給出。
      下面是一個反例:
      mysql> select prod_name, count(*) num_prods
       -> from products
       -> group by vend_id;
      +-------------------+-----------+
      | prod_name | num_prods |
      +-------------------+-----------+
      | 8 inch teddy bear | 3 |
      | Fish bean bag toy | 4 |
      | King doll | 2 |
      +-------------------+-----------+
      3 rows in set (0.00 sec)
      輸出結果沒有意義。
      
      mysql> select vend_id, prod_name
       -> from products;
      +---------+---------------------+
      | vend_id | prod_name |
      +---------+---------------------+
      | DLL01 | Fish bean bag toy |
      | DLL01 | Bird bean bag toy |
      | DLL01 | Rabbit bean bag toy |
      | BRS01 | 8 inch teddy bear |
      | BRS01 | 12 inch teddy bear |
      | BRS01 | 18 inch teddy bear |
      | DLL01 | Raggedy Ann |
      | FNG01 | King doll |
      | FNG01 | Queen doll |
      +---------+---------------------+
      9 rows in set (0.00 sec)
      10.3 過濾分組
      WHERE過濾行,而HAVING過濾分組。
      HAVING支持所有的WHERE操作符。
      注意:HAVING和WHERE的差別,WHERE在數(shù)據(jù)分組前進行過濾,HAVING在數(shù)據(jù)分組后進行過濾。也就是說,WHERE排出的行不包括在分組中。
      mysql> select vend_id, count(*) num_prods
       -> from products
       -> group by vend_id
       -> having count(*) >= 3;
      +---------+-----------+
      | vend_id | num_prods |
      +---------+-----------+
      | BRS01 | 3 |
      | DLL01 | 4 |
      +---------+-----------+
      2 rows in set (0.00 sec)
      
      HAVING和WHERE同時出現(xiàn):
      mysql> select vend_id, count(*) num_prods
       -> from products
       -> where prod_price >=4
       -> group by vend_id
       -> having count(*) >=2;
      +---------+-----------+
      | vend_id | num_prods |
      +---------+-----------+
      | BRS01 | 3 |
      | FNG01 | 2 |
      +---------+-----------+
      2 rows in set (0.00 sec)
      
      mysql> select vend_id, count(*) num_prods
       -> from products
       -> group by vend_id;
      +---------+-----------+
      | vend_id | num_prods |
      +---------+-----------+
      | BRS01 | 3 |
      | DLL01 | 4 |
      | FNG01 | 2 |
      +---------+-----------+
      3 rows in set (0.00 sec)
      10.4 分組和排序
      GROUP BY的輸出可能排序,也可能不排序。保證數(shù)據(jù)正確排序的方法是使用ORDER BY子句。
      10.5 SELECT子句順序
      SELECT
      FROM
      WHERE
      GROUP BY
      HAVING
      ORDER BY
      第11章 使用子查詢
      11.1 子查詢
      查詢(query):任何SQL語句都是查詢。但此術語一般指SELECT語句。
      子查詢(subquery):嵌套在其他查詢中的查詢。
      11.2 利用子查詢進行過濾
      格式化SQL:可以把子查詢分解為多行,并適當?shù)倪M行縮進。
      
      注意:作為子查詢的SELECT語句只能查詢(返回)單個列。企圖檢索多個列將返回錯誤。
     ?。ɡ勇裕?br />   11.3 作為計算字段使用子查詢
      (例子略)
      第12章 聯(lián)結表(join table)
      12.1 聯(lián)結
      聯(lián)結(join):join常見不規(guī)范譯法還有“聯(lián)接”、“連接”。
      12.1.1 關系表
      理解關系表的最好方法是來看一個現(xiàn)實世界中的例子。
      作者寫得非常好,是我見過的最通俗易懂的!中文暫時略
      英文如下:
      The best way to understand relational tables is to look at a real-world example.
      Suppose you had a database table containing a product catalog, with each catalog item in its own row. The kind of information you would store with each item would include a product description and price, along with vendor information about the company that creates the product.
      Now suppose that you had multiple catalog items created by the same vendor. Where would you store the vendor information (things like vendor name, address, and contact information)? You wouldn't want to store that data along with the products for several reasons:
      Because the vendor information is the same for each product that vendor produces, repeating the information for each product is a waste of time and storage space.
      If vendor information changes (for example, if the vendor moves or his area code changes), you would need to update every occurrence of the vendor information.
      When data is repeated (that is, the vendor information is used with each product), there is a high likelihood that the data will not be entered exactly the same way each time. Inconsistent data is extremely difficult to use in reporting.
      The key here is that having multiple occurrences of the same data is never a good thing, and that principle is the basis for relational database design. Relational tables are designed so that information is split into multiple tables, one for each data type. The tables are related to each other through common values (and thus the relational in relational design).
      In our example, you can create two tables, one for vendor information and one for product information. The Vendors table contains all the vendor information, one table row per vendor, along with a unique identifier for each vendor. This value, called a primary key, can be a vendor ID, or any other unique value.
      The Products table stores only product information, and no vendor specific information other than the vendor ID (the Vendors table's primary key). This key relates the Vendors table to the Products table, and using this vendor ID enables you to use the Vendors table to find the details about the appropriate vendor.
      What does this do for you? Well, consider the following:
      Vendor information is never repeated, and so time and space are not wasted.
      If vendor information changes, you can update a single record, the one in the Vendors table. Data in related tables does not change.
      As no data is repeated, the data used is obviously consistent, making data reporting and manipulation much simpler.
      The bottom line is that relational data can be stored efficiently and manipulated easily. Because of this, relational databases scale far better than nonrelational databases.
      
      Scale Able to handle an increasing load without failing. A well-designed database or application is said to scale well.
      
      
      Why Use Joins?
      As just explained, breaking data into multiple tables enables more efficient storage, easier manipulation, and greater scalability. But these benefits come with a price.
      If data is stored in multiple tables, how can you retrieve that data with a single SELECT statement?
      The answer is to use a join. Simply put, a join is a mechanism used to associate tables within a SELECT statement (and thus the name join). Using a special syntax, multiple tables can be joined so that a single set of output is returned, and the join associates the correct rows in each table on-the-fly.
      
      Using Interactive DBMS Tools It is important to understand that a join is not a physical entity—in other words, it does not exist in the actual database tables. A join is created by the DBMS as needed, and it persists for the duration of the query execution.
      Many DBMSs provide graphical interfaces that can be used to define table relationships interactively. These tools can be invaluable in helping to maintain referential integrity. When using relational tables, it is important that only valid data is inserted into relational columns. Going back to the example, if an invalid vendor ID is stored in the Products table, those products would be inaccessible because they would not be related to any vendor. To prevent this from occurring, the database can be instructed to only allow valid values (ones present in the Vendors table) in the vendor ID column in the Products table. Referential integrity means that the DBMS enforces data integrity rules. And these rules are often managed through DBMS provided interfaces.
      
      12.2 創(chuàng)建聯(lián)結
      mysql> select vend_name, prod_name, prod_price
       -> from vendors, products
       -> where vendors.vend_id = products.vend_id;
      +-----------------+---------------------+------------+
      | vend_name | prod_name | prod_price |
      +-----------------+---------------------+------------+
      | Bears R Us | 8 inch teddy bear | 5.99 |
      | Bears R Us | 12 inch teddy bear | 8.99 |
      | Bears R Us | 18 inch teddy bear | 11.99 |
      | Doll House Inc. | Fish bean bag toy | 3.49 |
      | Doll House Inc. | Bird bean bag toy | 3.49 |
      | Doll House Inc. | Rabbit bean bag toy | 3.49 |
      | Doll House Inc. | Raggedy Ann | 4.99 |
      | Fun and Games | King doll | 9.49 |
      | Fun and Games | Queen doll | 9.49 |
      +-----------------+---------------------+------------+
      9 rows in set (0.03 sec)
      
      笛卡爾積(cartesian product):由沒有連接條件的表關系返回的結果為笛卡爾積。檢索出的行數(shù)是第一個表的行數(shù)乘以第二個表的行數(shù)。
      為了理解這一點,請看沒有連接條件的SELECT語句及輸出。
      mysql> select vend_name, prod_name, prod_price
       -> from vendors, products;
      +-----------------+---------------------+------------+
      | vend_name | prod_name | prod_price |
      +-----------------+---------------------+------------+
      | Bear Emporium | Fish bean bag toy | 3.49 |
      | Bears R Us | Fish bean bag toy | 3.49 |
      | Doll House Inc. | Fish bean bag toy | 3.49 |
      | Fun and Games | Fish bean bag toy | 3.49 |
      | Furball Inc. | Fish bean bag toy | 3.49 |
      | Jouets et ours | Fish bean bag toy | 3.49 |
      | Bear Emporium | Bird bean bag toy | 3.49 |
      | Bears R Us | Bird bean bag toy | 3.49 |
      | Doll House Inc. | Bird bean bag toy | 3.49 |
      | Fun and Games | Bird bean bag toy | 3.49 |
      | Furball Inc. | Bird bean bag toy | 3.49 |
      | Jouets et ours | Bird bean bag toy | 3.49 |
      | Bear Emporium | Rabbit bean bag toy | 3.49 |
      | Bears R Us | Rabbit bean bag toy | 3.49 |
      | Doll House Inc. | Rabbit bean bag toy | 3.49 |
      | Fun and Games | Rabbit bean bag toy | 3.49 |
      | Furball Inc. | Rabbit bean bag toy | 3.49 |
      | Jouets et ours | Rabbit bean bag toy | 3.49 |
      | Bear Emporium | 8 inch teddy bear | 5.99 |
      | Bears R Us | 8 inch teddy bear | 5.99 |
      | Doll House Inc. | 8 inch teddy bear | 5.99 |
      | Fun and Games | 8 inch teddy bear | 5.99 |
      | Furball Inc. | 8 inch teddy bear | 5.99 |
      | Jouets et ours | 8 inch teddy bear | 5.99 |
      | Bear Emporium | 12 inch teddy bear | 8.99 |
      | Bears R Us | 12 inch teddy bear | 8.99 |
      | Doll House Inc. | 12 inch teddy bear | 8.99 |
      | Fun and Games | 12 inch teddy bear | 8.99 |
      | Furball Inc. | 12 inch teddy bear | 8.99 |
      | Jouets et ours | 12 inch teddy bear | 8.99 |
      | Bear Emporium | 18 inch teddy bear | 11.99 |
      | Bears R Us | 18 inch teddy bear | 11.99 |
      | Doll House Inc. | 18 inch teddy bear | 11.99 |
      | Fun and Games | 18 inch teddy bear | 11.99 |
      | Furball Inc. | 18 inch teddy bear | 11.99 |
      | Jouets et ours | 18 inch teddy bear | 11.99 |
      | Bear Emporium | Raggedy Ann | 4.99 |
      | Bears R Us | Raggedy Ann | 4.99 |
      | Doll House Inc. | Raggedy Ann | 4.99 |
      | Fun and Games | Raggedy Ann | 4.99 |
      | Furball Inc. | Raggedy Ann | 4.99 |
      | Jouets et ours | Raggedy Ann | 4.99 |
      | Bear Emporium | King doll | 9.49 |
      | Bears R Us | King doll | 9.49 |
      | Doll House Inc. | King doll | 9.49 |
      | Fun and Games | King doll | 9.49 |
      | Furball Inc. | King doll | 9.49 |
      | Jouets et ours | King doll | 9.49 |
      | Bear Emporium | Queen doll | 9.49 |
      | Bears R Us | Queen doll | 9.49 |
      | Doll House Inc. | Queen doll | 9.49 |
      | Fun and Games | Queen doll | 9.49 |
      | Furball Inc. | Queen doll | 9.49 |
      | Jouets et ours | Queen doll | 9.49 |
      +-----------------+---------------------+------------+
      54 rows in set (0.00 sec)
      其中,vendors有6行,products有9行。
      注意:應該保證所有的聯(lián)結都有WHERE子句。
      12.2.2 內部聯(lián)結
      目前為止所用的聯(lián)結稱為等值聯(lián)結(equi-join),它基于兩個表之間的相等測試。這種聯(lián)結也稱為內部聯(lián)結。其實,對于這種聯(lián)結可以使用稍微不同的語法來明確指定聯(lián)結的類型。
      mysql> select vend_name, prod_name, prod_price
       -> from vendors INNER JOIN products
       -> ON vendors.vend_id = products.vend_id;
      “正確的”語法——ANSI SQL規(guī)范首選INNER JOIN語法。
      12.2.3 聯(lián)結多個表
      多做實驗——
      第13章 創(chuàng)建高級聯(lián)結
      13.1 使用表別名
      mysql> select vend_name, prod_name, prod_price
       -> from vendors v, products p
       -> where v.vend_id = p.vend_id;
      +-----------------+---------------------+------------+
      | vend_name | prod_name | prod_price |
      +-----------------+---------------------+------------+
      | Bears R Us | 8 inch teddy bear | 5.99 |
      | Bears R Us | 12 inch teddy bear | 8.99 |
      | Bears R Us | 18 inch teddy bear | 11.99 |
      | Doll House Inc. | Fish bean bag toy | 3.49 |
      | Doll House Inc. | Bird bean bag toy | 3.49 |
      | Doll House Inc. | Rabbit bean bag toy | 3.49 |
      | Doll House Inc. | Raggedy Ann | 4.99 |
      | Fun and Games | King doll | 9.49 |
      | Fun and Games | Queen doll | 9.49 |
      +-----------------+---------------------+------------+
      9 rows in set (0.00 sec)
      注意:Oracle不支持AS關鍵詞。
      13.2 使用不同類型的聯(lián)結
      到現(xiàn)在為止,我們使用的只是成為內部聯(lián)結或等值聯(lián)結的簡單聯(lián)結。下面來看3中其他聯(lián)結:自聯(lián)結、自然聯(lián)結、外聯(lián)結。
      13.2.1 自聯(lián)結
      略
      13.2.2 自然聯(lián)結
      自然聯(lián)結:是每個列只返回一次,排除多次出現(xiàn)的列。怎樣完成這項工作呢?答案是,系統(tǒng)不完成這項工作,由你自己完成。這一般是對表使用通配符(select *),對所有其他表的列使用明確的子集完成的。
      例子略
      13.2.3 外部聯(lián)結
      目前為止所用的聯(lián)結稱為等值聯(lián)結(equi-join),它基于兩個表之間的相等測試。這種聯(lián)結也稱為內部聯(lián)結。
      mysql> select vend_name, prod_name, prod_price
       -> from vendors INNER JOIN products
       -> ON vendors.vend_id = products.vend_id;
      +-----------------+---------------------+------------+
      | vend_name | prod_name | prod_price |
      +-----------------+---------------------+------------+
      | Bears R Us | 8 inch teddy bear | 5.99 |
      | Bears R Us | 12 inch teddy bear | 8.99 |
      | Bears R Us | 18 inch teddy bear | 11.99 |
      | Doll House Inc. | Fish bean bag toy | 3.49 |
      | Doll House Inc. | Bird bean bag toy | 3.49 |
      | Doll House Inc. | Rabbit bean bag toy | 3.49 |
      | Doll House Inc. | Raggedy Ann | 4.99 |
      | Fun and Games | King doll | 9.49 |
      | Fun and Games | Queen doll | 9.49 |
      +-----------------+---------------------+------------+
      9 rows in set (0.03 sec)
      
      與內部聯(lián)結不同,外部聯(lián)結包括沒有關聯(lián)的行。下面的例子,LEFT OUTER JOIN選擇from子句左邊表的所有行。(即,列出所有供貨商及其產品,包括沒有提供產品的供貨商,下面一節(jié)——13.3 使用帶聚集函數(shù)的聯(lián)結中的例子更直接,更好理解。)
      mysql> select vend_name, prod_name, prod_price
       -> from vendors LEFT OUTER JOIN products
       -> ON vendors.vend_id = products.vend_id;
      +-----------------+---------------------+------------+
      | vend_name | prod_name | prod_price |
      +-----------------+---------------------+------------+
      | Bear Emporium | NULL | NULL |
      | Bears R Us | 8 inch teddy bear | 5.99 |
      | Bears R Us | 12 inch teddy bear | 8.99 |
      | Bears R Us | 18 inch teddy bear | 11.99 |
      | Doll House Inc. | Fish bean bag toy | 3.49 |
      | Doll House Inc. | Bird bean bag toy | 3.49 |
      | Doll House Inc. | Rabbit bean bag toy | 3.49 |
      | Doll House Inc. | Raggedy Ann | 4.99 |
      | Fun and Games | King doll | 9.49 |
      | Fun and Games | Queen doll | 9.49 |
      | Furball Inc. | NULL | NULL |
      | Jouets et ours | NULL | NULL |
      +-----------------+---------------------+------------+
      12 rows in set (0.00 sec)
      13.3 使用帶聚集函數(shù)的聯(lián)結
      對供貨商的產品進行計數(shù)
      mysql> select vend_name, count(prod_name)
       -> from vendors INNER JOIN products
       -> ON vendors.vend_id = products.vend_id
       -> group by vend_name;
      +-----------------+------------------+
      | vend_name | count(prod_name) |
      +-----------------+------------------+
      | Bears R Us | 3 |
      | Doll House Inc. | 4 |
      | Fun and Games | 2 |
      +-----------------+------------------+
      3 rows in set (0.00 sec)
      
      對供貨商的產品進行計數(shù),包括沒有提供產品的供貨商。
      mysql> select vend_name, count(prod_name)
       -> from vendors LEFT OUTER JOIN products
       -> ON vendors.ven
  •     花了兩個下午讀完了這本書,感覺內容比較淺,沒有什么太深奧和太思想的東西。當工具書感覺有些介紹的還不是很全,不過感覺初學SQL的可以讀讀,易讀性比較高,另外介紹的東西也都是很基礎的知識,感覺覺的不太重要,但是不懂還不行,就是一盤清口的小菜,雖然吃不飽,但吃完會讓你吃正餐的時候更舒服。
  •     第一感覺比較貴哈,但是這本書能淺顯地告訴我一些我一直以來比較模糊的概念,而且能增加我學習sql的信心,推薦。
  •     知道這本書是從一個推薦里看到的,覺得很不錯,仔細的看了一遍。
      可惜我覺得我要學SQL還是一個漫長的過程。
      忽然想,大學那會兒真該學IT的。。。。
  •     《SQL必知必會(第3版)》剛買來的時候就非常驚訝--居然這一200頁不到的小本本定的市場價是29元,32開的handbook可方便隨身帶,買來后一口氣就閱讀到了第七章,大呼過癮。里面的內容是以前知道但沒有理解,但沒有像這本書這么教得通俗易懂的,如形容數(shù)據(jù)表就像是一個文件夾,數(shù)據(jù)庫實例就像是一個文件柜這樣容易理解。
      
      這本書的原作者寫得不錯,翻譯的水平也極佳。這本書的定位是入門,所以一切都以點到就為止了。比如存儲過程那一章原作者就說了只以簡單的講一講。我們平時實際工作中寫得就復雜多了 。而且原作者把各種主流DBMS的區(qū)別與一般常見的實踐技巧全部傳授給了你,我們這樣的 SQL 菜鳥不正需要這些知識嗎?
  •     以前由于工作的緣故,要經常使用SQL語言去操作數(shù)據(jù)庫,很多東西記不住,或者記憶很模糊,查查這本書就可以了,適合入門的數(shù)據(jù)庫操作員,不錯的工具手冊。
  •     這本書買來兩個禮拜,認真地把我懂得的部分看完了,不懂得的部分還在努力在看……
      總體來說,這本書很適合入門。
  •     名副其實,書中的內容的確是關系型數(shù)據(jù)庫使用者“必須知道必須會”的,如果你連這些都不知道,那就太……
      這本書講得實在有夠淺,只適合徹底的新手和入門者。而對于一個有SQL使用經驗的人來說,本書前17章和20、22章的內容你一定了如指掌;如果你對18、 19、21章講解的視圖、存儲過程和游標功能很陌生,那么你一定和我一樣是古老的MySQL4.0的使用者——這個版本不支持這三種功能。
      如果你是新手的話,這本書還是值得一買的,內容淺顯易懂,能幫你很快入門,薄薄一本小冊子讀起來也很快(原書名直譯過來是“SQL 10分鐘入門”)。
      如果你已經使用SQL有一段時間了,那么,還是去尋找更高階的教程吧。
  •   如果希望了解數(shù)據(jù)庫的原理和設計,個人推薦《SQL權威指南》http://book.douban.com/subject/20395440/
  •   書名又不是關系型數(shù)據(jù)庫原理,會介紹原理和設計才怪了
  •   書名必知必會么,寫給所有杰出SQL數(shù)據(jù)庫人看的,顯然不可能有高深的內容
  •   比較好的面試佛腳書吧
 

250萬本中文圖書簡介、評論、評分,PDF格式免費下載。 第一圖書網 手機版

京ICP備13047387號-7