Hệ quản trị cơ sở dữ liệu - Lệnh SQL cơ bản

pdf 48 trang vanle 2040
Bạn đang xem 20 trang mẫu của tài liệu "Hệ quản trị cơ sở dữ liệu - Lệnh SQL cơ bản", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfhe_quan_tri_co_so_du_lieu_lenh_sql_co_ban.pdf

Nội dung text: Hệ quản trị cơ sở dữ liệu - Lệnh SQL cơ bản

  1. Khoa Công Nghệ Thông Tin Trường Đại Học Cần Thơ Lệnh SQL cơ bản Đỗ Thanh Nghị dtnghi@cit.ctu.edu.vn Cần Thơ 24-04-2005
  2. Nội dung  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu 2
  3.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu 3
  4.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Tạo bảng  Truy vấn dữ liệu  Cú pháp: CREATE [ TEMPORARY | TEMP ] TABLE table_name ( { column_name type [ column_constraint [ ] ] | table_constraint } [, ] ) [ INHERITS ( inherited_table [, ] ) ] 4
  5.  Tạo bảng  Xen dữ liệu Kiểu dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu  Các kiểu thông dụng  Số: integer, smallint, bigint, real, float8, numeric, serial  Chuỗi ký tự: char, varchar, text  Luận lý: boolean  Ngày giờ: date, time, timestamp  Nhị phân: bytea  Tiền tệ: money  Mãng: array  Chuỗi bit: bit, varbit  Địa chỉ mạng: inet, cidr, macaddr  Hình học: path, point, line, polygon, box, circle, lseg  Object Identifiers: oid, regclass, regtype, regproc, regoper  Composite: dùng create type để tạo dữ liệu cấu trúc  Pseudo: any, opaque, trigger, void, record, language_handler,etc 5
  6.  Tạo bảng  Xen dữ liệu Ràng buộc trên column  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu  column_constraint ::= [ CONSTRAINT column_constraint_name ] { NOT NULL | NULL | UNIQUE | PRIMARY KEY | DEFAULT default_value | CHECK (condition | REFERENCES foreign_table [ ( foreign_column ) ] [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] } action ::= { NO ACTION | RESTRICT | CASCADE | SET NULL | SET DEFAULT } 6
  7.  Tạo bảng  Xen dữ liệu Ràng buộc trên bảng  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu  table constraint ::= [ CONSTRAINT table_constraint_name ] { UNIQUE ( column_name [, ] ) | PRIMARY KEY ( column_name [, ] ) | CHECK ( condition ) | FOREIGN KEY ( column_name [, ] ) REFERENCES foreign_table [ ( foreign_column [, ] ) ] [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] } 7
  8.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 1  Truy vấn dữ liệu  CREATE TABLE s ( sid varchar(3) PRIMARY KEY, sname text NOT NULL, status smallint, city text);  CREATE TABLE p ( pid varchar(3) UNIQUE NOT NULL, pname text NOT NULL, color text, weight smallint, city text); 8
  9.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 2  Truy vấn dữ liệu  CREATE TABLE sp ( sid varchar(3), pid varchar(3), qty integer NOT NULL CHECK (qty > 0), CONSTRAINT sp_pkey PRIMARY KEY (sid, pid));  CREATE TABLE films ( code char(5) PRIMARY KEY, title text NOT NULL, date_prod date DEFAULT current_date); 9
  10.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 3  Truy vấn dữ liệu  CREATE TABLE favorite_books ( id varchar(5), books text[]);  CREATE TABLE sal_emp ( name text, pay_by_quarter integer[], schedule text[][]); 10
  11.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 4  Truy vấn dữ liệu  CREATE TABLE fruit ( name char(30), image OID);  CREATE TABLE motos ( id serial, owner text);  CREATE TABLE authors ( id char(4) PRIMARY KEY, name varchar(30), sex boolean DEFAULT false); 11
  12.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Chỉnh sửa bảng  Truy vấn dữ liệu  Cú pháp: ALTER TABLE table [ * ] ADD [ COLUMN ] column type ALTER TABLE table [ * ] ALTER [ COLUMN ] column { SET DEFAULT defaultvalue | DROP DEFAULT } ALTER TABLE table [ * ] RENAME [ COLUMN ] column TO newcolumn ALTER TABLE table RENAME TO newtable ALTER TABLE table ADD CONSTRAINT newconstraint definition ALTER TABLE table OWNER TO newowner 12
  13.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 5  Truy vấn dữ liệu  ALTER TABLE authors ADD COLUMN address text;  ALTER TABLE motos RENAME COLUMN id TO no;  ALTER TABLE authors DROP COLUMN address;  ALTER TABLE motos ALTER COLUMN owner TYPE varchar(30); 13
  14.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu 14
  15.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Xen dữ liệu vào bảng  Truy vấn dữ liệu  Cú pháp: INSERT INTO table [ ( column [, ] ) ] { VALUES ( expression [, ] ) | SELECT query } 15
  16.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 6  Truy vấn dữ liệu  Xen dữ liệu vào bảng s: INSERT INTO s VALUES (‘S01’, ‘Smith’, 20, ‘London’); INSERT INTO s VALUES (‘S02’, ‘Jones’, 10, ‘Paris’); INSERT INTO s VALUES (‘S03’, ‘Blacke’, 30, ‘Paris’);  Xen dữ liệu vào bảng p: INSERT INTO p VALUES (‘P01’, ‘Nut’, ‘red’, 12, ‘London’); INSERT INTO p VALUES (‘P02’, ‘Bolt’, ‘green’, 17, ‘Paris’); INSERT INTO p VALUES (‘P03’, ‘Screw’, ‘blue’, 17, ‘Roma’); INSERT INTO p VALUES (‘P04’, ‘Screw’, ‘red’, 14, ‘London’); 16
  17.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 7  Truy vấn dữ liệu  Xen dữ liệu vào bảng sp: INSERT INTO sp VALUES (‘S01’, ‘P01’, 300); INSERT INTO sp VALUES (‘S01’, ‘P02’, 200); INSERT INTO sp VALUES (‘S01’, ‘P03’, 400); INSERT INTO sp VALUES (‘S02’, ‘P01’, 300); INSERT INTO sp VALUES (‘S02’, ‘P02’, 400); INSERT INTO sp VALUES (‘S03’, ‘P02’, 200); 17
  18.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu 18
  19.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Xoá dữ liệu từ bảng  Truy vấn dữ liệu  Cú pháp: DELETE FROM table [ WHERE condition ]  Ví dụ: DELETE FROM films; DELETE FROM films WHERE code = ‘00013’; 19
  20.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu 20
  21.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Cập nhật dữ liệu của bảng  Truy vấn dữ liệu  Cú pháp: UPDATE table SET col = expression [, ] [ FROM fromlist ] [ WHERE condition ]  Ví dụ: UPDATE favorite_books SET books = ‘{“databases”, “data mining”, “machine learning”}’ WHERE id = ‘00013’; 21
  22.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu 22
  23.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Truy vấn dữ liệu  Truy vấn dữ liệu  Cú pháp: SELECT [ ALL | DISTINCT [ ON ( distinct_expression [, ] ) ] ] target_expression [ AS output_name ] [, ] [ FROM from_item [ { , | CROSS JOIN } ] ] [ WHERE condition ] [ GROUP BY aggregate_expression [, ] ] [ HAVING aggregate_condition [, ] ] [ { UNION | INTERSECT | EXCEPT [ALL] } select ] [ ORDER BY order_expression [ ASC | DESC | USING operator ] [, ] ] [ FOR UPDATE [ OF update_table [, ] ] ] [ LIMIT { ALL | count } [ { OFFSET | , } start ] ] 23
  24.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Mệnh đề FROM  Truy vấn dữ liệu  from_item ::= { [ ONLY ] table_name [ * ] [ [ AS ] from_alias [ ( column_alias_list ) ] ] | ( select ) [ [ AS ] alias [ ( column_alias_list ) ] ] | from_item [ NATURAL ] join_type from_item [ ON ( join_condition ) | USING ( join_column_list ) ] } 24
  25.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Kiểu kết nối (JOIN)  Truy vấn dữ liệu  join_type ::= [ INNER | LEFT [ OUTER ] | RIGHT [ OUTER ] | FULL [ OUTER ] ] JOIN 25
  26.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 8  Truy vấn dữ liệu 26
  27.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 9  Truy vấn dữ liệu 27
  28.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 10  Truy vấn dữ liệu 28
  29.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 11  Truy vấn dữ liệu 29
  30.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 12  Truy vấn dữ liệu 30
  31.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 13  Truy vấn dữ liệu 31
  32.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 14  Truy vấn dữ liệu 32
  33.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 15  Truy vấn dữ liệu 33
  34.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 16  Truy vấn dữ liệu 34
  35.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 17  Truy vấn dữ liệu 35
  36.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 18  Truy vấn dữ liệu 36
  37.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 19  Truy vấn dữ liệu 37
  38.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 20  Truy vấn dữ liệu 38
  39.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 21  Truy vấn dữ liệu 39
  40.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Tạo chỉ mục  Truy vấn dữ liệu  Cú pháp: CREATE [ UNIQUE ] INDEX index_name ON table [ USING method ] ( column [ op_class ] [, ] ) CREATE [ UNIQUE ] INDEX index_name ON table [ USING method ] ( func_name ( column [, ] ) [ op_class ])  B-Tree, Hash, R-Tree, GiST  Tăng tốc cho phép truy vấn 40
  41.  Tạo bảng Ví dụ 22  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu  Truy vấn dữ liệu 41
  42.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 23  Truy vấn dữ liệu  Dữ liệu binary: CREATE TABLE fruit ( name char(30), image OID); INSERT INTO fruit VALUES ('peach', lo_import('/usr/images/peach.jpg')); SELECT lo_export(fruit.image, '/tmp/outimage.jpg') FROM fruit WHERE name = 'peach'; SELECT lo_unlink(fruit.image) FROM fruit; 42
  43.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 24  Truy vấn dữ liệu  Dữ liệu mảng: CREATE TABLE array_test ( col1 INTEGER[5], col2 INTEGER[][], col3 INTEGER[2][2][]); INSERT INTO array_test VALUES ( '{1,2,3,4,5}', '{{1,2},{3,4}}', '{{{1,2},{3,4}},{{5,6}, {7,8}}}' ); SELECT * FROM array_test; SELECT col1[4] FROM array_test; SELECT col2[2][1] FROM array_test; SELECT col3[1][2][2] FROM array_test; 43
  44.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 25  Truy vấn dữ liệu  Câu truy vấn đệ quy: (suy diễn) CREATE TABLE parent ( f varchar(10), c varchar(10)); f là cha của c hay c là con của f INSERT INTO parent VALUES('Paul', 'Henri'); INSERT INTO parent VALUES('Jean', 'Marie'); INSERT INTO parent VALUES('Louis', 'Paul'); INSERT INTO parent VALUES('Katie', 'Louis'); INSERT INTO parent VALUES('Nicole', 'Jean'); 44
  45.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 25  Truy vấn dữ liệu  Câu truy vấn đệ quy: (suy diễn) tìm tổ tiên của Paul WITH RECURSIVE anc(An, De) AS ( SELECT f, c FROM parent UNION ALL SELECT a.An, p.c FROM anc a, parent p WHERE a.De = p.f ) SELECT * FROM anc WHERE De = ‘Paul'; 45
  46.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 26  Truy vấn dữ liệu  Câu truy vấn đệ quy: tính tổng dãy số nguyên 1, , 100 WITH RECURSIVE t(n) AS ( SELECT 1 UNION ALL SELECT n+1 FROM t WHERE n < 100 ) SELECT sum(n) FROM t; 46
  47.  Tạo bảng  Xen dữ liệu  Xóa dữ liệu  Cập nhật dữ liệu Ví dụ 27  Truy vấn dữ liệu  Câu truy vấn đệ quy: tính giai thừa của 5 WITH RECURSIVE gt(n, r) AS ( SELECT 1, 1 UNION ALL SELECT n+1, (n+1)*r FROM gt ) SELECT r FROM gt OFFSET 4 LIMIT 1; 47