Database PDF
Database PDF
Kostis Sagonas
Introduction to Databases 2
Q Databases touch all aspects of our lives Hard to add new constraints or change existing ones
Q Drawbacks of using file systems (cont.) Q Physical level describes how a record (e.g., customer) is stored.
+ Atomicity of updates Q Logical level: describes data stored in database, and the
Failures may leave database in an inconsistent state with partial relationships among the data.
updates carried out type customer = record
E.g. transfer of funds from one account to another should either name : string;
complete or not happen at all street : string;
+ Concurrent access by multiple users city : integer;
Concurrent accessed needed for performance end;
Uncontrolled concurrent accesses can lead to inconsistencies Q View level: application programs hide details of data types.
– E.g. two people reading a balance and updating it at the same Views can also hide information (e.g., salary) for security
time purposes.
+ Security problems
Q Database systems offer solutions to all the above problems
1
Instances and Schemas Data Models
192-83-7465 Johnson
Alma Palo Alto A-101
019-28-3746 Smith
North Rye A-215
192-83-7465 Johnson
Alma Palo Alto A-201
321-12-3123 Jones
Main Harrison A-217
019-28-3746 Smith
North Rye A-201
2
Data Definition Language (DDL) Data Manipulation Language (DML)
Q Specification notation for defining the database schema Q Language for accessing and manipulating the data organized by
+ E.g. the appropriate data model
create table account ( + DML also known as query language
account-number char(10),
Q Two classes of languages
balance integer)
+ Procedural – user specifies what data is required and how to get
Q DDL compiler generates a set of tables stored in a data those data
dictionary
+ Nonprocedural – user specifies what data is required without
Q Data dictionary contains metadata (i.e., data about data) specifying how to get those data
+ database schema Q SQL is the most widely used query language
+ Data storage and definition language
language in which the storage structure and access methods
used by the database system are specified
Usually an extension of the data definition language
SQL
Introduction to Databases 15
3
Basic Structure Attribute Types
Q Formally, given sets D1, D2, …. Dn a relation r is a subset of Q Each attribute of a relation has a name
D1 x D2 x … x Dn Q The set of allowed values for each attribute is called the domain
Thus a relation is a set of n-tuples (a1, a2, …, an) where of the attribute
ai ∈ D i
Q Attribute values are (normally) required to be atomic, that is,
Q Example: if indivisible
customer-name = {Jones, Smith, Curry, Lindsay} + E.g. multivalued attribute values are not atomic
customer-street = {Main, North, Park}
+ E.g. composite attribute values are not atomic
customer-city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield)}
is a relation over customer-name x customer-street x customer-city
customer
Database
Relations are Unordered
Q Order of tuples is irrelevant (tuples may be stored in an arbitrary order) Q A database consists of multiple relations
Q E.g. account relation with unordered tuples Q Information about an enterprise is broken up into parts, with each
relation storing one part of the information
4
The customer Relation The depositor Relation
Q Let K ⊆ R
Q K is a superkey of R if values for K are sufficient to identify a
unique tuple of each possible relation r(R) by “possible r” we
mean a relation r that could exist in the enterprise we are
modeling.
Example: {customer-name, customer-street} and
{customer-name}
are both superkeys of Customer, if no two customers can
possibly have the same name.
Q K is a candidate key if K is minimal
Example: {customer-name} is a candidate key for Customer,
since it is a superkey {assuming no two customers can possibly
have the same name), and no subset of it is a superkey.
Determining Keys from E-R Sets Schema Diagram for the Banking Enterprise
Q Strong entity set. The primary key of the entity set becomes
the primary key of the relation.
Q Weak entity set. The primary key of the relation consists of the
union of the primary key of the strong entity set and the
discriminator of the weak entity set.
Q Relationship set. The union of the primary keys of the related
entity sets becomes a super key of the relation.
+ For binary many-to-one relationship sets, the primary key of the
“many” entity set becomes the relation’s primary key.
+ For one-to-one relationship sets, the relation’s primary key can be
that of either entity set.
+ For many-to-many relationship sets, the union of the primary keys
becomes the relation’s primary key
5
Query Languages Relational Algebra
Q Language in which user requests information from the database. Q Procedural language
Q Categories of languages Q Six basic operators
+ procedural + select
+ non-procedural + project
Q “Pure” languages: + union
+ Relational Algebra + set difference
+ Tuple Relational Calculus + Cartesian product
+ Domain Relational Calculus + rename
Q Pure languages form underlying basis of query languages that Q The operators take two or more relations as inputs and give a
people use. new relation as a result.
Q Notation: σ p(r)
• Relation r A B C D
Q p is called the selection predicate
α α 1 7 Q Defined as:
α β 5 7 σp(r) = {t | t ∈ r and p(t)}
β β 12 3 Where p is a formula in propositional calculus consisting
of terms connected by : ∧ (and), ∨ (or), ¬ (not)
β β 23 10
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, ≠, >, ≥. <. ≤
• σA=B ^ D > 5 (r)
A B C D Q Example of selection:
σ branch-name=“Perryridge”(account)
α α 1 7
β β 23 10
Q Notation:
α 1 α 1
α 1 = β 1
β 1 β 2
β 2
6
Union Operation – Example Union Operation
Q Notation: r ∪ s
Q Relations r, s: A B A B
Q Defined as:
α 1 α 2 r ∪ s = {t | t ∈ r or t ∈ s}
α 2 β 3
β 1 s Q For r ∪ s to be valid:
r 1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column
of r deals with the same type of values as does the 2nd
r ∪ s: A B column of s)
Q Notation r – s
Q Relations r, s: A B A B
Q Defined as:
α 1 α 2 r – s = {t | t ∈ r and t ∉ s}
α 2 β 3 Q Set differences must be taken between compatible relations.
+ r and s must have the same arity
β 1 s
+ attribute domains of r and s must be compatible
r
r – s: A B
α 1
β 1
A B C D E
Q Notation r x s
Relations r, s:
Q Defined as:
α 1 α 10 a
β 10 a r x s = {t q | t ∈ r and q ∈ s}
β 2
β 20 b Q Assume that attributes of r(R) and s(S) are disjoint. (That is,
r γ 10 b R ∩ S = ∅).
s
Q If attributes of r(R) and s(S) are not disjoint, then renaming must
r x s:
be used.
A B C D E
α 1 α 10 a
α 1 β 19 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
7
Composition of Operations Rename Operation
Q Can build expressions using multiple operations Q Allows us to name, and therefore to refer to, the results of
Q Example: σA=C(r x s) relational-algebra expressions.
α 1 α 10 a
β 2 β 20 a
β 2 β 20 b
Introduction to Databases 43 Introduction to Databases 44
Q Find the names of all customers who have a loan at the Perryridge
Q Find the names of all customers who have a loan, an account, or branch.
both, from the bank
∏customer-name (σbranch-name=“Perryridge”
∏customer-name (borrower) ∪ ∏customer-name (depositor)
(σborrower.loan-number = loan.loan-number(borrower x loan)))
Q Find the names of all customers who have a loan at the Perryridge
Q Find the names of all customers who have a loan and an account
branch but do not have an account at any branch of the bank.
at bank.
∏customer-name (borrower) ∩ ∏customer-name (depositor) ∏customer-name (σbranch-name = “Perryridge”
– ∏customer-name(depositor)
8
Example Queries Example Queries
Q Find the names of all customers who have a loan at the Perryridge Find the largest account balance
branch. + Rename account relation as d
+ Query 1 + The query then is:
∏customer-name(σbranch-name = “Perryridge” ∏balance(account) - ∏account.balance
(σborrower.loan-number = loan.loan-number(borrower x loan))) (σaccount.balance < d.balance (account x ρd (account)))
+ Query 2
∏customer-name(σloan.loan-number = borrower.loan-number(
(σbranch-name = “Perryridge”(loan)) x
borrower)
)
Q Notation: r ∩ s Q Relation r, s: A B A B
Q Defined as: α 1 α 2
Q r ∩ s ={ t | t ∈ r and t ∈ s } α 2 β 3
β 1
Q Assume:
+ r, s have the same arity r s
+ attributes of r and s are compatible Q r∩s A B
Q Note: r ∩ s = r - (r - s)
α 2
9
Natural-Join Operation Natural Join Operation – Example
Q Notation: r s
Q Relations r, s:
Q Let r and s be relations on schemas R and S respectively.The result is a
relation on schema R ∪ S which is obtained by considering each pair of A B C D B D E
tuples tr from r and ts from s.
Q If tr and ts have the same value on each of the attributes in R ∩ S, a tuple t α 1 α a 1 a α
is added to the result, where β 2 γ a 3 a β
γ 4 β b 1 a γ
+ t has the same value as tr on r
α 1 γ a 2 b δ
+ t has the same value as ts on s δ 2 β b 3 b ∈
Q Example: r s
R = (A, B, C, D)
S = (E, B, D) r s A B C D E
Q Result schema = (A, B, C, D, E)
α 1 α a α
Q r s is defined as:
α 1 α a γ
∏r.A, r.B, r.C, r.D, s.E (σr.B = s.B r.D = s.D (r x s)) α 1 γ a α
α 1 γ a γ
δ 2 β b δ
r÷s Relations r, s: A B B
Q Suited to queries that include the phrase “for all”. α 1 1
α 2
Q Let r and s be relations on schemas R and S respectively
α 3 2
where
β 1 s
+ R = (A1, …, Am, B1, …, Bn) γ 1
+ S = (B1, …, Bn) δ 1
The result of r ÷ s is a relation on schema δ 3
δ 4
R – S = (A1, …, Am)
∈ 6
∈ 1
r ÷ s = { t | t ∈ ∏ R-S(r) ∧ ∀ u ∈ s ( tu ∈ r ) } β 2
r ÷ s: A r
α
β
Relations r, s: Q Property
A B C D E D E
+ Let q – r ÷ s
α a α a 1 a 1
α a γ a 1 b 1 + Then q is the largest relation satisfying q x s ⊆ r
α a γ b 1 s Q Definition in terms of the basic algebra operation
β a γ a 1 Let r(R) and s(S) be relations, and let S ⊆ R
β a γ b 3
γ a γ a 1
γ a γ b 1 r ÷ s = ∏R-S (r) –∏R-S ( (∏R-S (r) x s) – ∏R-S,S(r))
γ a β b 1
r To see why
+ ∏R-S,S(r) simply reorders attributes of r
r ÷ s: A B C
+ ∏R-S(∏R-S (r) x s) – ∏R-S,S(r)) gives those tuples t in
α a γ
γ a γ ∏R-S (r) such that for some tuple u ∈ s, tu ∉ r.
10
Assignment Operation Example Queries
Q The assignment operation (←) provides a convenient way to Q Find all customers who have an account from at least the
express complex queries, write query as a sequential program “Downtown” and the Uptown” branches.
consisting of a series of assignments followed by an expression
+ Query 1
whose value is displayed as a result of the query.
Q Assignment must always be made to a temporary relation ∏CN(σBN=“Downtown”(depositor account)) ∩
variable.
Q Example: Write r ÷ s as ∏CN(σBN=“Uptown”(depositor account))
11
Aggregate Operation – Example Aggregate Operation – Example
Q Relation r:
A B C
Q Relation account grouped by branch-name:
α α 7
branch-name account-number balance
α β 7
β β 3 Perryridge A-102 400
Perryridge A-201 900
β β 10 Brighton A-217 750
Brighton A-215 750
Redwood A-222 700
sum-C (account)
J
branch-name sum(balance)
sum(c) (r)
27
branch-name balance
Perryridge 1300
Brighton 1500
Redwood 700
Q Result of aggregation does not have a name Q The content of the database may be modified using the following
+ Can use rename operation to give it a name operations:
+ Deletion
+ For convenience, we permit renaming as part of aggregate
operation + Insertion
+ Updating
branch-name sum(balance) as sum-balance (account)
Q All these operations are expressed using the assignment
operator.
12
Insertion Insertion Examples
Q To insert data into a relation, we either: Q Insert information in the database specifying that Smith has
+ specify a tuple to be inserted $1200 in account A-973 at the Perryridge branch.
+ write a query whose result is a set of tuples to be inserted account ← account ∪ {(“Perryridge”, A-973, 1200)}
Q in relational algebra, an insertion is expressed by: depositor ← depositor ∪ {(“Smith”, A-973)}
r← r ∪ E
where r is a relation and E is a relational algebra expression. Q Provide as a gift for all loan customers in the Perryridge branch,
a $200 savings account. Let the loan number serve as the
Q The insertion of a single tuple is expressed by letting E be a
account number for the new savings account.
constant relation containing one tuple.
r1 ← (σbranch-name = “Perryridge” (borrower loan))
account ← account ∪ ∏branch-name, account-number,200 (r1)
depositor ← depositor ∪ ∏customer-name, loan-number,(r1)
Q A mechanism to change a value in a tuple without charging all Q Make interest payments by increasing all balances by 5 percent.
values in the tuple account ← ∏ AN, BN, BAL * 1.05 (account)
Q Use the generalized projection operator to do this task where AN, BN and BAL stand for account-number, branch-name
r ← ∏ F1, F2, …, FI, (r) and balance, respectively.
End of Part II
Introduction to Databases 78
13
Loan Number and the Amount of the Loan Names of All Customers Who Have
Either a Loan or an Account
Result of Πcustomer-name
Result of σ branch-name = “Perryridge” (borrower × loan)
14
Largest Account Balance in the Bank Customers Who Live on the Same Street and In the
Same City as Smith
Customers With Both an Account and a Loan Result of Πcustomer-name, loan-number, amount
at the Bank (borrower loan)
15
Result of Πcustomer-name, branch-name(depositor account) The credit-info Relation
16
Result of branch-name ς sum salary, max(salary) as Names of All Customers Who Have a
max-salary (pt-works) Loan at the Perryridge Branch
17
SQL Basic Structure
Q Basic Structure
Q SQL is based on set and relational operations with certain
Q Set Operations modifications and enhancements
Q Aggregate Functions Q A typical SQL query has the form:
Q Nested Subqueries select A1, A2, ..., An
Q Derived Relations from r1, r2, ..., rm
where P
Q Modification of the Database
+ Ais represent attributes
Q Data Definition Language + ris represent relations
Q Embedded SQL, ODBC and JDBC + P is a predicate.
Q This query is equivalent to the relational algebra expression.
Q The select clause corresponds to the projection operation of the Q SQL allows duplicates in relations as well as in query results.
relational algebra. It is used to list the attributes desired in the result of
a query. Q To force the elimination of duplicates, insert the keyword distinct
Q Find the names of all branches in the loan relation after select.
select branch-name Find the names of all branches in the loan relations, and remove
from loan duplicates
Q In the “pure” relational algebra syntax, the query would be: select distinct branch-name
∏branch-name(loan) from loan
Q An asterisk in the select clause denotes “all attributes”
Q The keyword all specifies that duplicates not be removed.
select *
from loan select all branch-name
from loan
NOTES:
+ SQL does not permit the ‘-’ character in names, so you would use, for
example, branch_name instead of branch-name in a real implementation.
We use ‘-’ since it looks nicer!
+ SQL names are case insensitive.
Q The select clause can contain arithmetic expressions involving Q The where clause corresponds to the selection predicate of the
the operation, +, –, ∗, and /, and operating on constants or relational algebra. If consists of a predicate involving attributes
attributes of tuples. of the relations that appear in the from clause.
Q The query: Q The find all loan number for loans made a the Perryridge branch
select loan-number, branch-name, amount ∗ 100 with loan amounts greater than $1200.
from loan select loan-number
from loan
would return a relation which is the same as the loan relations, where branch-name = ‘Perryridge’ and amount > 1200
except that the attribute amount is multiplied by 100.
Q Comparison results can be combined using the logical
connectives and, or, and not.
Q Comparisons can be applied to results of arithmetic expressions.
18
The where Clause (Cont.) The from Clause
Q SQL Includes a between comparison operator in order to simplify Q The from clause corresponds to the Cartesian product operation of the
where clauses that specify that a value be less than or equal to relational algebra. It lists the relations to be scanned in the evaluation of
some value and greater than or equal to some other value. the expression.
Q Find the loan number of those loans with loan amounts between Q Find the Cartesian product borrower x loan
$90,000 and $100,000 (that is, ≥$90,000 and ≤$100,000) select ∗
select loan-number from borrower, loan
from loan
Q Find the name, loan number and loan amount of all customers having a
where amount between 90000 and 100000
loan at the Perryridge branch.
select customer-name, borrower.loan-number, amount
from borrower, loan
where borrower.loan-number = loan.loan-number and
branch-name = ‘Perryridge’
Q The SQL allows renaming relations and attributes using the as Q Tuple variables are defined in the from clause via the use of the
clause: as clause.
old-name as new-name Q Find the customer names and their loan numbers for all
Q Find the name, loan number and loan amount of all customers; customers having a loan at some branch.
rename the column name loan-number as loan-id. select customer-name, T.loan-number, S.amount
from borrower as T, loan as S
select customer-name, borrower.loan-number as loan-id, amount where T.loan-number = S.loan-number
from borrower, loan Q Find the names of all branches that have greater assets than
where borrower.loan-number = loan.loan-number some branch located in Brooklyn.
Q SQL includes a string-matching operator for comparisons on character Q List in alphabetic order the names of all customers having a loan
strings. Patterns are described using two special characters: in Perryridge branch
+ percent (%). The % character matches any substring.
select distinct customer-name
+ underscore (_). The _ character matches any character.
from borrower, loan
Q Find the names of all customers whose street includes the substring where borrower loan-number - loan.loan-number and
“Main”. branch-name = ¶Perryridge·
select customer-name order by customer-name
from customer
Q We may specify desc for descending order or asc for ascending
where customer-street like ¶%Main%·
order, for each attribute; ascending order is the default.
Q Match the name “Main%”
+ E.g. order by customer-name desc
like ¶Main\%· escape ¶\·
Q SQL supports a variety of string operations such as
+ concatenation (using “||”)
+ converting from upper to lower case (and vice versa)
+ finding string length, extracting substrings, etc.
19
Duplicates Duplicates (Cont.)
Q In relations with duplicates, SQL can define how many copies of Q Example: Suppose multiset relations r1 (A, B) and r2 (C)
tuples appear in the result. are as follows:
Q Multiset versions of some of the relational algebra operators – r1 = {(1, a) (2,a)} r2 = {(2), (3), (3)}
given multiset relations r1 and r2: Q Then ΠB(r1) would be {(a), (a)}, while ΠB(r1) x r2 would be
1. If there are c1 copies of tuple t1 in r1, and t1 satisfies selections σθ,, {(a,2), (a,2), (a,3), (a,3), (a,3), (a,3)}
then there are c1 copies of t1 in σθ (r1). Q SQL duplicate semantics:
2. For each copy of tuple t1 in r1, there is a copy of tuple ΠA(t1) in ΠA(r1) select A1,, A2, ..., An
where ΠA(t1) denotes the projection of the single tuple t1. from r1, r2, ..., rm
3. If there are c1 copies of tuple t1 in r1 and c2 copies of tuple t2 in r2, where P
there are c1 x c2 copies of the tuple t1. t2 in r1 x r2
is equivalent to the multiset version of the expression:
Π A1,, A2, ..., An(σP (r1 x r2 x ... x rm))
Q The set operations union, intersect, and except operate on Q Find all customers who have a loan, an account, or both:
relations and correspond to the relational algebra operations (select customer-name from depositor)
∪, ∩, −. union
Q Each of the above operations automatically eliminates (select customer-name from borrower)
duplicates; to retain all duplicates use the corresponding multiset Q Find all customers who have both a loan and an account.
versions union all, intersect all and except all. (select customer-name from depositor)
intersect
Suppose a tuple occurs m times in r and n times in s, then, it (select customer-name from borrower)
occurs: Q Find all customers who have an account but no loan.
+ m + n times in r union all s (select customer-name from depositor)
+ min(m,n) times in r intersect all s except
(select customer-name from borrower)
+ max(0, m – n) times in r except all s
Q These functions operate on the multiset of values of a column of Q Find the average account balance at the Perryridge branch.
a relation, and return a value select avg (balance)
avg: average value from account
min: minimum value where branch-name = ‘Perryridge’
max: maximum value Q Find the number of tuples in the customer relation.
sum: sum of values
count: number of values select count (*)
from customer
Q Find the number of depositors in the bank.
select count (distinct customer-name)
from depositor
20
Aggregate Functions – Group By Aggregate Functions – Having Clause
Note: Attributes in select clause outside of aggregate functions Note: predicates in the having clause are applied after the
formation of groups whereas predicates in the where clause are
must appear in group by list
applied before forming groups
Q SQL provides a mechanism for the nesting of subqueries. Q Find all customers who have both an account and a loan at the
Q A subquery is a select-from-where expression that is nested bank.
within another query. select distinct customer-name
Q A common use of subqueries is to perform tests for set from borrower
membership, set comparisons, and set cardinality. where customer-name in (select customer-name
from depositor)
Q Find all customers who have a loan at the bank but do not have
an account at the bank
select distinct customer-name
from borrower
where customer-name not in (select customer-name
from depositor)
Q Find all customers who have both an account and a loan at the Q Find all branches that have greater assets than some branch
Perryridge branch located in Brooklyn.
select distinct customer-name select distinct T.branch-name
from borrower, loan from branch as T, branch as S
where borrower.loan-number = loan.loan-number and where T.assets > S.assets and
branch-name = “Perryridge” and S.branch-city = ¶Brooklyn·
(branch-name, customer-name) in Q Same query using > some clause
(select branch-name, customer-name
from depositor, account select branch-name
where depositor.account-number = from branch
account.account-number) where assets > some
(select assets
from branch
Q Note: Above query can be written in a much simpler manner. where branch-city = ¶Brooklyn·)
The formulation above is simply to illustrate SQL features.
21
Definition of Some Clause Definition of all Clause
0 0
(5< some 5 ) = true (5< all 5 ) = false
(read: 5 < some tuple in the relation)
6 6
0 6
(5< some 5 ) = false (5< all 10 ) = true
0 4
(5 = some 5 ) = true (5 = all 5 ) = false
0 4
(5 ≠ some 5 ) = true (since 0 ≠ 5) (5 ≠ all 6 ) = true (since 5 ≠ 4 and 5 ≠ 6)
(= some) ≡ in (≠ all) ≡ not in
However, (≠ some) ≡ not in However, (= all) ≡ in
Introduction to Databases 127 Introduction to Databases 128
Q Find the names of all branches that have greater assets than all Q The exists construct returns the value true if the argument
branches located in Brooklyn. subquery is nonempty.
select branch-name Q exists r ⇔ r ≠ Ø
from branch Q not exists r ⇔ r = Ø
where assets > all
(select assets
from branch
where branch-city = ‘Brooklyn’)
22
Example Query Example Queries
Q Find all customers who have at least two accounts at the Q A view consisting of branches and their customers
Perryridge branch. create view all-customer as
(select branch-name, customer-name
select distinct T.customer-name from depositor, account
from depositor T where depositor.account-number = account.account-number)
where not unique ( union
select R.customer-name (select branch-name, customer-name
from account, depositor as R from borrower, loan
where T.customer-name = R.customer-name and where borrower.loan-number = loan.loan-number)
R.account-number = account.account-number and Q Find all customers of the Perryridge branch
account.branch-name = ¶Perryridge·)
select customer-name
from all-customer
where branch-name = ‘Perryridge’
Q Delete the record of all accounts with balances below the Q Add a new tuple to account
average at the bank. insert into account
delete from account values (‘A-9732’, ‘Perryridge’,1200)
where balance < (select avg (balance) or equivalently
from account)
+ Problem: as we delete tuples from deposit, the average balance insert into account (branch-name, balance, account-number)
changes values (‘Perryridge’, 1200, ‘A-9732’)
+ Solution used in SQL: Q Add a new tuple to account with balance set to null
1. First, compute avg balance and find all tuples to delete insert into account
2. Next, delete all tuples found above (without recomputing avg or values (‘A-777’,‘Perryridge’, null)
retesting the tuples)
23
Modification of the Database – Insertion Modification of the Database – Updates
Q Provide as a gift for all loan customers of the Perryridge branch, a Q Increase all accounts with balances over $10,000 by 6%, all
$200 savings account. Let the loan number serve as the account other accounts receive 5%.
number for the new savings account
+ Write two update statements:
insert into account
update account
select loan-number, branch-name, 200
set balance = balance ∗ 1.06
from loan
where balance > 10000
where branch-name = ‘Perryridge’
insert into depositor
select customer-name, loan-number update account
from loan, borrower set balance = balance ∗ 1.05
where branch-name = ‘Perryridge’ where balance ≤ 10000
and loan.account-number = borrower.account-number
Q The select from where statement is fully evaluated before any of
+ The order is important!
its results are inserted into the relation (otherwise queries like
insert into table1 select * from table1
would cause problems
create table r (A1 D1, A2 D2, ..., An Dn, Q check (P), where P is a predicate
(integrity-constraint1),
..., Example: Declare branch-name as the primary key for
(integrity-constraintk)) branch and ensure that the values of assets are non-
negative.
+ r is the name of the relation
create table branch
+ each Ai is an attribute name in the schema of relation r (branch-name char(15),
+ Di is the data type of values in the domain of attribute Ai branch-city char(30)
Q Example: assets integer,
primary key (branch-name),
create table branch check (assets >= 0))
(branch-name char(15) not null,
branch-city char(30), primary key declaration on an attribute automatically
assets integer) ensures not null in SQL-92 onwards, needs to be
explicitly stated in SQL-89
Introduction to Databases 143 Introduction to Databases 144
24
Drop and Alter Table Constructs SQL Data Definition for Part of the Bank Database
Q The open statement causes the query to be evaluated Q JDBC is a Java API for communicating with database systems
EXEC SQL open c END-EXEC supporting SQL
Q The fetch statement causes the values of one tuple in the query Q JDBC supports a variety of features for querying and updating
result to be placed on host language variables. data, and for retrieving query results
EXEC SQL fetch c into :cn, :cc END-EXEC Q JDBC also supports metadata retrieval, such as querying about
Repeated calls to fetch get successive tuples in the query result relations present in the database and the names and types of
relation attributes
Q A variable called SQLSTATE in the SQL communication area
(SQLCA) gets set to ‘02000’ to indicate no more data is available Q Model for communicating with the database:
+ Open a connection
Q The close statement causes the database system to delete the
+ Create a “statement” object
temporary relation that holds the result of the query.
+ Execute queries using the Statement object to send queries and
EXEC SQL close c END-EXEC
fetch results
Note: above details vary with language. E.g. the Java embedding + Exception mechanism to handle errors
defines Java iterators to step through result tuples.
25
JDBC Code JDBC Code (Cont.)
public static void JDBCexample(String dbid, String userid, String passwd)
Q Update to database
{
try {
try {
stmt.executeUpdate( "insert into account values
Class.forName ("oracle.jdbc.driver.OracleDriver"); ('A-9732', 'Perryridge', 1200)");
Connection conn = DriverManager.getConnection( } catch (SQLException sqle) {
"jdbc:oracle:thin:@aura.bell-labs.com:2000:bankdb", userid, passwd);
System.out.println("Could not insert tuple. " + sqle);
Statement stmt = conn.createStatement();
}
… Do Actual Work ….
Q Execute query and fetch and print results
stmt.close();
ResultSet rset = stmt.executeQuery( "select branch_name, avg(balance)
conn.close(); from account
} group by branch_name");
catch (SQLException sqle) { while (rset.next()) {
System.out.println("SQLException : " + sqle); System.out.println(
} rset.getString("branch_name") + " " + rset.getFloat(2));
} }
26