4 SQL
4 SQL
4. SQL
Contents
Example Database
PRODUCTS(Prodname, Category)
Basic Structure
List the name of products that cost more than $10,000 and
less than $20,000.
select Prodname, Price from offers
where Price >= 10000 and Price <= 20000;
=
ˆ . . . where Price between 10000 and 20000
List all customers who are living in Davis and who have ordered
at least 10 MegaPCs.
select CUSTOMERS.FName, CUSTOMERS.LName, Quantity
from CUSTOMERS, orders
where CAddress like ’%Davis%’
and CUSTOMERS.FName = orders.FName
and CUSTOMERS.LName = orders.LName
and Prodname = ’MegaPC’ and Quantity > 10;
Set Operations
• Examples:
Nested Subqueries
• Give the name and chain of all suppliers located in Davis that
offer a MegaPC for less than $1,000.
Examples (cont.)
• Give all pairs of suppliers that offer exactly the same products.
Null Values
• If permitted by the schema definition for a table (i.e., no not
null constraints), attributes can have null values.
• null =
ˆ unknown, non-existent, or non-applicable value
• Result of any arithmetic expression involving null is null
• Result of where clause condition is false if it evaluates to null.
and true false null or true false null
true true false null true true true true
null null false null null true null null
false false false false false true false null
not
true false
null null
false true
Aggregate Functions
• Examples:
What is the total number of suppliers?
select count(SName) from SUPPLIERS;
Grouping
• Idea: Group tuples that have the same properties into groups,
and apply aggregate function to each group. Optionally,
consider only groups for the query result that satisfy a certain
group condition.
• Syntax in SQL:
select <attribute(s) [with aggregate function]>
from R1, R2, . . . , Rm
[where P ]
group by <grouping attribute(s)>
[having <condition on group>];
Grouping
• Examples:
For each supplier, list the name of the supplier and the total
number of products the supplier offers.
select SName, count(Prodname)
from offers
group by SName;
Grouping (cont.)
• More examples:
List all suppliers from Davis that offer more than 10 products.
Grouping (cont.)
Creating a Table
• Syntax:
create table <name> (
<attribute 1> <data type> [not null] [unique]
[<attribute constraint>],
.........
<attribute n> <data type> [not null] [unique]
[<attribute constraint>],
[<table constraint(s)>]
);
Integrity Constraints
• check <condition>
If <condition> only refers to one attribute
→ attribute constraint;
if <condition> includes more than one attribute of the relation
→ table constraint;
<condition> must be a simple condition that does not contain
queries or references to other relations!
• Example
create table Students (
StID number(9) constraint Students pk primary key,
FName varchar2(50) not null,
LName varchar2(50) not null,
DOB date constraint dob check
check(DOB is not null
and to char(DOB) > ’01-JAN-01’),
Major char(5) constraint fk majors references Majors,
ZipCode integer constraint check zip
check(ZipCode is not null and
ZipCode between 1 and 99999),
City varchar2(50),
Street varchar2(50),
Started date not null,
constraint dates check check(DOB < Started),
constraint name add unique(FName, LName, DOB)
);
I. Deletions:
• Syntax: delete from <relation> [where <condition>];
• Examples:
Delete all suppliers that don’t offer any product.
delete from SUPPLIERS
where SName not in (select SName from offers);
II. Insertions
• Add the customer Scott Tiger (who is living in Davis).
insert into CUSTOMERS
values(’Scott’,’Tiger’,’Davis’,null);
=
ˆ insert into CUSTOMERS(FName, LName, CAddress,
Account)
values(’Scott’,’Tiger’,’Davis’,null);
or insert into CUSTOMERS(FName, LName, CAddress)
values(’Scott’,’Tiger’,’Davis’);
III. Updates
• Increase the Account of the customer Scott Tiger by $5,000,
and change his address to Woodland.
update CUSTOMERS
set Account = Account+5000, CAddress = ’Woodland’
where LName=’Tiger’ and FName=’Scott’;
• Set Clark Kent’s account to the account of Scott Tiger.
update CUSTOMERS
set Account = (select Account from CUSTOMERS
where LName=’Tiger’ and FName=’Scott’)
where FName=’Clark’ and LName=’Kent’;
Views
• Offer a flexible mechanism to hide certain data from the view
of a certain user or application; used to realize external schema
definitions in the three level schema architecture
• Syntax of a view definition:
create view <name>[(<list of attribute names>)]
as <query>;
• The result set of a view is materialized only when the view is
queried ⇒ only the definition of a view requires space
• Examples:
create view PC SUPPLS as
select SName, SAddress, Chain
from SUPPLIERS S
where exists (select ∗ from offers
where SName = S.SName
and Prodname = ’MegaPC’);
Modifications of a View
• Consider the view
CUST ORDERS(FName, LName, Prodname, SName,
Quantity)
defined as
select C.FName, C.LName, Prodname, SName, Quantity
from CUSTOMERS C, orders O
where C.FName=O.FName and C.LName=O.LName;