CS121 Lec 01
CS121 Lec 01
Course Overview
2
Focus on:
The Relational Model and relational algebra
SQL (the Structured Query Language)
The Entity-Relationship model
Database schema design and normal forms
Various common uses of database systems
By end of course:
Should be very comfortable using relational databases
Familiar with basic relational database theory
Textbook
3
Assignments
4
Grading Policies
6
Other Administrivia
7
Database Terminology
8
Typical approach:
Ad-hoc or purpose-built data files
Special-built programs implemented various operations
against the database
Kinds of Databases
11
Data Models
12
Data Models
14
Before we start:
SQL is loosely based on the relational model
Some terms appear in both the relational model
and in SQL
but they arent exactly the same!
Relations
19
A relational database is
a set of relations
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
3 attributes
Domain of balance is the set
of nonnegative integers
account
Domain of branch_name is the set of all valid branch names
in the bank
Example:
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
account
Domain of acct_id is D1
Domain of branch_name is D2
Domain of balance is D3
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
account
Relation Schemas
24
Naming conventions:
Relation names are written as all lowercase
Relation schemas name is capitalized
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
Important note:
account
Relation Schemas
26
Examples:
Relations r(R) and s(S)
n
n
RS
n
RS
n
n
KR
n
Attribute Domains
27
Mainly a simplification
More formally:
account is a relation variable
A name associated with a
specific schema, and a set of
tuples that satisfies that schema
(sometimes abbreviated relvar)
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
Problem:
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
Distinguishing Tuples
30
Example:
Some accounts have
the same balance
Some accounts are at
the same branch
acct_id
branch_name
balance
A-301
A-307
A-318
A-319
A-322
New York
Seattle
Los Angeles
New York
Los Angeles
350
275
550
80
275
account
Keys
31
Example:
{acct_id} is a superkey
branch_name
balance
A-301
A-307
A-318
A-319
A-322
New York
Seattle
Los Angeles
New York
Los Angeles
350
275
550
80
275
acct_id
Yes! Every tuple will have a unique set of values for this
combination of attributes.
Is {branch_name} a superkey?
account
Primary Keys
33
cust_id
23-652
15-202
23-521
cust_name
cust_ssn
Joe Smith
Ellen Jones
Dave Johnson
330-25-8822
221-30-6551
005-81-2568
customer
cust_name
Joe Smith
Ellen Jones
Dave Johnson
Albert Stevens
cust_ssn
330-25-8822
221-30-6551
005-81-2568
450-22-5869
customer
n t1[R] = t2[R] is
equivalent to saying t1 = t2
Foreign Keys
38
n
n
depositor Relation
39
cust_id
23-652
15-202
23-521
cust_name
cust_ssn
Joe Smith
Ellen Jones
Dave Johnson
330-25-8822
221-30-6551
005-81-2568
acct_id
branch_name
balance
A-301
A-307
A-318
New York
Seattle
Los Angeles
350
275
550
customer
account
cust_id
15-202
23-521
23-652
acct_id
A-301
A-307
A-318
depositor
Every foreign key value must have a corresponding primary key value
Notes:
K is not required to be a candidate key for S, only R
K may also be part of a larger candidate key for S
cust_id
15-202
23-521
23-652
acct_id
A-301
A-307
A-318
depositor