Lecture 14: SQL Wednesday, October 31, 2001.

Slides:



Advertisements
Similar presentations
1 Lecture 5: SQL Schema & Views. 2 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL)
Advertisements

SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
1 Lecture 03: Advanced SQL. 2 Outline Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views Reading:
1 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL) Data types: Defines the types. Data.
SQL April 25 th, Agenda Grouping and aggregation Sub-queries Updating the database Views More on views.
1 Lecture 06: SQL Friday, January 14, Outline Indexes Defining Views (6.7) Constraints (Chapter 7) We begin E/R diagrams (Chapter 2)
1 Lecture 05: SQL Wednesday, October 8, Outline Outer joins (6.3.8) Database Modifications (6.5) Defining Relation Schema in SQL (6.6) Indexes.
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
1 Lecture 3: More SQL Friday, January 9, Agenda Homework #1 on the web site today. Sign up for the mailing list! Next Friday: –In class ‘activity’
Integrity Constraints An important functionality of a DBMS is to enable the specification of integrity constraints and to enforce them. Knowledge of integrity.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1 Lecture 4: More SQL Monday, January 13th, 2003.
1 Lecture 7: End of Normal Forms Outerjoins, Schema Creation and Views Wednesday, January 28th, 2004.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
SQL (almost end) April 26 th, Agenda HAVING clause Views Modifying views Reusing views.
SQL April 22 th, Agenda Union, intersections Sub-queries Modifying the database Views Modifying views Reusing views.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
1 SQL cont.. 2 Outline Unions, intersections, differences (6.2.5, 6.4.2) Subqueries (6.3) Aggregations (6.4.3 – 6.4.6) Hint for reading the textbook:
More SQL: Complex Queries, Triggers, Views, and Schema Modification UMM AL QURA UNIVERSITY College of Computer Dr. Ali Al Najjar 1.
1 Lecture 6: Views Friday, January 17th, Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
SQL Overview Structured Query Language
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
1 Lecture 5: Outerjoins, Schema Creation and Views Wednesday, January 15th, 2003.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
1 Lecture 03: SQL Monday, January 9, Project t/Default.aspxhttp://iisqlsrv.cs.washington.edu/444/Projec.
1 Lecture 06 Data Modeling: E/R Diagrams Wednesday, January 18, 2006.
1 Lecture 05: SQL Wednesday, October 8, Outline Database Modifications (6.5) Defining Relation Schema in SQL (6.6) Indexes Defining Views (6.7)
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
CPSC-310 Database Systems
SQL Query Getting to the data ……..
SQL.
Lecture 05: SQL Wednesday, January 12, 2005.
Cours 7: Advanced SQL.
Lecture 04: SQL Monday, January 10, 2005.
CS 480: Database Systems Lecture 13 February 13,2013.
Modifying the Database
Database Systems Subqueries, Aggregation
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Introduction to Database Systems CSE 444 Lecture 04: SQL
Introduction to Database Systems CSE 444 Lecture 03: SQL
SQL OVERVIEW DEFINING A SCHEMA
Lecture 4: Advanced SQL – Part II
Introduction to SQL Wenhao Zhang October 5, 2018.
Introduction to Database Systems CSE 444 Lecture 03: SQL
Lecture 4: Updates, Views, Constraints and Other Fun Features
Building a Database Application
SQL Introduction Standard language for querying and manipulating data
Lecture 05 Views, Constraints
Lecture 12: SQL Friday, October 20, 2000.
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Lecture 4: SQL Thursday, January 11, 2001.
Lectures 6: Introduction to SQL 5
Integrity Constraints
Lecture 3 Monday, April 8, 2002.
Lecture 06: SQL Monday, October 11, 2004.
Lecture 4: SQL Wednesday, April 10, 2002.
Lecture 03: SQL Friday, October 3, 2003.
CSE544 SQL Monday, April 5, 2004.
Lecture 04: SQL Monday, October 6, 2003.
Lecture 05: SQL Wednesday, October 9, 2002.
Presentation transcript:

Lecture 14: SQL Wednesday, October 31, 2001

Outline Defining Relation Schema in SQL (5.7) Defining Views (5.8) Aggregation (5.5)

Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL) Data types: Defines the types. Data definition: defining the schema. Create tables Delete tables Modify table schema Indexes: to improve peformance

Data Types in SQL Character strings (fixed of varying length) Bit strings (fixed or varying length) Integer (SHORTINT) Floating point Dates and times Domains (=types) will be used in table declarations. To reuse domains: CREATE DOMAIN address AS VARCHAR(55)

Creating Tables Example: CREATE TABLE Person( name VARCHAR(30), social-security-number INTEGER, age SHORTINT, city VARCHAR(30), gender BIT(1), Birthdate DATE );

Deleting or Modifying a Table Example: DROP Person; Altering: (adding or removing an attribute). ALTER TABLE Person ADD phone CHAR(16); ALTER TABLE Person DROP age; Example: What happens when you make changes to the schema?

Default Values Specifying 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 DATE The default of defaults: NULL

Indexes REALLY important to speed up query processing time. Suppose we have a relation Person (name, age, city) Sequential scan of the file Person may take long SELECT * FROM Person WHERE name = “Smith”

Indexes Create an index on name: B+ trees have fan-out of 100s: max 4 levels ! Adam Betty Charles …. Smith

Creating Indexes Syntax: CREATE INDEX nameIndex ON Person(name)

Creating Indexes Indexes can be created on more than one attribute: CREATE INDEX doubleindex ON Person (age, city) Example: SELECT * FROM Person WHERE age = 55 AND city = “Seattle” Helps in: SELECT * FROM Person WHERE city = “Seattle” But not in:

Creating Indexes Indexes can be useful in range queries too: B+ trees help in: Why not create indexes on everything? CREATE INDEX ageIndex ON Person (age) SELECT * FROM Person WHERE age > 25 AND age < 28

Defining Views Views are relations, except that they are not physically stored. For presenting different information to different users Employee(ssn, name, department, project, salary) Payroll has access to Employee, others only to Developers CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development”

A Different View Person(name, city) Purchase(buyer, seller, product, store) Product(name, maker, category) We have a new virtual table: Seattle-view(buyer, seller, product, store) CREATE VIEW Seattle-view AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer

A Different View We can later use the view: SELECT name, store FROM Seattle-view, Product WHERE Seattle-view.product = Product.name AND Product.category = “shoes”

What Happens When We Query a View ? SELECT name, Seattle-view.store FROM Seattle-view, Product WHERE Seattle-view.product = Product.name AND Product.category = “shoes” SELECT name, Purchase.store FROM Person, Purchase, Product WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer AND Purchase.poduct = Product.name AND Product.category = “shoes”

Types of Views Virtual views: Materialized views Used in databases Computed only on-demand – slow at runtime Always up to date Materialized views Used in data warehouses Precomputed offline – fast at runtime May have stale data

Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department, project, salary) CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development” If we make the following insertion: INSERT INTO Developers VALUES(“Joe”, “Optimizer”) INSERT INTO Employee VALUES(NULL, “Joe”, NULL, “Optimizer”, NULL) It becomes:

Non-Updatable Views CREATE VIEW Seattle-view AS SELECT seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer How can we add the following tuple to the view? (“Joe”, “Shoe Model 12345”, “Nine West”) We need to add “Joe” to Person first. One copy ? More copies ?

Aggregation SELECT Sum(price) FROM Product WHERE maker=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT

Aggregation: Count SELECT Count(*) FROM Product WHERE year > 1995 Except COUNT, all aggregations apply to a single attribute

Aggregation: Count COUNT applies to duplicates, unless otherwise stated: SELECT Count(name, category) same as Count(*) FROM Product WHERE year > 1995 Better: SELECT Count(DISTINCT name, category)

Simple Aggregation Purchase(product, date, price, quantity) Example 1: find total sales for the entire database SELECT Sum(price * quantity) FROM Purchase Example 1’: find total sales of bagels WHERE product = ‘bagel’

Simple Aggregations

Grouping and Aggregation Usually, we want aggregations on certain parts of the relation. Purchase(product, date, price, quantity) Example 2: find total sales after 9/1 per product. SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > “9/1” GROUPBY product

Grouping and Aggregation 1. Compute the relation (I.e., the FROM and WHERE). 2. Group by the attributes in the GROUPBY 3. Select one tuple for every group (and apply aggregation) SELECT can have (1) grouped attributes or (2) aggregates.

First compute the relation (date > “9/1”) then group by product:

Then, aggregate SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > “9/1” GROUPBY product

Another Example For every product, what is the total sales and max quantity sold? SELECT product, Sum(price * quantity) AS SumSales Max(quantity) AS MaxQuantity FROM Purchase GROUP BY product

HAVING Clause Same query, except that we consider only products that had at least 100 buyers. SELECT product, Sum(price * quantity) FROM Purchase WHERE date > “9/1” GROUP BY product HAVING Sum(quantity) > 30 HAVING clause contains conditions on aggregates.

General form of Grouping and Aggregation SELECT S FROM R1,…,Rn WHERE C1 GROUP BY a1,…,ak HAVING C2 S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER ATTRIBUTES C1 = is any condition on the attributes in R1,…,Rn C2 = is any condition on aggregate expressions

General form of Grouping and Aggregation SELECT S FROM R1,…,Rn WHERE C1 GROUP BY a1,…,ak HAVING C2 Evaluation steps: Compute the FROM-WHERE part, obtain a table with all attributes in R1,…,Rn Group by the attributes a1,…,ak Compute the aggregates in C2 and keep only groups satisfying C2 Compute aggregates in S and return the result