
SQL
(Structured Query Language)
(tiếp theo)
1 EE4509, EE6133 – HK2 2011/2012
TS. Đào Trung Kiên – ĐH Bách khoa Hà Nội

Xoá dữ liệu
Dùng câu lệnh “delete from…” để xoá các dữ liệu thoả
mãn một điều kiện mong muốn
Cú pháp:
delete from tên-quan-hệ
[where điều-kiện];
Mệnh đề where tương tự trong câu lệnh select
VD:
delete from Student where regdate > '2000-01-01';
delete from Book where
Xoá toàn bộ dữ liệu của quan hệ:
delete from quan-hệ;
truncate quan-hệ;
2 EE4509, EE6133 – HK2 2011/2012
TS. Đào Trung Kiên – ĐH Bách khoa Hà Nội

Sửa đổi dữ liệu
Dùng câu lệnh “update …” để cập nhật giá trị của các
thuộc tính trong một quan hệ bằng giá trị mới thoả mãn
một điều kiện mong muốn
Cú pháp:
update tên-quan-hệ
set thuộc-tính = giá-trị,...
where điều-kiện;
VD:
update Student set class = 'C‘
where name = 'Bill Gates';
update Book set borrowed = 1, date = now()
where id = 1234;
3 EE4509, EE6133 – HK2 2011/2012
TS. Đào Trung Kiên – ĐH Bách khoa Hà Nội

Số học
Các phép toán: +, -, *, /, %
Hàm: abs(), sqrt(), exp(), ln(), power(),
rand(),…
Ví dụ:
select sqrt(5) + power(40, 5);
update Product set price = price * (1 + ln(2))
where category = 'laptop';
4 EE4509, EE6133 – HK2 2011/2012
TS. Đào Trung Kiên – ĐH Bách khoa Hà Nội

Logic
Các phép toán: and, or, not
So sánh: >, <, >=, <=, =, <>, !=, between … and
…
Ví dụ:
select * from Product
where price >= 50 and count < 10;
So sánh với null: dùng “is null” và “is not null”
select id from Student
where phone is not null;
Tập hợp: in(…)
select id from Student
where class in ('A', 'B');
5 EE4509, EE6133 – HK2 2011/2012
TS. Đào Trung Kiên – ĐH Bách khoa Hà Nội