Holliday - COEN 1781 Introduction to SQL
Holliday - COEN 1782 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 1783 A Bank Database CustomerAccount LoanBranch street name Loan# amount name city assets makes borrower depositor has Acc# balance
Holliday - COEN 1784 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 1785 The Customer Table Customer-nameC-StreetC-city Bob123 Third StSan Jose Carol456 Main StSanta Clara Ted89 Blossom AveLos Gatos Alice64 Longwalk DrOakland
Holliday - COEN 1786 The Account Table Branch-nameAccount#Balance Oakland SJ-Main SJ-Main Santa Clara SJ-West251850
Holliday - COEN 1787 The Loan Table Branch-nameloan#Amount Oakland SJ-Main SJ-Main Santa Clara SJ-West
Holliday - COEN 1788 The Depositor Table Customer-nameAccount# Bob207 Carol311 Ted205 Alice101 Bob251
Holliday - COEN 1789 Creating a Table An SQL relation is defined with the create table command: create table (, ) 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 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 The Customer Table Example: create table Customer (customer-namechar(30), c-streetchar(30), c-citychar(30), primary key (customer-name) )
Holliday - COEN Branch Table with Constraints Example : create table Branch (branch-namechar(15), branch-citychar(30), assetsinteger, primary key (branch-name), check (assets >= 0) )
Holliday - COEN The select statement select A1, A2, … A n from r1, r2, …, r k 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 Queries on the Loan Table Loan = (branch-name, loan#, amount) Find the names of all the branches in the Loan relation select branch-name from Loan Note that the "where" clause is optional. Branch-name Oakland SJ-Main Santa Clara SJ-West
Holliday - COEN More Queries on the Loan Table An asterisk in the select clause denotes "all attributes" select * from Loan where amount > 3000 Result contains only those rows that meet the criteria
Holliday - COEN The where condition Find the loan numbers for all loans made at the Oakland branch with loan amounts greater than select loan# from Loan where branch-name="Oakland" and amount>1200
Holliday - COEN The from clause Form the cross product of the tables listed in the FROM clause Select A1, A2 from R1, R2 Result R = R 1 R 2 pairs each tuple t 1 of R 1 with each tuple t 2 of R 2 and puts in R a tuple t 1 t 2.
Holliday - COEN 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 S A B C D D E F A B C D D' E F
Holliday - COEN Cartesian Product Table A Bill Mary 4 5 two Table B A X B John Sue one four Bill Mary 4 5 Bill Mary 4 5 Bill Mary 4 5 Johnone Johnone Suefour Suefour twoBill twoBill
Holliday - COEN 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 Using the Cartesian Product Find the name of customers with an account at the Oakland branch. select customer-name from Depositor, Account where Depositor.account# = Account.account# and branch-name = "Oakland"
Depositor join Account Customer-nameAccount# Bob207 Carol311 Ted205 Alice101 Bob251 Branch-nameAccount#Balance Oakland SJ-Main SJ-Main Santa Clara SJ-West Customer-nameAccount#Branch-nameAccount#Balance Alice101Oakland Ted205SJ-Main Bob207SJ-Main Carol311Santa Clara Bob251SJ-West251850