Computing & Information Sciences Kansas State University Wednesday, 03 Sep 2008 CIS 560: Database System Concepts Lecture 3 of 42 Wednesday, 03 September 2008 William H. Hsu Department of Computing and Information Sciences, KSU KSOL course page: http://snipurl.com/1pq4c Course web site: http://www.kddresearch.org/Courses/Fall-2008/CIS560 Instructor home page: http:// www.cis.ksu.edu/~bhsu Reading for Next Class: Chapter 2, Silberschatz et al., 5 th edition – next week Problem Set 1 Relational Databases Discussion: Problem Set 1
40
Embed
Computing & Information Sciences Kansas State University Wednesday, 03 Sep 2008CIS 560: Database System Concepts Lecture 3 of 42 Wednesday, 03 September.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Lecture 3 of 42
Wednesday, 03 September 2008
William H. Hsu
Department of Computing and Information Sciences, KSU
KSOL course page: http://snipurl.com/1pq4c
Course web site: http://www.kddresearch.org/Courses/Fall-2008/CIS560
Instructor home page: http://www.cis.ksu.edu/~bhsu
Reading for Next Class:
Chapter 2, Silberschatz et al., 5th edition – next week
Problem Set 1
Relational DatabasesDiscussion: Problem Set 1
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Relational Algebra:Review
Relational Algebra:Review
Procedural language Six basic operators
select: project: union: set difference: – Cartesian product: x rename:
The operators take one or two relations as inputs and produce a new relation as a result.
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Example QueriesExample Queries
Find the names of all customers who have a loan at the Perryridge branch.
Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.
customer_name (branch_name = “Perryridge”
(borrower.loan_number = loan.loan_number(borrower x loan))) –
customer_name(depositor)
customer_name (branch_name=“Perryridge”
(borrower.loan_number = loan.loan_number(borrower x loan)))
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Example QueriesExample Queries
Find the names of all customers who have a loan at the Perryridge branch.
Notation: r s Defined as: r s = { t | t r and t s } Assume:
r, s have the same arity attributes of r and s are compatible
Note: r s = r – (r – s)
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Set-Intersection Operation – ExampleSet-Intersection Operation – Example
Relation r, s:
r s
A B
121
A B
23
r s
A B
2
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Notation: r s
Natural-Join OperationNatural-Join Operation
Let r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows: Consider each pair of tuples tr from r and ts from s.
If tr and ts have the same value on each of the attributes in R S, add a
tuple t to the result, where t has the same value as tr on r
t has the same value as ts on s
Example:R = (A, B, C, D)
S = (E, B, D) Result schema = (A, B, C, D, E) r s is defined as:
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Deletion:Review
Deletion:Review
A delete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database.
Can delete only whole tuples; cannot delete values on only particular attributes
A deletion is expressed in relational algebra by:
r r – E
where r is a relation and E is a relational algebra query.
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Deletion Examples:Review
Deletion Examples:Review
Delete all account records in the Perryridge branch.
Delete all accounts at branches located in Needham.
r1 branch_city = “Needham” (account branch )
r2 branch_name, account_number, balance (r1)
r3 customer_name, account_number (r2 depositor)
account account – r2
depositor depositor – r3
Delete all loan records with amount in the range of 0 to 50
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Insertion:Review
Insertion:Review
To insert data into a relation, we either: specify a tuple to be inserted write a query whose result is a set of tuples to be inserted
in relational algebra, an insertion is expressed by:
r r E
where r is a relation and E is a relational algebra expression. The insertion of a single tuple is expressed by letting E be a
constant relation containing one tuple.
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Insertion Examples:Review
Insertion Examples:Review
Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch.
Provide as a gift for all loan customers in the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account.
primary key declaration on an attribute automatically ensures not null in SQL-92 onwards, needs to be explicitly stated in SQL-89
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Drop and Alter Table Constructs:Review
Drop and Alter Table Constructs:Review
The drop table command deletes all information about the dropped relation from the database.
The alter table command is used to add attributes to an existing relation:
alter table r add A D
where A is the name of the attribute to be added to relation r and D is the domain of A. All tuples in the relation are assigned null as the value for the
new attribute.
The alter table command can also be used to drop attributes of a relation:
alter table r drop A
where A is the name of an attribute of relation r Dropping of attributes not supported by many databases
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Basic Query Structure:Review
SQL is based on set and relational operations with certain modifications and enhancements
A typical SQL query has the form:
select A1, A2, ..., An
from r1, r2, ..., rm
where P
Ai represents an attribute Ri represents a relation P is a predicate.
This query is equivalent to the relational algebra expression.
The result of an SQL query is a relation.
))(( 21,,, 21 mPAAA rrrn
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System Concepts
Test for Absence ofDuplicate Tuples
Test for Absence ofDuplicate Tuples
The unique construct tests whether a subquery has any duplicate tuples in its result.
Find all customers who have at most one account at the Perryridge branch.
select T.customer_name from depositor as T where unique (
select R.customer_name from account, depositor as R where T.customer_name = R.customer_name and
R.account_number = account.account_number and
account.branch_name = ‘ Perryridge’ )
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System ConceptsCIS 560: Database System Concepts
SummarySummary
Relational Algebra Relational Joins
Based on mathematical relations Operators
Computing & Information SciencesKansas State University
Wednesday, 03 Sep 2008CIS 560: Database System ConceptsCIS 560: Database System Concepts
TerminologyTerminology
Database Management Systems (DBMS) Data Manipulation Languages (DMLs) Data Description Languages (DDLs) Client-Server Architecture Relational Databases
Entity Relationship
Relations Subsets of Cartesian product of two or more sets Functions