20. L?nh c?p nh?t m?u tin (1/2) L?nh c?p nh?t m?u tin: C¨² ph¨¢p: UPDATE <t¨ºn b?ng> SET <t¨ºn c?t 1> = <bi?u th?c 1>, ¡ WHERE <?i?u ki?n>; Gi¨¢ tr? c¨¢c c?t c?a c¨¢c m?u tin th?a ?i?u ki?n WHERE s? ???c thay th? b?ng c¨¢c gi¨¢ tr? <bi?u th?c 1>,¡ t??ng ?ng. N?u kh?ng c¨® WHERE th¨¬ t?t c? c¨¢c m?u tin c?a b?ng s? ???c s?a ??i. VD: <Xem trang sau>
21. L?nh c?p nh?t m?u tin (2/2) VD: cho b?ng BANGDIEM m?n Automat nh? sau: Y¨ºu c?u : t?ng cho sinh vi¨ºn m?i ng??i 1 ?i?m C?u l?nh: UPDATE BANGDIEM SET DIEM=DIEM+1; K?t qu?: BANGDIEM MSSV DIEM 070011T 5 080130T 6 081307T 6 BANGDIEM MSSV DIEM 070111T 6 080130T 7 081307T 7
22. Truy v?n d? li?u Truy v?n d? li?u: C¨² ph¨¢p t?ng qu¨¢t Ph¨¢t bi?u SELECT v?i * Ph¨¢t bi?u SELECT v?i AS Ph¨¢t bi?u SELECT v?i TOP N Ph¨¢t bi?u SELECT v?i DISTINCT M?nh ?? WHERE M?nh ?? ORDER BY M?nh ?? GROUP BY M?nh ?? HAVING Truy v?n t? nhi?u b?ng Truy v?n con
23. C¨² ph¨¢p t?ng qu¨¢t C¨² ph¨¢p t?ng qu¨¢t: SELECT <danh s¨¢ch c?t> FROM <danh s¨¢ch b?ng> WHERE <?i?u ki?n> GROUP BY <t¨ºn c?t> HAVING <?i?u ki?n cho GROUP BY> ORDER BY <danh s¨¢ch c?t>
24. Ph¨¢t bi?u SELECT * Ph¨¢t bi?u SELECT v?i * : D?u * d¨´ng ?? ??i di?n cho t?t c? c¨¢c c?t C¨² ph¨¢p: SELECT * FROM <t¨ºn b?ng>; VD: 2 c?u truy v?n sau cho c¨´ng k?t qu?: C?u t?ng qu¨¢t: SELECT MSSV,DIEM FROM BANGDIEM; C?u c¨® d?u * : SELECT * FROM BANGDIEM; BANGDIEM MSSV DIEM 070011T 5 080130T 6
25. Ph¨¢t bi?u SELECT v?i AS Ph¨¢t bi?u SELECT v?i AS: D¨´ng ??t l?i t¨ºn c?t khi hi?n th? k?t qu? C¨² ph¨¢p: SELECT <t¨ºn c?t> AS <t¨ºn mu?n hi?n th?> VD: Y¨ºu c?u ??i t¨ºn hi?n th? c?a c?t DIEM th¨¤nh DIEMTHI C?u l?nh: SELECT DIEM AS DIEMTHI FROM BANGDIEM BANGDIEM MSSV DIEM 070011T 5 080130T 6 MSSV DIEMTHI 070011T 5 080130T 6
26. Ph¨¢t bi?u SELECT v?i TOP N Ph¨¢t bi?u SELECT v?i TOP N: Cho ph¨¦p l?y ra N m?u tin t? m?t b?ng. C¨² ph¨¢p: SELECT TOP <N> * FROM <t¨ºn b?ng> VD: Cho b?ng sau, l?y ra 2 m?u tin ??u ti¨ºn. C?u l?nh: SELECT TOP 2 * FROM BANGDIEM BANGDIEM MSSV DIEM 070111T 6 080130T 7 081307T 7 MSSV DIEM 070111T 6 080130T 7
27. Ph¨¢t bi?u SELECT v?i DISTINCT Ph¨¢t bi?u SELECT v?i DISTINCT: Khi k?t qu? tr? v? c¨® nhi?u m?u tin tr¨´ng nhay, ?? ch? l?y 1 m?u tin ta d¨´ng DISTINCT. C¨² ph¨¢p: SELECT DISTINCT <t¨ºn c?t> FROM <t¨ºn b?ng> VD: Cho b?ng sau cho bi?t c¨® nh?ng l?p n¨¤o C?u l?nh: SELECT DISTINCT MALOP FROM DANHSACHSV K?t qu?: DANHSACHSV MSSV MALOP TENSV 080206K 08KK1D H? Ho¨¤i Anh 080016Q 08QT1D L?u H??ng Giang 080123Q 08QT1D Ho¨¤ng Th¨´y Linh MALOP 08KK1D 08QT1D
28. M?nh ?? WHERE (1/2) M?nh ?? WHERE: D¨´ng ?? ??t ?i?u ki?n tr¨ªch d? li?u. C¨¢c to¨¢n t? c?a m?nh ?? WHERE: So s¨¢nh: >, <, >=, <=, =, <> Logic: And, Or, Not BETWEEN ¡ AND ¡: l?y gi¨¢ trong 1 v¨´ng. LIKE: so s¨¢nh g?n gi?ng IN: ph¨¦p so s¨¢nh trong 1 t?p h?p, danh s¨¢ch. IS NULL (IS NOT NULL) : ki?m tra gi¨¢ tr? c¨® r?ng hay kh?ng EXISTS: tr? v? TRUE n?u c¨® ¨ªt nh?t 1 m?u tin t?n t?i VD: <Xem trang sau>
29. M?nh ?? WHERE (2/2) VD: Cho b?ng sau, T¨¬m c¨¢c sinh vi¨ºn c¨® ng¨¤y sinh t? 01/06/1990 ??n 31/12/1990 C?u l?nh: SELECT * FROM SINHVIEN WHERE NGAYSINH BETWEEN ¡®01/06/1990 ¡¯ AND ¡¯31/12/1990¡¯; SINHVIEN MSSV HOTEN NGAYSINH QUEQUAN 080777K Nguy?n Qu?c ??t 20/10/1990 ??ng Th¨¢p 080302A Nguy?n Lan Anh 01/01/1990 V?ng T¨¤u 080096T Cao Minh Ti?n 03/10/1990 H? Ch¨ª Minh MSSV HOTEN NGAYSINH QUEQUAN 080777K Nguy?n Qu?c ??t 20/10/1990 ??ng Th¨¢p
30. M?nh ?? ORDER BY M?nh ?? ORDER BY: S?p x?p k?t qu? theo th? t? mong mu?n C¨² ph¨¢p: ORDER BY <danh s¨¢ch c?t> [ASC|DESC] V?i ASC s?p t?ng d?n ho?c DESC s?p x?p gi?m d?n. VD: Cho b?ng sau: Y¨ºu c?u: Tr¨ªch ra danh s¨¢ch sinh vi¨ºn s?p x?p t?ng d?n C?u l?nh: SELECT * FROM SINHVIEN ORDER BY MSSV ASC MSSV HOTEN NGAYSINH QUEQUAN 080777K Nguy?n Qu?c ??t 20/10/1990 ??ng Th¨¢p 080096T Cao Minh Ti?n 03/10/1990 H? Ch¨ª Minh MSSV HOTEN NGAYSINH QUEQUAN 080096T Cao Minh Ti?n 03/10/1990 H? Ch¨ª Minh 080777K Nguy?n Qu?c ??t 20/10/1990 ??ng Th¨¢p
31. M?nh ?? GROUP BY (1/2) M?nh ?? GROUP BY: Nh¨®m d? li?u theo t?ng nh¨®m ?? th?c hi?n c¨¢c ph¨¦p to¨¢n th?ng k¨º C¨² ph¨¢p: GROUP BY <danh s¨¢ch t¨ºn c?t> M?t s? h¨¤m th?ng d?ng d¨´ng v?i GROUP BY: AVG: t¨ªnh gi¨¢ tr? trung b¨¬nh. MIN: t¨ªnh gi¨¢ tr? nh? nh?t. MAX: t¨ªnh gi¨¢ tr? l?n nh?t. COUNT: ??m s? ph?n t?. SUM: t¨ªnh t?ng c¨¢c ph?n t? VD: <Xem trang sau>
32. M?nh ?? GROUP BY (2/2) VD: Cho b?ng sau, cho bi?t s? l??ng sinh vi¨ºn t?ng l?p C?u l?nh: SELECT MALOP, COUNT(*) AS SOLUONG FROM SINHVIEN GROUP BY MALOP K?t qu?: DANHSACHKHENTHUONG MSSV MALOP TENSV PHAI XEPLOAI 080010A 08AV1D B¨´i Long H?i Nam Gi?i 080120Q 08QT2D H? Minh T?m N? Gi?i 080133Q 08QT2D ?o¨¤n Th? Vinh Nam Kh¨¢ 080361T 08TN1D ??ng Long ?? Nam Kh¨¢ MALOP SOLUONG 08AV1D 1 08QT2D 2 08TN1D 1
33. M?nh ?? HAVING M?nh ?? HAVING: ??t ?i?u ki?n sau khi ?? nh¨®m d? li?u b?ng m?nh ?? GROUP BY VD: tr¨ªch danh s¨¢ch c¨¢c l?p c¨® t? 2 sinh vi¨ºn ???c th??ng C?u l?nh: SELECT MALOP, COUNT(*) AS SOSV FROM SINHVIEN GROUP BY MALOP HAVING COUNT (*)>=2; DANHSACHKHENTHUONG MALOP MSSV TENSV PHAI XEPLOAI 08AV1D 080010A B¨´i Long H?i Nam Gi?i 08QT2D 080120Q H? Minh T?m N? Gi?i 08QT2D 080133Q ?o¨¤n Th? Vinh Nam Kh¨¢ MALOP SOSV 08QT2D 2