際際滷

際際滷Share a Scribd company logo
Khoa HTTT - 畉i h畛c
CNTT 1
Bi 5: Ng担n ng畛 SQL
Hnh 畛ng
Khoa HTTT - 畉i h畛c CNTT 2
N畛i dung
1. Gi畛i thi畛u
2. C叩c ng担n ng畛 giao ti畉p
3. Ng担n ng畛 畛nh ngh挑a d畛 li畛u
4. Ng担n ng畛 thao t叩c d畛 li畛u
5. Ng担n ng畛 truy v畉n d畛 li畛u c坦 c畉u tr炭c
6. Ng担n ng畛 i畛u khi畛n d畛 li畛u
Khoa HTTT - 畉i h畛c CNTT 3
 L ng担n ng畛 chu畉n 畛 truy v畉n v thao t叩c tr棚n
CSDL quan h畛
 L ng担n ng畛 phi th畛 t畛c
 Kh畛i ngu畛n c畛a SQL l SEQUEL - Structured
English Query Language, nm 1974)
 C叩c chu畉n SQL
 SQL89
 SQL92 (SQL2)
 SQL99 (SQL3)
1. Gi畛i thi畛u
Khoa HTTT - 畉i h畛c CNTT 4
2. C叩c ng担n ng畛 giao ti畉p
 Ng担n ng畛 畛nh ngh挑a d畛 li畛u (Data Definition
Language - DDL): cho ph辿p khai b叩o c畉u tr炭c b畉ng,
c叩c m畛i quan h畛 v c叩c rng bu畛c.
 Ng担n ng畛 thao t叩c d畛 li畛u (Data Manipulation
Language - DML): cho ph辿p th棚m, x坦a, s動a d動 li畛u.
 Ng担n ng畛 truy v畉n d畛 li畛u (Structured Query
Language  SQL): cho ph辿p truy v畉n d畛 li畛u.
 Ng担n ng畛 i畛u khi畛n d畛 li畛u (Data Control
Language  DCL): khai b叩o b畉o m畉t th担ng tin, c但p
quy棚n va thu h担i quy棚n khai th叩c tr棚n c董 s董 d動 li棚味u.
Khoa HTTT - 畉i h畛c CNTT 5
3.1 L畛nh ta味o b畉ng (CREATE)
3.1.1 C炭 ph叩p
3.1.2 M畛t s畛 ki畛u d畛 li畛u
3.2 L畛nh s畛a c但u tr炭c b畉ng (ALTER)
3.2.1 Th棚m thu畛c t鱈nh
3.2.2 S畛a ki畛u d畛 li畛u c畛a thu畛c t鱈nh
3.2.3 Xo叩 thu畛c t鱈nh
3.2.4 Th棚m rng bu畛c ton v畉n
3.2.5 Xo叩 rng bu畛c ton v畉n
3.3 L畛nh x坦a b畉ng (DROP)
3. Ng担n ng畛 畛nh ngh挑a d畛 li畛u
Khoa HTTT - 畉i h畛c CNTT 6
3.1.1 C炭 ph叩p
CREATE TABLE <t棚n_b畉ng>
(
<t棚n_c畛t1> <ki畛u_d畛_li畛u> [not null],
<t棚n_c畛t2> <ki畛u_d畛_li畛u> [not null],

<t棚n_c畛t1> <ki畛u_d畛_li畛u> [not null],
khai b叩o kh坦a ch鱈nh, kh坦a ngo畉i, rng bu畛c
)
3.1 L畛nh t畉o b畉ng
Khoa HTTT - 畉i h畛c CNTT 7
Ki畛u d畛 li畛u SQL Server
Chu畛i k箪 t畛 varchar(n), char(n),nvarchar(n), nchar(n)
S畛 tinyint,smallint, int,
numeric(m,n), decimal(m,n),float, real,
smallmoney, money
Ngy th叩ng smalldatetime, datetime
Lu畉n l箪 bit
3.1 L畛nh t畉o b畉ng (2)
3.1.2 M畛t s畛 ki畛u d畛 li畛u
Khoa HTTT - 畉i h畛c CNTT 8
3.1 L畛nh t畉o b畉ng (3)
L動畛c 畛 CSDL qu畉n l箪 b叩n hng g畛m c坦 c叩c quan h畛 sau:
KHACHHANG (MAKH, HOTEN, DCHI, SODT, NGSINH,
DOANHSO, NGDK, CMND)
NHANVIEN (MANV,HOTEN, NGVL, SODT)
SANPHAM (MASP,TENSP, DVT, NUOCSX, GIA)
HOADON (SOHD, NGHD, MAKH, MANV, TRIGIA)
CTHD (SOHD,MASP,SL)
Khoa HTTT - 畉i h畛c CNTT 9
3.1 L畛nh t畉o b畉ng (4)
Create table KHACHHANG
(
MAKH char(4) primary key,
HOTEN varchar(40),
DCHI varchar(50),
SODT varchar(20),
NGSINH smalldatetime,
DOANHSO money,
NGDK smalldatetime,
CMND varchar(10)
)
Khoa HTTT - 畉i h畛c CNTT 10
3.1 L畛nh t畉o b畉ng (5)
Create table CTHD
(
SOHD int foreign key
hreferences OADON(SOHD),
MASP char(4) foreign key
references SANPHAM(MASP),
SL int,
constraint PK_CTHD primary key (SOHD,MASP)
)
Khoa HTTT - 畉i h畛c CNTT 11
3.2.1 Th棚m thu畛c t鱈nh
ALTER TABLE t棚nb畉ng ADD t棚nc畛t ki畛ud畛li畛u
 V鱈 d畛: th棚m c担味t Ghi_chu vo b畉ng kh叩ch hng
ALTER TABLE KHACHHANG ADD GHI_CHU varchar(20)
3.2.2 S畛a ki畛u d畛 li畛u thu畛c t鱈nh
ALTER TABLE t棚nb畉ng ALTER COLUMN t棚nc畛t
ki畛ud畛li畛u_m畛i
 L動u 箪:
Kh担ng ph畉i s畛a b畉t k畛 ki畛u d畛 li畛u no c滴ng 動畛c
3.2 S畛a c畉u tr炭c b畉ng(1)
Khoa HTTT - 畉i h畛c CNTT 12
 V鱈 d畛: S畛a C畛t Ghi_chu thnh ki畛u d畛 li畛u varchar(50)
ALTER TABLE KHACHHANG ALTER COLUMN GHI_CHU varchar(50)
 N畉u s畛a ki畛u d畛 li畛u c畛a c畛t Ghi_chu thnh varchar(5), m
tr動畛c 坦 達 nh畉p gi叩 tr畛 cho c畛t Ghi_chu c坦 畛 di h董n 5 k箪
t畛 th狸 kh担ng 動畛c ph辿p.
 Ho畉c s畛a t畛 ki畛u chu畛i k箪 t畛 sang ki畛u s畛, 
3.2.3 X坦a thu畛c t鱈nh
ALTER TABLE t棚n_b畉ng DROP COLUMN t棚n_c畛t
 V鱈 d畛: x坦a c畛t Ghi_chu trong b畉ng KHACHHANG
ALTER TABLE KHACHHANG DROP COLUMN Ghi_chu
3.2 S畛a c畉u tr炭c b畉ng(2)
Khoa HTTT - 畉i h畛c CNTT 13
3.2.4 Th棚m rng bu畛c ton v畉n
ALTER TABLE <t棚n_b畉ng>
ADD CONSTRAINT
<t棚n_rng_bu畛c>
UNIQUE t棚n_c畛t
PRIMARY KEY (t棚n_c畛t)
FOREIGN KEY (t棚n_c畛t)
REFERENCES t棚n_b畉ng
(c畛t_l_kh坦a_ch鱈nh) [ON
DELETE CASCADE] [ON
UPDATE CASCADE]
CHECK (t棚n_c畛t i畛u_ki畛n)
3.2 S畛a c畉u tr炭c b畉ng(3)
Khoa HTTT - 畉i h畛c CNTT 14
 V鱈 d畛
 ALTER TABLE NHANVIEN ADD CONSTRAINT PK_NV
PRIMARY KEY (MANV)
 ALTER TABLE CTHD ADD CONSTRAINT FK_CT_SP
FOREIGN KEY (MASP) REFERENCES
SANPHAM(MASP)
 ALTER TABLE SANPHAM ADD CONSTRAINT
CK_GIA CHECK (GIA >=500)
 ALTER TABLE KHACHHANG ADD CONSTRAINT
UQ_KH UNIQUE (CMND)
3.2 S畛a c畉u tr炭c b畉ng(4)
Khoa HTTT - 畉i h畛c CNTT 15
3.2.5 X坦a rng bu畛c ton v畉n
ALTER TABLE t棚n_b畉ng DROP CONSTRAINT
t棚n_rng_bu畛c
 V鱈 d畛:
 Alter table CTHD drop constraint FK_CT_SP
 Alter table SANPHAM drop constraint ck_gia
 L動u 箪: 畛i v畛i rng bu畛c kh坦a ch鱈nh, mu畛n x坦a
rng bu畛c ny ph畉i x坦a h畉t c叩c rng bu畛c kh坦a
ngo畉i tham chi畉u t畛i n坦
3.2 S畛a c畉u tr炭c b畉ng(5)
Khoa HTTT - 畉i h畛c CNTT 16
 C炭 ph叩p
DROP TABLE t棚n_b畉ng
 V鱈 d畛: x坦a b畉ng KHACHHANG.
DROP TABLE KHACHHANG
 L動u 箪: khi mu畛n x坦a m畛t b畉ng ph畉i x坦a t畉t
c畉 nh畛ng kh坦a ngo畉i tham chi畉u t畛i b畉ng 坦
tr動畛c.
3.3 L畛nh x坦a b畉ng
Khoa HTTT - 畉i h畛c CNTT 17
 G担m c叩c l畛nh:
4.1 L畛nh th棚m d畛 li棚味u (INSERT)
4.2 L畛nh s畛a d畛 li棚味u (UPDATE)
4.3 L畛nh x坦a d畛 li棚味u (DELETE)
4. Ng担n ng畛 thao t叩c d畛 li畛u
Khoa HTTT - 畉i h畛c CNTT 18
 C炭 ph叩p
INSERT INTO t棚n_b畉ng (c畛t1,,c畛tn) VALUES
(gi叩_tr畛_1,., gi叩_tr畛_n)
INSERT INTO t棚n_b畉ng VALUES (gi叩_tr畛_1, gi叩_tr畛_2,
, gi叩_tr畛_n)
 V鱈 d畛:
 insert into SANPHAM values('BC01','But chi', 'cay',
'Singapore', 3000)
 insert into SANPHAM(masp,tensp,dvt,nuocsx,gia)
values ('BC01','But chi','cay','Singapore',3000)
4.1 Th棚m d畛 li畛u
Khoa HTTT - 畉i h畛c CNTT 19
 C炭 ph叩p
UPDATE t棚n_b畉ng
SET c畛t_1 = gi叩_tr畛_1, c畛t_2 = gi叩_tr畛_2 .
[WHERE i畛u_ki畛n]
 L動u 箪: c畉n th畉n v畛i c叩c l畛nh x坦a v s畛a, n畉u kh担ng
c坦 i畛u ki畛n 畛 WHERE ngh挑a l x坦a ho畉c s畛a t畉t c畉.
 V鱈 d畛: Tng gi叩 10% 畛i v畛i nh畛ng s畉n ph畉m do
Trung Quoc s畉n xu畉t
UPDATE SANPHAM
SET Gia = Gia*1.1
WHERE Nuocsx=Trung Quoc
4.2 S畛a d畛 li畛u
Khoa HTTT - 畉i h畛c CNTT 20
4.3 X坦a d畛 li畛u
 C炭 ph叩p
DELETE FROM t棚n_b畉ng [WHERE i畛u_ki畛n]
 V鱈 d畛:
 X坦a ton b畛 nh但n vi棚n
DELETE FROM NHANVIEN
 X坦a nh畛ng s畉n ph畉m do Trung Qu畛c s畉n xu畉t c坦 gi叩
th畉p h董n 10000
DELETE FROM SANPHAM
WHERE (Gia <10000) and (Nuocsx=Trung Quoc)
Khoa HTTT - 畉i h畛c CNTT 21
5. Ng担n ng畛 truy v畉n d畛 li畛u c坦
c畉u tr炭c
5.1 C但u truy v畉n t畛ng qu叩t
5.2 Truy v畉n 董n gi畉n
5.3 Ph辿p k畉t
5.4 畉t b鱈 danh, s畛 d畛ng *, distinct
5.5 C叩c to叩n t畛
5.6 C但u truy v畉n con (subquery)
5.7 Ph辿p chia
5.8 Hm t鱈nh to叩n, gom nh坦m
Khoa HTTT - 畉i h畛c CNTT 22
SELECT [DISTINCT] *|t棚n_c畛t | hm
FROM b畉ng
[WHERE i畛u_ki畛n]
[GROUP BY t棚n_c畛t]
[HAVING i畛u_ki畛n]
[ORDER BY t棚n_c畛t ASC | DESC]
5.1C但u truy v畉n t畛ng qu叩t
Khoa HTTT - 畉i h畛c CNTT 23
5.2 Truy v畉n 董n gi畉n(1)
 SELECT
 T動董ng 動董ng ph辿p chi畉u c畛a SQH
 Li畛t k棚 c叩c thu畛c t鱈nh c畉n hi畛n th畛 trong k畉t qu畉
 WHERE
 T動董ng 畛ng v畛i i畛u ki畛n ch畛n trong SQH
 i畛u ki畛n li棚n quan t畛i thu畛c t鱈nh, s畛 d畛ng c叩c ph辿p n畛i
lu畉n l箪 AND, OR, NOT, c叩c ph辿p to叩n so s叩nh,
BETWEEN
 FROM
 Li畛t k棚 c叩c quan h畛 c畉n thi畉t, c叩c ph辿p k畉t
Khoa HTTT - 畉i h畛c CNTT 24
5.2 Truy v畉n 董n gi畉n(2)
 T狸m masp, tensp do Trung Quoc s畉n xu畉t
c坦 gi叩 t畛 20000 畉n 30000
Select masp,tensp
From SANPHAM
Where nuocsx=Trung Quoc
and gia between 20000 and 30000
Khoa HTTT - 畉i h畛c CNTT 25
5.3 Ph辿p k畉t(1)
 Inner Join, Left Join, Right Join, Full Join
 V鱈 d畛:
 In ra danh s叩ch c叩c kh叩ch hng (MAKH,
HOTEN) 達 mua hng trong ngy 1/1/2007.
select KHACHHANG.makh,hoten
from KHACHHANG inner join HOADON on
KHACHHANG.makh=HOADON.makh
where nghd='1/1/2007'
Khoa HTTT - 畉i h畛c CNTT 26
5.3 Ph辿p k畉t (2)
 V鱈 d畛: In ra danh s叩ch t畉t c畉 c叩c h坦a 董n v h畛 t棚n
c畛a kh叩ch hng mua h坦a 董n 坦 (n畉u c坦)
 Select sohd, hoten
From HOADON left join KHACHHANG on
HOADON.makh=KHACHHANG.makh
 Select sohd, hoten
From HOADON ,KHACHHANG
where HOADON.makh*=KHACHHANG.makh
Khoa HTTT - 畉i h畛c CNTT 27
5.4 畉t b鱈 danh, s畛 d畛ng *, distinct
 畉t b鱈 danh  Alias: cho thu畛c t鱈nh v quan h畛:
t棚n_c滴 AS t棚n_m畛i
 Select manv,hoten as [ho va ten] From NHANVIEN
 Li畛t k棚 t畉t c畉 c叩c thu畛c t鱈nh c畛a quan h畛:
 Select * from Nhanvien
 Select NHANVIEN.* from NHANVIEN
 Distinct: tr湛ng ch畛 l畉y m畛t l畉n
 Select distinct nuocsx from SANPHAM
 S畉p x畉p k畉t qu畉 hi畛n th畛: Order by
 Select * from SANPHAM order by nuocsx, gia DESC
Khoa HTTT - 畉i h畛c CNTT 28
5.5 To叩n t畛 truy v畉n(1)
 To叩n t畛 so s叩nh: =, >,<,>=,<=,<>
 To叩n t畛 logic: AND, OR, NOT
 Ph辿p to叩n: +, - ,* , /
 BETWEEN . AND
 IS NULL, IS NOT NULL
 LIKE (_ %)
 IN, NOT IN
 EXISTS , NOT EXISTS
 SOME, ALL
Khoa HTTT - 畉i h畛c CNTT 29
5.5 To叩n t畛 truy v畉n(2)
 IS NULL, IS NOT NULL
 Select sohd from HOADON where makh is Null
 Select * from HOADON where makh is Not Null
 To叩n t畛 so s叩nh, ph辿p to叩n
 Select gia*1.1 as [gia ban] from SANPHAM where
nuocsx<>Viet Nam
 Select * from SANPHAM where (gia between 20000 and
30000) OR (nuocsx=Viet Nam)
 To叩n t畛 IN, NOT IN
 Select * from SANPHAM where masp NOT IN
(BB01,BB02,BB03)
Khoa HTTT - 畉i h畛c CNTT 30
To叩n t畛 LIKE
 So s叩nh chu畛i t動董ng 畛i
 C炭 ph叩p: s LIKE p, p c坦 th畛 ch畛a % ho畉c _
 % : thay th畉 m畛t chu畛i k箪 t畛 b畉t k畛
 _ : thay th畉 m畛t k箪 t畛 b畉t k畛
 V鱈 d畛: Select masp,tensp from SANPHAM
where masp like 'B%01
5.5 To叩n t畛 so s叩nh(3)
Khoa HTTT - 畉i h畛c CNTT 31
5.6 C但u truy v畉n con (1)
In ho畉c Exists
 V鱈 d畛: T狸m c叩c s畛 h坦a 董n mua c湛ng l炭c 2 s畉n ph畉m
c坦 m達 s畛 BB01 v BB02.
 select distinct sohd
from CTHD where masp='BB01' and sohd IN
(select distinct sohd from CTHD where masp='BB02')
 select distinct A.sohd
from CTHD A where A.masp='BB01' and
EXISTS (select * from CTHD B
where B.masp='BB02 and A.sohd=B.sohd)
Khoa HTTT - 畉i h畛c CNTT 32
5.6 C但u truy v畉n con (2)
Not In ho畉c Not Exists
 V鱈 d畛: T狸m c叩c s畛 h坦a 董n c坦 mua s畉n ph畉m m達 s畛
BB01 nh動ng kh担ng mua s畉n ph畉m m達 s畛 BB02.
 select distinct sohd
from CTHD where masp='BB01' and sohd NOT IN
(select distinct sohd from CTHD where masp='BB02')
 select distinct A.sohd
from CTHD A where A.masp='BB01' and
NOT EXists (select * from CTHD B
where B.masp='BB02 and A.sohd=B.sohd)
Khoa HTTT - 畉i h畛c CNTT 33
5.7 Ph辿p chia
S畛 d畛ng NOT EXISTS
 V鱈 d畛: T狸m s畛 h坦a 董n 達 mua t畉t c畉 nh畛ng s畉n
ph畉m do Trung Quoc s畉n xu畉t.
 Select sohd from HOADON where not exists
(select * from SANPHAM
where nuocsx=Trung Quoc and not exists
(select * from CTHD where
HOADON.sohd=CTHD.sohd and
CTHD.masp=SANPHAM.masp))
Khoa HTTT - 畉i h畛c CNTT 34
5.8.1 C叩c hm t鱈nh to叩n c董 b畉n
 COUNT: 畉m s畛 b畛 d畛 li畛u c畛a thu畛c t鱈nh
 MIN: T鱈nh gi叩 tr畛 nh畛 nh畉t
 MAX: T鱈nh gi叩 tr畛 l畛n nh畉t
 AVG: T鱈nh gi叩 tr畛 trung b狸nh
 SUM: T鱈nh t畛ng gi叩 tr畛 c叩c b畛 d畛 li畛u
5.8 C叩c hm t鱈nh to叩n v gom
nh坦m (1)
Khoa HTTT - 畉i h畛c CNTT 35
NHANVIEN
MANV HOTEN PHAI MANQL PHONG LUONG
NV001 Nguy畛n Ng畛c Linh N畛 Null NC 2.800.000
NV002 inh B叩 Ti畉n Nam NV002 DH 2.000.000
NV003 Nguy畛n Vn M畉nh Nam NV001 NC 2.300.000
NV004 Tr畉n Thanh Long Nam NV002 DH 1.800.000
NV005 Nguy畛n Th畛 H畛ng V但n N畛 NV001 NC 2.500.000
NV006 Nguy畛n Minh Nam NV002 DH 2.000.000
NV007 H Duy L畉p Nam NV003 NC 1.800.000
NV008 Tr畉n Kim Duy棚n N畛 NV003 NC 1.800.000
NV009 Nguy畛n Kim Anh N畛 NV003 NC 2.000.000
Khoa HTTT - 畉i h畛c CNTT 36
V鱈 d畛
1. T鱈nh l動董ng th畉p nh畉t, cao nh畉t, trung b狸nh v t畛ng
l動董ng c畛a t畉t c畉 c叩c nh但n vi棚n.
2. C坦 t畉t c畉 bao nhi棚u nh但n vi棚n
3. Bao nhi棚u nh但n vi棚n c坦 ng動畛i qu畉n l箪
4. Bao nhi棚u ph嘆ng ban c坦 nh但n vi棚n tr畛c thu畛c
5. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n
6. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n theo
t畛ng ph嘆ng ban
Khoa HTTT - 畉i h畛c CNTT 37
1. T鱈nh l動董ng th畉p nh畉t, cao nh畉t, trung b狸nh
v t畛ng l動董ng c畛a t畉t c畉 c叩c nh但n vi棚n.
SELECT min(luong) as thapnhat,
max(luong) as caonhat,
avg(luong) as trungbinh,
sum(luong) as tongluong
FROM NhanVien
Khoa HTTT - 畉i h畛c CNTT 38
2. C坦 t畉t c畉 bao nhi棚u nh但n vi棚n
SELECT count(*) FROM NhanVien
3. Bao nhi棚u nh但n vi棚n c坦 ng動畛i qu畉n l箪
 Select count(*) FROM NhanVien WHERE manql is not null
 SELECT count(Manql) FROM NhanVien
4. Bao nhi棚u ph嘆ng ban c坦 nh但n vi棚n tr畛c thu畛c
SELECT count(distinct phong) FROM NhanVien
Khoa HTTT - 畉i h畛c CNTT 39
5.8 C叩c hm t鱈nh to叩n v gom
nh坦m (2)
5.8.2 Gom nh坦m: m畛nh 畛 GROUP BY
 S畛 d畛ng hm gom nh坦m tr棚n c叩c b畛 trong quan h畛.
 M畛i nh坦m b畛 bao g畛m t畉p h畛p c叩c b畛 c坦 c湛ng gi叩 tr畛 tr棚n
c叩c thu畛c t鱈nh gom nh坦m
 Hm gom nh坦m 叩p d畛ng tr棚n m畛i b畛 畛c l畉p nhau.
 SQL c坦 m畛nh 畛 GROUP BY 畛 ch畛 ra c叩c thu畛c t鱈nh
gom nh坦m, c叩c thu畛c t鱈nh ny ph畉i xu畉t hi畛n trong m畛nh
畛 SELECT
Khoa HTTT - 畉i h畛c CNTT 40
5. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n
SELECT avg(LUONG) as LUONGTB
FROM NhanVien
6. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n theo t畛ng
ph嘆ng ban.
SELECT phong, avg(LUONG) as LUONGTB
FROM NhanVien
GROUP BY phong
Khoa HTTT - 畉i h畛c CNTT 41
5.8 C叩c hm t鱈nh to叩n v gom
nh坦m (3)
5.8.3 i畛u ki畛n sau gom nh坦m: m畛nh 畛 HAVING
 L畛c k畉t qu畉 theo i畛u ki畛n, sau khi 達 gom nh坦m
 i畛u ki畛n 畛 HAVING 動畛c th畛c hi畛n sau khi gom nh坦m,
c叩c i畛u ki畛n c坦 li棚n quan 畉n thu畛c t鱈nh Group By
 V鱈 d畛: t狸m ph嘆ng c坦 s畛 l動畛ng nh但n vi棚n N畛 tr棚n 5 ng動畛i
SELECT phong
FROM NhanVien
WHERE phai = N畛
GROUP BY phong
HAVING count(manv) > 5

More Related Content

csdl - buoi7-8-9

  • 1. Khoa HTTT - 畉i h畛c CNTT 1 Bi 5: Ng担n ng畛 SQL Hnh 畛ng
  • 2. Khoa HTTT - 畉i h畛c CNTT 2 N畛i dung 1. Gi畛i thi畛u 2. C叩c ng担n ng畛 giao ti畉p 3. Ng担n ng畛 畛nh ngh挑a d畛 li畛u 4. Ng担n ng畛 thao t叩c d畛 li畛u 5. Ng担n ng畛 truy v畉n d畛 li畛u c坦 c畉u tr炭c 6. Ng担n ng畛 i畛u khi畛n d畛 li畛u
  • 3. Khoa HTTT - 畉i h畛c CNTT 3 L ng担n ng畛 chu畉n 畛 truy v畉n v thao t叩c tr棚n CSDL quan h畛 L ng担n ng畛 phi th畛 t畛c Kh畛i ngu畛n c畛a SQL l SEQUEL - Structured English Query Language, nm 1974) C叩c chu畉n SQL SQL89 SQL92 (SQL2) SQL99 (SQL3) 1. Gi畛i thi畛u
  • 4. Khoa HTTT - 畉i h畛c CNTT 4 2. C叩c ng担n ng畛 giao ti畉p Ng担n ng畛 畛nh ngh挑a d畛 li畛u (Data Definition Language - DDL): cho ph辿p khai b叩o c畉u tr炭c b畉ng, c叩c m畛i quan h畛 v c叩c rng bu畛c. Ng担n ng畛 thao t叩c d畛 li畛u (Data Manipulation Language - DML): cho ph辿p th棚m, x坦a, s動a d動 li畛u. Ng担n ng畛 truy v畉n d畛 li畛u (Structured Query Language SQL): cho ph辿p truy v畉n d畛 li畛u. Ng担n ng畛 i畛u khi畛n d畛 li畛u (Data Control Language DCL): khai b叩o b畉o m畉t th担ng tin, c但p quy棚n va thu h担i quy棚n khai th叩c tr棚n c董 s董 d動 li棚味u.
  • 5. Khoa HTTT - 畉i h畛c CNTT 5 3.1 L畛nh ta味o b畉ng (CREATE) 3.1.1 C炭 ph叩p 3.1.2 M畛t s畛 ki畛u d畛 li畛u 3.2 L畛nh s畛a c但u tr炭c b畉ng (ALTER) 3.2.1 Th棚m thu畛c t鱈nh 3.2.2 S畛a ki畛u d畛 li畛u c畛a thu畛c t鱈nh 3.2.3 Xo叩 thu畛c t鱈nh 3.2.4 Th棚m rng bu畛c ton v畉n 3.2.5 Xo叩 rng bu畛c ton v畉n 3.3 L畛nh x坦a b畉ng (DROP) 3. Ng担n ng畛 畛nh ngh挑a d畛 li畛u
  • 6. Khoa HTTT - 畉i h畛c CNTT 6 3.1.1 C炭 ph叩p CREATE TABLE <t棚n_b畉ng> ( <t棚n_c畛t1> <ki畛u_d畛_li畛u> [not null], <t棚n_c畛t2> <ki畛u_d畛_li畛u> [not null], <t棚n_c畛t1> <ki畛u_d畛_li畛u> [not null], khai b叩o kh坦a ch鱈nh, kh坦a ngo畉i, rng bu畛c ) 3.1 L畛nh t畉o b畉ng
  • 7. Khoa HTTT - 畉i h畛c CNTT 7 Ki畛u d畛 li畛u SQL Server Chu畛i k箪 t畛 varchar(n), char(n),nvarchar(n), nchar(n) S畛 tinyint,smallint, int, numeric(m,n), decimal(m,n),float, real, smallmoney, money Ngy th叩ng smalldatetime, datetime Lu畉n l箪 bit 3.1 L畛nh t畉o b畉ng (2) 3.1.2 M畛t s畛 ki畛u d畛 li畛u
  • 8. Khoa HTTT - 畉i h畛c CNTT 8 3.1 L畛nh t畉o b畉ng (3) L動畛c 畛 CSDL qu畉n l箪 b叩n hng g畛m c坦 c叩c quan h畛 sau: KHACHHANG (MAKH, HOTEN, DCHI, SODT, NGSINH, DOANHSO, NGDK, CMND) NHANVIEN (MANV,HOTEN, NGVL, SODT) SANPHAM (MASP,TENSP, DVT, NUOCSX, GIA) HOADON (SOHD, NGHD, MAKH, MANV, TRIGIA) CTHD (SOHD,MASP,SL)
  • 9. Khoa HTTT - 畉i h畛c CNTT 9 3.1 L畛nh t畉o b畉ng (4) Create table KHACHHANG ( MAKH char(4) primary key, HOTEN varchar(40), DCHI varchar(50), SODT varchar(20), NGSINH smalldatetime, DOANHSO money, NGDK smalldatetime, CMND varchar(10) )
  • 10. Khoa HTTT - 畉i h畛c CNTT 10 3.1 L畛nh t畉o b畉ng (5) Create table CTHD ( SOHD int foreign key hreferences OADON(SOHD), MASP char(4) foreign key references SANPHAM(MASP), SL int, constraint PK_CTHD primary key (SOHD,MASP) )
  • 11. Khoa HTTT - 畉i h畛c CNTT 11 3.2.1 Th棚m thu畛c t鱈nh ALTER TABLE t棚nb畉ng ADD t棚nc畛t ki畛ud畛li畛u V鱈 d畛: th棚m c担味t Ghi_chu vo b畉ng kh叩ch hng ALTER TABLE KHACHHANG ADD GHI_CHU varchar(20) 3.2.2 S畛a ki畛u d畛 li畛u thu畛c t鱈nh ALTER TABLE t棚nb畉ng ALTER COLUMN t棚nc畛t ki畛ud畛li畛u_m畛i L動u 箪: Kh担ng ph畉i s畛a b畉t k畛 ki畛u d畛 li畛u no c滴ng 動畛c 3.2 S畛a c畉u tr炭c b畉ng(1)
  • 12. Khoa HTTT - 畉i h畛c CNTT 12 V鱈 d畛: S畛a C畛t Ghi_chu thnh ki畛u d畛 li畛u varchar(50) ALTER TABLE KHACHHANG ALTER COLUMN GHI_CHU varchar(50) N畉u s畛a ki畛u d畛 li畛u c畛a c畛t Ghi_chu thnh varchar(5), m tr動畛c 坦 達 nh畉p gi叩 tr畛 cho c畛t Ghi_chu c坦 畛 di h董n 5 k箪 t畛 th狸 kh担ng 動畛c ph辿p. Ho畉c s畛a t畛 ki畛u chu畛i k箪 t畛 sang ki畛u s畛, 3.2.3 X坦a thu畛c t鱈nh ALTER TABLE t棚n_b畉ng DROP COLUMN t棚n_c畛t V鱈 d畛: x坦a c畛t Ghi_chu trong b畉ng KHACHHANG ALTER TABLE KHACHHANG DROP COLUMN Ghi_chu 3.2 S畛a c畉u tr炭c b畉ng(2)
  • 13. Khoa HTTT - 畉i h畛c CNTT 13 3.2.4 Th棚m rng bu畛c ton v畉n ALTER TABLE <t棚n_b畉ng> ADD CONSTRAINT <t棚n_rng_bu畛c> UNIQUE t棚n_c畛t PRIMARY KEY (t棚n_c畛t) FOREIGN KEY (t棚n_c畛t) REFERENCES t棚n_b畉ng (c畛t_l_kh坦a_ch鱈nh) [ON DELETE CASCADE] [ON UPDATE CASCADE] CHECK (t棚n_c畛t i畛u_ki畛n) 3.2 S畛a c畉u tr炭c b畉ng(3)
  • 14. Khoa HTTT - 畉i h畛c CNTT 14 V鱈 d畛 ALTER TABLE NHANVIEN ADD CONSTRAINT PK_NV PRIMARY KEY (MANV) ALTER TABLE CTHD ADD CONSTRAINT FK_CT_SP FOREIGN KEY (MASP) REFERENCES SANPHAM(MASP) ALTER TABLE SANPHAM ADD CONSTRAINT CK_GIA CHECK (GIA >=500) ALTER TABLE KHACHHANG ADD CONSTRAINT UQ_KH UNIQUE (CMND) 3.2 S畛a c畉u tr炭c b畉ng(4)
  • 15. Khoa HTTT - 畉i h畛c CNTT 15 3.2.5 X坦a rng bu畛c ton v畉n ALTER TABLE t棚n_b畉ng DROP CONSTRAINT t棚n_rng_bu畛c V鱈 d畛: Alter table CTHD drop constraint FK_CT_SP Alter table SANPHAM drop constraint ck_gia L動u 箪: 畛i v畛i rng bu畛c kh坦a ch鱈nh, mu畛n x坦a rng bu畛c ny ph畉i x坦a h畉t c叩c rng bu畛c kh坦a ngo畉i tham chi畉u t畛i n坦 3.2 S畛a c畉u tr炭c b畉ng(5)
  • 16. Khoa HTTT - 畉i h畛c CNTT 16 C炭 ph叩p DROP TABLE t棚n_b畉ng V鱈 d畛: x坦a b畉ng KHACHHANG. DROP TABLE KHACHHANG L動u 箪: khi mu畛n x坦a m畛t b畉ng ph畉i x坦a t畉t c畉 nh畛ng kh坦a ngo畉i tham chi畉u t畛i b畉ng 坦 tr動畛c. 3.3 L畛nh x坦a b畉ng
  • 17. Khoa HTTT - 畉i h畛c CNTT 17 G担m c叩c l畛nh: 4.1 L畛nh th棚m d畛 li棚味u (INSERT) 4.2 L畛nh s畛a d畛 li棚味u (UPDATE) 4.3 L畛nh x坦a d畛 li棚味u (DELETE) 4. Ng担n ng畛 thao t叩c d畛 li畛u
  • 18. Khoa HTTT - 畉i h畛c CNTT 18 C炭 ph叩p INSERT INTO t棚n_b畉ng (c畛t1,,c畛tn) VALUES (gi叩_tr畛_1,., gi叩_tr畛_n) INSERT INTO t棚n_b畉ng VALUES (gi叩_tr畛_1, gi叩_tr畛_2, , gi叩_tr畛_n) V鱈 d畛: insert into SANPHAM values('BC01','But chi', 'cay', 'Singapore', 3000) insert into SANPHAM(masp,tensp,dvt,nuocsx,gia) values ('BC01','But chi','cay','Singapore',3000) 4.1 Th棚m d畛 li畛u
  • 19. Khoa HTTT - 畉i h畛c CNTT 19 C炭 ph叩p UPDATE t棚n_b畉ng SET c畛t_1 = gi叩_tr畛_1, c畛t_2 = gi叩_tr畛_2 . [WHERE i畛u_ki畛n] L動u 箪: c畉n th畉n v畛i c叩c l畛nh x坦a v s畛a, n畉u kh担ng c坦 i畛u ki畛n 畛 WHERE ngh挑a l x坦a ho畉c s畛a t畉t c畉. V鱈 d畛: Tng gi叩 10% 畛i v畛i nh畛ng s畉n ph畉m do Trung Quoc s畉n xu畉t UPDATE SANPHAM SET Gia = Gia*1.1 WHERE Nuocsx=Trung Quoc 4.2 S畛a d畛 li畛u
  • 20. Khoa HTTT - 畉i h畛c CNTT 20 4.3 X坦a d畛 li畛u C炭 ph叩p DELETE FROM t棚n_b畉ng [WHERE i畛u_ki畛n] V鱈 d畛: X坦a ton b畛 nh但n vi棚n DELETE FROM NHANVIEN X坦a nh畛ng s畉n ph畉m do Trung Qu畛c s畉n xu畉t c坦 gi叩 th畉p h董n 10000 DELETE FROM SANPHAM WHERE (Gia <10000) and (Nuocsx=Trung Quoc)
  • 21. Khoa HTTT - 畉i h畛c CNTT 21 5. Ng担n ng畛 truy v畉n d畛 li畛u c坦 c畉u tr炭c 5.1 C但u truy v畉n t畛ng qu叩t 5.2 Truy v畉n 董n gi畉n 5.3 Ph辿p k畉t 5.4 畉t b鱈 danh, s畛 d畛ng *, distinct 5.5 C叩c to叩n t畛 5.6 C但u truy v畉n con (subquery) 5.7 Ph辿p chia 5.8 Hm t鱈nh to叩n, gom nh坦m
  • 22. Khoa HTTT - 畉i h畛c CNTT 22 SELECT [DISTINCT] *|t棚n_c畛t | hm FROM b畉ng [WHERE i畛u_ki畛n] [GROUP BY t棚n_c畛t] [HAVING i畛u_ki畛n] [ORDER BY t棚n_c畛t ASC | DESC] 5.1C但u truy v畉n t畛ng qu叩t
  • 23. Khoa HTTT - 畉i h畛c CNTT 23 5.2 Truy v畉n 董n gi畉n(1) SELECT T動董ng 動董ng ph辿p chi畉u c畛a SQH Li畛t k棚 c叩c thu畛c t鱈nh c畉n hi畛n th畛 trong k畉t qu畉 WHERE T動董ng 畛ng v畛i i畛u ki畛n ch畛n trong SQH i畛u ki畛n li棚n quan t畛i thu畛c t鱈nh, s畛 d畛ng c叩c ph辿p n畛i lu畉n l箪 AND, OR, NOT, c叩c ph辿p to叩n so s叩nh, BETWEEN FROM Li畛t k棚 c叩c quan h畛 c畉n thi畉t, c叩c ph辿p k畉t
  • 24. Khoa HTTT - 畉i h畛c CNTT 24 5.2 Truy v畉n 董n gi畉n(2) T狸m masp, tensp do Trung Quoc s畉n xu畉t c坦 gi叩 t畛 20000 畉n 30000 Select masp,tensp From SANPHAM Where nuocsx=Trung Quoc and gia between 20000 and 30000
  • 25. Khoa HTTT - 畉i h畛c CNTT 25 5.3 Ph辿p k畉t(1) Inner Join, Left Join, Right Join, Full Join V鱈 d畛: In ra danh s叩ch c叩c kh叩ch hng (MAKH, HOTEN) 達 mua hng trong ngy 1/1/2007. select KHACHHANG.makh,hoten from KHACHHANG inner join HOADON on KHACHHANG.makh=HOADON.makh where nghd='1/1/2007'
  • 26. Khoa HTTT - 畉i h畛c CNTT 26 5.3 Ph辿p k畉t (2) V鱈 d畛: In ra danh s叩ch t畉t c畉 c叩c h坦a 董n v h畛 t棚n c畛a kh叩ch hng mua h坦a 董n 坦 (n畉u c坦) Select sohd, hoten From HOADON left join KHACHHANG on HOADON.makh=KHACHHANG.makh Select sohd, hoten From HOADON ,KHACHHANG where HOADON.makh*=KHACHHANG.makh
  • 27. Khoa HTTT - 畉i h畛c CNTT 27 5.4 畉t b鱈 danh, s畛 d畛ng *, distinct 畉t b鱈 danh Alias: cho thu畛c t鱈nh v quan h畛: t棚n_c滴 AS t棚n_m畛i Select manv,hoten as [ho va ten] From NHANVIEN Li畛t k棚 t畉t c畉 c叩c thu畛c t鱈nh c畛a quan h畛: Select * from Nhanvien Select NHANVIEN.* from NHANVIEN Distinct: tr湛ng ch畛 l畉y m畛t l畉n Select distinct nuocsx from SANPHAM S畉p x畉p k畉t qu畉 hi畛n th畛: Order by Select * from SANPHAM order by nuocsx, gia DESC
  • 28. Khoa HTTT - 畉i h畛c CNTT 28 5.5 To叩n t畛 truy v畉n(1) To叩n t畛 so s叩nh: =, >,<,>=,<=,<> To叩n t畛 logic: AND, OR, NOT Ph辿p to叩n: +, - ,* , / BETWEEN . AND IS NULL, IS NOT NULL LIKE (_ %) IN, NOT IN EXISTS , NOT EXISTS SOME, ALL
  • 29. Khoa HTTT - 畉i h畛c CNTT 29 5.5 To叩n t畛 truy v畉n(2) IS NULL, IS NOT NULL Select sohd from HOADON where makh is Null Select * from HOADON where makh is Not Null To叩n t畛 so s叩nh, ph辿p to叩n Select gia*1.1 as [gia ban] from SANPHAM where nuocsx<>Viet Nam Select * from SANPHAM where (gia between 20000 and 30000) OR (nuocsx=Viet Nam) To叩n t畛 IN, NOT IN Select * from SANPHAM where masp NOT IN (BB01,BB02,BB03)
  • 30. Khoa HTTT - 畉i h畛c CNTT 30 To叩n t畛 LIKE So s叩nh chu畛i t動董ng 畛i C炭 ph叩p: s LIKE p, p c坦 th畛 ch畛a % ho畉c _ % : thay th畉 m畛t chu畛i k箪 t畛 b畉t k畛 _ : thay th畉 m畛t k箪 t畛 b畉t k畛 V鱈 d畛: Select masp,tensp from SANPHAM where masp like 'B%01 5.5 To叩n t畛 so s叩nh(3)
  • 31. Khoa HTTT - 畉i h畛c CNTT 31 5.6 C但u truy v畉n con (1) In ho畉c Exists V鱈 d畛: T狸m c叩c s畛 h坦a 董n mua c湛ng l炭c 2 s畉n ph畉m c坦 m達 s畛 BB01 v BB02. select distinct sohd from CTHD where masp='BB01' and sohd IN (select distinct sohd from CTHD where masp='BB02') select distinct A.sohd from CTHD A where A.masp='BB01' and EXISTS (select * from CTHD B where B.masp='BB02 and A.sohd=B.sohd)
  • 32. Khoa HTTT - 畉i h畛c CNTT 32 5.6 C但u truy v畉n con (2) Not In ho畉c Not Exists V鱈 d畛: T狸m c叩c s畛 h坦a 董n c坦 mua s畉n ph畉m m達 s畛 BB01 nh動ng kh担ng mua s畉n ph畉m m達 s畛 BB02. select distinct sohd from CTHD where masp='BB01' and sohd NOT IN (select distinct sohd from CTHD where masp='BB02') select distinct A.sohd from CTHD A where A.masp='BB01' and NOT EXists (select * from CTHD B where B.masp='BB02 and A.sohd=B.sohd)
  • 33. Khoa HTTT - 畉i h畛c CNTT 33 5.7 Ph辿p chia S畛 d畛ng NOT EXISTS V鱈 d畛: T狸m s畛 h坦a 董n 達 mua t畉t c畉 nh畛ng s畉n ph畉m do Trung Quoc s畉n xu畉t. Select sohd from HOADON where not exists (select * from SANPHAM where nuocsx=Trung Quoc and not exists (select * from CTHD where HOADON.sohd=CTHD.sohd and CTHD.masp=SANPHAM.masp))
  • 34. Khoa HTTT - 畉i h畛c CNTT 34 5.8.1 C叩c hm t鱈nh to叩n c董 b畉n COUNT: 畉m s畛 b畛 d畛 li畛u c畛a thu畛c t鱈nh MIN: T鱈nh gi叩 tr畛 nh畛 nh畉t MAX: T鱈nh gi叩 tr畛 l畛n nh畉t AVG: T鱈nh gi叩 tr畛 trung b狸nh SUM: T鱈nh t畛ng gi叩 tr畛 c叩c b畛 d畛 li畛u 5.8 C叩c hm t鱈nh to叩n v gom nh坦m (1)
  • 35. Khoa HTTT - 畉i h畛c CNTT 35 NHANVIEN MANV HOTEN PHAI MANQL PHONG LUONG NV001 Nguy畛n Ng畛c Linh N畛 Null NC 2.800.000 NV002 inh B叩 Ti畉n Nam NV002 DH 2.000.000 NV003 Nguy畛n Vn M畉nh Nam NV001 NC 2.300.000 NV004 Tr畉n Thanh Long Nam NV002 DH 1.800.000 NV005 Nguy畛n Th畛 H畛ng V但n N畛 NV001 NC 2.500.000 NV006 Nguy畛n Minh Nam NV002 DH 2.000.000 NV007 H Duy L畉p Nam NV003 NC 1.800.000 NV008 Tr畉n Kim Duy棚n N畛 NV003 NC 1.800.000 NV009 Nguy畛n Kim Anh N畛 NV003 NC 2.000.000
  • 36. Khoa HTTT - 畉i h畛c CNTT 36 V鱈 d畛 1. T鱈nh l動董ng th畉p nh畉t, cao nh畉t, trung b狸nh v t畛ng l動董ng c畛a t畉t c畉 c叩c nh但n vi棚n. 2. C坦 t畉t c畉 bao nhi棚u nh但n vi棚n 3. Bao nhi棚u nh但n vi棚n c坦 ng動畛i qu畉n l箪 4. Bao nhi棚u ph嘆ng ban c坦 nh但n vi棚n tr畛c thu畛c 5. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n 6. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n theo t畛ng ph嘆ng ban
  • 37. Khoa HTTT - 畉i h畛c CNTT 37 1. T鱈nh l動董ng th畉p nh畉t, cao nh畉t, trung b狸nh v t畛ng l動董ng c畛a t畉t c畉 c叩c nh但n vi棚n. SELECT min(luong) as thapnhat, max(luong) as caonhat, avg(luong) as trungbinh, sum(luong) as tongluong FROM NhanVien
  • 38. Khoa HTTT - 畉i h畛c CNTT 38 2. C坦 t畉t c畉 bao nhi棚u nh但n vi棚n SELECT count(*) FROM NhanVien 3. Bao nhi棚u nh但n vi棚n c坦 ng動畛i qu畉n l箪 Select count(*) FROM NhanVien WHERE manql is not null SELECT count(Manql) FROM NhanVien 4. Bao nhi棚u ph嘆ng ban c坦 nh但n vi棚n tr畛c thu畛c SELECT count(distinct phong) FROM NhanVien
  • 39. Khoa HTTT - 畉i h畛c CNTT 39 5.8 C叩c hm t鱈nh to叩n v gom nh坦m (2) 5.8.2 Gom nh坦m: m畛nh 畛 GROUP BY S畛 d畛ng hm gom nh坦m tr棚n c叩c b畛 trong quan h畛. M畛i nh坦m b畛 bao g畛m t畉p h畛p c叩c b畛 c坦 c湛ng gi叩 tr畛 tr棚n c叩c thu畛c t鱈nh gom nh坦m Hm gom nh坦m 叩p d畛ng tr棚n m畛i b畛 畛c l畉p nhau. SQL c坦 m畛nh 畛 GROUP BY 畛 ch畛 ra c叩c thu畛c t鱈nh gom nh坦m, c叩c thu畛c t鱈nh ny ph畉i xu畉t hi畛n trong m畛nh 畛 SELECT
  • 40. Khoa HTTT - 畉i h畛c CNTT 40 5. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n SELECT avg(LUONG) as LUONGTB FROM NhanVien 6. T鱈nh l動董ng trung b狸nh c畛a c叩c nh但n vi棚n theo t畛ng ph嘆ng ban. SELECT phong, avg(LUONG) as LUONGTB FROM NhanVien GROUP BY phong
  • 41. Khoa HTTT - 畉i h畛c CNTT 41 5.8 C叩c hm t鱈nh to叩n v gom nh坦m (3) 5.8.3 i畛u ki畛n sau gom nh坦m: m畛nh 畛 HAVING L畛c k畉t qu畉 theo i畛u ki畛n, sau khi 達 gom nh坦m i畛u ki畛n 畛 HAVING 動畛c th畛c hi畛n sau khi gom nh坦m, c叩c i畛u ki畛n c坦 li棚n quan 畉n thu畛c t鱈nh Group By V鱈 d畛: t狸m ph嘆ng c坦 s畛 l動畛ng nh但n vi棚n N畛 tr棚n 5 ng動畛i SELECT phong FROM NhanVien WHERE phai = N畛 GROUP BY phong HAVING count(manv) > 5