Database Systems SQL SL03

Similar documents
Database Systems SQL SL03

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Chapter 3: SQL. Chapter 3: SQL

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Silberschatz, Korth and Sudarshan See for conditions on re-use

Chapter 4: SQL. Basic Structure

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

QQ Group

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

DATABASE DESIGN I - 1DL300

DATABASE TECHNOLOGY - 1MB025

DATABASE TECHNOLOGY - 1MB025

The SQL database language Parts of the SQL language

DATABASTEKNIK - 1DL116

DATABASE TECHNOLOGY. Spring An introduction to database systems

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

The SQL data-definition language (DDL) allows defining :

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL

Simple SQL Queries (2)

Chapter 2: Relational Model

Chapter 6: Formal Relational Query Languages

Upon completion of this Unit, students will be introduced to the following

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

CS 582 Database Management Systems II

SQL (Structured Query Language)

Chapter 3: Relational Model

Chapter 5: Other Relational Languages

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

Relational Algebra. Procedural language Six basic operators

Database System Concepts

Relational Model, Relational Algebra, and SQL

Explain in words what this relational algebra expression returns:

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

CSCC43H: Introduction to Databases. Lecture 4

CS425 Fall 2017 Boris Glavic Chapter 4: Introduction to SQL

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

UNIT- II (Relational Data Model & Introduction to SQL)

Chapter 5: Other Relational Languages.! Query-by-Example (QBE)! Datalog

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

CSIE30600 Database Systems Basic SQL 2. Outline

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions...

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Relational Databases

Database System Concepts

Outline. Note 1. CSIE30600 Database Systems More SQL 2

Chapter 14: Query Optimization

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

Chapter 12: Query Processing

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

SQL - Basics. SQL Overview

Slides by: Ms. Shree Jaswal

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

1) Introduction to SQL

Midterm Review. Winter Lecture 13

CMSC 424 Database design Lecture 18 Query optimization. Mihai Pop

Principles of Data Management

Database Systems Introduction SL01

Database Management System 11

CMP-3440 Database Systems

Basant Group of Institution

Mahathma Gandhi University

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Chapter 14 Query Optimization

CSEN 501 CSEN501 - Databases I

Relational Algebra and SQL

SQL. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

SQL functions fit into two broad categories: Data definition language Data manipulation language

Other Relational Query Languages

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Overview of Query Processing and Optimization

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Ian Kenny. November 28, 2017

An Introduction to Structured Query Language

An Introduction to Structured Query Language

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 6 st, 2017

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Relational Database: The Relational Data Model; Operations on Database Relations

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 1 st, 2016

SQL STRUCTURED QUERY LANGUAGE

Integrity and Security

2.2.2.Relational Database concept

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

Relational Algebra and SQL

2.3 Algorithms Using Map-Reduce

Full file at

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Transcription:

Inf4Oec10, SL03 1/52 M. Böhlen, ifi@uzh Informatik für Ökonomen II Fall 2010 Database Systems SQL SL03 Data Definition Language Table Expressions, Query Specifications, Query Expressions Subqueries, Duplicates, Null Values Modification of the Database

Inf4Oec10, SL03 2/52 M. Böhlen, ifi@uzh Checking... 1. Assume R(A) = {(1),(2),(3)}. Write a relational algebra expression that determines the larges value in R. R1 A B R A 1 2 3 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 R2 A B 1 2 1 3 2 3 R3 A 3 R1 R ρ (B) (R) R2 σ A<B (R1) R3 R π A (R2)

Inf4Oec10, SL03 3/52 M. Böhlen, ifi@uzh Checking... 1. Identify and fix mistakes in the following relational algebra expressions. Assume a relation with schema R(A,B). σ R.A>5 (R) R.A is not an attribute name. Fix: σ A>5 (R) σ A,B (R) Selection requires predicate. Fix: π A,B (R) R R Name clashes. Fix: ρ T (R ρ S(C,D) (R))

Inf4Oec10, SL03 4/52 M. Böhlen, ifi@uzh History IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory Renamed Structured Query Language (SQL) ANSI and ISO standard SQL: SQL-86 SQL-89 SQL-92 (also called SQL2) entry level: roughly corresponds to SQL-89 intermediate level: half of the new features of SQL-92 full level SQL:2003 (also called SQL3) Commercial systems offer most, if not all, SQL-92 features, plus varying feature sets from later standards and special proprietary features.

Inf4Oec10, SL03 5/52 M. Böhlen, ifi@uzh Model and Terminology SQL is based on multisets (or bags) rather than sets. In a multiset an element may occur multiple times. SQL uses the terms table, column, and row. Comparison of terminology: SQL Algebra table relation column attribute row tuple query relational algebra expression In SQL there is no distinction between small and capital characters.

Inf4Oec10, SL03 6/52 M. Böhlen, ifi@uzh Data Definition Language Allows the specification of not information about each table, including: The schema for each table. The domain associated with each column. Integrity constraints that all valid instances must satisfy. The set of indices to be maintained for each table. Security and authorization information for each table. The physical storage structure of each table on disk.

Inf4Oec10, SL03 7/52 M. Böhlen, ifi@uzh Domain Types in SQL char(n) Fixed length character string, with user-specified length n. varchar(n) Variable length character strings, with user-specified maximum length n. integer Integer (a finite subset of the integers that is machine-dependent). smallint Small integer (a machine-dependent subset of the integer domain type). numeric(p,d) Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point. real, double precision Floating point and double-precision floating point numbers, with machine-dependent precision. float(n) Floating point number, with user-specified precision of at least n digits.

Inf4Oec10, SL03 8/52 M. Böhlen, ifi@uzh Create Table Construct An SQL table is defined using the create table command: create table r( A 1 D 1,A 2 D 2,...,A n D n, (integrity-constraint 1 ),..., (integrity-constraint k )) r is the name of the table each Ai is a column name in the schema of table Di is the data type of values in the domain of column A i Example: create table branch( BranchName char(15) not null, branch city char(30), assets integer)

Inf4Oec10, SL03 9/52 M. Böhlen, ifi@uzh Integrity Constraints in Create Table not null primary key (A 1,...,A n ) Example: Declare BranchName as the primary key for branch create table branch( BranchName char(15), BranchCity char(30), Assets integer, primary key (BranchName)) In SQL a primary key declaration on a column automatically ensures not null.

Inf4Oec10, SL03 10/52 M. Böhlen, ifi@uzh Drop and Alter Table Constructs The drop table command deletes all information about the dropped table from the database. The alter table command is used to add columns to an existing table: alter table r add A D where A is the name of the column to be added to table r and D is the domain of A. All tuples in the table are assigned null as the value for the new column. The alter table command can also be used to drop columns of a table: alter table r drop A where A is the name of an column of table r Dropping of columns not supported by many databases

Inf4Oec10, SL03 11/52 M. Böhlen, ifi@uzh Expressions and Predicates Powerful expressions and predicates make computer languages useful and convenient. Database vendors compete on the set of expressions and predicates they offer (functionality as well as speed). In databases the efficient evaluation of predicates is a major concern. Consider a table with 1 billion tuples and the following predicates: LastName = Miller LastName like %mann LastName like Ester% That is one of the reasons why additions of (user defined) functions and predicates to DBMS has been limited. We show some SQL expressions and predicates to give a rough idea of common database functionality.

Inf4Oec10, SL03 12/52 M. Böhlen, ifi@uzh Structure of SQL Queries/1 SQL is based on relations and relational operations with certain modifications and enhancements. It is used very heavily in the real world. Note that SQL is much more than simple select-from-where queries such as the following: select * from Employee where LName = Bohr Many people Do not understand the concepts of SQL Do not really understand how to work with a declarative language and sets (this requires some practice and getting used to) Underestimate SQL

Structure of SQL Queries/2 A typical SQL query has the form: select clause from clause where clause group clause having clause table expression query specification union select clause from clause where clause group clause having clause table expression query specification query expression The result of an SQL query is a (virtual) table. Inf4Oec10, SL03 13/52 M. Böhlen, ifi@uzh

Inf4Oec10, SL03 14/52 M. Böhlen, ifi@uzh Conceptual Evaluation of SQL Statements 1. Form the cross product of the tables in the from clause 2. Eliminate tuples that do not satisfy the where clause 3. Group the remaining tuples according to the group clause 4. Eliminate groups that do not satisfy the having clause 5. Evaluate the expressions in the select clause 6. One result tuple is produced for each group 7. Eliminate duplicates if distinct is specified 8. Compute query expressions independently and apply set operations (union, excpet, intersect) 9. Sort all result tuples according to order clause

Inf4Oec10, SL03 15/52 M. Böhlen, ifi@uzh The from Clause The from clause lists the tables involved in the query Corresponds to the Cartesian product operation of the relational algebra. Cartesian product borrower loan from borrower, loan Customer names and loan numbers (all combinations): from borrower as T,loan as S Renaming can become necessary if the same table is used multiple times in the from clause.

Inf4Oec10, SL03 16/52 M. Böhlen, ifi@uzh The where Clause The where clause specifies conditions that the result tuples must satisfy. The where clause takes the virtual table produced by the from clause and filters out those rows that do not satisfy the condition. Example: loan number of loans from the Perryridge branch with loan amounts greater than $1200. from loan where BraName = Perryridge and Amount > 1200 LoanNum BraName Amount L-260 Perryridge 1700 The where clause corresponds to the selection predicate. In SQL predicates can be combined using the logical connectives and, or, and not. The where clause can be used to specify join and selection conditions.

Inf4Oec10, SL03 17/52 M. Böhlen, ifi@uzh The group Clause The group clause takes the table produced by the where clause and returns a grouped table. The group clause groups multiple tuples together. Conceptually, grouping yields multiple tables. Example: Accounts grouped by branch. from account group by BrName account AccNr BrName Balance A-101 Downtown 500 A-215 Perryridge 700 A-102 Perryridge 400 A-305 Perryridge 350 A-222 Perryridge 700 A-201 Brighton 900 A-217 Brighton 750

Inf4Oec10, SL03 18/52 M. Böhlen, ifi@uzh The having Clause/1 The having clause takes a grouped table as input and returns a grouped table. The having condition is applied to each group. Only groups that fulfill the condition are returned. The having clause never returns individual tuples of a group. The having condition may include grouping columns or aggregated columns.

Inf4Oec10, SL03 19/52 M. Böhlen, ifi@uzh The having Clause/2 Consider branches with more than one account only: from account group by BrName having count(accnr) > 1 This having clause returns all groups with more than one tuple: account AccNr BrName Balance A-215 Perryridge 700 A-102 Perryridge 400 A-305 Perryridge 350 A-222 Perryridge 700 A-201 Brighton 900 A-217 Brighton 750

Inf4Oec10, SL03 20/52 M. Böhlen, ifi@uzh The select Clause/1 The select clause lists the columns that shall be in the result of a query. corresponds to the projection operation of the relational algebra Example: find the names of all branches in the loan table: select BraName from loan In the relational algebra, the query would be: π BraName (loan)

Inf4Oec10, SL03 21/52 M. Böhlen, ifi@uzh The select Clause/2 SQL allows duplicates in tables as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Find the names of all branches in the loan tables, and remove duplicates select distinct BraName from loan A * in the select clause denotes all columns select * from loan

Inf4Oec10, SL03 22/52 M. Böhlen, ifi@uzh The select Clause/3 In the select clause aggregate functions can be used. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values The aggregate functions operate on multiple rows. They process all rows of a group and compute an aggregated value for that group. Note: Columns in select clause outside of aggregate functions must appear in group by list

Inf4Oec10, SL03 23/52 M. Böhlen, ifi@uzh The select Clause/4 Find the average account balance at the Perryridge branch. select avg(balance) from account where BrName = Perryridge Find the number of tuples in the customer table. select count( ) from customer Find the number of accounts per branch. select count(accnr), BrName from account group by BrName

Inf4Oec10, SL03 24/52 M. Böhlen, ifi@uzh Checking... 1. Identify problem of the following SQL statements. Propose a fix. Determine for each branch where the largest balance is at least 500 the largest balance. select max(balance), BrName from account where max(balance) > 500 group by BrName The where clause does not permit aggregates (it filters tuples) select max(balance), BrName from account group by BrName having max(balance) > 500

Inf4Oec10, SL03 25/52 M. Böhlen, ifi@uzh Checking... 2. Identify problem of the following SQL statements. Determine for each branch the account with the largest balance. select max(balance), AccNr, BrName from account group by BrName account AccNr BrName Balance A-101 Downtown 500 A-215 Perryridge 700 A-102 Perryridge 400 A-305 Perryridge 350 A-222 Perryridge 700 A-201 Brighton 900 A-217 Brighton 750 In the select clause only aggregates or grouped columns are allowed. The reason for this constraint is that exactly one tuple per group shall be computed. The above query must be solved with a subquery.

Inf4Oec10, SL03 26/52 M. Böhlen, ifi@uzh SQL so far SQL versus relational algebra: SQL Algebra table relation column attribute row tuple query relational algebra expression A query specification in SQL has the following form: select clause from clause where clause group clause having clause table expression query specification

Inf4Oec10, SL03 27/52 M. Böhlen, ifi@uzh Illustration of Evaluation of SQL Statements/1 1. FROM: form the cross product of the tables in the from clause FROM... 2. WHERE: eliminate tuples that do not satisfy the where clause... WHERE 3. GROUP BY: group tuples according to the group clause GROUP BY

Inf4Oec10, SL03 28/52 M. Böhlen, ifi@uzh Illustration of Evaluation of SQL Statements/2 4. HAVING: eliminate groups that do not satisfy the having clause HAVING 5. SELECT: evaluate the expressions in the select clause and produce one result tuple for each group SELECT

Inf4Oec10, SL03 29/52 M. Böhlen, ifi@uzh Query Expressions/1 The set operations union, intersect, and except operate on tables and correspond to the relational algebra operations,, Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all, and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m+n times in r union all s min(m,n) times in r intersect all s max(0,m n) times in r except all s

Inf4Oec10, SL03 30/52 M. Böhlen, ifi@uzh Query Expressions/2 Find all customers who have a loan, an account, or both: (select CuName from depositor) union (select CstName from borrower) Find all customers who have both a loan and an account: (select CuName from depositor) intersect (select CstName from borrower) Find all customers who have an account but no loan: (select CuName from depositor) except (select CstName from borrower)

Inf4Oec10, SL03 31/52 M. Böhlen, ifi@uzh Subqueries/1 SQL provides a mechanism for the nesting of subqueries. A subquery is a query expression that is nested within another query expression. A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality.

Inf4Oec10, SL03 32/52 M. Böhlen, ifi@uzh Subqueries/2 Find all customers who have both an account and a loan at the bank. select distinct CstName from borrower where CstName in (select CuName from depositor) Find all customers who have a loan at the bank but do not have an account at the bank select distinct CstName from borrower where CstName not in (select CuName from depositor)

Inf4Oec10, SL03 33/52 M. Böhlen, ifi@uzh Subqueries/3 Find all customers who have both an account and a loan at the Perryridge branch select distinct CstName from borrower, loan where borrower.loanno = loan.loannr and BraName = Perryridge and (BraName, CstName) in ( select BrName, CuName from depositor, account where depositor.accnr = account.accnr)

Inf4Oec10, SL03 34/52 M. Böhlen, ifi@uzh Subqueries/4 Comparing sets: Find all branches that have greater assets than some branch located in Brooklyn. select distinct T.BranchName from branch as T,branch as S where T.Assets > S.Assets and S.BranchCity = Brooklyn Same query using > some clause select BranchName from branch where Assets > some (select Assets from branch where BranchCity = Brooklyn )

Inf4Oec10, SL03 35/52 M. Böhlen, ifi@uzh Subqueries/5 F <comp> some r t r such that (F <comp> t) Where <comp> can be: <,,>,=, (5 < some 0 5 6 ) = true (read: 5 < some tuple in the table) (5 < some 0 5 ) = false (5 = some 0 5 ) = true (5 some 0 5 ) = true (since 0 5) (= some) in However, ( some) not in

Inf4Oec10, SL03 36/52 M. Böhlen, ifi@uzh Subqueries/6 Find the names of all branches that have greater assets than all branches located in Brooklyn. select BranchName from branch where assets > all (select assets from branch where BranchCity = Brooklyn )

Inf4Oec10, SL03 37/52 M. Böhlen, ifi@uzh Subqueries/7 F <comp> all r t r (F <comp> t) (5 < all 0 5 6 ) = false (5 < all (5 = all 6 10 ) = true 4 5 ) = false (5 all 4 6 ) = true (since 5 4 and 5 6) ( all) not in However, (= all) in

Inf4Oec10, SL03 38/52 M. Böhlen, ifi@uzh Subqueries/8 The exists construct returns the value true if the argument subquery is nonempty. exists r r not exists r r = The exists (and not exists) subqueries are used frequently in SQL. Avoid in, all, any, some and use exists instead.

Inf4Oec10, SL03 39/52 M. Böhlen, ifi@uzh Subqueries/9 Find all customers who have an account at all branches located in Brooklyn. select distinct S.CuName from depositor as S where not exists ( (select BranchName from branch where BranchCity = Brooklyn ) except (select R.BrName from depositor as T, account as R where T.AccNr = R.AccNr and S.CuName = T.CuName)) Note that X Y = X Y Note: Cannot write this query using = all and its variants

Inf4Oec10, SL03 40/52 M. Böhlen, ifi@uzh Derived Tables SQL allows a subquery expression to be used in the from clause. That is important for orthogonality of a language. A derived table is defined by a query expression. Find the average account balance of those branches where the average account balance is greater than $1200. select BranchName, AvgBalance from (select BrName, avg(balance) from account group by account) as BranchAvg(BranchName, AvgBalance) where AvgBalance > 1200

Inf4Oec10, SL03 41/52 M. Böhlen, ifi@uzh Duplicates/1 In tables with duplicates, SQL can define how many copies of tuples appear in the result. Multiset versions of some of the relational algebra operators - given multiset relations r 1 and r 2 : 1. σ b θ (r 1): If there are c 1 copies of tuple t 1 in r 1, and t 1 satisfies selections σ θ, then there are c 1 copies of t 1 in σ b θ(r 1 ). 2. π b A (r 1): For each copy of tuple t 1 in r 1, there is a copy of tuple π A (t 1 ) in π b A (r 1) where π A (t 1 ) denotes the projection of the single tuple t 1. 3. r 1 b r 2 : If there are c 1 copies of tuple t 1 in r 1 and r 2 copies of tuple t 2 in r 2, there are c 1 c 2 copies of the tuple t 1, t 2 in r 1 b r 2

Inf4Oec10, SL03 42/52 M. Böhlen, ifi@uzh Duplicates/2 Example: Suppose multisets r 1 (A,B) and r 2 (C) are as follows: r 1 = {(1,a) (2,a)} r 2 = {(2), (3), (3)} Then π b B (r 1) would be {(a),(a)}, while π b B (r 1) r 2 would be SQL duplicate semantics: {(a,2), (a,2), (a,3), (a,3), (a,3), (a,3)} select A 1,A 2,...,A n from r 1,r 2,...,r m where P is equivalent to the multiset expression: π b A 1,A 2,...,A n (σ b P(r 1 b r 2 b... b r m ))

Inf4Oec10, SL03 43/52 M. Böhlen, ifi@uzh Null Values/1 It is possible for tuples to have a null value, denoted by null, for some of their columns null signifies an unknown value or that a value does not exist. The predicate is null can be used to check for null values. Example: Find all loan number which appear in the loan relation with null values for Amount. select LoanNr from loan where Amount is null The result of any arithmetic expression involving null is null Example: 5+null returns null However, aggregate functions simply ignore nulls More on next slide

Inf4Oec10, SL03 44/52 M. Böhlen, ifi@uzh Null Values/2 Any comparison with null returns unknown Example: 5 < null or null <> null or null = null Three-valued logic using the truth value unknown: OR AND NOT (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown (true and unknown) = unknown, (false and unknown) = false (unknown and unknown) = unknown (not unknown) = unknown P is unknown evaluates to true if predicate P evaluates to unknown Result of where clause predicate is treated as false if it evaluates to unknown

Inf4Oec10, SL03 45/52 M. Böhlen, ifi@uzh Null Values/3 Total all loan amounts select count(amount) from loan Above statement ignores null amounts Result is 0 if there is no non-null amount All aggregate operations, except count(*), ignore tuples with null values on the aggregated columns.

Inf4Oec10, SL03 46/52 M. Böhlen, ifi@uzh Ordering the Display of Tuples List in alphabetic order the names of all customers having a loan in Perryridge branch select distinct CstName from borrower, loan where borrower.loanno = loan.loannr and BraName = Perryridge order by CstName We may specify desc for descending order or asc for ascending order, for each column; ascending order is the default. Example: order by CustName desc

Inf4Oec10, SL03 47/52 M. Böhlen, ifi@uzh Deletions/1 Delete all account tuples at the Perryridge branch delete from account where BrName = Perryridge Delete all accounts at every branch located in the city Needham delete from account where BrName in (select BranchName from branch where BranchCity = Needham )

Inf4Oec10, SL03 48/52 M. Böhlen, ifi@uzh Insertions/1 Add a new tuple to account insert into account values ( A 9732, Perryridge,1200) or equivalently insert into account(brname, Balance, AccNr) values ( Perryridge,1200, A 9732 ) Add a new tuple to account with balance set to null insert into account values ( A 9732, Perryridge,null)

Inf4Oec10, SL03 49/52 M. Böhlen, ifi@uzh Updates/1 Increase all accounts with balances over $10,000 by 6%, all other accounts 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 The order is important Can be done better with a single update statement that uses a case statement.

Inf4Oec10, SL03 50/52 M. Böhlen, ifi@uzh Summary/1 DDL (data definition language) Used to create, alter, and drop tables. Table definitions include constraints (domain, not null, primary key, foreign key). Constraints ensure data quality, which in turn gives competitive advantages to businesses. Enforcing constraints is difficult and requires a focused effort. Data quality must be considered during data acquisition; the database system must enforce it; it cannot be fixed later. Expressions and predicates Database systems have been conservative since the efficient evaluation must be considered. The brute force evaluation of a condition on one billion tuples might not be good enough. Modern database systems are extensible with user defined functions.

Inf4Oec10, SL03 51/52 M. Böhlen, ifi@uzh Summary/2 SQL SQL is used very heavily in practice; the intergalactic data speak [Michael Stonebraker]. The building block of the DML are query specifications and query expressions. SQL is more than simple select-from-where. The set based semantics of SQL requires some practice. Know the conceptual evaluation/semantics of query expressions. Form the cross product of the tables in the from clause Eliminate tuples that do not satisfy the where clause Group the remaining tuples according to the group clause Eliminate groups that do not satisfy the having clause Evaluate the expressions in the select clause With aggregation one result tuple is produced for each group Eliminate duplicates if distinct is specified Compute query expressions independently and apply set operations (union, excpet, intersect) Sort all result tuples of order clause is specified

Inf4Oec10, SL03 52/52 M. Böhlen, ifi@uzh Summary/3 The difference between set and multisets (or bags) is relevant. Subqueries are natural and help the understanding; it is possible (and advisable) to only use exists and not exists. The SQL OLAP extensions (cf. Data Warehouse course) further extend SQL to better support report generation (cross tabs, moving averages, etc).