intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Hệ quản trị cơ sở dữ liệu - Chương 7: Ngôn ngữ SQL

Chia sẻ: Kiếp Này Bình Yên | Ngày: | Loại File: PPTX | Số trang:64

52
lượt xem
2
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bài giảng Hệ quản trị cơ sở dữ liệu - Chương 7 giới thiệu về ngôn ngữ SQL. Chương này trình bày một số nội dung cơ bản như: Câu truy vấn cơ bản; union, intersect, and except; nested queries;...và một số nội dung khác, mời các bạn cùng tham khảo.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Hệ quản trị cơ sở dữ liệu - Chương 7: Ngôn ngữ SQL

  1. Chapter 7 Ngôn ngữ SQL (Structured Query Language) 1
  2. 2 Ví dụ • Cho một cơ sở dữ liệu gồm các quan hệ Sailors, Reserves và Boats. – Sailors(sid: integer, sname: string, rating: integer , age: real) – Boats(bid: integer, bname: string, color: string) – Reserves(sid: integer , bid: integer , day: date)
  3. 3 Ví dụ
  4. 4 Câu truy vấn cơ bản • Cú pháp – SELECT: chỉ định các cột xuất hiện trong kết quả. – FROM: xác định các bảng trong câu truy vấn – WHERE: tùy chọn, chỉ định điều kiện chọn ra các dòng trên các bảng trong mệnh đề
  5. 5 Câu truy vấn cơ bản – From-list: tên của các quan hệ. – Select-list: danh sách các thuộc tính của các quan hệ trong from-list – Qualification: Các phép so sánh • , =, ≤, ≥, , AND, OR, NOT. – DISTINCT: loại bỏ các giá trị trùng trong kết quả.
  6. 6 Câu truy vấn cơ bản • Conceptual Evaluation Strategy: – Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: • Compute the cross-product of relation-list. • Discard resulting tuples if they fail qualifications. • Delete attributes that are not in target-list. • If DISTINCT is specified, eliminate duplicate rows.
  7. 7 Câu truy vấn cơ bản • A Note on Range Variables – Really needed only if the same relation appears twice in the FROM clause. SELECT  S.sname FROM     Sailors S, Reserves R WHERE  S.sid=R.sid AND bid=103 SELECT  sname FROM     Sailors, Reserves  WHERE  Sailors.sid=Reserves.sid  AND bid=103
  8. 8 Câu truy vấn cơ bản • Examples of Basic SQL Queries – Find the names and ages of all sailors. – Find the names and ages of all sailors
  9. 9 Câu truy vấn cơ bản – Find all sailors with a rating above 7. – When you want to retrieve all columns:
  10. 10 Câu truy vấn cơ bản – Find the sids of sailors who have reserved a red boat. – Find the names of sailors who have reserved a red boat.
  11. 11 Câu truy vấn cơ bản – Find the colors of boats reserved by Lubber – Find the names of sailors who have reserved at least one boat.
  12. 12 5.2 The Form of A Basic SQL Query • Expressions and Strings in the SELECT Command – Select-list: • Can be of the form expression AS column name, where expression is any arithmetic or string expression over column names and constants. • It can also contain aggregates such as sum and count.
  13. 13 Câu truy vấn cơ bản – Ex: Compute increments for the ratings of persons who have sailed two different boats on the same day
  14. 14 5.2 The Form of A Basic SQL Query – Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. SELECT  S.age, age1=S.age­5, 2*S.age AS age2 FROM  Sailors S WHERE  S.sname LIKE ‘B_%B’
  15. 15 Câu truy vấn cơ bản • String Comparisons – LIKE is used for string matching. – “_” stands for any one character and – “%”stands for 0 or more arbitrary characters Ex: Find the ages of sailors whose name begins and ends with B and has at least three characters.
  16. 16 Union, Intersect, And Except • UNION: – s1 UNION s2, result rows either in s1 or s2. • INTERSECT: – s1 INTERSECT s2, result rows in s1 and s2. • EXCEPT: – s1 EXCEPT s2, result rows in s1 but not in s2. (Some system recognize ‘MINUS’ for EXECPT) • IN, ANY, ALL, EXISTS to be covered in ‘Nested Queries’.
  17. 17 Union, Intersect, And Except – Find the names of sailors who have reserved a red or a green boat
  18. 18 Union, Intersect, And Except – Find the names of sailors who have reserved both a red and a green boat
  19. 19 Union, Intersect, And Except – The OR query previous can be rewritten as follows
  20. 20 Union, Intersect, And Except – The AND query previous can be rewritten as follows
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2