A 1 Intro
A 1 Intro
1
Contents
Design of databases.
E/R model, relational model,
semistructured model, XML, UML,
ODL.
Database programming.
SQL, XPath, XQuery, Relational
algebra, Datalog.
2
Textbook
The closest text for the course is
First Course in Database Systems/3rd
Edition.
You may prefer Database Systems:
Complete Book
3
Do You Know SQL?
Explain the difference between:
SELECT b
FROM R a b
5 20
WHERE a<10 OR a>=10; 10 30
and 20 40
… …
SELECT b
R
FROM R;
4
And How About These?
SELECT a
FROM R, S
WHERE R.b = S.b;
SELECT a
FROM R
WHERE b IN (SELECT b FROM S);
5
Course Requirements
1. Presentation: from selected
topics
2. Homeworks:
3. Midterm and Final.
6
Interesting Stuff About
Databases
It used to be about boring stuff:
employee records, bank records, etc.
Today, the field covers all the largest
sources of data, with many new ideas.
Web search.
Data mining.
Scientific and medical databases.
Integrating information.
7
Still More …
You may not notice it, but
databases are behind almost
everything you do on the Web.
Google searches.
Queries at Amazon, eBay, etc.
8
And More…
Databases often have unique
concurrency-control problems.
Many activities (transactions) at the
database at all times.
Must not confuse actions, e.g., two
withdrawals from the same account
must each debit the account.
9
What is a Data Model?
1. Mathematical representation of
data.
Examples: relational model = tables;
semistructured model =
trees/graphs.
2. Operations on data.
3. Constraints.
10
A Relation is a Table
Attributes
(column
headers) name manf
Tuples Winterbrew Pete’s
(rows) coke Lite
Anheuser-Busch
Relation drinks
name
11
Schemas
Relation schema = relation name
and attribute list.
Optionally: types of attributes.
Example: drinks(name, manf) or
drinks(name: string, manf: string)
Database = collection of relations.
Database schema = set of all
relation schemas in the database.
12
Why Relations?
Very simple model.
Often matches how we think
about data.
Abstract model that underlies SQL,
the most important database
language today.
13
Our Running Example
drinks(name, manf)
cafes(name, addr, license)
Drinkers(name, addr, phone)
Likes(drinker, drink)
Sells(cafe, drink, price)
Frequents(drinker, cafe)
Underline = key (tuples cannot have
the same value in all key attributes).
Excellent example of a constraint.
14
Database Schemas in SQL
SQL is primarily a query language,
for getting information from a
database.
But SQL also includes a data-
definition component for
describing database schemas.
15
Creating (Declaring) a Relation
Simplest form is:
CREATE TABLE <name> (
<list of elements>
);
To delete a relation:
DROP TABLE <name>;
16
Elements of Table Declarations
Most basic element: an attribute
and its type.
The most common types are:
INT or INTEGER (synonyms).
REAL or FLOAT (synonyms).
CHAR(n ) = fixed-length string of n
characters.
VARCHAR(n ) = variable-length string
of up to n characters.
17
Example: Create Table
CREATE TABLE Sells (
cafe CHAR(20),
drink VARCHAR(20),
price REAL
);
18
SQL Values
Integers and reals are represented
as you would expect.
Strings are too, except they
require single quotes.
Two single quotes = real quote, e.g.,
’Joe’’s cafe’.
Any value can be NULL.
19
Dates and Times
20
Times as Values
The form of a time value is:
TIME ’hh:mm:ss’
with an optional decimal point and
fractions of a second following.
Example: TIME ’15:30:02.5’ = two
and a half seconds after 3:30PM.
21
Declaring Keys
An attribute or list of attributes
may be declared PRIMARY KEY or
UNIQUE.
Either says that no two tuples of
the relation may agree in all the
attribute(s) on the list.
There are a few distinctions to be
mentioned later.
22
Declaring Single-Attribute Keys
Place PRIMARY KEY or UNIQUE after
the type in the declaration of the
attribute.
Example:
CREATE TABLE drinks (
name CHAR(20) UNIQUE,
manf CHAR(20)
);
23
Declaring Multiattribute
Keys
A key declaration can also be
another element in the list of
elements of a CREATE TABLE
statement.
This form is essential if the key
consists of more than one attribute.
May be used even for one-attribute
keys.
24
Example: Multiattribute
Key
The cafe and drink together are the key for
Sells:
CREATE TABLE Sells (
cafe CHAR(20),
drink VARCHAR(20),
price REAL,
PRIMARY KEY (cafe, drink)
);
25
PRIMARY KEY vs. UNIQUE
1. There can be only one PRIMARY
KEY for a relation, but several
UNIQUE attributes.
2. No attribute of a PRIMARY KEY
can ever be NULL in any tuple.
But attributes declared UNIQUE
may have NULL’s, and there may
be several tuples with NULL.
26
Semistructured Data
Another data model, based on
trees.
Motivation: flexible representation
of data.
Motivation: sharing of documents
among systems and databases.
27
Graphs of Semistructured Data
Nodes = objects.
Labels on arcs (like attribute names).
Atomic values at leaf nodes (nodes
with no arcs out).
Flexibility: no restriction on:
Labels out of a node.
Number of successors with a given label.
28
Example: Data Graph
root Notice a
new kind
drink drink of data.
cafe
manf manf prize
name A.B.
name
servedAt coke year award
29
XML
XML = Extensible Markup Language.
While HTML uses tags for formatting
(e.g., “italic”), XML uses tags for
semantics (e.g., “this is an address”).
Key idea: create tag sets for a domain
(e.g., genomics), and translate all data
into properly tagged XML documents.
30
XML Documents
Start the document with a
declaration, surrounded by <?xml
… ?> .
Typical:
<?xml version = “1.0” encoding
= “utf-8” ?>
Balance of document is a root tag
surrounding nested tags.
31
Tags
Tags, as in HTML, are normally
matched pairs, as <FOO> …
</FOO>.
Optional single tag <FOO/>.
Tags may be nested arbitrarily.
XML tags are case sensitive.
32
Example: an XML
Document
A NAME
<?xml version = “1.0” encoding = “utf-8” ?>
<cafeS> subobject
<cafe><NAME>Joe’s cafe</NAME>
<drink><NAME>coke</NAME>
<PRICE>2.50</PRICE></drink>
<drink><NAME>pepsi</NAME>
<PRICE>3.00</PRICE></drink> A drink
subobject
</cafe>
<cafe> …
</cafeS>
33
Attributes
Like HTML, the opening tag in XML
can have atttribute = value pairs.
Attributes also allow linking among
elements (discussed later).
34
cafes, Using Attributes
<?xml version = “1.0” encoding = “utf-8” ?>
<cafeS>
<cafe name = “Joe’s cafe”>
<drink name = “coke” price = 2.50 />
<drink name = “pepsi” price =
3.00 />
</cafe>
name and Notice drink elements
<cafe> … price are
have only opening tags
</cafeS> attributes with attributes.
35
DTD’s (Document Type
Definitions)
A grammatical notation for describing
allowed use of tags.
Definition form:
<!DOCTYPE <root tag> [
<!ELEMENT
<name>(<components>)>
. . . more elements . . .
]>
36
Example: DTD
A cafeS object has
<!DOCTYPE cafeS [ zero or more cafe’s
nested within.
<!ELEMENT cafeS (cafe*)>
<!ELEMENT cafe (NAME, drink+)> A cafe has one
<!ELEMENT NAME (#PCDATA)> NAME and one
or more drink
<!ELEMENT drink (NAME, PRICE)> subobjects.
37
Attributes
Opening tags in XML can have
attributes.
In a DTD,
<!ATTLIST E . . . >
declares an attribute for element
E, along with its datatype.
38
Example: AttributesNo closing
tag or
subelements
<!ELEMENT drink EMPTY>
<!ATTLIST name CDATA #REQUIRED,
manf CDATA #IMPLIED>
Example use:
<drink name=“coke” />
39