




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Database System Concepts, 6th Ed.Silberschatz, Korth and SudarshanSee www.db- for conditions on re-use Silberschatz, Korth and Sudarshanc.2Database System Concepts - 6th EditionnQuery-by-Example (QBE)nAccessnDatalogSilberschatz, Korth and Sudarshanc.3Database System Concepts - 6th EditionnBasic Stru
2、cturenQueries on One RelationnQueries on Several RelationsnThe Condition BoxnThe Result RelationnOrdering the Display of TuplesnAggregate Operations nModification of the DatabaseSilberschatz, Korth and Sudarshanc.4Database System Concepts - 6th EditionnA graphical query language which is based (roug
3、hly) on the domain relational calculusnTwo dimensional syntax system creates templates of relations that are requested by usersnQueries are expressed “by example”Silberschatz, Korth and Sudarshanc.5Database System Concepts - 6th EditionSilberschatz, Korth and Sudarshanc.6Database System Concepts - 6
4、th EditionSilberschatz, Korth and Sudarshanc.7Database System Concepts - 6th EditionnFind all loan numbers at the Perryridge branch.l _x is a variable (optional; can be omitted in above query)l P. means print (display)l duplicates are removed by defaultl To retain duplicates use P.ALL xSilberschatz,
5、 Korth and Sudarshanc.8Database System Concepts - 6th EditionnDisplay full details of all loanslMethod 1:lMethod 2: Shorthand notationSilberschatz, Korth and Sudarshanc.9Database System Concepts - 6th EditionnFind names of all branches that are not located in Brooklynn Find the loan number of all lo
6、ans with a loan amount of more than $700Silberschatz, Korth and Sudarshanc.10Database System Concepts - 6th EditionnFind the loan numbers of all loans made jointly to Smith and Jones.nFind all customers who live in the same city as Jones.Silberschatz, Korth and Sudarshanc.11Database System Concepts
7、- 6th EditionnFind the names of all customers who have a loan from the Perryridge branch.Silberschatz, Korth and Sudarshanc.12Database System Concepts - 6th EditionnFind the names of all customers who have both an account and a loan at the bank.Silberschatz, Korth and Sudarshanc.13Database System Co
8、ncepts - 6th EditionnFind the names of all customers who have an account at the bank, but do not have a loan from the bank. means “there does not exist”Silberschatz, Korth and Sudarshanc.14Database System Concepts - 6th EditionnFind all customers who have at least two accounts. means “not equal to”S
9、ilberschatz, Korth and Sudarshanc.15Database System Concepts - 6th EditionnAllows the expression of constraints on domain variables that are either inconvenient or impossible to express within the skeleton tables.nComplex conditions can be used in condition boxesnExample: Find the loan numbers of al
10、l loans made to Smith, to Jones, or to both jointlySilberschatz, Korth and Sudarshanc.16Database System Concepts - 6th EditionnQBE supports an interesting syntax for expressing alternative values.Silberschatz, Korth and Sudarshanc.17Database System Concepts - 6th EditionnFind all account numbers wit
11、h a balance greater than $1,300 and less than $1,500. nFind all account numbers with a balance greater than $1,300 and less than $2,000 but not exactly $1,500.Silberschatz, Korth and Sudarshanc.18Database System Concepts - 6th EditionnFind all branches that have assets greater than those of at least
12、 one branch located in Brooklyn.Silberschatz, Korth and Sudarshanc.19Database System Concepts - 6th EditionnFind the customer_name, account_number, and balance for all customers who have an account at the Perryridge branch.lWe need to:4Join depositor and account.4Project customer_name, account_numbe
13、r and balance.lTo accomplish this we:4Create a skeleton table, called result, with attributes customer_name, account_number, and balance.4Write the query.Silberschatz, Korth and Sudarshanc.20Database System Concepts - 6th EditionnThe resulting query is: Silberschatz, Korth and Sudarshanc.21Database
14、System Concepts - 6th EditionnAO = ascending order; DO = descending order.nExample: list in ascending alphabetical order all customers who have an account at the bank.nWhen sorting on multiple attributes, the sorting order is specified by including with each sort operator (AO or DO) an integer surro
15、unded by parentheses.nExample: List all account numbers at the Perryridge branch in ascending alphabetic order with their respective account balances in descending order.Silberschatz, Korth and Sudarshanc.22Database System Concepts - 6th EditionnThe aggregate operators are AVG, MAX, MIN, SUM, and CN
16、TnThe above operators must be postfixed with “ALL” (e.g., SUM.ALL. or AVG.ALL._x) to ensure that duplicates are not eliminated.nExample: Find the total balance of all the accounts maintained at the Perryridge branch.Silberschatz, Korth and Sudarshanc.23Database System Concepts - 6th EditionnUNQ is u
17、sed to specify that we want to eliminate duplicates. nFind the total number of customers having an account at the bank.Silberschatz, Korth and Sudarshanc.24Database System Concepts - 6th EditionnFind the average balance at each branch.nThe “G” in “P.G” is analogous to SQLs group by construct. nThe “
18、ALL” in the “P.AVG.ALL” entry in the balance column ensures that all balances are considered.nTo find the average account balance at only those branches where the average account balance is more than $1,200, we simply add the condition box:Silberschatz, Korth and Sudarshanc.25Database System Concept
19、s - 6th EditionnFind all customers who have an account at all branches located in Brooklyn. lApproach: for each customer, find the number of branches in Brooklyn at which they have accounts, and compare with total number of branches in Brooklyn.lQBE does not provide subquery functionality, so both a
20、bove tasks have to be combined in a single query. 4Can be done for this query, but there are queries that require subqueries and cannot always be expressed in QBE.nIn the query on the next pagelw specifies the number of distinct branches in Brooklyn. Note: The variable _w is not connected to other v
21、ariables in the query.lz specifies the number of distinct branches in Brooklyn at which customer x has an account.Silberschatz, Korth and Sudarshanc.26Database System Concepts - 6th EditionSilberschatz, Korth and Sudarshanc.27Database System Concepts - 6th EditionnDeletion of tuples from a relation
22、is expressed by use of a D. command. In the case where we delete information in only some of the columns, null values, specified by , are inserted.nDelete customer SmithnDelete the branch_city value of the branch whose name is “Perryridge”.Silberschatz, Korth and Sudarshanc.28Database System Concept
23、s - 6th EditionnDelete all loans with a loan amount greater than $1300 and less than $1500.lFor consistency, we have to delete information from loan and borrower tablesSilberschatz, Korth and Sudarshanc.29Database System Concepts - 6th EditionnDelete all accounts at branches located in Brooklyn.Silb
24、erschatz, Korth and Sudarshanc.30Database System Concepts - 6th EditionnInsertion is done by placing the I. operator in the query expression. nInsert the fact that account A-9732 at the Perryridge branch has a balance of $700.Silberschatz, Korth and Sudarshanc.31Database System Concepts - 6th Editio
25、nnProvide as a gift for all loan customers of the Perryridge branch, a new $200 savings account for every loan account they have, with the loan number serving as the account number for the new savings account. Silberschatz, Korth and Sudarshanc.32Database System Concepts - 6th EditionnUse the U. ope
26、rator to change a value in a tuple without changing all values in the tuple. QBE does not allow users to update the primary key fields.nUpdate the asset value of the Perryridge branch to $10,000,000. nIncrease all balances by 5 percent.Silberschatz, Korth and Sudarshanc.33Database System Concepts -
27、6th EditionnMicrosoft Access supports a variant of QBE called Graphical Query By Example (GQBE).nGQBE differs from QBE in the following wayslAttributes of relations are listed vertically, one below the other, instead of horizontally.lInstead of using variables, lines (links) between attributes are u
28、sed to specify that their values should be the same.4Links are added automatically on the basis of attribute name, and the user can then add or delete links.4By default, a link specifies an inner join, but can be modified to specify outer joins.lConditions, values to be printed, as well as group by
29、attributes are all specified together in a box called the design grid.Silberschatz, Korth and Sudarshanc.34Database System Concepts - 6th EditionnExample query: Find the customer_name, account_number and balance for all accounts at the Perryridge branch.Silberschatz, Korth and Sudarshanc.35Database
30、System Concepts - 6th EditionnFind the name, street and city of all customers who have more than one account at the bank.Silberschatz, Korth and Sudarshanc.36Database System Concepts - 6th EditionnThe row labeled Total specifies: lwhich attributes are group by attributes.lwhich attributes are to be
31、aggregated upon (and the aggregate function). lFor attributes that are neither group by nor aggregated, we can still specify conditions by selecting where in the Total row and listing the conditions below.nAs in SQL, if group by is used, only group by attributes and aggregate results can be output.S
32、ilberschatz, Korth and Sudarshanc.37Database System Concepts - 6th EditionnBasic Structure nSyntax of Datalog RulesnSemantics of Nonrecursive DatalognSafety nRelational Operations in DatalognRecursion in DatalognThe Power of RecursionSilberschatz, Korth and Sudarshanc.38Database System Concepts - 6t
33、h EditionnProlog-like logic-based language that allows recursive queries; based on first-order logic.nA Datalog program consists of a set of rules that define views. nExample: define a view relation v1 containing account numbers and balances for accounts at the Perryridge branch with a balance of ov
34、er $700.v1 (A, B ) : account (A, “Perryridge”, B ), B 700.nRetrieve the balance of account number “A-217” in the view relation v1.? v1 (“A-217”, B ).nTo find account number and balance of all accounts in v1 that have a balance greater than 800 ? v1 (A,B ), B 800Silberschatz, Korth and Sudarshanc.39D
35、atabase System Concepts - 6th EditionnEach rule defines a set of tuples that a view relation must contain.lE.g., v1 (A, B ) : account (A, “ Perryridge”, B ), B 700 is read as: for all A, B if (A, “Perryridge”, B ) account and B 700 then (A, B ) v1nThe set of tuples in a view relation is then defined
36、 as the union of all the sets of tuples defined by the rules for the view relation.nExample:interest_rate (A, 5) : account (A, N, B ) , B = 10000Silberschatz, Korth and Sudarshanc.40Database System Concepts - 6th EditionnDefine a view relation c that contains the names of all customers who have a de
37、posit but no loan at the bank:c(N) : depositor (N, A), not is_borrower (N).is_borrower (N) :borrower (N,L).nNOTE: using not borrower (N, L) in the first rule results in a different meaning, namely there is some loan L for which N is not a borrower. lTo prevent such confusion, we require all variable
38、s in negated “predicate” to also be present in non-negated predicates.Silberschatz, Korth and Sudarshanc.41Database System Concepts - 6th EditionnDatalog rules use a positional notation that is convenient for relations with a small number of attributes.nIt is easy to extend Datalog to support named
39、attributes. lE.g., v1 can be defined using named attributes as v1 (account_number A, balance B ) : account (account_number A, branch_name “ Perryridge”, balance B ), B 700.Silberschatz, Korth and Sudarshanc.42Database System Concepts - 6th EditionnWe formally define the syntax and semantics (meaning
40、) of Datalog programs, in the following steps:HWe define the syntax of predicates, and then the syntax of rules.HWe define the semantics of individual rules.HWe define the semantics of non-recursive programs, based on a layering of rules.HIt is possible to write rules that can generate an infinite n
41、umber of tuples in the view relation. To prevent this, we define what rules are “safe”. Non-recursive programs containing only safe rules can only generate a finite number of answers.1.It is possible to write recursive programs whose meaning is unclear. We define what recursive programs are acceptab
42、le, and define their meaning.Silberschatz, Korth and Sudarshanc.43Database System Concepts - 6th EditionnA positive literal has the formp (t1, t2 ., tn )lp is the name of a relation with n attributesleach ti is either a constant or variablenA negative literal has the form not p (t1, t2 ., tn )nCompa
43、rison operations are treated as positive predicates lE.g., X Y is treated as a predicate (X,Y )l“” is conceptually an (infinite) relation that contains all pairs of values such that the first value is greater than the second valuenArithmetic operations are also treated as predicateslE.g., A = B + C
44、is treated as +(B, C, A), where the relation “+” contains all triples such that the third value is thesum of the first twoSilberschatz, Korth and Sudarshanc.44Database System Concepts - 6th EditionnRules are built out of literals and have the form:p (t1, t2, ., tn ) : L1, L2, ., Lm. head bodyleach L
45、i is a literallhead the literal p (t1, t2, ., tn ) lbody the rest of the literalsnA fact is a rule with an empty body, written in the form:p (v1, v2, ., vn ).lindicates tuple (v1, v2, ., vn ) is in relation pnA Datalog program is a set of rules.Silberschatz, Korth and Sudarshanc.45Database System Co
46、ncepts - 6th EditionnA ground instantiation of a rule (or simply instantiation) is the result of replacing each variable in the rule by some constant.lE.g., Rule defining v1 v1(A,B) : account (A,“Perryridge”, B ), B 700.lAn instantiation above rule: v1 (“A-217”, 750) :account ( “A-217”, “Perryridge”
47、, 750),750 700.nThe body of rule instantiation R is satisfied in a set of facts (database instance) l if1. For each positive literal qi (vi,1, ., vi,ni ) in the body of R, l contains the fact qi (vi,1, ., vi,ni ).2. For each negative literal not qj (vj,1, ., vj,nj ) in the body of R, l does not cont
48、ain the fact qj (vj,1, ., vj,nj ).Silberschatz, Korth and Sudarshanc.46Database System Concepts - 6th EditionnWe define the set of facts that can be inferred from a given set of facts l using rule R as:infer(R, l) = p (t1, ., tn) | there is a ground instantiation R of R where p (t1, ., tn ) is the h
49、ead of R, and the body of R is satisfied in l nGiven an set of rules = R1, R2, ., Rn, we defineinfer( , l) = infer (R1, l ) infer (R2, l ) . infer (Rn, l )Silberschatz, Korth and Sudarshanc.47Database System Concepts - 6th EditionnDefine the interest on each account in Perryridgeinterest(A, l) : per
50、ryridge_account (A,B), interest_rate(A,R), l = B * R/100.perryridge_account(A,B) : account (A, “Perryridge”, B).interest_rate (A,5) : account (N, A, B), B = 10000.nLayering of the view relationsSilberschatz, Korth and Sudarshanc.48Database System Concepts - 6th EditionnA relation is a layer 1 if all
51、 relations used in the bodies of rules defining it are stored in the database.nA relation is a layer 2 if all relations used in the bodies of rules defining it are either stored in the database, or are in layer 1.nA relation p is in layer i + 1 if lit is not in layers 1, 2, ., ilall relations used i
52、n the bodies of rules defining a p are either stored in the database, or are in layers 1, 2, ., iFormally:Silberschatz, Korth and Sudarshanc.49Database System Concepts - 6th EditionnDefine I0 = set of facts stored in the database.nRecursively define li+1 = li infer ( i+1, li )nThe set of facts in th
53、e view relations defined by the program (also called the semantics of the program) is given by the set of facts ln corresponding to the highest layer n.Let the layers in a given program be 1, 2, ., n. Let i denote theset of all rules defining view relations in layer i.Note: Can instead define semant
54、ics using view expansion likein relational algebra, but above definition is better for handlingextensions such as recursion.Silberschatz, Korth and Sudarshanc.50Database System Concepts - 6th EditionnIt is possible to write rules that generate an infinite number of answers.gt(X, Y) : X Ynot_in_loan
55、(B, L) : not loan (B, L)To avoid this possibility Datalog rules must satisfy the following conditions.lEvery variable that appears in the head of the rule also appears in a non-arithmetic positive literal in the body of the rule.4This condition can be weakened in special cases based on the semantics
56、 of arithmetic predicates, for example to permit the rulep (A ) :- q (B ), A = B + 1lEvery variable appearing in a negative literal in the body of the rule also appears in some positive literal in the body of the rule.Silberschatz, Korth and Sudarshanc.51Database System Concepts - 6th EditionnProjec
57、t out attribute account_name from account.query (A) :account (A, N, B ).nCartesian product of relations r1 and r2.query (X1, X2, ., Xn, Y1, Y1, Y2, ., Ym ) :r1 (X1, X2, ., Xn ), r2 (Y1, Y2, ., Ym ).nUnion of relations r1 and r2. query (X1, X2, ., Xn ) :r1 (X1, X2, ., Xn ), query (X1, X2, ., Xn ) :r2
58、 (X1, X2, ., Xn ), nSet difference of r1 and r2.query (X1, X2, ., Xn ) :r1(X1, X2, ., Xn ), not r2 (X1, X2, ., Xn ), Silberschatz, Korth and Sudarshanc.52Database System Concepts - 6th EditionnSuppose we are given a relation manager (X, Y )containing pairs of names X, Y such that Y is a manager of X
59、 (or equivalently, X is a direct employee of Y).nEach manager may have direct employees, as well as indirect employees.lIndirect employees of a manager, say Jones, are employees of people who are direct employees of Jones, or recursively, employees of people who are indirect employees of Jones.nSupp
60、ose we wish to find all (direct and indirect) employees of manager Jones. We can write a recursive Datalog program. empl_jones (X ) :- manager (X, Jones ). empl_jones (X ) :- manager (X, Y ), empl_jones (Y ).Silberschatz, Korth and Sudarshanc.53Database System Concepts - 6th EditionnAssumption (for
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 工業設計在現代制造中的作用和價值
- 工業遺產保護與再利用的環境設計策略
- 工業藝術區規劃設計及其產業融合案例分享
- 工業設計創新與技術美學探討
- 工作效率提升的實踐案例分享
- 工作場所的安全規范培訓
- 工廠企業防火培訓教材
- 工作報告編制技巧與實戰分享
- 工程設計中的數學模型構建
- 市場分析與目標用戶畫像的技巧
- 2024制冷系統管路結構設計指導書
- 國際檔案日介紹主題班會
- 足外傷個案護理
- 慢性非傳染性疾病綜合防控示范區申報考評細則培訓課件
- 浙江省城鎮生活垃圾分類標準
- 語言學綱要期末復習考點(完整)
- 2024年廣西中考道德與法治試卷真題(含答案解析)
- DL-T+796-2012風力發電場安全規程
- 2024中考地理一輪復習專題1地球和地球儀(講義)(原卷版)
- DL-T-1642-2016環形混凝土電桿用腳扣
- SF-36生活質量調查表(SF-36-含評分細則)
評論
0/150
提交評論