Danh mục tài liệu

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

Số trang: 64      Loại file: pptx      Dung lượng: 1.72 MB      Lượt xem: 27      Lượt tải: 0    
Xem trước 7 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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.
Nội dung trích xuất từ tài liệu:
Bài giảng Hệ quản trị cơ sở dữ liệu - Chương 7: Ngôn ngữ SQL Chapter7NgônngữSQL(StructuredQueryLanguage) 1 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) 3Ví dụ 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 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 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 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. SELECTS.sname FROMSailorsS,ReservesR WHERES.sid=R.sidANDbid=103 SELECTsname FROMSailors,Reserves WHERESailors.sid=Reserves.sidANDbid=103 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 Câu truy vấn cơ bản– Find all sailors with a rating above 7.– When you want to retrieve all columns: 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 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 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 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 145.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. SELECTS.age,age1=S.age5,2*S.ageASage2 FROMSailorsS WHERES.snameLIKE‘B_%B’ 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 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’. 17Union, Intersect, And Except– Find the names of sailors who have reserved a red or a green boat 18Uni ...