You are on page 1of 41

Bi 5: Ngn ng SQL

Khoa HTTT - i hc CNTT

Ni dung
1. 2. 3. 4. 5. 6. Gii thiu Cc ngn ng giao tip Ngn ng nh ngha d liu Ngn ng thao tc d liu Ngn ng truy vn d liu c cu trc Ngn ng iu khin d liu
Khoa HTTT - i hc CNTT 2

1. Gii thiu

L ngn ng chun truy vn v thao tc trn CSDL quan h L ngn ng phi th tc Khi ngun ca SQL l SEQUEL - Structured English Query Language, nm 1974) Cc chun SQL SQL89 SQL92 (SQL2) SQL99 (SQL3)
Khoa HTTT - i hc CNTT 3

2. Cc ngn ng giao tip


Ngn ng nh ngha d liu (Data Definition Language - DDL): cho php khai bo cu trc bng, cc mi quan h v cc rng buc. Ngn ng thao tc d liu (Data Manipulation Language - DML): cho php thm, xa, sa d liu. Ngn ng truy vn d liu (Structured Query Language SQL): cho php truy vn d liu. Ngn ng iu khin d liu (Data Control Language DCL): khai bo bo mt thng tin, cp quyn v thu hi quyn khai thc trn c s d liu.
Khoa HTTT - i hc CNTT 4

3. Ngn ng nh ngha d liu


3.1 Lnh to bng (CREATE) 3.1.1 C php 3.1.2 Mt s kiu d liu 3.2 Lnh sa cu trc bng (ALTER) 3.2.1 Thm thuc tnh 3.2.2 Sa kiu d liu ca thuc tnh 3.2.3 Xo thuc tnh 3.2.4 Thm rng buc ton vn 3.2.5 Xo rng buc ton vn 3.3 Lnh xa bng (DROP)
Khoa HTTT - i hc CNTT 5

3.1 Lnh to bng


3.1.1 C php CREATE TABLE <tn_bng> ( <tn_ct1> <kiu_d_liu> [not null], <tn_ct2> <kiu_d_liu> [not null], <tn_ct1> <kiu_d_liu> [not null], khai bo kha chnh, kha ngoi, rng buc )
Khoa HTTT - i hc CNTT 6

3.1 Lnh to bng (2)


3.1.2 Mt s kiu d liu
Kiu d liu Chui k t S SQL Server varchar(n), char(n),nvarchar(n), nchar(n) tinyint,smallint, int, numeric(m,n), decimal(m,n),float, real, smallmoney, money smalldatetime, datetime bit
Khoa HTTT - i hc CNTT 7

Ngy thng Lun l

3.1 Lnh to bng (3)


Lc CSDL qun l bn hng gm c cc 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 hc CNTT 8

3.1 Lnh to bng (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 hc CNTT 9

3.1 Lnh to bng (5)


Create table CTHD ( SOHD int foreign key references HOADON(SOHD), MASP char(4) foreign key references SANPHAM(MASP), SL int, constraint PK_CTHD primary key (SOHD,MASP) )
Khoa HTTT - i hc CNTT 10

3.2 Sa cu trc bng(1)


3.2.1 Thm thuc tnh
ALTER TABLE tnbng ADD tnct kiudliu

V d: thm ct Ghi_chu vo bng khch hng


ALTER TABLE KHACHHANG ADD GHI_CHU varchar(20)

3.2.2 Sa kiu d liu thuc tnh


ALTER TABLE tnbng ALTER COLUMN tnct kiudliu_mi

Lu : Khng phi sa bt k kiu d liu no cng c


Khoa HTTT - i hc CNTT 11

3.2 Sa cu trc bng(2)

V d: Sa Ct Ghi_chu thnh kiu d liu varchar(50) Nu sa kiu d liu ca ct Ghi_chu thnh varchar(5), m trc nhp gi tr cho ct Ghi_chu c di hn 5 k t th khng c php. Hoc sa t kiu chui k t sang kiu s,

ALTER TABLE KHACHHANG ALTER COLUMN GHI_CHU varchar(50)

3.2.3 Xa thuc tnh


ALTER TABLE tn_bng DROP COLUMN tn_ct V d: xa ct Ghi_chu trong bng KHACHHANG ALTER TABLE NHANVIEN DROP COLUMN Ghi_chu
Khoa HTTT - i hc CNTT 12

3.2 Sa cu trc bng(3)


3.2.4 Thm rng buc ton vn
UNIQUE tn_ct
ALTER TABLE <tn_bng> ADD CONSTRAINT <tn_rng_buc>

PRIMARY KEY (tn_ct) FOREIGN KEY (tn_ct) REFERENCES tn_bng (ct_l_kha_chnh) [ON DELETE CASCADE] [ON UPDATE CASCADE] CHECK (tn_ct iu_kin)

Khoa HTTT - i hc CNTT

13

3.2 Sa cu trc bng(4)

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)
Khoa HTTT - i hc CNTT 14

3.2 Sa cu trc bng(5)


3.2.5 Xa rng buc ton vn
ALTER TABLE tn_bng DROP CONSTRAINT tn_rng_buc

V d:
Alter table CTHD drop constraint FK_CT_SP Alter table SANPHAM drop constraint ck_gia

Lu : i vi rng buc kha chnh, mun xa rng buc ny phi xa ht cc rng buc kha ngoi tham chiu ti n
Khoa HTTT - i hc CNTT 15

3.3 Lnh xa bng

C php
DROP TABLE tn_bng

V d: xa bng KHACHHANG.
DROP TABLE KHACHHANG

Lu : khi mun xa mt bng phi xa tt c nhng kha ngoi tham chiu ti bng trc.
Khoa HTTT - i hc CNTT 16

4. Ngn ng thao tc d liu


Gm cc lnh: 4.1 Lnh thm d liu (INSERT) 4.2 Lnh sa d liu (UPDATE) 4.3 Lnh xa d liu (DELETE)

Khoa HTTT - i hc CNTT

17

4.1 Thm d liu

C php
INSERT INTO tn_bng (ct1,,ctn) VALUES (gi_tr_1,., gi_tr_n) INSERT INTO tn_bng 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)
Khoa HTTT - i hc CNTT 18

4.2 Sa d liu
C php UPDATE tn_bng SET ct_1 = gi_tr_1, ct_2 = gi_tr_2 . [WHERE iu_kin] Lu : cn thn vi cc lnh xa v sa, nu khng c iu kin WHERE ngha l xa hoc sa tt c. V d: Tng gi 10% i vi nhng sn phm do Trung Quoc sn xut

UPDATE SANPHAM SET Gia = Gia*1.1 WHERE Nuocsx=Trung Quoc


Khoa HTTT - i hc CNTT 19

4.3 Xa d liu

C php
DELETE FROM tn_bng [WHERE iu_kin]

V d:

Xa ton b nhn vin DELETE FROM NHANVIEN Xa nhng sn phm do Trung Quc sn xut c gi thp hn 10000 DELETE FROM SANPHAM WHERE (Gia <10000) and (Nuocsx=Trung Quoc)
Khoa HTTT - i hc CNTT

20

5. Ngn ng truy vn d liu c cu trc


5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 Cu truy vn tng qut Truy vn n gin Php kt t b danh, s dng *, distinct Cc ton t Cu truy vn con (subquery) Php chia Hm tnh ton, gom nhm
Khoa HTTT - i hc CNTT 21

5.1Cu truy vn tng qut


SELECT [DISTINCT] *|tn_ct | hm FROM bng [WHERE iu_kin] [GROUP BY tn_ct] [HAVING iu_kin] [ORDER BY tn_ct ASC | DESC]

Khoa HTTT - i hc CNTT

22

5.2 Truy vn n gin(1)


SELECT

Tng ng php chiu ca SQH Lit k cc thuc tnh cn hin th trong kt qu Tng ng vi iu kin chn trong SQH iu kin lin quan ti thuc tnh, s dng cc php ni lun l AND, OR, NOT, cc php ton so snh, BETWEEN Lit k cc quan h cn thit, cc php kt
Khoa HTTT - i hc CNTT 23

WHERE

FROM

5.2 Truy vn n gin(2)


Tm masp, tensp do Trung Quoc sn xut c gi t 20000 n 30000 Select masp,tensp From SANPHAM Where nuocsx=Trung Quoc and gia between 20000 and 30000

Khoa HTTT - i hc CNTT

24

5.3 Php kt(1)


Inner Join, Left Join, Right Join, Full Join V d:

In ra danh sch cc khch 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 hc CNTT

25

5.3 Php kt (2)


V d: In ra danh sch tt c cc ha n v h tn ca khch hng mua ha n (nu c)

Select From Select From where

sohd, hoten HOADON left join KHACHHANG on HOADON.makh=KHACHHANG.makh sohd, hoten HOADON ,KHACHHANG HOADON.makh*=KHACHHANG.makh

Khoa HTTT - i hc CNTT

26

5.4 t b danh, s dng *, distinct


t b danh Alias: cho thuc tnh v quan h: tn_c AS tn_mi

Select manv,hoten as [ho va ten] From NHANVIEN Select * from Nhanvien Select NHANVIEN.* from NHANVIEN Select distinct nuocsx from SANPHAM Select * from SANPHAM order by nuocsx, gia DESC
Khoa HTTT - i hc CNTT 27

Lit k tt c cc thuc tnh ca quan h:


Distinct: trng ch ly mt ln

Sp xp kt qu hin th: Order by

5.5 Ton t truy vn(1)

Ton t so snh: =, >,<,>=,<=,<> Ton t logic: AND, OR, NOT Php ton: +, - ,* , / BETWEEN . AND IS NULL, IS NOT NULL LIKE (_ %) IN, NOT IN EXISTS , NOT EXISTS SOME, ALL
Khoa HTTT - i hc CNTT 28

5.5 Ton t truy vn(2)


IS NULL, IS NOT NULL

Select sohd from HOADON where makh is Null Select * from HOADON where makh is Not Null 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) Select * from SANPHAM where masp NOT IN (BB01,BB02,BB03)
Khoa HTTT - i hc CNTT 29

Ton t so snh, php ton


Ton t IN, NOT IN

5.5 Ton t so snh(3)


Ton t LIKE

So snh chui tng i C php: s LIKE p, p c th cha % hoc _ % : thay th mt chui k t bt k _ : thay th mt k t bt k V d: Select masp,tensp from SANPHAM where masp like 'B%01
Khoa HTTT - i hc CNTT 30

5.6 Cu truy vn con (1)


In hoc Exists V d: Tm cc s ha n mua cng lc 2 sn phm 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 hc CNTT 31

5.6 Cu truy vn con (2)


Not In hoc Not Exists V d: Tm cc s ha n c mua sn phm m s BB01 nhng khng mua sn phm 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 EXITST (select * from CTHD B where B.masp='BB02 and A.sohd=B.sohd)
Khoa HTTT - i hc CNTT 32

5.7 Php chia


S dng NOT EXISTS V d: Tm s ha n mua tt c nhng sn phm do Trung Quoc sn xut. 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 hc CNTT 33

5.8 Cc hm tnh ton v gom nhm (1)


5.8.1 Cc hm tnh ton c bn

COUNT: m s b d liu ca thuc tnh MIN: Tnh gi tr nh nht MAX: Tnh gi tr ln nht AVG: Tnh gi tr trung bnh SUM: Tnh tng gi tr cc b d liu
Khoa HTTT - i hc CNTT 34

NHANVIEN MANV HOTEN PHAI MANQL PHONG N Nam Nam Nam N Nam Nam N N Null NV002 NV001 NV002 NV001 NV002 NV003 NV003 NV003 NC DH NC DH NC DH NC NC NC LUONG 2.800.000 2.000.000 2.300.000 1.800.000 2.500.000 2.000.000 1.800.000 1.800.000 2.000.000
35

NV001 Nguyn Ngc Linh NV002 inh B Tin NV003 Nguyn Vn Mnh NV004 Trn Thanh Long NV005 Nguyn Th Hng Vn NV006 Nguyn Minh NV007 H Duy Lp NV008 Trn Kim Duyn NV009 Nguyn Kim Anh

Khoa HTTT - i hc CNTT

V d
1. Tnh lng thp nht, cao nht, trung bnh v tng lng ca tt c cc nhn vin. 2. C tt c bao nhiu nhn vin 3. Bao nhiu nhn vin c ngi qun l 4. Bao nhiu phng ban c nhn vin trc thuc 5. Tnh lng trung bnh ca cc nhn vin 6. Tnh lng trung bnh ca cc nhn vin theo tng phng ban
Khoa HTTT - i hc CNTT 36

1. Tnh lng thp nht, cao nht, trung bnh v tng lng ca tt c cc nhn vin. SELECT min(luong) as thapnhat, max(luong) as caonhat, avg(luong) as trungbinh, sum(luong) as tongluong FROM NhanVien
Khoa HTTT - i hc CNTT 37

2. C tt c bao nhiu nhn vin SELECT count(*) FROM NhanVien 3. Bao nhiu nhn vin c ngi qun l

Select count(*) FROM NhanVien WHERE manql is not null SELECT count(Manql) FROM NhanVien

4. Bao nhiu phng ban c nhn vin trc thuc SELECT count(distinct phong) FROM NhanVien
Khoa HTTT - i hc CNTT 38

5.8 Cc hm tnh ton v gom nhm (2)


5.8.2 Gom nhm: mnh GROUP BY
S dng hm gom nhm trn cc b trong quan h. Mi nhm b bao gm tp hp cc b c cng gi tr trn cc thuc tnh gom nhm Hm gom nhm p dng trn mi b c lp nhau. SQL c mnh GROUP BY ch ra cc thuc tnh gom nhm, cc thuc tnh ny phi xut hin trong mnh SELECT
Khoa HTTT - i hc CNTT 39

5. Tnh lng trung bnh ca cc nhn vin


SELECT FROM avg(LUONG) as LUONGTB NhanVien

6. Tnh lng trung bnh ca cc nhn vin theo tng phng ban.
SELECT FROM GROUP BY phong, avg(LUONG) as LUONGTB NhanVien phong
Khoa HTTT - i hc CNTT 40

5.8 Cc hm tnh ton v gom nhm (3)


5.8.3 iu kin sau gom nhm: mnh HAVING

Lc kt qu theo iu kin, sau khi gom nhm iu kin HAVING c thc hin sau khi gom nhm, cc iu kin c lin quan n thuc tnh Group By V d: tm phng c s lng nhn vin N trn 5 ngi
SELECT FROM WHERE GROUP BY HAVING phong NhanVien phai = N phong count(manv) > 5

Khoa HTTT - i hc CNTT

41