第1章 資料庫系統的基礎觀念
Download
Report
Transcript 第1章 資料庫系統的基礎觀念
第六章
學習SQL語言
學習目標
熟悉資料庫系統的SQL查詢語言。
透過實例來學習SQL的語法。
安裝與設定MySQL。
利用MySQL測試SQL語法。
從疑惑中開始學習
SQL是什麼 ?
SQL有什麼用途 ?
關聯式資料庫查詢系統
資料查詢語言
relational data model與
programming language
的computation model不
同的特性也稱為
impedance mismatch。
資料庫語言
資料查詢語言
(DQL, Data Query Language)
資料定義語言 (DDL, Data Definition
Language)
資料處理語言 (DML, Data Manipulation
Language)
資料控制語言(DCL, Data Control Language)
SQL的基本語法
SQL的基本語法實例
select * from X,Y
SQL來表示X join Y
多表格連結的結果與運算過程(1)
多表格連結的結果與運算過程(2)
從建立表格的語法開始
create table customers
(cust-id char(3) not null,
cust-name char(10), region char(2),
phone char(13));
資料庫、表格與應用系統的關係
新增記錄
insert into customers(cust-id, cust-name,
region, phone) values(‘I01’, ‘John’,
‘TW’, ’02-232-1111-3’);
insert into customers (cust-id, cust-name,
region, phone) values(‘I02’, ‘Mary’,
‘JP’, ’03-393-7457-4’);
insert into customers (cust-id, cust-name,
region, phone) values(‘I03’, ‘Anne’,
‘CA’, ’02-999-1234-5’);
新增表格中的記錄
C ust-id
C ust-name
region
Phone
I01
John
TW
02-232-1111-3
I02
M ary
JP
03-393-7457-4
I03
A nne
CA
02-999-1234-5
一次新增多筆記錄
input customers
‘I01’,’John’,’TW’,’02-232-1111-3’
‘I02’,’Mary’,’JP’,’03-393-7457-4’
‘I03’,’Anne’,’CA’,’02-999-1234-5’
刪除資料記錄
delete from customers
where cust-id =‘I01’;
資料記錄的修改
update customers
set phone=’02-292-3311-4’
where cust-id=’I01’;
聚集函數的使用
(表格名稱:orders)
order-no
it em
quant it y
D at e
P001
I01
40
02/01/85
P002
I03
20
03/04/85
P003
I01
50
02/15/85
P004
I03
10
05/01/85
P005
I02
15
05/09/85
select count (distinct item) from orders;
(結果為 3)
select avg (quantity) * count (distinct item) from orders;
(結果為 81)
資料集的觀念
資料集主要的用途
提昇資料存取使用的彈性
安全性
一致性
資料集的定義與使用(1)
資料集的定義與使用(2)
資料集的定義與使用(3)
資料記錄的排序與分組
有時候查詢所得到的資料很多,經過排序
(sorting)與分組(grouping)以後,會比較容易瀏
覽
資料記錄的排序可以用「order by」的語法,
然後以關鍵字「asc」代表升冪(Ascending)
的排序,以「desc」代表降冪(Descending)
的排序
「次查詢」(Subquery)的語法
select item from orders
where cust-id = (select cust-id from
customers where name=’Marty’);
建立學習SQL的環境
MySQL的取得
MySQL的安裝
MySQL使用的練習
MySQL的連線設定視窗
SQL的指令
建立使用環境與資料庫的指令
資料維護的指令
查詢的指令
進階的查詢指令
與系統相關的指令
更改資料庫結構的指令
ANSI-1989訂的關聯運算
關聯運算(relational operator)
作用
=
比較是否相等(equal)
<>
比較是否不相等(not equal)
>
比較是否大於
<
比較是否小於
>=
比較是否大於等於
<=
比較是否小於等於