SQL - DML
SQL - DML
Data modification
§ Extraction
§ Insertion, Updating, Deletion
→ SQL: Basic Structure
Data Manipulation Language (DML)
Database Manipulation
Front-end; Database
end-user applications
Schema Database design
Implementation
SQL prompt MSSQLServer, MySql, Oracle, etc.
Submitting
SQL statements
S Q L (DDL) Databases
App-2
SQL Physical
(DML) (RDBMS) Database Schemas
+ DATA
...
App-n
2
Extraction
SQL: Basic Structure
® SQL is based on set and relational operations with certain modifications and
enhancements
A typical SQL query has the form:
select A1, A2, …, An
from r1, r2, …, rm
where P
§ Ai represent attributes
§ ri represent relations
§ P is a predicate.
® This query is equivalent to the relational algebra expression:
3
Consider the following
Banking database
Depositor Account
cname accid
accid bname
balance
Branch
Customer Borrower bname
cname
loanid city
cstreet cname assets
ccity
Loan
loanid
bname
amount branch (bname, bcity, assets)
customer (cname, cstreet, ccity)
depositor (#cname, #accid)
borrower (#cname, #loanid)
account (accid, #bname, balance)
loan (loanid, #bname, amount)
4
SQL: select … from … where
5
SQL: select … from … where
Example of query:
select bname, loanid, amount *100
from loan
6
SQL: select … from … where
select loanid
from loan
where bname = “Perryridge” and amount >1200
® SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the
comparison operators.
Note: attributes used in a query (both select and where parts) must be defined in the relations in
the from clause.
® SQL includes the between operator for convenience.
Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is,
³ $90,000 and £ $100,000)
select loanid
from loan
where amount between 90000 and 100000
7
SQL: select … from … where
Find the name and loan number of all customers having a loan at the Perryridge branch.
8
SQL: select … from … where
Find the name and loan number of all customers having a loan at the Perryridge
branch; replace the column name loanid with the name loan-id.
borrower (#cname, #loanid)
loan (loanid, #bname, amount)
select distinct cname, b.loanid as loan-id
from borrower as b, loan as l
where b.loanid = l.loanid
and bname = “Perryridge”
9
SQL: select … from … where
Tuple Variables/Alias
® Tuple variables are defined in the from clause via the use of the as clause.
Find the customer names and their loan numbers for all customers having a loan at some branch.
10
SQL: select … from … where
String Operations
® Character attributes can be compared to a pattern:
§ % matches any substring.
§ _ matches any single character.
Find the name of all customers whose street includes the substring
‘Main’.
11
SQL: select … from … where
12
SQL: select … from … where
Set Operations
® The set operation union, intersect, and except operate on
relations and correspond to the relational algebra
operations È, Ç and -.
§ Each of the above operations automatically eliminates
duplicate;
§ To retain all duplicates use the corresponding multiset versions union all,
intersect all and except all.
13
SQL: select … from … where …union / intersect / except branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, #branch-name, balance)
Set operations loan (loan-number, #branch-name, amount)
depositor (#customer-name, #account-number)
borrower (#customer-name, #loan-number)
Find all customers who have a loan, an account,
or both:
® Join condition – defines which tuples in the two relations match, and what
attributes are present in the result of the join.
natural
Join Conditions on <predicate>
using (A1, A2, ..., An)
® Join type – defines how tuples in each relation that do not match any tuple in
the other relation (based on the join condition) are treated.
inner join
left outer join
Join Types right outer join
full outer join
15
® An extension of the join operation that avoids loss of information.
® Computes the join and then adds tuples form one relation that does not
Outer Join match tuples in the other relation to the result of the join
Difference between LEFT and RIGHT OUTER JOIN in SQL: In short, the following are
some notable difference between LEFT and RIGHT outer join in SQL :
1. LEFT OUTER join includes unmatched rows from the
left table while RIGHT OUTER join includes
unmatched rows from the right side of the table.
§ In Transact-SQL syntax left outer join is written as *= and right outer join is written as =*, Sybase
database supports both syntaxes and you can write join queries in both ANSI and T-SQL syntax.
16
SELECT columns Relation loan Relation borrower
FROM table1 LEFT/RIGHT/FULL JOIN table2
loan-number branch-name amount customer-name loan-number
L-170 Downtown 3000 Jones L-170
ON column-name1 = column-name2 L-230 Redwood 4000 Smith L-230
WHERE condition L-260 Perryridge 1700 Hayes L-155
17
SQL JOINS; Summary…
There are three major types of joins.
Type 1: INNER JOIN - only where both tables match; INNER JOIN or JOIN
Type 2: OUTER JOINS where either one or both tables match
1. LEFT OUTER JOIN aka LEFT JOIN
2. RIGHT OUTER JOIN aka RIGHT JOIN
3. FULL OUTER JOIN aka FULL JOIN
Type 3: CROSS JOIN - Cartesian product(all possible combos of each table)
18
Aggregate functions - Group by
Relational Algebra:
19
account (accid, #bname, balance)
Aggregate functions - Group by Aggregate function types:
avg average value
groupByK min minimum value
select a1,a2,…, an, agg-function(….) max maximum value
from account sum sum of values
Where …. groupByK count number of values
group by a1,a2,…, an
Where
20
SQL: select …avg/min… from … where
22
Aggregate functions - Group by
List the branches where there is 100 or more accounts.
select bname, count(distinct accid) as Nbacc
from account
group by bname
having Nbacc >=100
For each group of tuples with the same branch name (bname), apply
aggregate function count and distinct to accid
grouping
bname accid balance
Perryridge A102 400
Brighton A217 750 bname accid balance counting
Perryridge A102 400
Perryridge A201 900
Perryridge A201 900 bname count-accid
Brighton A215 750
Brighton A217 750 Perryridge 2
Redwood a222 700
Brighton A215 750 Brighton 2
Redwood a222 700 Redwood 1
23
Aggregate/Group by with Join
Find the number of depositors for each branch.
24
Aggregate Functions-Having Clause
Find the names of all branches where the average account
balance is more than $1200
select bname, avg(balance)
from account
group by bname
having avg (balance) >1200
predicates in the having clause are applied to each group after the formation
of groups
bname accid balance
Perryridge A102 400
Perryridge A201 900
Brighton A217 750
Brighton A215 750
Redwood a222 700
25
Null 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.
® Find all loan numbers which appear in the loan relation with null values
for amount.
select loanid
from loan
where amount is null
® Total all loan amounts
select sum(amount)
from loan
Above statement ignores null amounts; result is null if there is no non-null amount.
27
SQL - 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 loan
where amount > 1200 where amount > (select avg(amount)
from loan )
MIllegal if the sub-query returns the wrong type for the
comparison
28
Set Membership
® F in r Û $ t Î r ( t = F )
F is typically an attribute name and the set of values is typically
returned from a subquery
0
4
(5 in 5 ) returns true
0
Example Query
4 Find all customers who have both an
(5 in 6 ) returns false account and a loan in the bank.
select distinct cname
0 from borrower
4
where cname in (select cname
(5 not in 6 ) returns true
from depositor)
29
Example
Example Query
Find all customers who have a loan at the bank but do not have an
account at the bank. depositor (#cname, #accid)
select distinct cname borrower (#cname, #loanid)
from borrower account (accid, #bname, balance)
where cname not in (select cname loan (loanid, #bname, amount)
from depositor )
Find all customers who have both an account and a loan at the
Perryridge branch.
select distinct cname
from borrower, loan
where borrower.loanid = loan.loanid and
bname =“Perryridge” and
(bname , cname ) in
(select bname , cname
from depositor, account
where depositor.accid = account.accid)
30
Set Comparison
Find all branches that have greater assets than
some branches located in Brooklyn.
branch (bname, bcity, assets)
31
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 <query expression> create view result as
where: select bname ,
• <query expression> is any legal SQL query avg(balance) as avg-balance
• the view name is represented by v from account
group by bname
Example Queries
• A view consisting of branches and their customers
create view all-customer as
(select bname , cname
from depositor, account
where depositor.accid = account.name-number)
union (select bname , cname
from borrower, loan
where borrower.loanid = loan.loan-number)
Find all customers of the Perryridge branch
select cname
from all-customer
where bname = “Perryridge”
32
Data Manipulation Language(DML)
Data modification
§ Extraction
§ Insertion, Updating, Deletion
→ SQL: Basic Structure
SQL: Modification of the database – delete, update, insert
Modification of the Database - Deletion
® Delete all account records at the Perryridge branch
account (accid, #bname, balance)
delete from account
where bname = “Perryridge”
34
SQL: Modification of the database – delete, update, insert
Modification of the Database - Deletion
® Delete all accounts at every branch located in Needham.
36
SQL: Modification of the database – delete, update, insert
37
SQL: Modification of the database – delete, update, insert
38
SQL: Modification of the database – delete, update, insert
Modification of the database- Updates
® Increase all accounts with balance over $10,000 by 6%, all
other accounts receive 5%.
account (accid, #bname, balance)
§ Write two update statements:
update account Sailors
sid sname rating age
set balance = balance *1.06 22 Dustin 7 45.0
29 Brutus 1 33.0
where balance >10000 31 Lubber 8 55.5
32 Andy 8 25.5
update account 58
update
Rusty
Sailors
10 35.0
64 Horatio 7 35.0
set balance = balance *1.05 71 set rating = 10
Zorba rating+116.0
where balance £ 10000 74
85
Horatio age >40
where
Art
9
3
35.0
25.5
95 Bob 3 63.5
§ the order is important
§ can be done better using the case statement
39
SQL: Modification of the database – delete, update, insert
Update of a View
® Create a view of all loan data in the loan relation, hiding the amount
attribute
create view branch-loan as
select bname , loan-number
from loan
loan (loanid, #bname, amount)
® Add a new tuple to branch-loan
insert 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.
40