Dbms

download Dbms

If you can't read please download the document

description

Contain all concept of DBMS

Transcript of Dbms

  • 1.Chapter 1: IntroductionPurpose of Database SystemsView of DataData ModelsData Definition LanguageData Manipulation LanguageTransaction ManagementStorage ManagementDatabase AdministratorDatabase UsersOverall System Structure

2. Database Management System(DBMS)interrelated data Collection ofSet of programs to access the dataDBMS contains information about a particular enterpriseDBMS provides an environment that is both convenient and efficient to use.Database Applications: Banking: all transactions Airlines: reservations, schedules Universities: registration, grades Sales: customers, products, purchases Manufacturing: production, inventory, orders, supply chain Human resources: employee records, salaries, tax deductionsDatabases touch all aspects of our lives 3. Purpose of Database SystemIn the early days, database applications werebuilt on top of file systemsDrawbacks of using file systems to storedata: Data redundancy and inconsistency Multiple file formats, duplication of information indifferent files Difficulty in accessing data Need to write a new program to carry out each newtask Data isolation multiple files and formats Integrity problems Integrity constraints (e.g. account balance > 0)become part of program code Hard to add new constraints or change existingones 4. Purpose of Database Systems(Cont.)Drawbacks of using file systems (cont.) Atomicity of updates Failures may leave database in an inconsistent statewith partial updates carried out E.g. transfer of funds from one account to anothershould either complete or not happen at all Concurrent access by multiple users Concurrent accessed needed for performance Uncontrolled concurrent accesses can lead toinconsistencies E.g. two people reading a balance and updating it at thesame time Security problemsDatabase systems offer solutions to all theabove problems 5. Levels of AbstractionPhysical level describes how a record (e.g.,customer) is stored.Logical level: describes data stored in database, andthe relationships among the data. type customer = recordname : string;street : string;city : integer;end;View level: application programs hide details of datatypes. Views can also hide information (e.g.,salary) for security purposes. 6. View ofaData systemAn architecture for database 7. Instances and SchemasSimilar to types and variables in programming languagesSchema the logical structure of the database e.g., the database consists of information about a set ofcustomers and accounts and the relationship betweenthem) Analogous to type information of a variable in a program Physical schema: database design at the physical level Logical schema: database design at the logical levelInstance the actual content of the database at a particular point in time Analogous to the value of a variablePhysical Data Independence the ability to modify the physical schema without changing the logical schema Applications depend on the logical schema In general, the interfaces between the various levels andcomponents should be well defined so that changes insome parts do not seriously influence others. 8. Data ModelsA collection of tools for describing data data relationships data semantics data constraintsEntity-Relationship modelRelational modelOther models: object-oriented model semi-structured data models Older models: network model andhierarchical model 9. Entity-Relationship ModelExample of schema in the entity-relationship model 10. Entity Relationship Model(Cont.)E-R model of real world Entities (objects) E.g. customers, accounts, bank branch Relationships between entities E.g. Account A-101 is held by customerJohnson Relationship set depositor associates customerswith accountsWidely used for database design Database design in E-R model usuallyconverted to design in the relational model(coming up next) which is used for storageand processing 11. Relational Model AttributesExample of tabular data in the relational model customer- customer- customer- account- Customer- namestreetcitynumber id 192-83-7465 JohnsonAlma Palo Alto A-101 019-28-3746 SmithNorthRye A-215 192-83-7465 JohnsonAlma Palo Alto A-201 321-12-3123 JonesMain HarrisonA-217 019-28-3746 SmithNorthRye A-201 12. A Sample Relational Database 13. Data Definition Language (DDL)Specification notation for defining the databaseschema E.g.create table account ( account-number char(10), balanceinteger)DDL compiler generates a set of tables stored in adata dictionaryData dictionary contains metadata (i.e., data aboutdata) database schema Data storage and definition language language in which the storage structure and accessmethods used by the database system are specified Usually an extension of the data definition language 14. Data Manipulation Language(DML)Language for accessing and manipulatingthe data organized by the appropriatedata model DML also known as query languageTwo classes of languages Procedural user specifies what data isrequired and how to get those data Nonprocedural user specifies what data isrequired without specifying how to get thosedataSQL is the most widely used querylanguage 15. SQLSQL: widely used non-procedural language E.g. find the name of the customer with customer-id 192-83-7465 select customer.customer-name fromcustomer where customer.customer-id = 192-83-7465 E.g. find the balances of all accounts held by the customer withcustomer-id 192-83-7465 select account.balance fromdepositor, account where depositor.customer-id = 192-83-7465 anddepositor.account-number = account.account-numberApplication programs generally access databases throughone of Language extensions to allow embedded SQL Application program interface (e.g. ODBC/JDBC) which allow SQLqueries to be sent to a database 16. Database UsersUsers are differentiated by the way they expect tointeract with the systemApplication programmers interact with systemthrough DML callsSophisticated users form requests in a databasequery languageSpecialized users write specialized databaseapplications that do not fit into the traditionaldata processing frameworkNave users invoke one of the permanentapplication programs that have been writtenpreviously E.g. people accessing database over the web, banktellers, clerical staff 17. Database AdministratorCoordinates all the activities of the databasesystem; the database administrator has agood understanding of the enterprisesinformation resources and needs.Database administrators duties include: Schema definition Storage structure and access method definition Schema and physical organization modification Granting user authority to access the database Specifying integrity constraints Acting as liaison with users Monitoring performance and responding tochanges in requirements 18. Transaction ManagementA transaction is a collection of operations thatperforms a single logical function in adatabase applicationTransaction-management component ensuresthat the database remains in a consistent(correct) state despite system failures (e.g.,power failures and operating systemcrashes) and transaction failures.Concurrency-control manager controls theinteraction among the concurrenttransactions, to ensure the consistency ofthe database. 19. Storage ManagementStorage manager is a program module thatprovides the interface between the low-level data stored in the database and theapplication programs and queriessubmitted to the system.The storage manager is responsible to thefollowing tasks: interaction with the file manager efficient storing, retrieving and updating ofdata 20. Overall System Structure 21. Application ArchitecturesTwo-tier architecture: E.g. client programs using ODBC/JDBC to communicate with a databaseThree-tier architecture: E.g. web-based applications, and applications built using middleware 22. Chapter 2: Entity-RelationshipModel Entity Sets Relationship Sets Design Issues Mapping Constraints Keys E-R Diagram Extended E-R Features Design of an E-R Database Schema Reduction of an E-R Schema to Tables 23. Entity Sets A database can be modeled as: a collection of entities, relationship among entities. An entity is an object that exists and isdistinguishable from other objects. Example: specific person, company, event, plant Entities have attributes Example: people have names and addresses An entity set is a set of entities of the sametype that share the same properties. Example: set of all persons, companies, trees,holidays 24. Entity Sets customer and loancustomer-id customer- customer- customer-loan- amountname street citynumber 25. Attributes An entity is represented by a set ofattributes, that is descriptive propertiespossessed by all members of an entity set.Example:customer = (customer-id, customer-name, customer-street, customer-city)loan = (loan-number, amount) Domain the set of permitted values for eachattribute Attribute types: Simple and composite attributes. Single-valued and multi-valued attributes E.g. multivalued attribute: phone-numbers Derived attributes Can be computed from other attributes E.g. age, given date of birth 26. Composite Attributes 27. Relationship Sets A relationship is an association among severalentitiesExample:Hayes depositor A-102customer entityrelationship setaccount entity A relationship set is a mathematical relationamong n 2 entities, each taken from entity sets {(e1, e2, en) | e1 E 1 , e2E2, , enEn}where (e1, e2, , en) is a relationship Example:(Hayes, A-102) depositor 28. Relationship Set borrower 29. Relationship Sets (Cont.) An attribute can also be property of arelationship set. For instance, the depositorrelationship set between entity setscustomer and account may have theattribute access-date 30. Degree of a Relationship Set Refers to number of entity sets thatparticipate in a relationship set. Relationship sets that involve two entitysets are binary (or degree two).Generally, most relationship sets in adatabase system are binary. Relationship sets may involve morethan two entity sets. E.g. Suppose employees of a bank may have jobs (responsibilities) at multiple branches, with different jobs at different branches. Then there is a ternary relationship set between entity sets employee, job and branch 31. Mapping Cardinalities Express the number of entities towhich another entity can beassociated via a relationship set. Most useful in describing binaryrelationship sets. For a binary relationship set themapping cardinality must be one ofthe following types: One to one One to many Many to one Many to many 32. Mapping CardinalitiesOne to one One to manyNote: Some elements in A and B may not be mapped to anyelements in the other set 33. Mapping Cardinalities Many to oneMany to many Note: Some elements in A and B may not be mapped to any elements in the other set 34. Mapping Cardinalities affect ER Design Can make access-date an attribute of account, instead of arelationship attribute, if each account can have only one customer I.e., the relationship from account to customer is many to one,or equivalently, customer to account is one to many 35. E-R Diagrams Rectangles represent entity sets. Diamonds represent relationship sets. Lines link attributes to entity sets and entity sets to relationship sets. Ellipses represent attributes Double ellipses represent multivalued attributes. Dashed ellipses denote derived attributes. Underline indicates primary key attributes (will study later) 36. E-R Diagram With Composite, Multivalued, and DerivedAttributes 37. Relationship Sets withAttributes 38. Roles Entity sets of a relationship need notbe distinct The labels manager and worker are called roles; they specifyhow employee entities interact via the works-for relationshipset. Roles are indicated in E-R diagrams by labeling the lines thatconnect diamonds to rectangles. Role labels are optional, and are used to clarify semantics ofthe relationship 39. Cardinality Constraints We express cardinality constraints bydrawing either a directed line ( ),signifying one, or an undirected line(), signifying many, between therelationship set and the entity set. E.g.: One-to-one relationship: A customer is associated with at most oneloan via the relationship borrower A loan is associated with at most onecustomer via borrower 40. One-To-Many Relationship In the one-to-many relationship a loan is associated with at most one customer via borrower, a customer is associated with several (including 0) loans via borrower 41. Many-To-One Relationships In a many-to-one relationship a loanis associated with several (including 0)customers via borrower, a customer isassociated with at most one loan viaborrower 42. Many-To-Many Relationship A customer is associated withseveral (possibly 0) loans viaborrower A loan is associated with several(possibly 0) customers viaborrower 43. Participation of an Entity Set ina Relationship SetTotal participation (indicated by double line): every entity in the entity setparticipates in at least one relationship in the relationship set E.g. participation of loan in borrower is total every loan must have a customer associated to it via borrower Partial participation: some entities may not participate in any relationship in therelationship set E.g. participation of customer in borrower is partial 44. Alternative Notation forCardinality Limits Cardinality limits can also express participation constraints 45. Keys A super key of an entity set is a setof one or more attributes whosevalues uniquely determine eachentity. A candidate key of an entity set is aminimal super key Customer-id is candidate key ofcustomer account-number is candidate key ofaccount Although several candidate keys may exist, one of the candidate 46. Keys for Relationship Sets The combination of primary keys ofthe participating entity sets forms asuper key of a relationship set. (customer-id, account-number) is thesuper key of depositor NOTE: this means a pair of entity sets canhave at most one relationship in aparticular relationship set. E.g. if we wish to track all access-dates toeach account by each customer, we cannotassume a relationship for each access. Wecan use a multivalued attribute though 47. E-RDiagram with a TernaryRelationship 48. Cardinality Constraints onTernary at most one arrow out of aWe allow Relationshipternary (or greater degree) relationshipto indicate a cardinality constraint E.g. an arrow from works-on to jobindicates each employee works on atmost one job at any branch. If there is more than one arrow, there aretwo ways of defining the meaning. E.g a ternary relationship R between A, B andC with arrows to B and C could mean 1. each A entity is associated with a uniqueentity from B and C or 49. Binary Vs. Non-Binary to beSome relationships that appearRelationshipsnon-binary may be better representedusing binary relationships E.g. A ternary relationship parents, relating a child to his/her father and mother, is best replaced by two binary relationships, father and mother Using two binary relationships allows partialinformation (e.g. only mother being know) But there are some relationships that are naturally non-binary E.g. works-on 50. Converting Non-Binary Relationships to an artificial entity set.Binary Form In general, any non-binary relationship can be represented usingbinary relationships by creating Replace R between entity sets A, B and C by an entity set E, and three relationship sets: 1. RA, relating E and A 2.RB, relating E and B 3. RC, relating E and C Create a special identifying attribute for E Add any attributes of R to E For each relationship (ai , bi , ci) in R, create1. a new entity ei in the entity set E 2. add (ei , ai ) to RA3. add (ei , bi ) to RB4. add (ei , ci ) to RC 51. Converting Non-BinaryRelationships (Cont.) Also need to translate constraints Translating all constraints may not bepossible There may be instances in the translatedschema thatcannot correspond to any instance of R Exercise: add constraints to the relationshipsRA, RB and RC to ensure that a newly created entity corresponds to exactly one entity in each of entity sets A, B and C We can avoid creating an identifying attribute by making E a weak entity set (described shortly) identified by the three 52. Design Issues Use of entity sets vs. attributesChoice mainly depends on the structureof the enterprise being modeled, and onthe semantics associated with theattribute in question. Use of entity sets vs. relationship setsPossible guideline is to designate arelationship set to describe an actionthat occurs between entities Binary versus n-ary relationship setsAlthough it is possible to replace anynonbinary (n-ary, for n > 2) relationshipset by a number of distinct binary 53. HOW ABOUT DOINGAN ER DESIGNINTERACTIVELY ONTHE BOARD?SUGGEST ANAPPLICATION TO BEMODELED. 54. Weak Entity Sets An entity set that does not have aprimary key is referred to as a weakentity set. The existence of a weak entity set depends on the existence of a identifying entity set it must relate to the identifying entity setvia a total, one-to-many relationship setfrom the identifying to the weak entity set Identifying relationship depicted using adouble diamond 55. Weak Entity Sets (Cont.) We depict a weak entity set bydouble rectangles. We underline the discriminator of aweak entity set with a dashed line. payment-number discriminator ofthe payment entity set Primary key for payment (loan-number, payment-number) 56. Weak Entity Sets (Cont.) Note: the primary key of the strongentity set is not explicitly stored withthe weak entity set, since it is implicitin the identifying relationship. If loan-number were explicitly stored,payment could be made a strongentity, but then the relationshipbetween payment and loan would beduplicated by an implicit relationshipdefined by the attribute loan-number 57. More Weak Entity Set Examples In a university, a course is a strongentity and a course-offering can bemodeled as a weak entity The discriminator of course-offeringwould be semester (including year) andsection-number (if there is more thanone section) If we model course-offering as a strongentity we would model course-numberas an attribute.Then the relationship with course wouldbe implicit in the course-number 58. Specialization Top-down design process; wedesignate subgroupings within anentity set that are distinctive from otherentities in the set. These subgroupings become lower-level entity sets that have attributes orparticipate in relationships that do notapply to the higher-level entity set. Depicted by a triangle componentlabeled ISA (E.g. customer is aperson). Attribute inheritance a lower-level 59. Specialization Example 60. Generalization A bottom-up design process combine a number of entity sets thatshare the same features into a higher-level entity set. Specialization and generalization aresimple inversions of each other; theyare represented in an E-R diagram inthe same way. The terms specialization andgeneralization are usedinterchangeably. 61. Specialization and Generalization (Contd.) Can have multiple specializations of anentity set based on different features. E.g. permanent-employee vs.temporary-employee, in addition toofficer vs. secretary vs. teller Each particular employee would be a member of one of permanent-employeeor temporary-employee, and also a member of one of officer,secretary, or teller The ISA relationship also referred to as 62. Design Constraints on aSpecialization/Generalization Constraint on which entities can be members of a given lower-level entity set. condition-defined E.g. all customers over 65 years aremembers of senior-citizen entity set;senior-citizen ISA person. user-defined Constraint on whether or not entities may belong to more than one lower- level entity set within a single generalization. Disjoint 63. Design Constraints on a Specialization/Generalization(Contd.)Completeness constraint -- specifies whether or not an entity in the higher- level entity set must belong to at least one of the lower-level entity sets within a generalization. total : an entity must belong to one of thelower-level entity sets partial: an entity need not belong to one ofthe lower-level entity sets 64. Aggregation Consider the ternary relationship works-on, which we saw earlier Suppose we want to record managers for tasks performed by an employee at a branch 65. Aggregation (Cont.) Relationship sets works-on and manages represent overlapping information Every manages relationship corresponds to aworks-on relationship However, some works-on relationships maynot correspond to any manages relationships So we cant discard the works-on relationship Eliminate this redundancy via aggregation Treat relationship as an abstract entity Allows relationships between relationships Abstraction of relationship into new entity Without introducing redundancy, the 66. E-R Diagram With Aggregation 67. E-R Design Decisions The use of an attribute or entity set torepresent an object. Whether a real-world concept is bestexpressed by an entity set or arelationship set. The use of a ternary relationshipversus a pair of binary relationships. The use of a strong or weak entity set. The use ofspecialization/generalization 68. E-R Diagram for a BankingEnterprise 69. HOW ABOUT DOINGANOTHER ER DESIGNINTERACTIVELY ONTHE BOARD? 70. Summary of Symbols Used in E-RNotation 71. Summary of Symbols (Cont.) 72. Alternative E-R Notations 73. UML UML: Unified Modeling Language UML has many components tographically model different aspects ofan entire software system UML Class Diagrams correspond to E-R Diagram, but several differences. 74. Summary of UML Class DiagramNotation 75. UML Class Diagrams (Contd.) Entity sets are shown as boxes, andattributes are shown within the box,rather than as separate ellipses in E-Rdiagrams. Binary relationship sets are representedin UML by just drawing a line connectingthe entity sets. The relationship setname is written adjacent to the line. The role played by an entity set in arelationship set may also be specified bywriting the role name on the line,adjacent to the entity set. 76. UML Class Diagram Notation(Cont.) overlappingdisjoint*Note reversal of position in cardinality constraint depiction*Generalization can use merged or separate arrows independent of disjoint/overlapping 77. UML Class Diagrams (Contd.) Cardinality constraints are specified inthe form l..h, where l denotes theminimum and h the maximum number ofrelationships an entity can participate in. Beware: the positioning of the constraintsis exactly the reverse of the positioningof constraints in E-R diagrams. The constraint 0..* on the E2 side and0..1 on the E1 side means that each E2entity can participate in at most onerelationship, whereas each E1 entity canparticipate in many relationships; in other 78. Reduction of an E-R Schema toTables Primary keys allow entity sets and relationship sets to be expressed uniformly as tables which represent the contents of the database. A database which conforms to an E-R diagram can be represented by a collection of tables. For each entity set and relationship set there is a unique table which is assigned the name of the corresponding entity set or 79. Representing Entity Sets as A strong entity set reduces to a tableTables same attributes.with the 80. Composite and Multivalued Composite attributes are flattened out by Attributescreating a separate attribute for eachcomponent attribute E.g. given entity set customer with composite attribute name with component attributes first-name and last-name the table corresponding to the entity set has two attributesname.first-name and name.last- name A multivalued attribute M of an entity E isrepresented by a separate table EM Table EM has attributes corresponding to the primary key of E and an attribute corresponding to multivalued attribute M 81. Representing Weak Entitycolumn for A weak entity set becomes a table that includes aSetsthe primary key of the identifying strong entity set 82. Representing Relationship a A many-to-many relationship set is represented asSets as columns for the primary keys of the two of table with Tables participating entity sets, and any descriptive attributes the relationship set. E.g.: table for relationship set borrower 83. Redundancy of Tables Many-to-one and one-to-many relationship sets that are totalon the many-side can be represented by adding an extraattribute to the many side, containing the primary key of theone side E.g.: Instead of creating a table for relationship account-branch, add an attribute branch to the entity set account 84. Redundancy of Tables (Cont.) For one-to-one relationship sets,either side can be chosen to act as themany side That is, extra attribute can be added to either of the tables corresponding to the two entity sets If participation is partial on the manyside, replacing a table by an extraattribute in the relation correspondingto the many side could result in nullvalues The table corresponding to a 85. Representing Specialization asTablesMethod 1: Form a table for the higher level entity Form a table for each lower level entity set, include primarykey of higher level entity set and local attributestabletable attributespersonname, street, citycustomername, credit-ratingemployeename, salary Drawback: getting information about, e.g., employeerequires accessing two tables 86. Representing Specialization as Method 2: (Cont.) Tables Form a table for each entity set with all local and inheritedattributestabletable attributesperson name, street, citycustomer name, street, city, credit-ratingemployee name, street, city, salary If specialization is total, table for generalized entity (person)not required to store information Can be defined as a view relation containing union ofspecialization tables But explicit table may still be needed for foreign keyconstraints Drawback: street and city may be stored redundantly forpersons who are both customers and employees 87. Relations Corresponding toAggregation To represent aggregation, create a table containing primary key of the aggregated relationship, the primary key of the associated entity set Any descriptive attributes 88. Relations Corresponding to Aggregation manager, create a table(Cont.) E.g. to represent aggregation manages between relationshipworks-on and entity set manages(employee-id, branch-name, title, manager-name) Table works-on is redundant provided we are willing to storenull values for attribute manager-name in table manages 89. END OF CHAPTER 2 90. E-R Diagram for Exercise 2.10 91. E-R Diagram for Exercise 2.15 92. E-R Diagram for Exercise 2.22 93. E-R Diagram for Exercise 2.15 94. Existence Dependencies If the existence of entity x depends on the existence of entity y, then x is said to be existence dependent on y. y is a dominant entity (in example below, loan) x is a subordinate entity (in example below, payment) loan loan-payment paymentIf a loan entity is deleted, then all its associated payment entitiesmust be deleted also. 95. Chapter 3: Relational Model Structure of RelationalDatabases Relational Algebra Tuple Relational Calculus Domain Relational Calculus Extended Relational-Algebra-Operations Modification of the Database Views 96. Example of a Relation 97. Basic Structure Formally, given sets D1, D2, . Dn a relation r is asubset ofD1 x D 2 x x D nThus a relation is a set of n-tuples (a1, a2, , an)whereeach ai Di Example: if customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city = {Harrison, Rye, Pittsfield}Then r = { (Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} is a relation over customer-name x customer-streetx customer-city 98. Attribute Types Each attribute of a relation has a name The set of allowed values for each attribute iscalled the domain of the attribute Attribute values are (normally) required to beatomic, that is, indivisible E.g. multivalued attribute values are not atomic E.g. composite attribute values are not atomic The special value null is a member of everydomain The null value causes complications in thedefinition of many operations we shall ignore the effect of null values in our mainpresentation and consider their effect later 99. Relation Schema A1, A2, , An are attributes R = (A1, A2, , An ) is a relationschemaE.g. Customer-schema =(customer-name,customer-street, customer-city) r(R) is a relation on the relationschema R E.g. customer (Customer-schema) 100. Relation Instance The current values (relation instance)of a relation are specified by a table An element t of r is a tuple,represented by a row in a tableattributes(or columns)customer-name customer-street customer-cityJones MainHarrisontuplesSmith North Rye(or rows)Curry North Rye LindsayParkPittsfield customer 101. Relations are Unordered Order of tuples is irrelevant (tuples may be stored in an arbitrary order) E.g. account relation with unordered tuples 102. Database A database consists of multiple relations Information about an enterprise is broken up intoparts, with each relation storing one part of theinformation E.g.: account :stores information aboutaccounts depositor : stores information about whichcustomerowns which account customer : stores information aboutcustomers Storing all information as a single relation such asbank(account-number, balance, customer-name, ..)results in repetition of information (e.g. two customers own an account) the need for null values (e.g. represent a customer withoutan account) Normalization theory (Chapter 7) deals with how todesign relational schemas 103. The customer Relation 104. The depositor Relation 105. E-R Diagram for the BankingEnterprise 106. Keys Let K R K is a superkey of R if values for K aresufficient to identify a unique tuple of each possible relation r(R) by possible r we mean a relation r thatcould exist in the enterprise we aremodeling. Example: {customer-name, customer-street} and{customer-name}are both superkeys of Customer, if no two 107. Determining Keys from E-R Sets Strong entity set. The primary key ofthe entity set becomes the primarykey of the relation. Weak entity set. The primary key ofthe relation consists of the union ofthe primary key of the strong entityset and the discriminator of the weakentity set. Relationship set. The union of theprimary keys of the related entity 108. Schema Diagram for the BankingEnterprise 109. Query Languages Language in which user requestsinformation from the database. Categories of languages procedural non-procedural Pure languages: Relational Algebra Tuple Relational Calculus Domain Relational Calculus Pure languages form underlying basis 110. Relational Algebra Procedural language Six basic operators select project union set difference Cartesian product rename The operators take one or more relations as inputs and give a new 111. Select Operation Example Relation rA B CD 17 57 12 3 23 10 ^ D > 5 (r) A=BA BCD 17 23 10 112. Select Operation Notation: p(r) p is called the selection predicate Defined as:p(r) = {t | t r and p(t)}Where p is a formula inpropositional calculus consistingof terms connected by : (and),(or), (not)Each term is one of: op or 113. Project Operation Example Relation r: A BC 10 1 20 1 30 1 40 2 (r) A,CA CA C 1 1 1 = 1 1 2 2 114. Project Operation Notation:(r)A1, A2, , Akwhere A1, A2 are attribute names and ris a relation name. The result is defined as the relation ofk columns obtained by erasing thecolumns that are not listed Duplicate rows removed from result,since relations are sets 115. Union Operation Example Relations r,As: BA B 1 2 2 3 1 s r r s: A B 1 2 1 3 116. Union Operation Notation: rs Defined as: rs = {t | t r or t s} For r s to be valid. 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (e.g., 2nd columnof r deals with the same type of 117. Set Difference Operation Example Relations r, s:A B A B 1 2 2 3 1srr s:A B 1 1 118. Set Difference Operation Notation r s Defined as:r s = {t | t r and t s} Set differences must be taken betweencompatible relations. r and s must have the same arity attribute domains of r and s must be compatible 119. Cartesian-Product Operation- ExampleA BC DERelations r, s: 1 10 a 2 10 a 20 br 10 bsr x s:A B C DE 1 10 a 1 10 a 1 20 b 1 10 b 2 10 a 2 10 a 2 20 b 2 10 b 120. Cartesian-Product Operation Notation r x s Defined as:r x s = {t q | t r and q s} Assume that attributes of r(R) and s(S)are disjoint. (That is,R S = ). If attributes of r(R) and s(S) are notdisjoint, then renaming must be used. 121. Composition of Operations Can build expressions using multiple A BC D Eoperations 1 10 a Example: 1 A=C 1 (r x s) 10 20ab rxs 1 2 10 10ba 2 10 a 2 20 b 2 10 b A B C DE 1 10 a 2 20 a 2 20 b 122. Rename Operation Allows us to name, and therefore torefer to, the results of relational-algebra expressions. Allows us to refer to a relation by more than one name.Example:x (E)returns the expression E under the name XIf a relational-algebra expression E has 123. Banking Examplebranch (branch-name, branch-city, assets)customer (customer-name, customer-street, customer-only)account (account-number, branch-name, balance)loan (loan-number, branch-name, amount)depositor (customer- 124. Example Queries Find all loans of over $1200 amount > 1200 (loan)Find the loan number for each loan of an amount greater than$1200 loan-number (amount > 1200 (loan)) 125. Example Queries Find the names of all customers who have a loan, an account, or both, from the customer-name (borrower) customer-name (depositor) bankFind the names of all customers who have a loan and an account at bank. customer-name (borrower) customer-name (depositor) 126. Example Queries Find the names of all customers who have a loan at the Perryridge branch. customer-name ( branch-name=Perryridge(borrower.loan-number = loan.loan-number(borrower x loan))) Find the names of all customers who have a loan at thePerryridge branch but do not have an account at any branch ofthe bank.customer-name (branch-name = Perryridge ( borrower.loan-number = loan.loan-number(borrower x loan))) customer-name(depositor) 127. Example Queries Find the names of all customers who have aloan at the Perryridge branch.Query 1customer-name(branch-name = Perryridge (borrower.loan-number = loan.loan-number(borrower x loan))) Query 2 customer-name( loan.loan-number = borrower.loan-number( (branch-name = Perryridge(loan)) x borrower)) 128. Example QueriesFind the largest account balance Rename account relation as d The query is:balance(account) - account.balance( account.balance < d.balance (account x d (account))) 129. Formal Definition A basic expression in the relationalalgebra consists of either one of thefollowing: A relation in the database A constant relation Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra expressions: E1 E2 E1 - E2 E1 x E2 130. Additional OperationsWe define additional operations that donot add any power to therelational algebra, but that simplifycommon queries. Set intersection Natural join Division Assignment 131. Set-Intersection Operation Notation: r s Defined as: r s ={ t | t r and t s} Assume: r, s have the same arity attributes of r and s are compatible Note: rs = r - (r - s) 132. Set-Intersection Operation -Example A B A B Relation r, s:1 2 2 3 1r sA B 2 r s 133. Natural-Join Operation Notation: r s Let r and s be relations on schemas Rand S respectively.Then, rs is a relation on schema RS obtained as follows: Consider each pair of tuples tr from r and tsfrom s. If tr and ts have the same value on each ofthe attributes in R S, add a tuple t to theresult, where t has the same value as tr on r t has the same value as ts on s Example: 134. Natural Join Operation ExampleRelations r, s:A B C D B D E 1 a 1 a 2 a 3 a 4 b 1 a 1 a 2 b 2 b 3 b r sr sA B C D E 1 a 1 a 1 a 1 a 2 b 135. Division Operation r s Suited to queries that include thephrase for all. Let r and s be relations onschemas R and S respectivelywhere R = (A1, , Am, B1, , Bn) S = (B1, , Bn)The result of r s is a relation onschemaR S = (A1, , Am) 136. Division Operation ExampleRelations r, s: A BB 1 1 2 2 3 1 s 1 1 3 4 6 1 2r s: A r 137. Another Division ExampleRelations r, s: A B C D E D Ea 1 a a 1b 1 a a 1 a b 1 s a a 1 a b 3 a a 1 a b 1 a b 1rr s: A B C a a 138. Division Operation (Cont.) Property Let q rs Then q is the largest relation satisfyingqxs r Definition in terms of the basicalgebra operationLet r(R) and s(S) be relations, and letS Rr s=R-S (r) R-S (( R-S (r) x s) R-S,S(r)) 139. Assignment Operation The assignment operation ( ) provides a convenient wayto express complex queries. Write query as a sequential program consisting of a series of assignments followed by an expression whose value is displayed as a result of the query. Assignment must always be made to a temporary relation variable. Example: Write r s astemp1R-S (r)temp2R-S ((temp1 x s) R-S,S (r))result = temp1 temp2 The result to the right of theis assigned to the relation variable on the 140. Example Queries Find all customers who have an account from at least the Downtown and the(BN=Downtown (depositor account)) Uptown branches. Query 1 CN (CN BN=Uptown(depositor account)) where CN denotes customer-name and BN denotes branch-name. Query 2 customer-name, branch-name (depositor account) temp(branch-name) ({(Downtown), (Uptown)}) 141. Example Queries Find all customers who have an account at all branches located in Brooklyn city. customer-name, branch-name (depositor account) branch-name (branch-city = Brooklyn (branch)) 142. Extended Relational-Algebra- Operations Generalized Projection Outer Join Aggregate Functions 143. Generalized Projection Extends the projection operation byallowing arithmetic functions to beused in the projection list. (E) F1, F2, , Fn E is any relational-algebra expression Each of F1, F2, , Fn are are arithmeticexpressions involving constants andattributes in the schema of E. Given relation credit-info(customer- 144. Aggregate Functions andOperations Aggregation function takes acollection of values and returns asingle value as a result.avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values Aggregate operation in relationalalgebra 145. Aggregate Operation Example Relation r:AB C 7 7 3 10 sum-Cg sum(c) (r) 27 146. Aggregate Operation Example Relation account grouped bybranch-name: account-numberbranch-name balancePerryridgeA-102400PerryridgeA-201900BrightonA-217750BrightonA-215750Redwood A-222700branch-name g sum(balance) (account)branch-namebalance Perryridge 1300 Brighton 1500 Redwood 700 147. Aggregate Functions (Cont.) Result of aggregation does not have aname branch-name g rename operation to give it a Can use sum(balance) as sum-balance (account)name For convenience, we permit renaming aspart of aggregate operation 148. Outer Join An extension of the join operationthat avoids loss of information. Computes the join and then addstuples form one relation that do notmatch tuples in the other relation tothe result of the join. Uses null values: null signifies that the value is unknown ordoes not exist All comparisons involving null are(roughly speaking) false by definition. 149. Outer Join Example Relation loan loan-numberbranch-nameamountL-170Downtown3000L-230Redwood 4000L-260Perryridge1700 Relation borrowercustomer-nameloan-numberJones L-170Smith L-230Hayes L-155 150. Outer Join Example Inner Join loan Borrower loan-numberbranch-nameamount customer-nameL-170Downtown3000 JonesL-230Redwood 4000 Smith Left Outer Joinloan Borrowerloan-numberbranch-name amountcustomer-nameL-170 Downtown3000JonesL-230 Redwood 4000SmithL-260 Perryridge1700null 151. Outer Join Example Right Outer Joinloan borrowerloan-numberbranch-name amountcustomer-nameL-170 Downtown3000JonesL-230 Redwood 4000SmithL-155 null null Hayes Full Outer Join loanborrower loan-number branch-name amount customer-nameL-170 Downtown3000JonesL-230 Redwood 4000SmithL-260 Perryridge1700nullL-155 null null Hayes 152. Null Values It is possible for tuples to have a nullvalue, denoted by null, for some oftheir attributes null signifies an unknown value orthat a value does not exist. The result of any arithmeticexpression involving null is null. Aggregate functions simply ignorenull values Is an arbitrary decision. Could have 153. Null Values Comparisons with null values return the special truth value unknown If false was used instead of unknown, then not (A < 5) would not be equivalent to A >= 5 Three-valued logic using the truth value unknown: OR: (unknown or true)= true, (unknown or false)= unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, 154. Modification of the Database The content of the database may bemodified using the followingoperations: Deletion Insertion Updating All these operations are expressed using the assignment operator. 155. Deletion A delete request is expressed similarlyto a query, except instead ofdisplaying tuples to the user, theselected tuples are removed from thedatabase. Can delete only whole tuples; cannotdelete values on only particularattributes A deletion is expressed in relationalalgebra by: 156. Deletion Examples Delete all account records in the Perryridge branch. account account branch-name = Perryridge (account)Delete all loan records with amount in the range of 0 to 50loan loan amount and amount 50 (loan) 0Delete all accounts at branches located in Needham.r1 ranch-city = Needham (account b branch)r2 branch-name, account-number, balance (r1)r3 customer-name, account-number (r2 depositor)account account r2depositor depositor r3 157. Insertion To insert data into a relation, weeither: specify a tuple to be inserted write a query whose result is a set of tuples to be inserted in relational algebra, an insertion is expressed by: r r E where r is a relation and E is a relational algebra expression. 158. Insertion Examples Insert information in the databasespecifying that Smith has $1200 inaccount account {(Perryridge, A-973, 1200)} account A-973 at the Perryridge depositor depositor {(Smith, A-973)}branch. Provide as a gift for all loan customers in the Perryridgebranch, a $200 savings account. Let the loan number serveas the account number for the new savings account.r1 (branch-name = Perryridge (borrower loan))account account branch-name, account-number,200 (r1)depositor depositor customer-name, loan-number(r1) 159. Updating A mechanism to change a value in atuple without charging all values inthe tuple Use the generalized projectionoperator to do this task rF F F (r) 1, 2, , I, Each Fi is either the ith attribute of r, if the ith attribute isnot updated, or, if the attribute is to be updated Fi is an 160. Update Examples Make interest payments by increasing all balances by 5 percent. account AN, BN, BAL * 1.05 (account)where AN, BN and BAL stand for account-number, branch-nameand balance, respectively. Pay all accounts with balances over $10,000 6 percent interestand pay all others 5 percent account AN, BN, BAL * 1.06 (BAL 10000 (account)) BN, BAL * 1.05 ( 10000 (account)) AN,BAL 161. Views In some cases, it is not desirable for allusers to see the entire logical model(i.e., all the actual relations stored inthe database.) Consider a person who needs to knowa customers loan number but has noneed to see the loan amount. Thisperson should see a relationdescribed, in the relational algebra, by customer-name, loan-number (borrower loan) Any relation that is not of theconceptual model but is made visible 162. View Definition A view is defined using the create viewstatement which has the form create view v as is anylegal relational algebra queryexpression. The view name isrepresented by v. Once a view is defined, the view namecan be used to refer to the virtualrelation that the view generates. 163. View Examples Consider the view (named all- customer) consisting of branches and their view all-customer ascreate customers.branch-name, customer-name (depositor account) branch-name, customer-name (borrower loan) We can find all customers of the Perryridge branch by writing:customer-name ( branch-name = Perryridge (all-customer)) 164. Updates Through View Database modifications expressed asviews must be translated tomodifications of the actual relations inthe database. Consider the person who needs to see all loan data in the loan relation except amount. The view given to the person, branch-loan, is defined as:create view branch-loan asbranch-name, loan-number (loan) 165. Updates Through Views (Cont.) The previous insertion must berepresented by an insertion into theactual relation loan from which the viewbranch-loan is constructed. An insertion into loan requires a valuefor amount. The insertion can be dealtwith by either. rejecting the insertion and returning anerror message to the user. inserting a tuple (L-37, Perryridge, null)into the loan relation Some updates through views are 166. Views Defined Using OtherViews One view may be used in theexpression defining another view A view relation v1 is said to dependdirectly on a view relation v2 if v2 isused in the expression defining v1 A view relation v1 is said to depend onview relation v2 if either v1 dependsdirectly to v2 or there is a path of dependenciesfrom v1 to v2 A view relation v is said to be 167. View Expansion A way to define the meaning of viewsdefined in terms of other views. Let view v1 be defined by anexpression e1 that may itself containuses of view relations. View expansion of an expressionrepeats the following replacementstep:repeat Find any view relation vi in e1 168. Tuple Relational Calculus A nonprocedural query language, whereeach query is of the form{t | P (t) } It is the set of all tuples t such thatpredicate P is true for t t is a tuple variable, t[A] denotes thevalue of tuple t on attribute A t r denotes that tuple t is in relation r P is a formula similar to that of thepredicate calculus 169. Predicate Calculus Formula1. Set of attributes and constants2. Set of comparison operators: (e.g., , , , , , )3. Set of connectives: and ( ), or (v)not ( )4. Implication ( ): xy, if x if true,then y is truexy xvy5. Set of quantifiers: t r (Q(t)) there exists a tuple in t in 170. Banking Example branch (branch-name, branch-city,assets) customer (customer-name, customer-street, customer-city) account (account-number, branch-name, balance) loan (loan-number, branch-name,amount) depositor (customer-name, account-number) 171. Example Queries Find the loan-number, branch-name, and amount for loans of over{t | t loan t [amount] 1200}$1200Find the loan number for each loan of an amount greater than $1200{t | s loan (t[loan-number] = s[loan-number] s [amount] 1200)} Notice that a relation on schema [loan-number] is implicitly defined by the query 172. Example Queries Find the names of all customers having a loan, an account, or both at the |bank {t s borrower( t[customer-name] = s[customer-name]) u depositor( t[customer-name] = u[customer-name]) Find the names of all customers who have a loan and an accountat the bank {t | s borrower( t[customer-name] = s[customer-name]) u depositor( t[customer-name] = u[customer-name]) 173. Example Queries Find the names of all customershaving a loan at the Perryridge branch{t | s borrower(t[customer-name] = s[customer-name] u loan(u[branch-name] = Perryridge u[loan-number] = s[loan-number]))} Find the names of all customers who have a loan at thePerryridge branch, but no account at any branch of the bank {t | s borrower( t[customer-name] = s[customer-name]u loan(u[branch-name] = Perryridge u[loan-number] = s[loan-number]))not v depositor (v[customer-name] = t[customer-name]) } 174. Example Queries Find the names of all customershaving a loan from the Perryridge {t | s loan(s[branch-name]cities they live inbranch, and the = Perryridge u borrower (u[loan-number] = s[loan-number] t [customer-name] = u[customer-name]) v customer (u[customer-name] = v[customer-name] t[customer-city] = v[customer-city])))} 175. Example Queries Find the names of all customers who have an account at all branches located in Brooklyn: = c[customer-name]) {t | c customer (t[customer.name] s branch(s[branch-city] = Brooklyn u account ( s[branch-name] = u[branch-name] s depositor ( t[customer-name] = s[customer-name] s[account-number] = u[account-number] )) )} 176. Safety of Expressions It is possible to write tuple calculusexpressions that generate infiniterelations. For example, {t | t r} results in aninfinite relation if the domain of anyattribute of relation r is infinite To guard against the problem, werestrict the set of allowableexpressions to safe expressions. An expression {t | P(t)} in the tuple 177. Domain Relational Calculus A nonprocedural query languageequivalent in power to the tuplerelational calculus Each query is an expression of the form:{ x1, x2, , xn | P(x1, x2, , xn)} x1, x2, , xn represent domain variables 178. Example Queries Find the loan-number, branch-name, and amount l, b, loans of over $1200 {l, b, a |for a loan a > 1200} Find the names of all customers who have a loan of over $1200 {c | l, b, a (c, l borrower l, b, a loan a > 1200)} Find the names of all customers who have a loan from the Perryridge branch and the loan amount: {c, a | l (c, l borrower b(l, b, a loan b = Perryridge))}or {c, a | l (c, l borrower l, Perryridge, a loan)} 179. Example Queries Find the names of all customers having a loan, an account, or both at the | l ({c, l borrower{c Perryridge branch: b,a(l, b, a loan b = Perryridge)) a(c, a depositor b,n(a, b, n account b = Perryridge))} Find the names of all customers who have an account at allbranches located in Brooklyn:{c | s, n (c, s, n customer) x,y,z(x, y, z branch y = Brooklyn) a,b(x, y, z account c,a depositor)} 180. Safety of Expressions{ x1, x2, , xn | P(x1, x2, , xn)}is safe if all of the following hold:1.All values that appear in tuples ofthe expression are values fromdom(P) (that is, the values appeareither in P or in a tupleof arelation mentioned in P).2.For every there exists subformulaof the form x (P1(x)), the 181. Result of branch-name = Perryridge(loan) 182. Loan Number and the Amount ofthe Loan 183. Names of All Customers WhoHave Either a Loan or an Account 184. Customers With An Account ButNo Loan 185. Result of borrower loan 186. Result of branch-name = Perryridge(borrower loan) 187. Result of customer-name 188. Result of the Subexpression 189. Largest Account Balance in theBank 190. Customers Who Live on the Same Street and In the SameCity as Smith 191. Customers With Both an Accountand a Loan at the Bank 192. Result of customer-name, loan-number,amount (borrowerloan) 193. Result of branch-name(customer-city =Harrison(customer account depositor)) 194. Result of branch-name( branch-city =Brooklyn(branch)) 195. Result of customer-name, branch-name(depositor account) 196. The credit-info Relation 197. Result of customer-name, (limit credit-balance) as credit-available(credit-info). 198. The pt-works Relation 199. The pt-works Relation AfterGrouping 200. Result of branch-name sum(salary) (pt-works) 201. Result of branch-namesum salary,max(salary) as max-salary (pt-works) 202. The employee and ft-worksRelations 203. The Result of employee ft-works 204. The Result of employee ft-works 205. Result of employee ft-works 206. Result of employee ft-works 207. Tuples Inserted Into loan andborrower 208. Names of All Customers WhoHave a Loan at the PerryridgeBranch 209. E-R Diagram 210. The branch Relation 211. The loan Relation 212. The borrower Relation 213. Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Joined Relations Data Definition Language Embedded SQL, ODBC and JDBC 214. Schema Used in Examples 215. Basic Structure SQL is based on set and relational operations withcertain modifications and enhancements A typical SQL query has the form:select A1, A2, ..., Anfrom r1, r2, ..., rmwhere P Ais represent attributes ris represent relations P is a predicate. This query is equivalent to the relational algebraexpression. ( P (r1 x r2 x ... x rm)) A1, A2, ..., An The result of an SQL query is a relation. 216. The select Clause The select clause list the attributes desired in the resultof a query corresponds to the projection operation of the relational algebra E.g. find the names of all branches in the loan relationselect branch-namefrom loan In the pure relational algebra syntax, the query wouldbe:branch-name(loan) NOTE: SQL does not permit the - character in names, Use, e.g., branch_name instead of branch-name in a real implementation. We use - since it looks nicer! NOTE: SQL names are case insensitive, i.e. you can usecapital or small letters. You may wish to use upper case where-ever we use bold font. 217. The select Clause (Cont.) SQL allows duplicates in relations as well asin query results. To force the elimination of duplicates, insertthe keyword distinct after select. Find the names of all branches in the loanrelations, and remove duplicates select distinct branch-name from loan The keyword all specifies that duplicates notbe removed. select all branch-name from loan 218. The select Clause (Cont.) An asterisk in the select clause denotes all attributes select * from loan The select clause can contain arithmetic expressionsinvolving the operation, +, , , and /, and operatingon constants or attributes of tuples. The query: select loan-number, branch-name,amount 100 from loanwould return a relation which is the same as the loanrelations, except that the attribute amount ismultiplied by 100. 219. The where Clause The where clause specifies conditions that theresult must satisfy corresponds to the selection predicate of the relationalalgebra. To find all loan number for loans made at thePerryridge branch with loan amounts greater than$1200. select loan-number from loan where branch-name = Perryridge andamount > 1200 Comparison results can be combined using thelogical connectives and, or, and not. Comparisons can be applied to results ofarithmetic expressions. 220. The where Clause (Cont.) SQL includes a between comparison operator E.g. Find the loan number of those loans withloan amounts between $90,000 and $100,000(that is, $90,000 and $100,000)select loan-number from loan where amount between 90000 and 100000 221. The from Clause The from clause lists the relations involved in the query corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product borrower x loan select Find the name, loan number and loan amount of all customershaving a loan at the Perryridge branch.from borrower, loanselect customer-name, borrower.loan-number, amountfrom borrower, loanwhere borrower.loan-number = loan.loan-number and branch-name = Perryridge 222. The Rename Operation The SQL allows renaming relations and attributes using the as clause:old-name as new-name Find the name, loan number and loan select customer-name, borrower.loan-number as loan-id, amountamount of all customers; rename the from borrower, loan column name loan-number as loan-id. where borrower.loan-number = loan.loan-number 223. Tuple Variables Tuple variables are defined in the fromclause via the use of the as clause. Find the customer names and theirloan numbers for T.loan-number, S.amountselect customer-name, all customers havingfrom borrower as T, loan as Sa loan at some branch.where T.loan-number = S.loan-number Find the names of all branches that have greater assets thansome branch located in Brooklyn. select distinct T.branch-name from branch as T, branch as S where T.assets > S.assets and S.branch-city = Brooklyn 224. String Operations SQL includes a string-matching operator for comparisons oncharacter strings. Patterns are described using two specialcharacters: percent (%). The % character matches anysubstring. underscore (_). The _ character matchesany character. Find the names of all customers whose street includes thesubstring Main. select customer-name from customer where customer-street like %Main% Match the name Main% like Main% escape SQL supports a variety of string operations such as concatenation (using ||) 225. Ordering the Display of Tuples List in alphabetic order the names ofall customers having a loan inPerryridge branchselect distinct customer-namefrom borrower, loanwhere borrower loan-number =loan.loan-number andbranch-name = Perryridgeorder by customer-name We may specify desc for descendingorder or asc for ascending order, foreach attribute; ascending order is the 226. Duplicates In relations with duplicates, SQL candefine how many copies of tuplesappear in the result. Multiset versions of some of the relational algebra operators given multiset relations r1 and r2:1. (r1): If there are c1 copies of tuple t1 inr1, and t1 satisfies selections ,, thenthere are c1 copies of t1 in(r1).2.A(r1): For each copy of tuple t1 in r1,there is a copy of tuple A(t1) in A(r1) 227. Duplicates (Cont.) Example: Suppose multisetrelations r1 (A, B) and r2 (C) are asfollows:r1 = {(1, a) (2,a)} r2 = {(2),(3), (3)} Then B(r1) would be {(a), (a)}, whileB(r1) x r2 would be {(a,2), (a,2), (a,3), (a,3), (a,3),(a,3)} SQL duplicate semantics: select A1,, A2, ..., An 228. Set Operations The set operations union, intersect,and except operate on relations andcorrespond to the relational algebraoperations Each of the above operationsautomatically eliminates duplicates; toretain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r 229. Set Operations Find all customers who have a loan, anaccount, or both: from depositor) (select customer-name union (select customer-name from borrower) Find all customers who have both a loan and an account. (select customer-name from depositor) intersect (select customer-name from borrower) Find all customers who have an account but no loan.(select customer-name from depositor)except(select customer-name from borrower) 230. Aggregate Functions These functions operate on themultiset of values of a column of arelation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values 231. Aggregate Functions (Cont.) Find the average account balance at the Perryridge branch. select avg (balance)from accountwhere branch-name = Perryridge Find the number of tuples in the customer relation.select count (*)from customer Find the number of depositors in the bank.select count (distinct customer-name)from depositor 232. Aggregate Functions Group By Find the number of depositors for each branch.select branch-name, count (distinct customer-name)from depositor, accountwhere depositor.account-number = account.account-numbergroup by branch-nameNote: Attributes in select clause outside of aggregate functions mustappear in group by list 233. Aggregate Functions HavingClause Find the names of all branches where the average account balance is more than $1,200.select branch-name, avg (balance)from accountgroup by branch-namehaving avg (balance) > 1200Note: predicates in the having clause are applied after the formation of groups whereas predicates in the whereclause are applied before forming groups 234. Null Values It is possible for tuples to have a nullvalue, denoted by null, for some oftheir attributes null signifies an unknown value orthat a value does not exist. The predicate is null can be used tocheck for null values. E.g. Find all loan number which appear in the loan relation with null values for amount.select loan-numberfrom loan 235. Null Values and Three ValuedLogic Any comparison with null returns unknown E.g. 5 < null or null null or null = null Three-valued logic using the truth value unknown: OR: (unknown or true) = true, (unknownor false) = unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown,(false and unknown) = false, 236. Null Values and Aggregates Total all loan amounts select sum (amount) from loan Above statement ignores null amounts result is null if there is no non-nullamount All aggregate operations except count(*)ignore tuples with null values on theaggregated attributes. 237. Nested Subqueries SQL provides a mechanism for thenesting of subqueries. A subquery is a select-from-whereexpression that is nested withinanother query. A common use of subqueries is toperform tests for set membership, setcomparisons, and set cardinality. 238. Example Query Find all customers who have both anaccount and a loan at the bank. select distinct customer-name from borrower where customer-name in (select customer-namefrom depositor) Find all customers who have a loan at the bank but do not havean account at the bankselect distinct customer-namefrom borrowerwhere customer-name not in (select customer-namefrom depositor) 239. Example Query Find all customers who have both anaccount and a loan at the Perryridgebranch select distinct customer-namefrom borrower, loanwhere borrower.loan-number = loan.loan-number andbranch-name = Perryridge and (branch-name, customer-name) in(select branch-name, customer-namefrom depositor, accountwhere depositor.account-number =account.account-number) Note: Above query can be written in a much simpler manner. Theformulation above is simply to illustrate SQL features.(Schema used in this example) 240. Set Comparison Find all branches that have greaterassets than some branch located inBrooklyn.from branch as T, branch as Sselect distinct T.branch-namewhere T.assets > S.assets andS.branch-city = Brooklyn Same query using > some clauseselect branch-namefrom branchwhere assets > some (select assetsfrom branchwhere branch-city = Brooklyn) 241. Definition of Some Clause F some rtr s.t. (F t) Where 0 can be: (5< some 5 ) = true(read: 5 < some tuple in the relation)60 (5< some 5 ) = false0(5 = some 5 ) = true0(5 some 5 ) = true (since 0 5)(= some) inHowever, ( some) not in 242. Definition of all Clause F all r tr (F 0t) (5< all 5 ) = false 66 (5< all 10) = true 4(5 = all 5 ) = false 4(5 all 6 ) = true (since 5 4 and 5 6)( all) not inHowever, (= all)in 243. Example Query Find the names of all branches that have greater assets than all branches located in Brooklyn. select branch-namefrom branchwhere assets > all (select assets from branch where branch-city = Brooklyn) 244. Test for Empty Relations The exists construct returns the valuetrue if the argument subquery isnonempty. exists r r not exists r r= 245. Example Query Find all customers who have an account at all branches located in Brooklyn. select distinct S.customer-namefrom depositor as Swhere not exists ( (select branch-name from branch where branch-city = Brooklyn)except (select R.branch-name from depositor as T, account as R where T.account-number = R.account-number and S.customer-name = T.customer-name)) (Schema used in this example) Note that X Y = X Y Note: Cannot write this query using = all and its variants 246. Test for Absence of Duplicate Tuples The unique construct tests whether asubquery has any duplicate tuples inits result. Find all customers who have at mostone account at the Perryridge branch.select T.customer-namefrom depositor as Twhere unique ( select R.customer-name from account, depositor as R where T.customer-name =R.customer-name and 247. Example Query Find all customers who have at leasttwo accounts at the Perryridge branch. select distinct T.customer-name from depositor T where not unique ( select R.customer-name from account, depositor as R where T.customer-name = R.customer-name and R.account-number = account.account-number and account.branch-name = Perryridge)(Schema used in this example) 248. Views Provide a mechanism to hide certain data from the view of certain users. To create a view we use the command: create view v as where: is any legal expression The view name is represented by v 249. Example Queries A view consisting of branches and their customerscreate view all-customer as (select branch-name, customer-namefrom depositor, accountwhere depositor.account-number = account.account-number) union(select branch-name, customer-name from borrower, loan where borrower.loan-number = loan.loan-number) Find all customers of the Perryridge branchselect customer-namefrom all-customerwhere branch-name = Perryridge 250. Derived Relations Find the average account balance ofthose branches where the averageaccount balance is greater than$1200. select branch-name, avg-balance from (select branch-name, avg (balance)from accountgroup by branch-name)as result (branch-name, avg- balance) 251. With Clause With clause allows views to be definedlocally to a query, rather than globally.Analogous to procedures in aprogramming language. Find all accounts with the maximum balance with max-balance(value) asselect max (balance)from account select account-number 252. Complex Query using With Clausebranches where the totalFind all account deposit is greater than the average of the total account depositswith branch-total (branch-name, value) as at all branches. sum (balance) select branch-name, from account group by branch-namewith branch-total-avg(value) as select avg (value) from branch-totalselect branch-namefrom branch-total, branch-total-avgwhere branch-total.value >= branch-total-avg.value 253. Modification of the Database Deletion account records at the Delete allPerryridge branch delete from account where branch-name =Perryridge Delete all accounts at every branchlocated in Needham city.delete from accountwhere branch-name in (select branch- name from branch where branch-city = 254. Example Query Delete the record of all accounts with balances below the average at the bank.from accountdeletewhere balance < (select avg (balance) from account) Problem: as we delete tuples from deposit, the average balancechanges Solution used in SQL: 1.First, compute avg balance and find all tuples to delete 2.Next, delete all tuples found above (without recomputing avg or retesting the tuples) 255. Modification of the Database Insertion Add a new tuple to account insert into account values (A-9732,Perryridge,1200)or equivalentlyinsert into account (branch-name,balance, account-number) values (Perryridge, 1200, A-9732) 256. Modification of the Database Insertion a gift for all loan customers Provide as of the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings accountinsert into accountselect loan-number, branch-name, 200from loanwhere branch-name = Perryridgeinsert into depositorselect customer-name, loan-number 257. Modification of the Database Updates Increase all accounts with balancesover $10,000 by 6%, all otheraccounts receive 5%. Write two update statements: update account set balance = balance 1.06 where balance > 10000 update account set balance = balance 1.05 where balance 10000 258. Case Statement for ConditionalUpdates Same query as before: Increase allaccounts with balances over $10,000by 6%, all other accounts receive 5%.update accountset balance = case when balance = 0))primary key declaration on an attribute automatically ensures not null in SQL-92 onwards, needs to be explicitly stated in SQL-89 272. Drop and Alter Table Constructs The drop table command deletes allinformation about the droppedrelation from the database. The alter table command is used toadd attributes to an existingrelation. alter table r add A Dwhere A is the name of theattribute to be added to relation rand D is the domain of A. All tuples in the relation are assigned null as the value for the new attribute. 273. Embedded SQL The SQL standard defines embeddingsof SQL in a variety of programminglanguages such as Pascal, PL/I,Fortran, C, and Cobol. A language to which SQL queries areembedded is referred to as a hostlanguage, and the SQL structurespermitted in the host languagecomprise embedded SQL. The basic form of these languages 274. Example QueryFrom within a host language, find the names and cities ofcustomers with more than the variable amount dollars in someaccount. Specify the query in SQL and declare acursor for it EXEC SQL declare c cursor for select customer-name, customer-city from depositor, customer, account where depositor.customer-name =customer.customer-name and depositor account-number = 275. Embedded SQL (Cont.) The open statement causes the queryto be evaluated EXEC SQL open c END-EXEC The fetch statement causes the valuesof one tuple in the query result to beplaced on host language variables.EXEC SQL fetch c into :cn, :cc END-EXECRepeated calls to fetch get successivetuples in the query result 276. Updates Through Cursors Can update tuples fetched by cursor by declaring that the cursor is for update declare c cursor for select * from account where branch-name = Perryridge for update To update tuple at the current location of cursor update account set balance = balance + 100 where current of c 277. Dynamic SQL Allows programs to construct andsubmit SQL queries at run time. Example of the use of dynamic SQLfrom within a C program. char * sqlprog = update account set balance = balance * 1.05where account-number = ? EXEC SQL prepare dynprog from :sqlprog; 278. ODBC Open DataBase Connectivity(ODBC) standard standard for application program to communicate with a database server. application program interface (API) to open a connection with a database, send queries and updates, get back results. Applications such as GUI,spreadsheets, etc. can use ODBC 279. ODBC (Cont.) Each database system supporting ODBC provides a "driver"library that must be linked with the client program. When client program makes an ODBC API call, the code in thelibrary communicates with the server to carry out the requestedaction, and fetch results. ODBC program first allocates an SQL environment, then adatabase connection handle. Opens database connection using SQLConnect(). Parameters forSQLConnect: connection handle, the server to which to connect the user identifier, password Must also specify types of arguments: SQL_NTS denotes previous argument is anull-terminated string. 280. ODBC Code int ODBCexample() { RETCODE error; HENV env;/* environment */ HDBC conn; /* database connection */ SQLAllocEnv(&env); SQLAllocConnect(env, &conn); SQLConnect(conn, "aura.bell-labs.com", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); { . Do actual work } SQLDisconnect(conn); SQLFreeConnect(conn); 281. ODBC Code (Cont.) Program sends SQL commands to the database by usingSQLExecDirect Result tuples are fetched using SQLFetch() SQLBindCol() binds C language variables to attributes of the queryresult When a tuple is fetched, its attribute values are automatically stored in corresponding C variables. Arguments to SQLBindCol() ODBC stmt variable, attribute position in query result The type conversion from SQL to C. The address of the variable. For variable-length types like character arrays, The maximum length of the variable Location to store actual length when a tuple is fetched. 282. ODBC Code (Cont.) Main body of program char branchname[80]; float balance; int lenOut1, lenOut2; HSTMT stmt; SQLAllocStmt(conn, &stmt); char * sqlquery = "select branch_name, sum (balance) from account group by branch_name"; error = SQLExecDirect(stmt, sqlquery, SQL_NTS); if (error == SQL_SUCCESS) { SQLBindCol(stmt, 1, SQL_C_CHAR, 283. More ODBC Features Prepared Statement SQL statement prepared: compiled at thedatabase Can have placeholders: E.g. insert intoaccount values(?,?,?) Repeatedly executed with actual values for theplaceholders Metadata features finding all the relations in the database and finding the names and types of columns of a query result or a relation in the database. By default, each SQL statement is treated as a separate transactionthat is 284. ODBC Conformance Levels Conformance levels specify subsets of the functionality defined by the standard. Core Level 1 requires support for metadataquerying Level 2 requires ability to send andretrieve arrays of parameter values andmore detailed catalog information. SQL Call Level Interface (CLI) standard similar to ODBC interface, but with some minor differences. 285. JDBC JDBC is a Java API for communicatingwith database systems supporting SQL JDBC supports a variety of features forquerying and updating data, and forretrieving query results JDBC also supports metadata retrieval,such as querying about relationspresent in the database and thenames and types of relation attributes Model for communicating with the 286. JDBC static void JDBCexample(String dbid, publicCodeString userid, String passwd){try {Class.forName ("oracle.jdbc.driver.OracleDriver");Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@aura.bell-labs.com:2000:bankdb", userid, passwd);Statement stmt = conn.createStatement(); Do Actual Work .stmt.close();conn.close();} 287. JDBC Code (Cont.) Update to databasetry {stmt.executeUpdate( "insert into accountvalues (A-9732, Perryridge, 1200)");} catch (SQLException sqle) {System.out.println("Could not insert tuple." + sqle);} Execute query and fetch and print resultsResultSet rset = stmt.executeQuery( "select branch_name, avg(balance) 288. JDBC Code Details Getting result fields: rs.getString(branchname) and rs.getString(1) equivalent if branchname is the first argument of select result. Dealing with Null valuesint a = rs.getInt(a);if (rs.wasNull()) Systems.out.println(Got null value); 289. Prepared Statement Prepared statement allows queries to be compiled and executed multiple times with different arguments PreparedStatement pStmt = conn.prepareStatement( insert into account values(?,?,?)); pStmt.setString(1, "A-9732");pStmt.setString(2, "Perryridge");pStmt.setInt(3, 1200);pStmt.executeUpdate();pStmt.setString(1, "A-9733");pStmt.executeUpdate(); 290. Other SQL Features SQL sessions client connects to an SQL server,establishing a session executes a series of statements disconnects the session can commit or rollback the workcarried out in the session An SQL environment containsseveral components, including auser identifier, and a schema,which identifies which of severalschemas a session is using. 291. Schemas, Catalogs, and Environments for namingThree-level hierarchyrelations. Database contains multiple catalogs each catalog can contain multiple schemas SQL objects such as relations and views are contained within a schema e.g. catalog5.bank-schema.account Each user has a default catalog andschema, and the combination isunique to the user. Default catalog and schema are set upfor a connection 292. Procedural Extensions and Stored Procedures SQL provides a module language permits definition of procedures in SQL,with if-then-else statements, for and whileloops, etc. more in Chapter 9 Stored Procedures Can store procedures in the database then execute them using the call statement permit external applications to operate on the database without knowing about internal details These features are covered in Chapter 293. Transactions in JDBC As with ODBC, each statement getscommitted automatically in JDBC To turn off auto commit useconn.setAutoCommit(false); To commit or abort transactions use conn.commit() or conn.rollback() To turn auto commit on again, useconn.setAutoCommit(true); 294. Procedure and Function Calls inJDBC provides a class CallableStatement JDBC which allows SQL stored procedures/functions to be invoked. CallableStatement cs1 = conn.prepareCall( {call proc (?,?)} ) ; CallableStatement cs2 = conn.prepareCall( {? = call func (?,?)} ); 295. Result Set MetaData The class ResultSetMetaData providesinformation about all the columns ofthe ResultSet. Instance of this class is obtained bygetMetaData( ) function of ResultSet. Provides Functions for getting numberof columns, column name, type,precision, scale, table from which thecolumn is derived etc. ResultSetMetaData rsmd =rs.getMetaData ( ); 296. Database Meta Data The class DatabaseMetaData provides information about databaserelations Has functions for getting all tables, all columns of the table, primarykeys etc. E.g. to print column names and types of a relationDatabaseMetaData dbmd = conn.getMetaData( ); ResultSet rs = dbmd.getColumns( null, BANK-DB, account,% ); //Arguments: catalog, schema-pattern, table-pattern,column-pattern // Returns: 1 row for each column, with several attributessuch as //COLUMN_NAME, TYPE_NAME, etc.while ( rs.next( ) ) {System.out.println( rs.getString(COLUMN_NAME) , rs.getString(TYPE_NAME);} There are also functions for getting information such as 297. Application Architectures Applications can be built using one oftwo architectures Two tier model Application program running at user site directly uses JDBC/ODBC to communicate with the database Three tier model Users/programs running at user sites communicate with an application server. The application server in turn communicates with the database 298. Two-tier Model E.g. Java code runs at client site anduses JDBC to communicate with thebackend server Benefits: flexible, need not be restricted to predefined queries Problems: Security: passwords available at client site,all database operation possible More code shipped to client Not appropriate across organizations, or inlarge ones like universities 299. Three Tier Model CGI Program Application/HTTPJDBCDatabase Servlets ServerServerHTTP/Application Specific Protocol Network Client Client Client 300. Three-tier Model (Cont.) E.g. Web client + Java Servlet usingJDBC to talk with database server Client sends request over http orapplication-specific protocol Application or Web server receivesrequest Request handled by CGI program orservlets Security handled by application atserver Better security 301. The loan and borrowerRelations 302. The Result of loan inner joinborrower on loan.loan-number =borrower.loan-number 303. The Result of loan left outer joinborrower on loan-number 304. The Result of loan natural innerjoin borrower 305. Join Types and Join Conditions 306. The Result of loan natural rightouter join borrower 307. The Result of loan full outer joinborrower using(loan-number) 308. SQL Data Definition for Part of the Bank Database 309. Chapter 5: Other RelationalLanguages Query-by-Example (QBE) Datalog 310. Query-by-Example (QBE) Basic Structure Queries on One Relation Queries on Several Relations The Condition Box The Result Relation Ordering the Display of Tuples Aggregate Operations Modification of the Database 311. QBE Basic Structure A graphical query language which isbased (roughly) on the domainrelational calculus Two dimensional syntax systemcreates templates of relations that arerequested by users Queries are expressed by example 312. QBE Skeleton Tables for the BankExample 313. QBE Skeleton Tables (Cont.) 314. Queries on One the Perryridge branch. Find all loan numbers atRelation _x is a variable (optional; can be omitted in above query) P. means print (display) duplicates are removed by default To retain duplicates use P.ALL 315. Queries on One Relation (Cont.) Display full details of all loans Method 1: P._xP._y P._z Method 2: Shorthand notation 316. Queries on One Relation (Cont.) Find the loan number of all loans with a loan amount of more than $700 Find names of all branches that are not located in Brooklyn 317. Queries on One Relation (Cont.) Find the loan numbers of all loans made jointly to Smith and Jones. Find all customers who live in the same city as Jones 318. Queries on Several Relations Find the names of all customers who have a loan from the Perryridge branch. 319. Queries on Several Relations (Cont.) names of all customers whoFind the have both an account and a loan at the bank. 320. Negation in QBE Find the names of all customers whohave an account at the bank, but donot have a loan from the bank. means there does not exist 321. Negation in QBE (Cont.) Find all customers who have atleast two accounts. means not equal to 322. The Condition Box Allows the expression of constraintson domain variables that are eitherinconvenient or impossible toexpress within the skeleton tables. Complex conditions can be used incondition boxes E.g. Find the loan numbers of allloans made to Smith, to Jones, or toboth jointly 323. Condition Box (Cont.) QBE supports an interesting syntax forexpressing alternative values 324. Condition Box (Cont.) a balanceFind all account numbers withbetween $1,300 and $1,500Find all account numbers with a balance between $1,300 and $2,000 but not exactly $1,500. 325. Condition Box (Cont.) Find all branches that have assetsgreater than those of at least onebranch located in Brooklyn 326. The Result Relation Find the customer-name, account-number, and balance for alllcustomers who have an account at thePerryridge branch. We need to: Join depositor and account. Project customer-name, account-number and balance. To accomplish this we: Create a skeleton table, called result, with attributes customer-name, account- 327. The Result Relation (Cont.) The resulting query is: 328. Ordering the Display of Tuples AO = ascending order; DO = descendingorder. E.g. list in ascending alphabetical orderall customers who have an account atthe bank When sorting on multiple attributes, the sorting order is specified by including with each sort operator (AO or DO) an 329. Aggregate Operations The aggregate operators areAVG, MAX, MIN, SUM, and CNT The above operators must bepostfixed with ALL(e.g., SUM.ALL.or AVG.ALL._x) toensure that duplicates are noteliminated. E.g. Find the total balance of all theaccounts maintained at thePerryridge branch. 330. Aggregate Operations (Cont.) UNQ is used to specify that we want toeliminate duplicates Find the total number of customershaving an account at the bank. 331. Querythe average balance at each Find Examples branch. The G in P.G is analogous to SQLs group by construct The ALL in the P.AVG.ALL entry in the balance columnensures that all balances are considered To find the average account balance at only those brancheswhere the average account balance is more than $1,200, wesimply add the condition box: 332. Query Example Find all customers who have anaccount at all branches located inBrooklyn. Approach: for each customer, find thenumber of branches in Brooklyn at whichthey have accounts, and compare withtotal number of branches in Brooklyn QBE does not provide subquery In the query on the next page CNT.UNQ.ALL._w specifies the number of distinct branches in Brooklyn.Note: The variable _w is soconnected above tasksthe query tofunctionality, not both to other variables in havebe combined in a single query. CNT.UNQ.ALL._z specifies the number of distinct branches in Brooklyn atwhich customer x has an account. Can be done for this query, but there arequeries that require subqueries and cannotbe expressed in QBE always be done. 333. Query Example (Cont.) 334. Modification of the Database Deletion Deletion of tuples from a relation isexpressed by use of a D. command. Inthe case where we delete informationin only some of the columns, nullvalues, specified by , are inserted. Delete customer Smith Delete the branch-city value of the 335. Deletion Query Examples Delete all loans with a loan amount between $1300 and $1500. For consistency, we have to delete information from loan and borrower tables 336. Deletion Query Examples (Cont.) Delete all accounts at branches located in Brooklyn. 337. Modification of the Database Insertion Insertion is done by placing the I.operator in the query expression. Insert the fact that account A-9732 at the Perryridge branch hasa balance of $700. 338. Modification of the Database Insertion (Cont.) Provide as a gift for all loan customers of the Perryridge branch, anew $200 savings account for every loan account they have, withthe loan number serving as the account number for the newsavings account. 339. Modification of the Database Updates Use the U. operator to change a valuein a tuple without changing all valuesin the tuple. QBE does not allow usersto update the primary key fields. Update the asset value of thePerryridge branch to $10,000,000. Increase all balances by 5 percent. 340. Microsoft Access QBE Microsoft Access supports a variant ofQBE called Graphical Query By Example(GQBE) GQBE differs from QBE in the followingways Attributes of relations are listed vertically,one below the other, instead ofhorizontally Instead of using variables, lines (links)between attributes are used to specify thattheir values should be the same. Links are added automatically on the basis ofattribute name, and the user can then add 341. An Example Query in Microsoft Access QBE Example query: Find the customer-name, account-number and balance for all accounts at the Perryridge branch 342. An Aggregation Query in Access QBE Find the name, street and city of all customers who have morethan one account at the bank 343. Aggregation in Access QBE The row labeled Total specifies which attributes are group by attributes which attributes are to be aggregatedupon (and the aggregate function). For attributes that are neither group bynor aggregated, we can still specifyconditions by selecting where in the Totalrow and listing the conditions below As in SQL, if group by is used, only group by attributes and aggregate results can be output 344. Datalog Basic Structure Syntax of Datalog Rules Semantics of Nonrecursive Datalog Safety Relational Operations in Datalog Recursion in Datalog The Power of Recursion 345. Basic Structure Prolog-like logic-based language thatallows recursive queries; based onfirst-order logic. A Datalog program consists of a set ofrules that define views. Example: define a view relation v1containing account numbers andbalances for accounts at the Perryridgebranch with a balance of over $700. v1(A, B) : account(A, Perryridge,B), B > 700. Retrieve the balance of account 346. Example Queries Each rule defines a set of tuples that a view relation must contain. E.g. v1(A, B) : account(A, Perryridge, B), B > 700is read asfor all A, Bif (A, Perryridge, B) account and B > 700then (A, B) v1 The set of tuples in a view relation isthen defined as the union of all thesets of tuples defined by the rules forthe view relation. Example: interest-rate(A, 5) : account(A, N, 347. Negation in Datalog Define a view relation c that containsthe names of all customers who havea deposit but no loan at the bank:c(N) : depositor(N, A), not is-borrower(N).is-borrower(N) :borrower (N,L). NOTE: 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. 348. Named Attribute Notation Datalog rules use a positional notation,which is convenient for relations with asmall number of attributes It is easy to extend Datalog to supportnamed attributes. E.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. 349. Formal Syntax and Semantics ofDatalog We formally define the syntax andsemantics (meaning) of Datalogprograms, in the following steps1. We define the syntax of predicates, and then the syntax of rules2. We define the semantics of individual rules3. We define the semantics of non-recursive programs, based on a layering of rules4. It is possible to write rules that can generate an infinite number of tuples in 350. Syntax of Datalog Rules A positive literal has the formp(t1, t2 ..., tn) p is the name of a relation with nattributes each ti is either a constant or variable A negative literal has the formnot p(t1, t2 ..., tn) Comparison operations are treated aspositive predicates E.g. X > Y is treated as a predicate >(X,Y) > is conceptually an (infinite) relation that contains all pairs of values such that 351. Syntax of Datalog Rules (Cont.) Rules are built out of literals and havethe form:p(t1, t2, ..., tn) : L1, L2, ..., Lm. headbody each of the Lis is a literal head the literal p(t1, t2, ..., tn) body the rest of the literals A fact is a rule with an empty body, written in the form: 352. Semantics of a Rule A ground instantiation of a rule (orsimply instantiation) is the result ofreplacing each variable in the rule by some constant. Eg. Rule defining v1 v1(A,B) : account (A,Perryridge, B), B > 700. An instantiation above rule: v1(A-217, 750) :account(A-217, Perryridge, 750), 750 > 700. The body of rule instantiation R is satisfied in a set of facts (database instance) l if 353. Semantics of a Rule (Cont.) We define the set of facts that can beinferred from a given set of facts l usingrule R as:infer(R, l) = {p(t1, ..., tn) | there is aground instantiation R of R where p(t1, ..., tn ) isthe head of R, and the body of R issatisfied in l } Given an set of rules= {R1, R2, ..., Rn},we defineinfer( , l) = infer(R , l) infer(R , l) 354. Layering of Rules Define the interest on eachaccount in Perryridge interest(A, l) : perryridge-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. interest-rate(A,6) :account(N, A,B), B >= 10000. Layering of the view relations 355. Layering Rules (Cont.)Formally: A relation is a layer 1 if all relations used in the bodies of rules defining it are stored in the database. A 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. A relation p is in layer i + 1 if it is not in layers 1, 2, ..., i all relations used in the bodies of rules 356. Semantics of a ProgramLet the layers in a given program be 1, 2, ..., n. Let i denote theset of all rules defining view relations in layer i. Define I0 = set of facts stored in the databa