ORACLE SQL Fundamental I 1-4 章重點 Chap 1 Introduction Chap 2 Retrieving Data Using the SQL SELECT Statement Chap 3 Restricting and Sorting Data Chap 4 Using.

Slides:



Advertisements
Similar presentations
SQL1-ch2 限制和排序資料. 考古題題號  80 題: 27 、 51  140 題: 23 、 58 、 70.
Advertisements

FCU, Department of ECE, IC Design Research Lab. TEL: # 4945 Pre-SIm , Post-Sim.
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
Section 1.2 Describing Distributions with Numbers 用數字描述分配.
3Com Switch 4500 切VLAN教學.
T-SQL 運算子介紹 11/29. 運算子的總類 指定運算子 算術運算子 比較運算子 邏輯運算子 位元運算子 字串連結運算子 單一運算元運算子.
BY OX. 檢視表與資料表的差異性 查詢 (query) 檢視表 (View) 的紀錄,是經由查詢 (query) 而來,而檢 視表的資料來源可以是單一資料表或是多資料表,甚 至其他檢視表 但檢視表中的紀錄只存在資料表中.
1 第一章 Word 的基本觀念 內容概要: Word 的特色 啟動與離開 Word 的方法 滑鼠游標與外型的介紹 基本操作 Word 視窗法則 使用 Word 遭遇問題時, 應如何利用軟體特 性而獲得輔助解說.
1.1 線性方程式系統簡介 1.2 高斯消去法與高斯-喬登消去法 1.3 線性方程式系統的應用(-Skip-)
1 Web of Science 利用指引 單元二 瀏覽與處理查詢結果. 2 瀏覽檢索結果 查出的結果,預設以時間排列, 使用者可改變結果的排列方式: 還可以依被引用次數、相關度、 第一作者、刊名、出版年等排序 回到前先查的結果畫面 點選想看資料的完整書目 本館訂購範圍的期刊 全文,便可直接連結.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
Last modified 2004/02 An Introduction to SQL (Structured Query Language )
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
: The Playboy Chimp ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10611: The Playboy Chimp 解題者:蔡昇宇 解題日期: 2010 年 2 月 28 日 題意:給一已排序的數列 S( 升冪.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
:Problem D: Bit-wise Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10232: Problem D: Bit-wise Sequence 解題者:李濟宇 解題日期: 2006 年 4 月 16.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
圖層的操作與管理 圖層的作用就如同一張張透明的賽璐璐片, 你可以將動畫中的每項物件, 放置在不同圖 層中, 圖層交疊就形成完整的畫面。在各圖 層中的物件, 做任何的移動或變化, 都不會 相互干擾, 所以當你編輯一個物件時, 只要 在物件所在的圖層進行操作, 將可大幅降低 製作過程的複雜度與難度。
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
資料庫程式設計與系統管理 SQL Server 2005 Express 第六章 進階資料庫設計.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
從此處輸入帳號密碼登入到管理頁面. 點選進到檔案管理 點選「上傳檔案」上傳資料 點選瀏覽選擇電腦裡的檔案 可選擇公開或不公開 為平台上的資料夾 此處為檔案分類,可顯示在展示頁面上,若要參加 MY EG 競賽,做品一律上傳到 “ 98 MY EG Contest ” 點選此處確定上傳檔案.
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Management Abstracts Retrieval System; MARS 檢索操作.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
: Expect the Expected ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11427: Expect the Expected 解題者:李重儀 解題日期: 2008 年 9 月 21 日 題意:玩一種遊戲 (a game.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 10 m-way 搜尋樹與B-Tree
: Help My Brother ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11033: Help My Brother 解題者: 呂明璁 解題日期: 2007 年 5 月 14 日.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
第 6 章 迴圈結構 6-1 計數迴圈 6-1 計數迴圈 6-2 條件迴圈 6-2 條件迴圈 6-3 巢狀迴圈 6-3 巢狀迴圈 6-4 While/End While 迴圈 6-4 While/End While 迴圈 6-5 跳出迴圈 6-5 跳出迴圈 6-6 VB.NET 的錯誤處理 6-6 VB.NET.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/30 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH7.1~CH /12/26.
Visual C++重點複習.
電子書 ( Netlibrary ) 檢索說明 龍華科技大學圖書館. 檢索類型 檢索欄位與限制 在檢索中使用布林邏輯運算元 檢索結果 特殊檢索.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
數字系統與資料表示法 教師: 陳炯勳 數系轉換 r進制數字 稱為 base r或 radix r 有r個計數符號,計數順序逢r歸零(進位) A n A n - 1 ‥‥A 2 A 1 A 0 ﹒A -1 A -2 ‥‥A -m 其中A n 及A.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
SQL1-ch1 使用SQL SELECT 敘述句擷取資料.
SQL1-ch3 使用單列函數自訂輸出. 考古題題號  80 題: 7 、 37  140 題: 30 、 43 、 52 、 84 、 86 、 127.
SQL1-ch4 群組函數與聚總資料. 題號  80 題: 33 、 39  140 題: 10 、 59 、 95 、 110.
Presentation transcript:

ORACLE SQL Fundamental I 1-4 章重點 Chap 1 Introduction Chap 2 Retrieving Data Using the SQL SELECT Statement Chap 3 Restricting and Sorting Data Chap 4 Using Single-Row Functions to Customize Output

軟體: Oracle DB 11g Enterprise  從 下載  Oracle DB 11g Enterprise, 包含 HR 的資料  HR 資料用於範例說明及上機實習  安裝時 1. 下載的檔案要先解壓縮 2.HR 的帳號要解鎖, 且設密碼 ( 如 : hr 或 Oracle11g ) P.S. 1.Oracle DB 11g Express 2.Oracle DB 12c 為雲端的資料庫 2

3

第一種編輯軟體 ─ ─ SQLdeveloper  安裝 SQLdeveloper : 1. 從 / downloads /developer tools 下載 2. 須先安裝 JDK ( 版本不要太新, 1.6 版即可 ) 3. 進入 SQLdeveloper, 連線登入時需輸入 SID( 資料庫 ) 1)orcl : 11g Enterprise 2)xe : 11g Express » 右鍵 /property/save 密碼  執行 SQLdeveloper : 1. 先執行桌面的『 Open Oracle Service 』 2. 再執行桌面的『 sqldeveloper 』 3. 進入 SQLdeveloper 後,點選左邊視窗的 hr ,密碼輸入 hr 4

第二種編輯軟體 ─ ─ SQLPLUS  SQLPLUS :  在『命令提示字元』 (DOS 環境 ) 直接下指令 C:>sqlplus hr/hr SQL> select * from hr.employees; SQL> select table_name from user_tables; …….. 離開 SQLPLUS ……………. SQL> exit 或使用管理者進去 C:>sqlplus sys/oracle as sysdba SQL> alter user hr identified by hr account unlock; 5

更改密碼 SQL> alter user 帳號 identified by 密碼 ; 6

1-20Data Models 7

1-23 Entity Relationship Model  實體間的關係 :  Assign one or more employees to a department  Some departments do not yet have assigned employees  屬性 :  Mandatory( 強制 ) marked with 『 * 』 ( 一定要輸入 )  optional ( 選項 ) marked with 『 o 』  Primary( 主鍵 ) marked with 『 # 』 8

Relationship - Hyphen 連接號 -- dash 破折號 / slash 斜線 \ backslash 反斜線 # hash sign

1-25 Relating Multiple Tables  Primary key: 主鍵  Foreign key: 外來鍵 10

Introduction Updates, inserts, or deletes a row conditionally into/from a table

1-34 補充 14

Retrieving Data Using the SQL SELECT Statement  Projection:  Selects the columns in a table that are returned by a query  Selection:  Selects the rows in a table that are returned by a query  Joins:  Brings together data that is stored in different tables by specifying the link between them

SELECT {*|[DISTINCT] column|expression [alias],...} FROM table;  * 選擇全部欄位  DISTINCT Suppresses duplicates  重複值只取一個  SQL 指令不分大小寫 (not case sensitive)  SQL 指令以 ; (semicolon) 結束 , SQL*Plus 一定要加『 ; 』  Run Script icon 或 [F5] : ( 執行全部指令 )  run the command or commands in the SQL Worksheet  executes all the statements in the Enter SQL Statement box  Execute Statement icon 或 press [F9] : ( 執行指標所指的指令 )  run a SQL statement in the SQL Worksheet  executes the statement at the mouse pointer in the Enter SQL Statement box 有 DISTINCT 時, SELECT 只能列一個欄位

 SQL Developer:  標題( heading )預設靠左( Left-aligned )  標題大寫 (Uppercase) SELECT last_name, hire_date, salary FROM employees;  SQL*Plus:  標題( heading ):  字元 (Character) 和日期 (Date) 靠左( Left-aligned )  數字 (Number) 靠右 (right-aligned)  標題大寫 (Uppercase)

 Arithmetic Expressions (+, -, *, / ) 可用於:  數字 (Number)  日期 (Date)  DATE 和 TIMESTAMP 只能用 + 和 -  除了 From 子句  Oracle Server 會忽略運算子前後的空白( blank spaces )  NULL 是 unavailable, unassigned, unknown, or inapplicable  NULL 不是 0 也不是空白 (blank space) 2-14

SELECT last_name, 12*salary*commission_pct FROM employees;  Arithmetic expressions containing a null value evaluate to null  null 做運算 (+-*/) 後還是 null  例如 :  除以 0(division by zero) 會產生錯誤  但,除以 null (divide a number by null), 結果還是 null 或 unknown

 alias (別名) :  緊接在欄位後  可使用 AS  若別名包含空白、特殊字元、大小寫  以「 " " 」雙引號 (double quotation marks) 括住  且有分大小寫 SELECT last_name "Name", salary*12 "Annual Salary" FROM employees; p.s. 若 alias 沒以雙引號括住,則標頭列印時, 為大寫

2-18 SELECT last_name AS name, commission_pct comm FROM employees; SELECT last_name "Name", salary*12 "Annual Salary" FROM employees; 21

補充 :106. View the Exhibit and examine the description of the EMPLOYEES table. Your company wants to give 5% bonus to all the employees on their annual salary. The SALARY column stores the monthly salary for an employee. To check the total for annual salary and bonus amount for each employee, you issued the following SQL statement: SELECT first_name, salary, salary*12+salary*12*.05 "ANNUAL SALARY + BONUS" FROM employees; Which statement is true regarding the above query? A. It would execute and give you the desired output. B. It would not execute because the AS keyword is missing between the column name and the alias. C. It would not execute because double quotation marks are used instead of single quotation marks for assigning alias for the third column. D. It would execute but the result for the third column would be inaccurate because the parentheses for overriding the precedence of the operator are missing. Answer: A 22 題庫 106 公司要給所有員工年薪 5% 的獎金, SALARY 為月薪 別名 alias 別名之前的 AS 可省略 double quotation marks 雙引號 是對的 這裡不加括弧 (parentheses) 沒關係

 Concatenation Operator (連接運算子):  two vertical bars (||)  連接 columns 或 字串  concatenate 字串和 null, 結果為字串  LAST_NAME || NULL 結果是 LAST_NAME  date 型態也可以做 concatenate  Date 和 character literal values 必須包含在單引號『 ' ' 』中 SELECT last_name || ' is a ' || job_id AS "Employee Details" FROM employees; 2-21

 Alternative Quote (q) Operator  指定自己的 quotation mark delimiter( 定義符號 )  mark delimiter 可以是 single-byte or multibyte, or any of the following character pairs: [ ], { }, ( ), or SELECT department_name || q'[ Department's Manager Id: ]' || manager_id AS "Department and Manager" FROM departments;

補充 :104. SELECT product_name || 'it's not available for order' FROM product_information WHERE product_status = 'obsolete'; You received the following error while executing the above query: ERROR: ORA-01756: quoted string not properly terminated What would you do to execute the query successfully? A. Enclose the character literal string in the SELECT clause within the double quotation marks. B. Do not enclose the character literal string in the SELECT clause within the single quotation marks. C. Use Quote (q) operator and delimiter to allow the use of single quotation mark in the literal character string. D. Use escape character to negate the single quotation mark inside the literal character string in the SELECT clause. Answer: C 25 題庫 104 常數字串必須包含在單引號『 ‘ ’ 』中, 但若常數字串本身已內含單引號, 就要指定自己的 quotation mark delimiter 更正為 : SELECT product_name || q'[it's not available for order]' FROM product_information WHERE product_status = 'obsolete';

 DISTINCT :去除重複值 SELECT DISTINCT department_id FROM employees;  不加 DISTINCT : SELECT department_id FROM employees; SELECT DISTINCT department_id, employee_id FROM employees; 沒有去除重複資料 去除重複資料

 DESCRIBE :  可以簡寫為 DESC  顯示 table 的結構

 NUMBER(p,s)  Number value having a maximum number of digits p, with s digits to the right of the decimal point  最長 p 位,小數 s 位  VARCHAR2(s)  Variable-length character value of maximum size s  變動長度字串,最長 s 位

2-28 Quiz  Identify the two SELECT statements that execute successfully. 29

Restricting and Sorting Data SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ;  where 子句不可使用別名

 數字常數:  不需要用單引號  字串:  為 case-sensitive ( 有分大小寫 )  字串及日期值:  須用單引號括起來 SELECT last_name FROM employees WHERE hire_date = '17-FEB-96' ;  錯誤 : 不是有效的月份  查詢現在的日期及格式: select sysdate from dual; SELECT last_name,hire_date FROM employees WHERE hire_date = '07-6 月 -02' ;  date 為 format-sensitive ( 有分日期格式 )  預設 date 顯示格式為 DD-MON-RR Alter session set nls_date_format =‘YYYY-MM-DD:HH24:MI:SS’; Alter session set nls_date_language =American; NLS : National Language Support

 比較運算子( Comparison Operators ):  不等於可用 <>, !=, ^=

 between 低 and 高  包含最低和最高值  且須先寫最低值  可用於數字、字串 SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500 ; SELECT last_name FROM employees WHERE last_name BETWEEN 'King' AND 'Smith' ; 若改為 BETWEEN 3500 AND 2500 語法沒有錯誤, 但結果不對  找不 到資料

 in (.,.,.)  括號內的值, 不用按順序排列  可用於任何型態  oracle 內部是使用 or 來處理 in, 所以, in 沒有效率優勢 SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201) ; 相當於 WHERE manager_id =100 or manager_id =101 or manager_id =201

 LIKE  萬用字元( wildcard )搜尋  % : 0 或多個字元  _ :一個字元 SELECT first_name FROM employees WHERE first_name LIKE 'S%' ;  尋找 January, 2002 and December, 2002 期間進公司的員工 SELECT last_name, hire_date FROM employees WHERE hire_date LIKE '%02' ;

 % 和 _ 並用: SELECT last_name FROM employees WHERE last_name LIKE '_o%' ;  若要找的字剛好是萬用字元 % 或 _, 則需用 escape '\' 來識別 SELECT job_id FROM jobs WHERE job_id LIKE '%SA\_%' ESCAPE '\'

 NULL Conditions :  WHERE 子句  要用 is null  不可以用 = null SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL ; SELECT last_name, manager_id FROM employees WHERE manager_id = NULL ; SELECT last_name, manager_id FROM employees WHERE manager_id IS NOT NULL ; 語法沒有錯誤 但結果不對  找不到資料

 AND Truth Table (強勢 : false > null > true )  TRUE AND NULL = NULL  FALSE AND NULL = FALSE  NULL AND NULL = NULL SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= AND job_id LIKE '%MAN%' ;

 OR Truth Table (強勢 : true > null > false )  NULL or FALSE = NULL  NULL or NULL = NULL  NULL or TRUE = TRUE SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= OR job_id LIKE '%MAN%' ;

補充 :23. display PRODUCT_NAME from the table where the CATEGORY_ID column has values 12 or 13, and the SUPPLIER_ID column has the value SELECT product_name FROM product_information WHERE (category_id = 12 AND category_id = 13) AND supplier_id = ; A. It would execute but the output would return no rows. B. It would execute and the output would display the desired result. C. It would not execute because the entire WHERE clause condition is not enclosed within the parentheses. D. It would not execute because the same column has been used in both sides of the AND logical operator to form the condition. Answer: A 40 題庫 23 查詢 CATEGORY_ID 欄位值 12 or 13, 且 SUPPLIER_ID 為 的產品名稱 (PRODUCT_NAME) 產品的類別 CATEGORY_ID 欄位值不可能既為 12 又為 13, 所以查出來為 NULL 值

 NOT... WHERE job_id NOT IN ('AC_ACCOUNT', 'AD_VP')... WHERE salary NOT BETWEEN AND WHERE last_name NOT LIKE '%A%'... WHERE commission_pct IS NOT NULL

 Rules of Precedence (優先順序)

SELECT last_name, job_id, salary FROM employees WHERE job_id = 'SA_REP' OR job_id = 'AD_PRES' AND salary > 15000; SELECT last_name, job_id, salary FROM employees WHERE ( job_id = 'SA_REP' OR job_id = 'AD_PRES' ) AND salary > 15000; 先做 AND, 再做 OR 用括弧, 使 OR 先做, 再做 AND

order by  order by  排序, 一定要在 select 敘述的最後  可以用別名 SELECT employee_id, last_name, salary*12 annsal FROM employees ORDER BY annsal  可以用運算式 SELECT employee_id, last_name, salary*12 annsal FROM employees ORDER BY salary*12  若沒用 order by, 則每次輸出順序可能都不一樣  ASC: Ascending ,預設由小到大,可省略  DESC: Descending ,由大到小  order by 的欄位 : 不用一定要出現在 select lists Where 及 group by 不可用別名

3-24 order by  ASC : null 值放後頭 select commission_pct from employees order by commission_pct ;  DESC: null 值放前頭 select commission_pct from employees order by commission_pct desc ; 45

補充 :7. the product names and the date of expiration of warranty for all the products, if the product is purchased today. The products that have no warranty should be displayed at the top and the products with maximum warranty period should be displayed at the bottom. 46 題庫 7 查詢產品名稱及保證到期日, 假設產品今天購買, 最大的 產品保證到期日放在後面

補充 :7.( 續 ) A. SELECT product_name, SYSDATE+warranty_period AS "Warranty expire date" FROM product_information ORDER BY SYSDATE-warranty_period; B. SELECT product_name, SYSDATE+warranty_period AS "Warranty expire date" FROM product_information ORDER BY SYSDATE+warranty_period; C. SELECT product_name, SYSDATE+warranty_period AS "Warranty expire date" FROM product_information ORDER BY SYSDATE; D. SELECT product_name, SYSDATE+warranty_period "Warranty expire date" FROM product_information WHERE warranty_period > SYSDATE; Answer: B 47 題庫 7 更正為 : ORDER BY SYSDATE+warranty_period;

order by  控制 null 順序 :  NULLS FIRST:  若有 row 含 NULL 值, 則放在最前面 select commission_pct from employees order by commission_pct nulls first ;  NULLS LAST:  若有 row 含 NULL 值, 則放在最後面 select commission_pct from employees order by commission_pct desc nulls last ;

 以欄位的數字位置( column’s numeric position )排序: SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY 3 ;  多個欄位排序 SELECT last_name, department_id, salary FROM employees ORDER BY department_id, salary DESC; 亦即以 department_id 做排序 department_id 以 ASC 做排序 salary 以 DESC 做排序

補充 :70. SELECT product_name, list_price, min_price, list_price - min_price Difference FROM product_information sorted output in ascending order of the price difference between LIST_PRICE and MIN_PRICE? (Choose all that apply.) A. ORDER BY 4 B. ORDER BY MIN_PRICE C. ORDER BY DIFFERENCE D. ORDER BY LIST_PRICE E. ORDER BY LIST_PRICE - MIN_PRICE Answer: A C E 50 題庫 70 將 LIST_PRICE and MIN_PRICE 的 price difference ( 差額 ) 做升冪排序

補充 :58. SELECT first_name, department_id, salary FROM employees ORDER BY department_id, first_name, salary desc; Which two statements are true regarding the output of the above query? (Choose two.) A. The values in all the columns would be sorted in the descending order. B. The values in the SALARY column would be sorted in descending order for all the employees having the same value in the DEPARTMENT_ID column. C. The values in the FIRST_NAME column would be sorted in ascending order for all the employees having the same value in the DEPARTMENT_ID column. D. The values in the FIRST_NAME column would be sorted in the descending order for all the employees having the same value in the DEPARTMENT_ID column. E. The values in the SALARY column would be sorted in descending order for all the employees having the same value in the DEPARTMENT_ID and FIRST_NAME column. Answer: C E 51 題庫 58 先以 department_id 升冪排序, 當 department_id 值相同時, 再以 first_name 升冪排序, 當 first_name 值相同時, 再以 salary 降冪排序

52 SQL 執行順序 from  where  group by  having  select  order by

 Substitution Variables (代入變數)使用於: 1.WHERE conditions 2.ORDER BY clauses 3.Column expressions 4.Table names 5.Entire SELECT statements  變數值可從以下幾種方式而得 1.file 2.Person 3. 另一個 select statement

單一的 &  單一的 &  每一次都會提示使用者輸入  數值型態 : SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num ; 113

單一的 &  date 和 character 值要用單引號括起來 SELECT last_name, department_id, salary*12 FROM employees WHERE job_id = '&job_title' ; IT_PROG

單一的 & SELECT employee_id, last_name, job_id, &column_name FROM employees WHERE &condition ORDER BY &order_column ; salary Salary >15000 last_name

&&  &&  可 reuse 變數  只需提示一次, 不用每次提示輸入  且若再執行一次 query 時, 不會再被提示 SELECT employee_id, last_name, job_id, &&column_name FROM employees ORDER BY &column_name ; department_id

&&  DEFINE  新增變數並指定變數值  UNDEFINE  移除變數  substitution variable 存在  直到 undefine 該變數或離開該 session DEFINE employee_num = 200 ; SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num ; UNDEFINE employee_num ;

 使用 SET VERIFY ON  顯示取代變數前與後的值 SET VERIFY ON SELECT employee_id, last_name, salary FROM employees WHERE employee_id = &employee_num;

3-37 Quiz  Which four of the following are valid operators for the WHERE clause? a. >= b. IS NULL c. != d. IS LIKE e. IN BETWEEN f. <> 60

及 4-5 Using Single-Row Functions to Customize Output  SQL function : 不一定有參數, 但一定會回傳一個值  Two Types of SQL Functions 1.Single-row function : 作用在單一列, 且每列回傳一個值 2.Multiple-Row Functions : 作用在一組列中, 每一組回傳一個值, 又 稱為 group functions

Single-Row Functions  Single-Row Functions :  修改資料型態  nested( 巢狀 )  參數可以是欄位或運算式( expression )  可用在 1.Select 2.Where 3.Order by

Single-Row Functions  Character functions:  輸入 character, 回傳 both character and number 值  Number functions:  輸入 numeric ,回傳 numeric 值  Date functions:  操作在 DATE data type  MONTHS_BETWEEN function, 回傳 number  其餘回傳 DATE 值  Conversion functions:  轉換 data type to another  General functions:  NVL  NVL2  NULLIF  COALESCE  CASE  DECODE

Character Functions INITCAP : 第一個字母大寫

4-11 SELECT 'The job id for ' || UPPER(last_name) || ' is ' || LOWER(job_id) AS "EMPLOYEE DETAILS" FROM employees; 65

4-12 SELECT employee_id, last_name, department_id FROM employees WHERE LOWER(last_name) = 'higgins'; 66

及 4-10 補充  substr ( column|exp, m[,n] ) :取子字串  從第 m 位回傳 n 個值回來  若 m 為負值, 則從右邊開始 m 位, 但取值時, 一樣是由左到右取 n 位  若 n 值忽略, 則取從 m 位到最後 select substr('ABCDEFGH', 1,3) from dual ;  ABC select substr('ABCDEFGH', -3,3) from dual;  FGH select substr('ABCDEFGH', 5) from dual ;  少最後參數, 則取全部 EFGH  instr ( column|exp, ‘string’, [,m], [n] ) :子字串的位置, 傳回數值  從來源字串第 m 位開始找出現第 n 次的子字串, 傳回該位置  m 和 n 的預設值為 1 select instr('ABCABC', 'A') from dual ;  1 select instr('ABCABCABC', 'A',2, 2) from dual ;  7 select instr('ABCABCABC', 'A',2, 3) from dual ;  0, 找不到第 3 個 A 從第 2 個位置開始找 找尋第 2 個 A

68 補充  CONCAT : 結合兩個參數  若要結合三個參數以上 select concat(concat('A','B'),'C') from dual ;  ABC 或 select 'A'|| 'B'||'C' from dual ;  LENGTH :求字串長度, 傳回數值 select LENGTH('ABCDEFGH') from dual;  8  LPAD(column|exp,n,’string’)  LPAD( 來源, 補齊長度, 補齊的字串 )  左邊補齊 select LPAD('ABC',5,'*') from dual ;  **ABC  RPAD(column|exp,n,’string’)  RPAD( 來源, 補齊長度, 補齊的字串 )  右邊補齊 select RPAD('ABC',5,'*') from dual ;  ABC ** select RPAD('ABC',5,'*?') from dual ;  ABC *?

補充  TRIM (leading|trailing|both trim_char FROM trim_source)  去頭尾, 預設為 both, 亦即預設頭尾皆去掉 select TRIM( both 'A' from 'BCABA') from dual ;  BCAB select TRIM( leading 'A' from 'AAAACABA') from dual ;  CABA  REPLACE: 取代  REPLACE(text,search_string, replacement_string) select REPLACE('Black and Blue', 'Bl', 'J') from dual ;  Jack and Jue 69

Using the Character-Manipulation Functions SELECT employee_id, CONCAT(first_name, last_name) NAME, job_id, LENGTH (last_name), INSTR(last_name, 'a') "Contains 'a'?" FROM employees WHERE SUBSTR(job_id, 4) = 'REP' ;  last_nam 最後一個字母為 ‘y’ 的員工 select last_name from employees where SUBSTR(last_name, -1, 1) = 'y';

Numeric Functions  ROUND (column|exp,n):  四捨五入到指定小數位數 ( specified decimal)  若 n 省略, 則到整數位, 若 n 為負值, 則從整數位向左數 n 位  TRUNC (column|exp,n): 無條件捨去到指定小數位數  若 n 省略, 其預設值為 0  MOD(m,n): 求餘數

Numeric Functions  numeric function :只能接受數字, 並輸出數字 SQL> select ROUND(AA, 2) from dual; select ROUND(AA, 2) from dual * ERROR at line 1: ORA-00904: "AA": invalid identifier SQL> select ROUND('AA', 2) from dual; select ROUND('AA', 2) from dual * ERROR at line 1: ORA-01722: invalid number

Using the ROUND Function  DUAL  一個 public table 用以查詢 functions and calculations 結果 (view results)  dual 為 SYS 擁有, 可以給所有使用者使用, 包含一個欄位 DUMMY, 一列值為 X  round : 除了數字也可用於 date function SELECT ROUND(45.923,2), ROUND(45.923,0),ROUND(45.923,-1) FROM DUAL;

 trunc : 也可用於 date function SELECT TRUNC(45.923,2), TRUNC(45.923), TRUNC(45.923,-1) FROM DUAL; Using the TRUNC Function  mod  常用於決定奇, 偶數  mod 是 oracle 的 hash function SELECT last_name, salary, MOD(salary, 5000) FROM employees WHERE job_id = 'SA_REP'; 4-19 Using the MOD Function

Working with Dates  dates :  Oracle Database stores dates in an internal numeric format: century, year, month, day, hours, minutes, and seconds  預設日期格式為 DD-MON-RR  有效日期 (Valid Oracle dates )  between January 1, 4712 B.C., and December 31, 9999 A.D. SELECT last_name, hire_date FROM employees WHERE hire_date < '01-FEB-88' ; select sysdate from dual; SELECT last_name,hire_date FROM employees WHERE hire_date = '07-6 月 -02'; 不是有效的月份

Working with Dates alter session set nls_date_format ='YYYY-MM-DD:HH24:MI:SS'; alter session set nls_date_format ='DD-MON-RR'; alter session set nls_date_language =french alter session set nls_date_language =american SQL> select sysdate from dual; SYSDATE 月 -13

RR Date Format  RR :  類似 YY, RR 可以指明不同的世紀 (centuries)

Using the SYSDATE Function  sysdate  顯示資料庫所在地點的時間 select sysdate from dual;  current_date  顯示該 session time zone 的時間 select current_date from dual; 4-25  database 將 date 儲存成數字

Using Arithmetic Operators with Dates SELECT last_name, (SYSDATE-hire_date)/7 AS WEEKS FROM employees WHERE department_id = 90;

Date-Manipulation Functions  months_between(date1,date2)  兩個日期間的月份數, 回傳值可為正或負  若 date1 > date 2, 則回傳正值  add_months(date,n)  日期加上 n  n 需為整數字且可為負值  last_day 當月最後一天  next_day (date,'char')  下一個指定日 ( 星期幾 ) 的日期  如下週一的日期 next_day(sysdate,' 星期一 ')  char 可為字串或數字, 若 char 為數字, 在 America 1 代表 Sunday

Using Date Functions select MONTHS_BETWEEN('01-9 月 -95','11-1 月 -94') from dual;  select MONTHS_BETWEEN('11-1 月 -94','01-9 月 -95') from dual;  select ADD_MONTHS ('31-1 月 -96',1) from dual;  '29-2 月 -96' select ADD_MONTHS ('31-1 月 -96',-1) from dual;  '31-12 月 -95' select NEXT_DAY ('01-9 月 -95',' 星期五 ') from dual;  '08-9 月 -95' select LAST_DAY ('01-2 月 -95') from dual;  '28-2 月 -95'

補充 :118. a monthly bonus of $50 to all the employees who have completed five years in the company. SELECT last_name, department_id, salary+50*12 "Annual Compensation" FROM employees WHERE MONTHS_BETWEEN(SYSDATE, hire_date)/12 >= 5; When you execute the statement, the "Annual Compensation" is not computed correctly. What changes would you make to the query to calculate the annual compensation correctly? A. Change the SELECT clause to SELECT last_name, department_id, salary*12+50 "Annual Compensation". B. Change the SELECT clause to SELECT last_name, department_id, salary+(50*12) "Annual Compensation". C. Change the SELECT clause to SELECT last_name, department_id, (salary+50)*12 "Annual Compensation". D. Change the SELECT clause to SELECT last_name, department_id, (salary*12)+50 "Annual Compensation". Answer: C 83 題庫 118 對於來公司滿 5 年的員工, 要給他每個月 50 元的 獎金 ; 查詢的欄位有 last_name, department_id 和 Annual Compensation ( 年薪 ) 執行時 Annual Compensation ( 年薪 ) 計算不正確, 如何修正 ?

Using Date Functions SELECT employee_id, hire_date, MONTHS_BETWEEN (SYSDATE, hire_date) TENURE, ADD_MONTHS (hire_date, 6) REVIEW, NEXT_DAY (hire_date,'FRIDAY'), LAST_DAY(hire_date) FROM employees WHERE MONTHS_BETWEEN (SYSDATE, hire_date) < 150 ;

補充 :30. SELECT first_name, employee_id, NEXT_DAY(ADD_MONTHS (hire_date, 6), 1) "Review" FROM employees; The review date is the first Monday after the completion of six months of the hiring. The NLS_TERRITORY parameter is set to AMERICA in the session. Which statement is true regarding this query? A. The query would execute to give the desired output. B. The query would not execute because date functions cannot be nested. C. The query would execute but the output would give review dates that are Sundays. D. The query would not execute because the NEXT_DAY function accepts a string as argument. Answer: C 85 題庫 30 查詢員工受雇滿 6 個月後的第一個星期一, 使用美國日期格式, 注意 : 美國每星期的第一天是星期日

Using ROUND and TRUNC Functions with Dates  round(date,'fmt')  以 fmt 格式來做四捨五入  若 fmt 為 month, 則 day( 日 ) >=16 進位到下個月的 1 日, <=15 為當 月的 1 日  若 fmt 為 year, 則 month( 月份 ) 為 1~6 月為當年 1 月 1 日, 7~12 月為下 一年的 1 月 1 日  若 fmt 省略, 則為 day

Using ROUND and TRUNC Functions with Dates SELECT employee_id, hire_date, ROUND(hire_date, 'MONTH'), TRUNC(hire_date, 'MONTH') FROM employees WHERE hire_date LIKE '%03' ;

Quiz  Which four of the following statements are true about single-row functions? a. Manipulate data items b. Accept arguments and return one value per argument c. Act on each row that is returned d. Return one result per set of rows e. May not modify the data type f. Can be nested g. Accept arguments that can be a column or an expression

補充 :86. Which three statements are true regarding single-row functions? (Choose three.) A. They can accept only one argument. B. They can be nested up to only two levels. C. They can return multiple values of more than one data type. D. They can be used in SELECT, WHERE, and ORDER BY clauses. E. They return data type can be different from the data type of the argument that is referenced. F. They can accept a column name, expression, variable name, or a user-supplied constant as arguments. Answer: D E F 89 題庫 86 可接受不只一個參數, 如 : months_between(date1,date2) 可以巢狀不只二層 回傳的值只能一個且只能為一種資料型態 參數資料型態不一樣, 回傳的資料型態也會不一樣, 如 : round(date,‘fmt’)  回傳日期的資料型態 round(45.923,2)  回傳數字的資料型態