Upload
imogen-stevenson
View
220
Download
0
Embed Size (px)
Citation preview
Holliday - COEN 178 2
DB Tables and SQL
• The data is stored in the database in relations or tables
• Data types are constrained like in an excel spreadsheet
• Column headings are the entity attributes
• SQL is a language for extracting information from the tables
Holliday - COEN 178 3
A Bank Database
Customer Account
Loan Branch
street
name
Loan#
amount
name
city
city
assets
makes
borrower
depositor
has
Acc# balance
Holliday - COEN 178 4
Bank Database Schema
• Branch = (branch-name, branch-city, assets)• Customer = (customer-name, customer-street,
customer-city)• Account = (branch-name, account#, balance)• Depositor = (customer-name, account#)• Loan = (branch-name, loan#, amount)• Borrower = (customer-name, loan#)
Holliday - COEN 178 5
The Customer TableCustomer-name C-Street C-city
Bob 123 Third St San Jose
Carol 456 Main St Santa Clara
Ted 89 Blossom Ave Los Gatos
Alice 64 Longwalk Dr Oakland
Holliday - COEN 178 6
The Account Table
Branch-name Account# Balance
Oakland 101 2000
SJ-Main 205 7500
SJ-Main 207 4500
Santa Clara 311 3100
SJ-West 251 850
Holliday - COEN 178 7
The Loan Table
Branch-name loan# Amount
Oakland 2301 5000
SJ-Main 5155 700
SJ-Main 5709 9000
Santa Clara 1541 1800
SJ-West 4321 250
Holliday - COEN 178 8
The Depositor Table
Customer-name Account#
Bob 207
Carol 311
Ted 205
Alice 101
Bob 251
Holliday - COEN 178 9
Creating a Table• An SQL relation is defined with the create table
command:
create table <name> ( <column-desc-list>, <constraint-list> )
• Column-description-list is a comma separated list of column names and their domains.
• Constraint-list is a comma separated list of integrity constraints.
Holliday - COEN 178 10
Domains
char(n) Fixed length character string.
varchar(n) Variable length character string, max length n.
integer Integer (actual range is DB dependent)
date Dates with 4 digit year, 2 digit month an day.
time Day, hours, minutes, seconds
• Null values are allowed in all domain types.
Declaring an attribute to be not null excludes the null value from the domain.
Holliday - COEN 178 11
The Customer Table
Example:
create table Customer(customer-name char(30), c-street char(30), c-city char(30),
primary key (customer-name) )
Holliday - COEN 178 12
Branch Table with Constraints
Example:
create table Branch (branch-name char(15), branch-city char(30), assets integer,
primary key (branch-name), check (assets >= 0) )
Holliday - COEN 178 13
The select statement
select A1, A2, … An
from r1, r2, … , rk where P
• Ai's are attribute names, r's are tables and P is a predicate (condition).
• Cartesian product of the tables in the from clause is formed. Then, the criteria in the where clause is used to select rows from the product. Last, the columns and expressions in the select clause are projected to form the result.
Holliday - COEN 178 14
Queries on the Loan Table
• Loan = (branch-name, loan#, amount)• Find the names of all the branches in
the Loan relation
select branch-namefrom Loan
• Note that the "where" clause is optional.
Branch-name
Oakland
SJ-Main
SJ-Main
Santa Clara
SJ-West
Holliday - COEN 178 15
More Queries on the Loan Table
• An asterisk in the select clause denotes "all attributes"
select *from Loanwhere amount > 3000
• Result contains only those rows that meet
the criteria
Holliday - COEN 178 16
The where condition
• Find the loan numbers for all loans made at the Oakland branch with loan amounts greater than 1200.
select loan#from Loanwhere branch-name="Oakland"
and amount>1200
Holliday - COEN 178 17
The from clause
• Form the cross product of the tables listed in the FROM clause
Select A1, A2 from R1, R2Result R = R1 R2
• pairs each tuple t1 of R1 with each tuple t2 of R2 and puts in R a tuple t1t2.
Holliday - COEN 178 18
Cartesian Product ()
arity(R) = k1 arity(R S) = k1 + k2
arity(S) = k2 card(R S) = card(R) card(S)
R S is the set all possible (k1 + k2)-tuples
whose first k1 attributes are a tuple in R
last k2 attributes are a tuple in S
R S R SA B C D D E F A B C D D' E F
Holliday - COEN 178 19
Cartesian ProductTable A
BillMary
45
two
Table B
A X B
JohnSue
onefour
Bill
Mary
4
5
Bill
Bill
Mary
4
5
Bill
Mary
4
5
John one
John one
Sue four
Sue fourtwoBill
twoBill
Holliday - COEN 178 20
Using the Cartesian Product
• Find the name of customers with an account at the Oakland branch.
• Account = (branch-name, account#, balance)• Depositor = (customer-name, account#)
• Cartesian product of Account and Depositor will match each row of Account with each row of Depositor – we need a way to retain only those rows with matching account#
Holliday - COEN 178 21
Using the Cartesian Product
• Find the name of customers with an account at the Oakland branch.
select customer-namefrom Depositor, Accountwhere Depositor.account# = Account.account# and branch-name = "Oakland"
Depositor join AccountCustomer-name Account#
Bob 207
Carol 311
Ted 205
Alice 101
Bob 251
Branch-name Account#
Balance
Oakland 101 2000
SJ-Main 205 7500
SJ-Main 207 4500
Santa Clara 311 3100
SJ-West 251 850
Customer-name Account# Branch-name Account#
Balance
Alice 101 Oakland 101 2000
Ted 205 SJ-Main 205 7500
Bob 207 SJ-Main 207 4500
Carol 311 Santa Clara 311 3100
Bob 251 SJ-West 251 850