Download presentation
Presentation is loading. Please wait.
Published byImogen Stevenson Modified over 9 years ago
1
Holliday - COEN 1781 Introduction to SQL
2
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
3
Holliday - COEN 1783 A Bank Database CustomerAccount LoanBranch street name Loan# amount name city assets makes borrower depositor has Acc# balance
4
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#)
5
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
6
Holliday - COEN 1786 The Account Table Branch-nameAccount#Balance Oakland1012000 SJ-Main2057500 SJ-Main2074500 Santa Clara3113100 SJ-West251850
7
Holliday - COEN 1787 The Loan Table Branch-nameloan#Amount Oakland23015000 SJ-Main5155700 SJ-Main57099000 Santa Clara15411800 SJ-West4321250
8
Holliday - COEN 1788 The Depositor Table Customer-nameAccount# Bob207 Carol311 Ted205 Alice101 Bob251
9
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.
10
Holliday - COEN 17810 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.
11
Holliday - COEN 17811 The Customer Table Example: create table Customer (customer-namechar(30), c-streetchar(30), c-citychar(30), primary key (customer-name) )
12
Holliday - COEN 17812 Branch Table with Constraints Example : create table Branch (branch-namechar(15), branch-citychar(30), assetsinteger, primary key (branch-name), check (assets >= 0) )
13
Holliday - COEN 17813 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.
14
Holliday - COEN 17814 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
15
Holliday - COEN 17815 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
16
Holliday - COEN 17816 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
17
Holliday - COEN 17817 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.
18
Holliday - COEN 17818 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
19
Holliday - COEN 17819 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
20
Holliday - COEN 17820 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#
21
Holliday - COEN 17821 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"
22
Depositor join Account Customer-nameAccount# Bob207 Carol311 Ted205 Alice101 Bob251 Branch-nameAccount#Balance Oakland1012000 SJ-Main2057500 SJ-Main2074500 Santa Clara3113100 SJ-West251850 Customer-nameAccount#Branch-nameAccount#Balance Alice101Oakland1012000 Ted205SJ-Main2057500 Bob207SJ-Main2074500 Carol311Santa Clara3113100 Bob251SJ-West251850
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.