You are on page 1of 71

# Chng 3:

I. I S QUAN H

Php kt ni (Join)

Php hp (Union)

## Php giao (Intersect)

Php tr (Difference)

III. NGN NG SQL

Truy vn d liu

## IV. NGN NG QBE

I. I S QUAN H
1.1. Php chn (Selection)
Cho quan h r nh ngha trn lc quan h R(U), E l biu
thc chn pht biu trn U. Php chn trn quan h r theo iu kin E,
k hiu

## E(r) cho ta mt quan h mi vi tp thuc tnh U v cc b l

cc b ca r tho mn iu kin E.
Ta vit:

E(r) = {t | tr v E(t)=ng}

## Trong E(t) l gi tr ca biu thc E khi thay mi thuc tnh Ai

trong E ca t bi t[Ai].

I. I S QUAN H
1.1. Php chn (tip)

I. I S QUAN H
1.1. Php chn (tip)
V d 1: Cho quan h KHACHHANG nh sau:

## ) (TenKH='L Thanh Tm' ) (DiaChiKH='H Ni') (KHACHHANG)

I. I S QUAN H
1.1. Php chn (tip)
V d 1: Cho quan h KHACHHANG nh sau:

## ) (DiaChiKH='H Ni') (DiaChiKH='Ngh An') (KHACHHANG)

I. I S QUAN H
1.2. Php chiu (projection)
Cho quan h r nh ngha trn lc quan h R(U) vi U={A1, ..., An}
v tp thuc tnh X U. Php chiu quan h r ln tp thuc tnh X, k
hiu X(r) cho ta mt quan h mi vi tp thuc tnh X v cc b l hn
ch trn X ca cc b t r.
Ta vit

X(r) = {t[X] | t r }.

I. I S QUAN H
1.2. Php chiu (tip)
V d 2:
Cho quan h KHACHHANG nh sau:

a. a ra m, tn khch hng ?

) {MaKH, TenKH}(KHACHHANG)

{MaKH, TenKH}(KHACHHANG)

I. I S QUAN H
1.2. Php chiu (tip)
V d 2:
Cho quan h KHACHHANG nh sau:

b. a ra a ch khch hng ?

) {DiaChiKH}(KHACHHANG)

{DiaChiKH}(KHACHHANG)

I. I S QUAN H
V d 3:
Cho quan h KHACHHANG nh sau:

## a ra tn khch hng c a ch H Ni?

) {TenKH}(DiachiKH=H Ni(KHACHHANG))
DiachiKH=H Ni(KHACHHANG)

{TenKH}(DiachiKH=H Ni(KHACHHANG))

I. I S QUAN H
1.3. Tch cc (Cartesian product)
Cho hai quan h r nh ngha trn lc quan h R(U) v s nh
ngha trn lc quan h S(V) , vi U= {A1,A2, ..., An} , V= {B1,B2,
..., Bm}. Tch cc ca r v s, k hiu rs cho ta mt quan h mi
vi tp thuc tnh U V v cc b c dng:
t= (a1,a2,..., an,b1, b2, ..., bm)
trong (a1 , ..., an ) r v (b1,b2, ..., bm ) s.
Ta vit:
r s={t=(a1,a2...,an ,b1,b2, ...,bm ) | (a1,a2, ..., an) r v (b1,b2, ..., bm)s}.

I. I S QUAN H
1.3. Tch cc (tip)
V d 4:

I. I S QUAN H
1.4. Php kt ni (Join)
a, Php kt ni
Cho hai quan h r nh ngha trn lc quan h R(U) v s nh ngha
trn lc quan h S(V) vi U={A1, ..., An}, V ={B1, ..., Bm}. Ai v Bj l cc
thuc tnh tng ng thuc U v V sao cho Dom(Ai)=Dom(Bj). Gi l mt
trong cc php ton {=, >, >=, <, <=, }.
Php kt ni quan h r vi s theo biu thc Ai Bj , k hiu

s cho ta

## mt quan h mi vi tp thuc tnh U V v cc b c xc nh bi

{(u,v) | u=(a1,a2, ..., an) r, v=(b1,b2, ..., bm)s v u[Ai] v[Bj]} = ng}.
Ta vit:
r

## s = {(u,v) |u=(a1,a2, ..., an)r, v=(b1,b2, ...,bm)s v u[Ai] v[Bj]}=ng}.

I. I S QUAN H
1.4. Php kt ni (tip)
a, Php kt ni
V d 5:

I. I S QUAN H
1.4. Php kt ni (tip)
b, Php kt ni t nhin (Natural join)
Cho hai quan h r nh ngha trn lc quan h R(U) v s nh
ngha trn lc quan h S(V) vi UV. Php kt ni t nhin
gia quan h r vi s, k hiu r*s cho ta mt quan h mi vi tp
thuc tnh U V v cc b c xc nh bi
{t | t[U] r v t[V] s}.
Ta vit:

## r*s = {t | t[U] r v t[V] s }.

I. I S QUAN H
1.4. Php kt ni (tip)
b, Php kt ni t nhin
V d 6:

I. I S QUAN H
1.4. Php kt ni (tip)
V d 7:

SINHVIEN

SINHVIEN * KHOA

KHOA

I. I S QUAN H
V d 8a:

SINHVIEN

KHOA

SINHVIEN * KHOA

## Hoten, Tenkhoa( Tenkhoa=L( SINHVIEN * KHOA))

I. I S QUAN H
V d 8b:
Cho 2 quan h:
SINHVIEN (MaSV, Hoten, QueQuan, Makhoa)
KHOA(Makhoa, Tenkhoa)
Hy vit biu thc i s quan h a ra h tn, tn khoa ca sinh
vin khoa L?
Gii

## Hoten, Tenkhoa(Tenkhoa=L(SINHVIEN * KHOA))

I. I S QUAN H
Bi tp:
Cho c s d liu:
K_HANG(MaKH, TenKH, DiaChiKH, DTKH)
M_HANG(MaMH, TenMH, DVT, DonGia)
HD_MH(SoHD, MaMH, SoLuong, ThanhTien)
Hy tr li cc cu hi sau bng biu thc i s quan h:
a.

b.

c.

d.

## a ra tn nhng mt hng bn trong qu 1 nm 2009?

I. I S QUAN H
1.5. Php chia
Cho hai quan h r nh ngha trn lc quan h R(U) v s nh
ngha trn lc quan h S(V) vi VU v s . t X=U \ V.
Thng ca php chia quan h r cho quan h s, k hiu r s cho ta
quan h mi vi tp thuc tnh l X v cc b c xc nh bi:
{u [X] | (u r v v s th (u[X] ,v) r }
Ta vit:
r s = { u [X] | (u r v v s th (u[X] , v ) r }.

I. I S QUAN H
1.5. Php chia (tip)
V d 9:

I. I S QUAN H
1.6. Cc php ton tp hp
a, Quan h kh hp
Hai quan h r v s c gi l kh hp (tng thch) nu chng
c cng tp thuc tnh.
b. Php hp (Union)
Cho hai quan h kh hp r v s xc inh trn lc quan h
R(U). Php hp 2 quan h r v s, k hiu r s cho ta mt quan h mi
vi tp thuc tnh U v cc b l cc b thuc r hoc thuc s.
Ta vit:
r s = { t | t r hoc t s}.

I. I S QUAN H
1.6. Cc php ton tp hp (tip)
V d 10:

I. I S QUAN H
1.6. Cc php ton tp hp (tip)
c. Php giao (Intersect)
Cho hai quan h kh hp r v s xc inh trn lc quan h
R(U). Php giao 2 quan h r v s, k hiu rs cho ta mt quan h mi
vi tp thuc tnh U v cc b l cc b thuc r v thuc s.
Ta vit:
r s = { t | t r v t s}.

I. I S QUAN H
1.6. Cc php ton tp hp (tip)
V d 11:

I. I S QUAN H
1.6. Cc php ton tp hp (tip)
d. Php tr (Difference)
Cho hai quan h kh hp r v s xc inh trn lc quan h
R(U). Php tr quan h r cho s, k hiu r-s cho ta mt quan h mi vi
tp thuc tnh U v cc b l cc b thuc r nhng khng thuc s.
Ta vit:
r - s = { t | t r v t s}.

I. I S QUAN H
1.6. Cc php ton tp hp (tip)
V d 12:

I. I S QUAN H
Bi tp
Cho c s d liu:
K_HANG(MaKH, TenKH, DiaChiKH, DTKH)
M_HANG(MaMH, TenMH, DVT, DonGia)
HD_MH(SoHD, MaMH, SoLuong, ThanhTien)
Hy tr li cc cu hi sau bng biu thc i s quan h:
a.

b.

c.

## Cho bit nhng mt hng bn c trong thng 12/2008 nhng khng

bn c trong thng 1/2009?

(T c gio trnh)

QUERY LANGUAGE )

## 3.1. Gii thiu

SQL l sn phm ca cng ty IBM San jose California USA. Ngn
ng SQL c pht trin t ngn ng SEQUEL (Strructured English
Query Language) gii thiu nm 1976. Ngay sau khi ra i SQL
c ng dng rng ri trong lnh vc khai thc c s d liu quan h.
Hin nay, hu ht cc h qun tr c s d liu u c phn ngn ng
ny.
Kh nng ca SQL gm:

## III. NGN NG SQL

3.2. Ngn ng nh ngha d liu
a. To bng
C php:
CREATE TABLE <Tn_bng> (
<Tn_ct_1> <Kiu_d_liu> [Not null] [unique] ,
<Tn_ct_2> <Kiu_d_liu> [Not null] [unique] ,
...
<Tn_ct_n> <Kiu_d_liu> [Not null] [unique],
PRIMARY KEY (Kho_chnh),
[UNIQUE (Kho), ...]
[FOREIGN KEY (Kho_ngoi) REFERENCES
[on delete cascade | on delete restrict] ,..]
[CHECK (<iu_kin_rng_buc>)] );

## III. NGN NG SQL

3.2. Ngn ng nh ngha d liu (tip)
z

s thp phn.

## Float: S thc dng du chm ng

Char(n): Xu k t c di c nh n ( n <=255 )

## III. NGN NG SQL

3.2. Ngn ng nh ngha d liu (tip)
b. Xo bng
C php:
DROP TABLE <Tn_bng>;
V d 1: Xo bng HD_MH.
DROP TABLE HD_MH;

## III. NGN NG SQL

3.2. Ngn ng nh ngha d liu (tip)
c. Thm ct ca bng
C php:
ALTER TABLE <Tn_bng>
V d 2:
Thm ct MoTaMH (m t mt hng) vi kiu d liu
Varchar(100) cho bng MatHang.
ALTER TABLE MatHang

## III. NGN NG SQL

3.2. Ngn ng nh ngha d liu (tip)
d. Xo ct ca bng
C php:
ALTER TABLE <Tn_bng>
DROP <Tn_ct>;
V d 3: Xo ct TKkhoanNCC ca bng NhaCungCap.
ALTER TABLE NhaCungCap
DROP TKhoanNCC;

## III. NGN NG SQL

3.3. Truy vn d liu
a. Cu trc c bn ca cu lnh SELECT...
SELECT A1, A2,., An
FROM r1, r2, , rm
WHERE P;
Trong :
- Ai (i=1..n ) l tn cc thuc tnh c mt trong kt qu truy vn,
- ri (i=1..m ) l tn cc quan h,
- P l biu thc logic m cc b trong kt qu truy vn phi tho mn.
Truy vn trn tng ng vi biu thc i s quan h sau:

## III. NGN NG SQL

3.3. Truy vn d liu (tip)
b, Mnh SELECT
Mnh SELECT lit k cc thuc tnh cn a ra trong kt qu truy
vn. Mnh ny tng ng vi php chiu trong i s quan h.
V d 3.5.5:
a ra m, tn ca tt c cc mt hng c bn ti ca hng
SELECT MaMH, TenMH
FROM MATHANG;
Ch : Cc tn (thuc tnh, quan h) trong SQL khng phn bit ch hoa
v ch thng; ch bao gm ch ci, ch s v du gch di.

## III. NGN NG SQL

3.3. Truy vn d liu
b, Mnh SELECT (tip)
- SQL cho php lp cc b trong kt qu truy vn. loi b cc b lp ta
vit thm t kho DISTINCT vo sau SELECT.
V d 3.5.6:
a ra danh sch tn tnh/thnh ph c khch hng mua hng?
SELECT DISTINCT DiaChiKH
FROM KHACHHANG;

## III. NGN NG SQL

3.3. Truy vn d liu
b, Mnh SELECT (tip)
- gi li cc b lp ta thm t kho ALL v sau SELECT.
V d 3.5.7:
SELECT ALL DiaChiKH
FROM KHACHHANG;

## III. NGN NG SQL

3.3. Truy vn d liu
b, Mnh SELECT (tip)
- Nu gia cc quan h trong mnh FROM c thuc tnh chung, th
ta phi ch r thuc tnh l ca quan h no, bng cch vit tn quan
h ngay trc tn thuc tnh, gia chng c ngn cch bi du
chm (.).
V d 3.5.8:
SELECT KHACHHANG.MaKH,TenKH, SoDH, NgayHD

## III. NGN NG SQL

3.3. Truy vn d liu
b, Mnh SELECT (tip)
- Trong trng hp ta mun a ra tt c cc thuc tnh ca cc quan
h trong mnh FROM, ta ch cn dng du '*' thay cho vic lit k
cc thuc tnh.
V d 3.5.9:
SELECT *

## III. NGN NG SQL

3.3. Truy vn d liu
b, Mnh SELECT (tip)
- Trong mnh SELECT c th cha biu thc ton hc gm cc
php ton: +, -, *, / .
V d 3.5.11:
a ra danh sch cc mt hng vi n gi c gim 10%
SELECT MaMH, TenMH,DonGia - DonGia*0.1
FROM MATHANG;

## III. NGN NG SQL

3.3. Truy vn d liu (tip)
c, Mnh FROM
Mnh FROM lit k cc quan h phi c trong truy vn. Tng ng
vi tch Cc trong i s quan h.

## III. NGN NG SQL

3.3. Truy vn d liu (tip)
d, Mnh WHERE

## Mnh WHERE xc nh iu kin m kt qu truy vn phi tho

mn. Tng ng vi php chn ca i s quan h.

## III. NGN NG SQL

3.3. Truy vn d liu
d, Mnh WHERE (tip)
V d 3.5.12:
a ra danh sch cc mt hng ca nh cung cp 'DELL' c gi thp
hn 1000000.
SELECT *
FROM MATHANG, NHACUNGCAP
WHERE (MATHANG.MaNCC=NHACUNGCAP.MaNCC) AND
(TenNCC = 'DELL') AND
(DonGia < 1000000);
Ghi ch: Cc hng k t v ngy thng trong SQL c trong cp du
nhy n ().

## III. NGN NG SQL

3.3. Truy vn d liu
e, Mt s ton t khc
- Ton t so snh BETWEEN AND
V d 3.5.13: Tm tt c cc mt hng c gi t 100000 n 200000.
SELECT *
FROM MATHANG
WHERE (DonGia >=100000) AND (DonGia <=200000);
Cu lnh trn c th vit li nh sau:
SELECT *
FROM MATHANG
WHERE DonGia between 100000 and 200000;

## III. NGN NG SQL

3.3. Truy vn d liu
e, Mt s ton t khc
- Ton t so snh xu k t like
V d 3.5.14: a ra danh sch cc mt hng m tn mt hng cha cm
t 'HP'.
SELECT *
FROM MATHANG
WHERE TenMH like '%HP%';
Ta c th s dng 2 k t '%' hay '_ ' thay th cho mt xu hay mt
k t bt k.
z
z

## % : Thay cho mt chui k t bt k

_: Thay cho mt k t bt k

## III. NGN NG SQL

5.5. Sp xp kt qu truy vn
sp xp kt qu truy vn theo th t tng hay gim dn gi tr
ca mt hay mt s ct no , ta vit mnh ORDER BY vo sau
truy vn.
Cch vit:
ORDER BY B1 [DESC | ASC ] [,B2 [DESC | ASC ]] [,Bk [DESC | ASC ]]

## III. NGN NG SQL

5.6. Cc php ton tp hp (Set Operations)
thc hin php hp, giao v tr trong SQL ta dng cc php
ton: union, intersect v except.
Vi cc php trn, SQL s t ng loi b cc b trng nhau;
gi li cc b trng nhau ta vit t kho all vo pha sau cc php ton,
tc l: union all, intersect all v except all.

## III. NGN NG SQL

5.7. Gi tr NULL v cc hm tng hp ca SQL
(Aggregate Functions)
a, Gi tr NULL
Trong mi dng ca bng, c th c mt s thuc tnh m gi tr
ca chng l cha bit, hay khng tn ti. Trong SQL biu th gi tr
cha bit hay khng tn ti ngi ta dng hng NULL.
kim tra gi tr ca thuc tnh c l NULL hay khng ta dng
ton t IS.

## III. NGN NG SQL

5.7. Gi tr NULL v cc hm tng hp ca SQL
b, Cc php ton trn gi tr NULL:
- Khi thc hin cc php ton s hc vi gi tr NULL, kt qu l mt gi tr
NULL.
V d 3..5.19: 6+ NULL =NULL
- Khi thc hin php so snh (>,>=,<,<=,<>,=) vi gi tr NULL kt qu tr
v l FALSE.

## III. NGN NG SQL

5.7. Gi tr NULL v cc hm tng hp ca SQL
c, Cc hm tng hp
Cc hm tng hp ca SQL gm:
- SUM(Tn_ct): Tnh tng gi tr theo ct c tn l Tn_ct ca cc
b.
- MAX(Tn_ct): Cho gi tr ln nht trong ct c tn l Tn_ct.
- MIN(Tn_ct): Cho gi tr nh nht trong ct c tn l Tn_ct.
- AVG(Tn_ct): Tnh gi tr trung bnh theo ct c tn l Tn_ct ca
cc b.

## III. NGN NG SQL

5.7. Gi tr NULL v cc hm tng hp ca SQL
c, Cc hm tng hp
- COUNT(*|Tn_ct |DISTINCT Tn_ct): m s bn ghi trong bng
theo tu chn:
z

NULL

## DISTINCT Tn_ct : m cc bn ghi m gi tr ca ct

Tn_ct khc NULL v cc bn ghi ging nhau ch tnh mt.

## III. NGN NG SQL

5.8. Cc hm tng hp vi vic nhm d liu
nhm d liu trn mt hay mt s ct, ta dng mnh
GROUP BY c vit ngay sau mnh WHERE hay FROM (nu
khng c WHERE).
C php:
GROUP BY <danh sch ct>
[HAVING <iu kin>]
Trong :
-

tho mn.

## III. NGN NG SQL

5.8. Cc hm tng hp vi vic nhm d liu
V d 1:
Cho bit s loi mt hng ca mi nh cung cp cung ng?
SELECT NHACUNGCAP.MaNCC, TenNCC, COUNT(*)
FROM NHACUNGCAP, MATHANG
WHERE NHACUNGCAP.MaNCC= MATHANG.MaNCC
GROUP BY NHACUNGCAP.MaNCC, TenNCC;

## III. NGN NG SQL

5.8. Cc hm tng hp vi vic nhm d liu
V d 2:
Cho bit cc s ho n m trn bn t 2 mt hng tr ln?
HAVING COUNT(*)>=2;

## III. NGN NG SQL

5.8. Cc hm tng hp vi vic nhm d liu
Ghi ch:
z

GROUP BY

## III. NGN NG SQL

5.9. Truy vn lng (Nested Subqueries)
SQL cung cp mt c ch cho php cho php lng cc truy vn
con trong mnh FROM v WHERE. Mi truy vn con l mt truy vn
c dng SELECT FROMWHERE.
#Thng cc truy vn con c s dng kim tra mt phn t
thuc tp hp (quan h), so snh tp hp.

## III. NGN NG SQL

5.9. Truy vn lng (Nested Subqueries)
V d 1 a ra tn mt hng c gi cao nht?
SELECT TenMH
FROM MATHANG
WHERE DonGia in (SELECT MAX(DonGia) FROM MATHANG);

## III. NGN NG SQL

5.9. Truy vn lng (Nested Subqueries)
V d 2: a ra tn cc mt hng khng c bn trong ngy '05/06/2006'
SELECT TenMH
FROM MATHANG
WHERE MaMH not in (SELECT MaMH
AND (NgayHD='05/06/2006'));

## III. NGN NG SQL

5.10. Khung nhn (Views)
SQL cung cp mt c ch cho php che du i mt s d liu i
vi ngi s dng bng vic s dng mt khung nhn. to khung
nhn ta dng lnh:
CREATE VIEW <tn khung nhn> AS
<biu thc truy vn>

## III. NGN NG SQL

5.10. Khung nhn (Views)
V d 1: To khung nhn cha danh sch cc mt hng c cung ng
bi nh cung cp 'Sam sung'.
CREATE VIEW HangSS AS
SELECT MaMH, TenMH, DonGia
FROM MATHANG, NHACUNGCAP
WHERE (MATHANG.MaNCC= NHACUNGCAP.MaNCC)
AND (TenNCC='Sum sung');

## III. NGN NG SQL

5.10. Khung nhn (Views)
V d 2: Cho bit tn mt hng bn chy nht?
(1)
CREATE VIEW TH AS
SELECT TenMH, SUM(Soluong) AS sl
FROM MATHANG, HD_MH
WHERE MATHANG.MaMH= HD_MH.MaMH
GROUP BY TenMH;
(2)
SELECT *
FROM TH
WHERE sl IN (SELECT MAX(sl) FROM TH);

## III. NGN NG SQL

5.11. Cc lnh cp nht d liu
a. Vo d liu cho bng
chn thm mt bn ghi mi vo bng, ta dng lnh sau:
INSERT INTO <Tn_bng>[(B1, B2,,Bk)]
VALUES (C1, C2,., Ck);
Trong :
- Bi (i=1,..k, k<=n _ n l s thuc tnh ca <Tn_bng>) l cc thuc tnh
ca <Tn_bng>. Nu khng c danh sch thuc tnh ny SQL s ly
ton b thuc tnh.
- Ci (i=1,..k) l gi tr tng ng vi cc thuc tnh Bi. Nu Ci l gi tr Null,
ta dng hng NULL cho Ci.

## III. NGN NG SQL

5.11. Cc lnh cp nht d liu
a. Vo d liu cho bng
V d: Thm bn ghi (SO, 'Sony, Nht, NULL, '13.111.02.987') vo
bng NHACUNGCAP.
INSERT INTO NHACUNGCAP
VALUES ('SO', 'Sony', 'Nht', NULL, '13.111.02.987');

## III. NGN NG SQL

5.11. Cc lnh cp nht d liu
a. Vo d liu cho bng
chn thm mt bn ghi mi vo bng, ta dng lnh sau:
INSERT INTO <Tn_bng>[(B1, B2,,Bk)]
VALUES (C1, C2,., Ck);
Trong :
- Bi (i=1,..k, k<=n _ n l s thuc tnh ca <Tn_bng>) l cc thuc tnh
ca <Tn_bng>. Nu khng c danh sch thuc tnh ny SQL s ly
ton b thuc tnh.
- Ci (i=1,..k) l gi tr tng ng vi cc thuc tnh Bi. Nu Ci l gi tr Null,
ta dng hng NULL cho Ci.

## III. NGN NG SQL

5.11. Cc lnh cp nht d liu
b. Xo bn ghi trong bng
DELETE FROM Tn_bng
[WHERE iu_kin]
V d: Loi b cc mt hng c cung ng bi nh cung cp c m 'HP'
DELETE FROM MATHANG
WHERE MaNCC=HP

## III. NGN NG SQL

5.11. Cc lnh cp nht d liu
c. Sa ni dung cc bn ghi trong bng
UPDATE Tn_bng
SET <Tn_ct 1> = <Biu_thc 1>,
..
<Tn_ct k> = <Biu_thc k>
[WHERE iu_kin]
Trong :
- <Tn_bng> l bng c bn ghi cn thay i ni dung,
- <Tn_ct i> (i=1,..k) l cc ct cn thay i gi tr bi cc gi tr mi l
<Biu_thci> tng ng.

## III. NGN NG SQL

5.11. Cc lnh cp nht d liu
c. Sa ni dung cc bn ghi trong bng
V d: Vit lnh thc hin vic gim gi 10% cho cc mt hng ca
nh cung cp c m 'TO'.
UPDATE MATHANG
SET DonDia = DonGia - DonGia * 0.1
WHERE MaNCC=TO

## III. NGN NG SQL

5.12. Kt ni cc quan h
Php kt ni cho php kt ni 2 quan h v kt qu tr v l mt
quan h khc. SQL cung cp mt s loi kt ni sau: