Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SQL Holliday - COEN 178.

Similar presentations


Presentation on theme: "Introduction to SQL Holliday - COEN 178."— Presentation transcript:

1 Introduction to SQL 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 name Acc# balance street depositor Customer Account
city borrower has makes name Loan Branch Loan# city amount assets 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 Table Customer-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 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 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 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
Branch-name Oakland SJ-Main Santa Clara SJ-West 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. Holliday - COEN 178

15 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 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 Loan where 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, R2 Result 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 arity(R) = k1 arity(R  S) = k1 + k2
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 178

19 Cartesian Product Table A Table B Bill 4 John one Mary 5 Sue four Bill
two A X B Bill 4 John one Bill 4 Sue four Bill 4 Bill two Mary 5 John one Mary 5 Sue four Mary 5 Bill two 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-name from Depositor, Account where Depositor.account# = Account.account# and branch-name = "Oakland" Holliday - COEN 178

22 Depositor join Account
Customer-name Account# Bob 207 Carol 311 Ted 205 Alice 101 251 Branch-name Account# Balance Oakland 101 2000 SJ-Main 205 7500 207 4500 Santa Clara 311 3100 SJ-West 251 850 Customer-name Account# Branch-name Balance Alice 101 Oakland 2000 Ted 205 SJ-Main 7500 Bob 207 4500 Carol 311 Santa Clara 3100 251 SJ-West 850


Download ppt "Introduction to SQL Holliday - COEN 178."

Similar presentations


Ads by Google