Chapter 7 Introduction to SQL 11Chapter 6 2015

  • Slides: 45
Download presentation
Chapter 7: Introduction to SQL 註 : 於 11版為Chapter 6 楊立偉教授 台灣大學 管系 2015

Chapter 7: Introduction to SQL 註 : 於 11版為Chapter 6 楊立偉教授 台灣大學 管系 2015 Fall 1

SQL Overview n n Structured Query Language 結構式查詢語言 The standard for relational database management

SQL Overview n n Structured Query Language 結構式查詢語言 The standard for relational database management systems (RDBMS) n n n 1986成為ANSI標準, 1987成為ISO標準 各家廠商的實作可能略有不同 RDBMS: A database management system that manages data as a collection of tables in which all relationships are represented by common values in related tables Chapter 7 2

History of SQL n n n 1970–E. Codd develops relational database concept 1974 -1979–System

History of SQL n n n 1970–E. Codd develops relational database concept 1974 -1979–System R with Sequel (later SQL) created at IBM Research Lab 1979–Oracle markets first relational DB with SQL 1986–ANSI SQL standard released 1989, 1992, 1999, 2003–Major ANSI standard updates Current–SQL is supported by most major database vendors n Oracle, Microsoft SQL Server, IBM DB 2, My. SQL, Postgre SQL, etc. Chapter 7 3

Purpose of SQL Standard n Specify syntax/semantics for data definition and manipulation 資料定義與操作的語法 n

Purpose of SQL Standard n Specify syntax/semantics for data definition and manipulation 資料定義與操作的語法 n Define data structures 定義了資料結構 n Enable portability 實現了可攜性 n Allow for later growth/enhancement to standard 允許日後做擴充 Chapter 7 4

Benefits of a Standardized Relational Language n n n Reduced training costs 降低學習成本 Productivity

Benefits of a Standardized Relational Language n n n Reduced training costs 降低學習成本 Productivity 提高生產力 Application portability 應用程式可攜性 Application longevity 應用程式長久性 Reduced dependence on a single vendor 減少依賴單一廠商 Cross-system communication 有助跨系統 溝通 Chapter 7 5

n Catalog n n Commands that define a database, including creating, altering, and dropping

n Catalog n n Commands that define a database, including creating, altering, and dropping tables and establishing constraints Data Manipulation Language (DML) n n The structure that contains descriptions of objects created by a user (base tables, views, constraints) Data Definition Language (DDL) n n A set of schemas that constitute the description of a database Schema n n SQL Environment Commands that maintain and query a database Data Control Language (DCL) n Commands that control a database, including administering privileges and committing data Chapter 7 6

Figure 7 -1 A simplified schematic of a typical SQL environment, as described by

Figure 7 -1 A simplified schematic of a typical SQL environment, as described by the SQL: 2008 standard 不同的Environment (或稱Space) 開發用 Chapter 7 正式用 7

Figure 7 -4 DDL, DML, DCL, and the database development process DDL : CREATE

Figure 7 -4 DDL, DML, DCL, and the database development process DDL : CREATE TABLE ALTER TABLE DROP TABLE … DML : INSERT UPDATE DELETE SELECT … 設計 開發實作 維護 Chapter 7 9

SQL Database Definition n Data Definition Language (DDL) n Major CREATE statements: n CREATE

SQL Database Definition n Data Definition Language (DDL) n Major CREATE statements: n CREATE SCHEMA–defines a portion of the database owned by a particular user n CREATE TABLE–defines a table and its columns n CREATE VIEW–defines a logical view from one or more tables 由一至多張表格所構成的虛擬表格 (視界 ) Chapter 7 10

Table Creation Steps in table creation: Figure 7 -5 General syntax for CREATE TABLE

Table Creation Steps in table creation: Figure 7 -5 General syntax for CREATE TABLE 1. Identify data types for attributes 2. Identify columns that can and cannot be null 3. Identify columns that must be unique (candidate keys) 4. Identify primary key– foreign key mates 5. Determine default values 6. Identify constraints on columns (domain specifications) 語法表示 [ ] 表選項, 可填可不填 { } 表多選, 多個選一個 Chapter 7 7. (optional) Create the table and associated 11 indexes

The following slides create tables for this E-R model Chapter 7 12

The following slides create tables for this E-R model Chapter 7 12

Figure 7 -6 SQL database definition commands for Pine Valley Furniture Overall table definitions

Figure 7 -6 SQL database definition commands for Pine Valley Furniture Overall table definitions Chapter 7 13

Defining attributes and their data types 為 key 取一個名字 Chapter 7 decimal [(p[, s])]

Defining attributes and their data types 為 key 取一個名字 Chapter 7 decimal [(p[, s])] 和 numeric [(p[ , s])] • p 固定有效位數,小數點左右兩側都包括在內 • s 小數位數的數字。 • numeric 與 decimal 的功能相同。 語法參考 http: //technet. microsoft. com/zh-tw/library/ms 187746. aspx 14

Non-nullable specification Primary keys can never have NULL values Identifying primary key Chapter 7

Non-nullable specification Primary keys can never have NULL values Identifying primary key Chapter 7 15

Non-nullable specifications Primary key 為 key 取一個名字 Some primary keys are composite– composed of

Non-nullable specifications Primary key 為 key 取一個名字 Some primary keys are composite– composed of multiple attributes Chapter 7 注意PK為複合欄位時的寫法 16

Controlling the values in attributes Default value 指定預設值 Domain constraint Chapter 7 17

Controlling the values in attributes Default value 指定預設值 Domain constraint Chapter 7 17

Identifying foreign keys and establishing relationships Primary key of parent table Foreign key of

Identifying foreign keys and establishing relationships Primary key of parent table Foreign key of dependent table Chapter 7 18

Data Integrity Controls n n Referential integrity–constraint that ensures that foreign key values of

Data Integrity Controls n n Referential integrity–constraint that ensures that foreign key values of a table must match primary key values of a related table in 1: M relationships Restricting: Deletes of primary records n Updates of primary records n Inserts of dependent records n Chapter 7 19

Figure 7 -7 Ensuring data integrity through updates Relational integrity is enforced via the

Figure 7 -7 Ensuring data integrity through updates Relational integrity is enforced via the primary-key to foreign-key match 1 自動檢查完整性 有四種指定方法 2 註 : 有些較簡易的 RDBMS可能未支援 3 4 Chapter 7 20

Changing and Removing Tables n ALTER TABLE statement allows you to change column specifications:

Changing and Removing Tables n ALTER TABLE statement allows you to change column specifications: n n ALTER TABLE CUSTOMER_T ADD (TYPE VARCHAR(2)) ALTER TABLE CUSTOMER_T DROP TYPE 尚包含改名、改型別等功能;其它請參考各RDBMS語法 DROP TABLE statement allows you to remove tables from your schema: n DROP TABLE CUSTOMER_T Chapter 7 21

Create column index n Speed up in specific columns 替某個或某些欄位建立索引 n Example n CREATE

Create column index n Speed up in specific columns 替某個或某些欄位建立索引 n Example n CREATE INDEX indexname ON CUSTOMER_T(CUSTOMER_NAME) n This makes an index for the CUSTOMER_NAME field of the CUSTOMER_T table 該欄位的查詢速度會大幅增加 n Every key field (PK or FK) is suggested to add index 加快跨表關聯 Chapter 7 22

Insert Statement n n Adds data to a table 開始加入資料至表格內 Inserting into a table

Insert Statement n n Adds data to a table 開始加入資料至表格內 Inserting into a table n n Inserting a record that has some null attributes requires identifying the fields that actually get data n n INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’, ‘ 1355 S. Himes Blvd. ’, ‘Gainesville’, ‘FL’, 32601); INSERT INTO PRODUCT_T (PRODUCT_ID, PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8); Inserting from another table 直接將查詢結果加入 n INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T WHERE STATE = ‘CA’; Chapter 7 23

Creating Tables with Identity Columns New with SQL: 2008 自動編號欄位型別 Inserting into a table

Creating Tables with Identity Columns New with SQL: 2008 自動編號欄位型別 Inserting into a table does not require explicit customer ID entry or field list 加入資料時不需指定該欄位之值 INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’, ‘ 1355 S. Himes Blvd. ’, ‘Gainesville’, ‘FL’, 32601); Chapter 7 24

Delete Statement n n Removes rows from a table 將表格內[部份]資料刪除 Delete certain rows n

Delete Statement n n Removes rows from a table 將表格內[部份]資料刪除 Delete certain rows n n n DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’; 使用WHERE條件子句 Delete all rows n DELETE FROM CUSTOMER_T; Chapter 7 25

Update Statement n n Modifies data in existing rows 修改表格內資料之值 Update a certain row

Update Statement n n Modifies data in existing rows 修改表格內資料之值 Update a certain row n n n UPDATE PRODUCT_T SET UNIT_PRICE = 775 WHERE PRODUCT_ID = 7; 使用WHERE條件子句 (欄位條件的布林邏輯組合) Delete a lot of rows 小心使用! n n UPDATE PRODUCT_T SET PRODUCT_DESCRIPTION=“”; 清空欄位 UPDATE PRODUCT_T SET UNIT_PRICE = 775; 何意? Chapter 7 26

SELECT Statement n n Used for queries on single or multiple tables Clauses of

SELECT Statement n n Used for queries on single or multiple tables Clauses of the SELECT statement: n SELECT 要取出哪些欄位 n n FROM 從哪張表 n n Indicategorization of results HAVING 若紀錄有合併, 是否要再做篩選 (條件子句) n n Indicate the conditions under which a row will be included in the result GROUP BY 紀錄是否要合併, 用哪些欄位合併 n n Indicate the table(s) or view(s) from which data will be obtained WHERE 要取出哪些筆紀錄 (條件子句) n n List the columns (and expressions) that should be returned from the query Indicate the conditions under which a category (group) will be included ORDER BY 依哪些欄位做排序 n Sorts the result according to specified criteria Chapter 7 27

Figure 7 -10 SQL statement processing order 內部RDBMS在解釋 這句命令時的處理順序 Chapter 7 28

Figure 7 -10 SQL statement processing order 內部RDBMS在解釋 這句命令時的處理順序 Chapter 7 28

SELECT Example (1) n Find products with standard price less than $275 SELECT PRODUCT_NAME,

SELECT Example (1) n Find products with standard price less than $275 SELECT PRODUCT_NAME, STANDARD_PRICE FROM PRODUCT_V WHERE STANDARD_PRICE < 275; Table 7 -3: Comparison Operators in SQL Chapter 7 29

SELECT Example (2) Using Alias n Alias is an alternative column or table name

SELECT Example (2) Using Alias n Alias is an alternative column or table name 原句 SELECT CUSTOMER_V. CUSTOMER, CUSTOMER_V. CUSTOMER_ADDRESS FROM CUSTOMER_V WHERE CUSTOMER_V. CUSTOMER = ‘Home Furnishings’; 使用 SELECT CUSTOMER AS NAME, 別名 FROM CUSTOMER_V CUSTOMER_ADDRESS WHERE NAME = ‘Home Furnishings’; 取個別名, 比較方便指定, 也可省去重複打字 Chapter 7 30

SELECT Example (3) Using a Function n n 可以使用函數對欄位做運算 n 例如 COUNT(), MAX(), MIN(),

SELECT Example (3) Using a Function n n 可以使用函數對欄位做運算 n 例如 COUNT(), MAX(), MIN(), SUM(), AVERAGE()…等 n 依RDBMS不同另有許多擴充函數 Using the COUNT aggregate function to find totals 找出總筆數 Note: with aggregate functions you can’t have single-valued columns included in the SELECT clause SELECT COUNT(*) FROM ORDER_LINE_V WHERE ORDER_ID = 1004; Chapter 7 * 是 "所有欄位" 的簡寫 改以特定欄位亦可 31

SELECT Example (4) Boolean Operators n AND, OR, and NOT Operators for customizing conditions

SELECT Example (4) Boolean Operators n AND, OR, and NOT Operators for customizing conditions in WHERE clause SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE FROM PRODUCT_V WHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’ OR PRODUCT_DESCRIPTION = ‘Round Table’) AND STANDARD_PRICE > 300; Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed Chapter 7 LIKE 是做字串比對用的, 支援萬用字元%或_ (或以*與? 表示) 32

LIKE operator and wildcards n n n % or * : zero to many

LIKE operator and wildcards n n n % or * : zero to many of any characters _ or ? : one of any characters Example n n Mic* matches Mickey, Michael, Michelle, etc. *son matches Dickson, Jackson, Bobson, etc. s? n matches sun, son, san, sin, etc. 可以多個混合使用 例 c? ? p* matches computer, camp Chapter 7 33

Venn Diagram from Previous Query 集合圖 Chapter 7 By default, processing order of Boolean

Venn Diagram from Previous Query 集合圖 Chapter 7 By default, processing order of Boolean operators is NOT, then AND, then OR 34

SELECT Example (5) Sorting Results with the ORDER BY Clause 將查詢結果做排序 n Sort the

SELECT Example (5) Sorting Results with the ORDER BY Clause 將查詢結果做排序 n Sort the results first by STATE, and within a state by CUSTOMER_NAME SELECT CUSTOMER_NAME, CITY, STATE FROM CUSTOMER_V WHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’) ORDER BY STATE, CUSTOMER_NAME; Note: the IN operator in this example allows you to include rows whose STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions 跟寫 STATE=‘FL’ OR STATE=‘TX’ OR … 是一樣的效果 Chapter 7 ORDER BY field 1 [ASC|DESC] [, field 2 [ASC|DESC]…] 可用ASC或DESC來指定升冪或降冪排列 35

SELECT Example (6) Categorizing Results Using the GROUP BY Clause n For use with

SELECT Example (6) Categorizing Results Using the GROUP BY Clause n For use with aggregate functions 需配合集合函數使用 n Scalar aggregate: single value returned from SQL query with aggregate function 若單只使用集合函數, 只傳回單筆紀錄, 如count(*) n Vector aggregate: multiple values returned from SQL query with aggregate function (via GROUP BY) 若配合GROUP BY將傳回多筆 SELECT CUSTOMER_STATE, COUNT(CUSTOMER_STATE) FROM CUSTOMER_V GROUP BY CUSTOMER_STATE; Note: you can use single-value fields with aggregate functions if they are included in the GROUP BY clause Chapter 7 36

原始 表格 Chapter 7 SELECT area, count(*) FROM member GROUP BY area; SELECT gender,

原始 表格 Chapter 7 SELECT area, count(*) FROM member GROUP BY area; SELECT gender, count(*) FROM member GROUP BY gender; 37

原始 表格 SELECT gender, education, count(*) AS ppl FROM member GROUP BY gender, education;

原始 表格 SELECT gender, education, count(*) AS ppl FROM member GROUP BY gender, education; Chapter 7 38

原始 表格 SELECT gender, education, count(*) AS ppl FROM member GROUP BY gender, education

原始 表格 SELECT gender, education, count(*) AS ppl FROM member GROUP BY gender, education ORDER BY count(*) DESC; Chapter 7 39

原始 表格 SELECT gender, education, count(*) AS ppl, max(age) FROM member GROUP BY gender,

原始 表格 SELECT gender, education, count(*) AS ppl, max(age) FROM member GROUP BY gender, education; Chapter 7 使用不同的函數 40

SELECT Example (7) Qualifying Results by Categories Using the HAVING Clause n For use

SELECT Example (7) Qualifying Results by Categories Using the HAVING Clause n For use with GROUP BY n n 將GROUP BY後的結果再用條件過濾的意思 語法與WHERE一樣 SELECT CUSTOMER_STATE, COUNT(CUSTOMER_STATE) FROM CUSTOMER_V GROUP BY CUSTOMER_STATE HAVING COUNT(CUSTOMER_STATE) > 1; Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result Chapter 7 41

SELECT gender, education, count(*) AS ppl FROM member GROUP BY gender, education; SELECT gender,

SELECT gender, education, count(*) AS ppl FROM member GROUP BY gender, education; SELECT gender, education, count(*) AS ppl FROM member GROUP BY gender, education HAVING education='大學'; HAVING可以想成是GROUP BY後的WHERE Chapter 7 42

Using and Defining Views n n Views provide users controlled access to tables Ex.

Using and Defining Views n n Views provide users controlled access to tables Ex. 只可看到某些欄位, 或建立某些常用查詢 Dynamic View n n A “virtual table” created dynamically No data actually stored Based on SQL SELECT statement on base tables or other views Materialized View n n n Copy or replication of data Data actually stored Must be refreshed periodically to match the corresponding base tables 需資料更新以維持一致性, 少用 Chapter 7 43

Sample CREATE VIEW EXPENSIVE_STUFF_V AS SELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICE FROM PRODUCT_T WHERE UNIT_PRICE >300

Sample CREATE VIEW EXPENSIVE_STUFF_V AS SELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICE FROM PRODUCT_T WHERE UNIT_PRICE >300 ; § View has a name § View is based on a SELECT statement, and acts like Table § 可分為 read-only view 或 updateable view (多為前者) Chapter 7 44

Advantages of Views n Simplify query commands n Provide customized view for user 常用查詢可建立為view

Advantages of Views n Simplify query commands n Provide customized view for user 常用查詢可建立為view 善用view可簡化複雜查詢 Disadvantages of Views n Use processing time each time view is referenced n May or may not be directly updateable 處理速度可能稍慢 有些RDBMS不支援updateable view Chapter 7 45