31
CS 370 Database Systems Lecture 13 Introduction to SQL

CS 370 Database Systems Lecture 13 Introduction to SQL

Embed Size (px)

Citation preview

Page 1: CS 370 Database Systems Lecture 13 Introduction to SQL

CS 370 Database SystemsCS 370 Database Systems

Lecture 13 Introduction to SQL

Page 2: CS 370 Database Systems Lecture 13 Introduction to SQL

SQLAggregate Functions

Page 3: CS 370 Database Systems Lecture 13 Introduction to SQL

Aggregate FunctionsAggregate Functions

• Operates on a column of a relation, and return a value

avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values

Page 4: CS 370 Database Systems Lecture 13 Introduction to SQL

Aggregate Functions(cont.)Aggregate Functions(cont.)

• Find the average account balance at the Perryridge branch.

select avg(balance)from accountwhere branch-name=“Perryridge”

account select balancefrom accountwhere branch-name =“Perryridge”

Avg()120,000

balance

Page 5: CS 370 Database Systems Lecture 13 Introduction to SQL

Aggregate Functions(cont.)Aggregate Functions(cont.)

• Find the numbers of tuples in the customer relation.select count(*)from customer

– remember * stands for all attributes– compare to:

select count(customer-city)from customer

• Find the number of depositors in the bankselect count (distinct customer-name)from depositor

– distinct is redundant if you know customer-name is a key

Page 6: CS 370 Database Systems Lecture 13 Introduction to SQL

Modes of Interaction between Users and DBMS

Page 7: CS 370 Database Systems Lecture 13 Introduction to SQL

DBMS

userAd hoc query

result

Stored procedure queryresult

Embedded SQL C/Java program

result

SQL

Page 8: CS 370 Database Systems Lecture 13 Introduction to SQL

Back to SQL … Set and Nested Queries

Page 9: CS 370 Database Systems Lecture 13 Introduction to SQL

Null valuesNull values

• It is possible for tuples to have a null value, denoted by null, for some of their attributes; null signifies an unknown value or that a value does not exist.

• The result of any arithmetic expression involving null is null.• Roughly speaking, all comparisons involving null return false.

More precisely,– Any comparison with null returns unknown– (true or unknown) = true, (false or unknown) = unkno

wn (unknown or unknown) = unknown,(true and unknown) = unknown, (false and unknown) = false (unknown and unknown) = unknown

– Result of where clause predicate is treated as false if it evaluates to unknown

Page 10: CS 370 Database Systems Lecture 13 Introduction to SQL

Null Values (cont.)Null Values (cont.)

• Find all loan numbers which appear in the loan relation with null values for amount.

select loan-numberfrom loanwhere amount is null

• Total of all loan amountsselect sum(amount)from loan

• All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes.

Page 11: CS 370 Database Systems Lecture 13 Introduction to SQL

SQL - Nested SubqueriesSQL - Nested Subqueries

• Every SQL statement returns a relation/set in the result; remember a relation could be null or merely contain a single atomic value

• You can replace a value or set of values with a SQL statement (ie., a subquery)

select * select *from loan from loanwhere amount > 1200 where amount > select avg(amount)

from loan• Illegal if the subquery returns the wrong type for the compariso

n

Page 12: CS 370 Database Systems Lecture 13 Introduction to SQL

Check for each borrowerif he/she is also a depositor

Returns the set of depositors

Example Nested QueryExample Nested Query

• Find all customers who have both an account and a loan in the bank.

select distinct customer-namefrom borrowerwhere customer-name in (select customer-name from depositor)

Page 13: CS 370 Database Systems Lecture 13 Introduction to SQL

Example QueryExample Query

• Find all customers who have a loan at the bank but do not have an account at the bank.

select distinct customer-namefrom borrowerwhere customer-name not in (select customer-name from depositor)

Page 14: CS 370 Database Systems Lecture 13 Introduction to SQL

Returns borrowers withloan at Perryridge

Returns depositor information

Set Membership ExampleSet Membership Example

Important link:(Perryridge, D Lee)

• Find all customers who have both an account and a loan at the Perryridge branch.

select distinct customer-namefrom borrower, loanwhere borrower.loan-number = loan.loan-number and

branch-name=“Perryridge” and(branch-name, customer-name) in

(select branch-name, customer-name from depositor, account where depositor.account-number =

account.account-number)

Page 15: CS 370 Database Systems Lecture 13 Introduction to SQL

ViewsViews

• Provide a mechanism to hide certain data from the view of certain users. To create a view we use the command:

create view view-name as <query expression>where:– <query expression> is any legal SQL query– the name of the view is represented by view-name

Page 16: CS 370 Database Systems Lecture 13 Introduction to SQL

Example QueriesExample Queries

• A view consisting of branches and their customerscreate view all-customer as

(select branch-name, customer-namefrom depositor, accountwhere depositor.account-number = account.name-num

ber)union

(select branch-name, customer-namefrom borrower, loanwhere borrower.loan-number = loan.loan-number)

• Find all customers of the Perryridge branchselect customer-namefrom all-customerwhere branch-name = “Perryridge”

Page 17: CS 370 Database Systems Lecture 13 Introduction to SQL

• Delete all account records at the Perryridge branch

delete from accountwhere branch-name = “Perryridge”

• Conceptually, delete is done in two steps:– find the tuples you want to delete:

select * from accountwhere branch-name = “Perryridge”

– delete the tuples you found.

Modification of the Database - DeletionModification of the Database - Deletion

Page 18: CS 370 Database Systems Lecture 13 Introduction to SQL

• Delete all accounts at every branch located in Needham.

delete from depositorwhere account-number in (select account-number

from branch, accountwhere branch-city = “Needham”and branch.branch-name = account.branch-

name)delete from accountwhere branch-name in (select branch-name from branch

where branch-city = “Needham”)

• The first delete is needed to enforce “every depositor must have at least one account”. Note that the two deletes can’t be reordered.

Modification of the Database - DeletionModification of the Database - Deletion

Page 19: CS 370 Database Systems Lecture 13 Introduction to SQL

Example QueryExample Query

• Delete the records of all accounts with balances below the average at the bank

delete from accountwhere balance < (select avg (balance)

from account)– Problem: as we delete tuples from deposit, the average b

alance changes– solution used in SQL:

• First, compute and save the average balance of the bank in a variable

• Next, delete all accounts with balance less than the average

Page 20: CS 370 Database Systems Lecture 13 Introduction to SQL

Modification of the database - InsertionModification of the database - Insertion

• Add a new tuple to account

insert into account values (“Perryridge”, A-9732, 1200)

To reorder attributes, specify attribute names explicitly:

insert into account (branch-name, balance, account-number) values (“Perryridge”, 1200, A-9732)

• Add a new tuple to account with balance set to null

insert into account values ( “Perryridge”, “A-777”, null)

Page 21: CS 370 Database Systems Lecture 13 Introduction to SQL

Modification of the Database - InsertionModification of the Database - Insertion

• Create a $200 savings account for all loan customers of the Perryridge branch. Let the loan number serve as the account number for the new savings account.

insert into accountselect branch-name, loan-number, 200from loanwhere branch-name=“Perryridge”

insert into depositorselect customer-name, loan-numberfrom loan, borrowerwhere branch-name=“Perryridge”and loan.account-number = borrower.account-number

Page 22: CS 370 Database Systems Lecture 13 Introduction to SQL

Modification of the database - UpdatesModification of the database - Updates

• Increase all accounts with balance over $10,000 by 6%, all other accounts receive 5%.– Write two update statements:

update accountset balance = balance *1.06where balance >10000

update account set balance = balance *1.05where balance 10000

– the order is important– can be done better using the case statement

Page 23: CS 370 Database Systems Lecture 13 Introduction to SQL

Case Statement for Conditional UpdatesCase Statement for Conditional Updates

• Same query as before: Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%.

update account set balance = case when balance <= 10000

then balance *1.05 else balance * 1.06 end

Page 24: CS 370 Database Systems Lecture 13 Introduction to SQL

Update of a ViewUpdate of a View

• Create a view of all loan data in the loan relation, hiding the amount attribute

create view branch-loan as select branch-name, loan-numberfrom loan

• Add a new tuple to branch-loaninsert into branch-loan

values(“Perryridge”, “L-307”)This insertion must be represented by the insertion of the tuple

(“Perryridge”, “L-307”,null)into the loan relation.

• Updates on more complex views are difficult or impossible to translate, and hence are disallowed.

Page 25: CS 370 Database Systems Lecture 13 Introduction to SQL

View Update Problems

Why can’t views be updated?create view works-on-view as select employee-name, project-name, hours from employee, project, works-on where employee.id=works-on.eid and project.id=works-on.pid

id name789 Dik Lee678 Peter Yeung567 Amy Chan

id name123 database456 internet789 AI

eid pid hour789 123 10678 456 21

ename pnameDik Lee databasePeter Yeung internet

employee projectworks-on

works-on-view

AI

AI incorrect!789 correct

Page 26: CS 370 Database Systems Lecture 13 Introduction to SQL

Find the tuple relating “Dik Lee” to the “database” project

update works-onset pid = ( select id from project where name=`AI’ )where eid = ( select id from employee where name = `Dik Lee’ )and pid = ( select id from project where name = ‘database’ )

Replace old pid with the pid of “AI”

Correct Update Procedure

• The correct update procedure is application dependent and must be written by the database application programmer

• A trigger can invoke the procedure automatically upon updates

Page 27: CS 370 Database Systems Lecture 13 Introduction to SQL

Rules for Updatable Views

Rules for legal view updates:

• A view built on a single defining table is updatable if the view contains the primary key of the defining table

• Views defined on multiple tables are in general not updatable

• Views involving aggregate functions on the defining table are not updatable

Page 28: CS 370 Database Systems Lecture 13 Introduction to SQL

• The SQL syntax for CREATE TABLE is

CREATE TABLE "table_name"

("column 1" "data_type_for_column_1",

"column 2" "data_type_for_column_2",

... )

• So, if we are to create the customer table specified as above, we would type in

CREATE TABLE customer

(First_Name char(50),

Last_Name char(50),

Address char(50),

City char(50),

Country char(25),

Birth_Date date)

Review: Data Definition Language

Page 29: CS 370 Database Systems Lecture 13 Introduction to SQL

sid sname rating age

1 Frodo 7 22

2 Bilbo 2 39

3 Sam 8 27

Sailors

sid bid day

1 102 9/12

2 102 9/13

Reserves

bid bname color

101 Nina red

102 Pinta blue

103 Santa Maria

red

Boats

CREATE TABLE Sailors (sid INTEGER, sname CHAR(20), rating INTEGER, age REAL, PRIMARY KEY sid)

CREATE TABLE Boats (bid INTEGER, bname CHAR (20), color CHAR(10) PRIMARY KEY bid)

CREATE TABLE Reserves (sid INTEGER, bid INTEGER, day DATE, PRIMARY KEY (sid, bid, day), FOREIGN KEY sid REFERENCES Sailors, FOREIGN KEY bid REFERENCES Boats)

NOT NULL,

NOT NULL,

NOT NULL,NOT NULL,

NOT NULL,

Review: Data Definition Language

Page 30: CS 370 Database Systems Lecture 13 Introduction to SQL

CS370 Spring 2007

• A foreign key constraint is an Integrity Constraint: – a condition that must be true for any instance of the

database; – Specified when schema is defined.– Checked when relations are modified.

• Primary/foreign key constraints; but databases support more general constraints as well.– e.g. domain constraints like:

• Rating must be between 1 and 10

ALTER TABLE SAILORS ADD CONSTRAINT RATING CHECK (RATING >= 1 AND RATING < 10)

• Or even more complex (and potentially nonsensical):ALTER TABLE SAILORS ADD CONSTRAINT RATING CHECK (RATING*AGE/4 <= SID)

Integrity Constraints (ICs)

Page 31: CS 370 Database Systems Lecture 13 Introduction to SQL

CS370 Spring 2007

Integrity Constraints can help prevent data consistency errors

• …but they have drawbacks:– Expensive– Can’t always return a meaningful error back to the

application. e.g: What if you saw this error when you enrolled in a

course online?“A violation of the constraint imposed by a unique index or a

unique constraint occurred”. – Can be inconvenient

e.g. What if the ‘Sailing Class’ application wants to register new (unrated) sailors with rating 0?

• So they aren’t widely used– Software developers often prefer to keep the integrity

logic in applications instead