DOC PREVIEW
UW CSE 444 - Aggregation

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

AggregationGrouping and AggregationHAVING ClauseModifying the DatabaseMore Interesting InsertionsDeletionsUpdatesData Definition in SQLData Types in SQLCreating TablesDeleting or Modifying a TableDefault ValuesIndexesCreating IndexesDefining ViewsA Different ViewUpdating ViewsNon-Updatable ViewsAggregationSELECT Sum(price)FROM ProductWHERE manufacturer=“Toyota”SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT, all aggregations apply to a single attributeSELECT Count(*)FROM PurchaseGrouping and AggregationUsually, we want aggregations on certain parts of the relation.Find how much we sold of every productSELECT product, Sum(price)FROM Product, PurchaseWHERE Product.name = Purchase.productGROUPBY Product.name1. Compute the relation (I.e., the FROM and WHERE).2. Group by the attributes in the GROUPBY3. Select one tuple for every group (and apply aggregation)SELECT can have (1) grouped attributes or (2) aggregates.HAVING ClauseSELECT product, Sum(price)FROM Product, PurchaseWHERE Product.name = Purchase.productGROUPBY Product.nameHAVING Count(buyer) > 100Same query, except that we consider only products that hadat least 100 buyers.HAVING clause contains conditions on aggregates.Modifying the DatabaseWe have 3 kinds of modifications: insertion, deletion, update. Insertion: general form -- INSERT INTO R(A1,…., An) VALUES (v1,…., vn)Insert a new purchase to the database: INSERT INTO Purchase(buyer, seller, product, store) VALUES (Joe, Fred, wakeup-clock-espresso-machine, “The Sharper Image”)If we don’t provide all the attributes of R, they will be filled with NULL. We can drop the attribute names if we’re providing all of them in order.More Interesting InsertionsINSERT INTO PRODUCT(name) SELECT DISTINCT product FROM Purchase WHERE product NOT IN (SELECT name FROM Product)The query replaces the VALUES keyword.Note the order of querying and inserting.DeletionsDELETE FROM PURCHASEWHERE seller = “Joe” AND product = “Brooklyn Bridge”Factoid about SQL: there is no way to delete only a single occurrence of a tuple that appears twice in a relation.UpdatesUPDATE PRODUCTSET price = price/2WHERE Product.name IN (SELECT product FROM Sales WHERE Date = today);Data Definition in SQLSo far, SQL operations on the data.Data definition: defining the schema.• Create tables• Delete tables• Modify table schemaBut first: Define data types.Finally: define indexes.Data Types in SQL• Character strings (fixed of varying length)• Bit strings (fixed or varying length)• Integer (SHORTINT)• Floating point• Dates and timesDomains will be used in table declarations. To reuse domains: CREATE DOMAIN address AS VARCHAR(55)Creating TablesCREATE TABLE Person( name VARCHAR(30), social-security-number INTEGER, age SHORTINT, city VARCHAR(30), gender BIT(1), Birthdate DATE );Deleting or Modifying a TableDeleting: DROP Person; Altering: ALTER TABLE Person ADD phone CHAR(16); ALTER TABLE Person DROP age;Default ValuesThe default of defaults: NULLSpecifying default values: CREATE TABLE Person( name VARCHAR(30), social-security-number INTEGER, age SHORTINT DEFAULT 100, city VARCHAR(30) DEFAULT “Seattle”, gender CHAR(1) DEFAULT “?”, Birthdate DATEIndexesREALLY important to speed up query processing time.Suppose we have a relation Person (name, social security number, age, city)An index on “social security number” enables us to fetch a tuple for a given ssn ve ry efficiently (not have to scan the whole relation).The problem of deciding which indexes to put on the relations isvery hard! (it’s called: physical database design).Creating IndexesCREATE INDEX ssnIndex ON Person(social-security-number)Indexes can be created on more than one attribute:CREATE INDEX doubleindex ON Person (name, social-security-number)Why not create indexes on everything?Defining ViewsViews are relations, except that they are not physically stored.They are used mostly in order to simplify complex queries andto define conceptually different views of the database to differentclasses of users.View: purchases of telephony products:CREATE VIEW telephony-purchases AS SELECT product, buyer, seller, store FROM Purchase, Product WHERE Purchase.product = Product.name AND Product.category = “telephony”A Different ViewCREATE VIEW Seattle-view AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyerWe can later use the views: SELECT name, store FROM Seattle-view, Product WHERE Seattle-view.product = Product.name AND Product.category = “shoes”What’s really happening when we query a view??Updating ViewsHow can I insert a tuple into a table that doesn’t exist?CREATE VIEW bon-purchase AS SELECT store, seller, product FROM Purchase WHERE store = “The Bon Marche”If we make the following insertion: INSERT INTO bon-purchase VALUES (“the Bon Marche”, Joe, “Denby Mug”)We can simply add a tuple (“the Bon Marche”, Joe, NULL, “Denby Mug”)to relation Purchase.Non-Updatable ViewsCREATE VIEW Seattle-view AS SELECT seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyerHow can we add the following tuple to the view? (Joe, “Shoe Model 12345”, “Nine


View Full Document

UW CSE 444 - Aggregation

Documents in this Course
XML

XML

48 pages

SQL

SQL

25 pages

SQL

SQL

42 pages

Recovery

Recovery

30 pages

SQL

SQL

36 pages

Indexes

Indexes

35 pages

Security

Security

36 pages

Wrap-up

Wrap-up

6 pages

SQL

SQL

37 pages

More SQL

More SQL

48 pages

SQL

SQL

35 pages

XML

XML

46 pages

Triggers

Triggers

26 pages

Load more
Download Aggregation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Aggregation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Aggregation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?