Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Introduction
About courseAbout courseRelational Model, Schemas, SQL
Semistructured Model, XML
1
TextbookA First Course in Database SystemsJeff Ullman and Jennifer WidomJeff Ullman, and Jennifer WidomDatabase Systems: Complete BookHector Garcia-Molina, Jeff Ullman, and Jennifer Widomand Jennifer Widom. Veri Tabanı SistemleriÜnal Yarımağan
2
Content
Design of databases.E/R model, relational model, semistructured model, XML, UML, ODL., , ,
Database programming.SQL XPath XQuery Relational algebraSQL, XPath, XQuery, Relational algebra, Datalog.
3
ER Model
PerformsArtist SongArtist Song
4
Content
XPath (XML Path Language) is a language for selecting nodes from an XML document
5
XPath//türkü[@yöresi=”Giresun”]/@uzunluk
< 3><mp3><başlık> Türk Halk Müziği </başlık> <sanatçı>
<ad> Taner </ad><soyadı> Tezcan </soyadı>
</sanatçı><türkü yöresi="Giresun" uzunluk =”4:02”>
Giresun Kayıkları </türkü><türkü yöresi="Giresun" uzunluk =”3:52”>y
Bir Fındığın İçini </türkü><türkü yöresi="Trabzon" uzunluk =”5:10”>
Trabzon Ninnileri </türkü>
6
Trabzon Ninnileri </türkü></mp3>
Content
XQuery is a query language (with some programming language features) that is designed to query collections of g q yXML data
7
XQuery
for $s in doc(“mp3.xml”)/mp3/sanatçı return <isim> {$s/ad} </isim>
8
Do You Know SQL?
Explain the difference between:SELECT bFROM R a bFROM RWHERE a<10 OR a>=10;
a b5 2010 30
andSELECT b
20 40… …
SELECT bFROM R; R
9
And How About These?
SELECT aFROM R, SWHERE R b = S b;WHERE R.b = S.b;
SELECT aFROM RFROM RWHERE b IN (SELECT b FROM S);
10
Course Requirements
1. Firstterm (30%, Classic)2. Midterm (20%, Test)3 Final (50% Classic)3. Final (50%, Classic)
11
Database Processing
Application
Database
Application Programs
DBMSDBMS
12
Interesting Stuff About Databases
It used to be about boring stuff: employee records, bank records, etc.Today the field covers all the largestToday, the field covers all the largest sources of data, with many new ideas.
W b hWeb search.Data mining.Scientific and medical databases.Integrating information
13
Integrating information.
More Interesting Stuff
Database programming centers around limited programming languages.
14
Still More …
You may not notice it, but databases are behind almost everything you do on the Web.
Google searches.Queries at Amazon eBay etcQueries at Amazon, eBay, etc.
15
And More…
Databases often have unique concurrency-control problems
Many activities (transactions) at theMany activities (transactions) at the database at all times.Must not confuse actions e g twoMust not confuse actions, e.g., two withdrawals from the same account must each debit the accounteach debit the account.
16
Advantages of DBMS
Data IntegrityConcurrencyData SecurityData Security
17
What is a Data Model?
1. Mathematical representation of data.Examples: relational model = tables; semistructured model = trees/graphs./g p
2. Operations on data.3 C t i t3. Constraints.
18
A Relation is a Table
Attributes(column
name manfWinterbrew Pete’s
(columnheaders)
Winterbrew Pete sBud Lite Anheuser-Busch
Tuples(rows)
LemonadesLemonadesRelationname
19
name
Schemas
Relation schema = relation name and tt ib t li tattribute list.Optionally: types of attributes.Example: Lemonades(name, manf) or Lemonades(name: string, manf: string)
Database = collection of relations.Database schema = set of all relationDatabase schema = set of all relation schemas in the database.
20
Why Relations?
Very simple model.Often matches how we think about datadata.Abstract model that underlies SQL, the
i d b lmost important database language today.y
21
Our Running ExampleOur Running Example
Lemonades(name, manf)Bars(name, addr, license)Bars(name, addr, license)Drinkers(name, addr, phone)Likes(drinker lemonade)Likes(drinker, lemonade)Sells(bar, lemonade, price)Frequents(drinker, bar)
Underline = key (tuples cannot haveUnderline key (tuples cannot have the same value in all key attributes).
E ll t l f t i t22
Excellent example of a constraint.
Database Schemas in SQL
SQL is primarily a query language, for getting information from a database.But SQL also includes a data-definitionBut SQL also includes a data definitioncomponent for describing database schemasschemas.
23
Creating (Declaring) a Relation
Simplest form is:CREATE TABLE <name> (
<list of elements><list of elements>);
To delete a relation:DROP TABLE <name>;DROP TABLE <name>;
24
Elements of Table Declarations
Most basic element: an attribute and its ttype.The most common types are:yp
INT or INTEGER (synonyms).REAL or FLOAT (synonyms).REAL or FLOAT (synonyms).CHAR(n ) = fixed-length string of ncharacters.characters.VARCHAR(n ) = variable-length string of up to n characters.
25
up to n characters.
Example: Create Table
CREATE TABLE Sells (bar CHAR(20),lemonade VARCHAR(20)lemonade VARCHAR(20),price REAL
);
26
SQL Values
Integers and reals are represented as you would expect.Strings are too except they requireStrings are too, except they require single quotes.
T i l t l tTwo single quotes = real quote, e.g., ’Joe’’s Bar’.
Any value can be NULL.
27
Dates and Times
DATE and TIME are types in SQLDATE and TIME are types in SQL.The form of a date value is:
DATE ’yyyy-mm-dd’Example: DATE ’2009-03-11’ for MarchExample: DATE 2009 03 11 for March11, 2009.
28
Times as Values
The form of a time value is:The form of a time value is:TIME ’hh:mm:ss’
with an optional decimal point and fractions of a second following.fractions of a second following.
Example: TIME ’15:30:02.5’ = two and a half seconds after 3:30PMand a half seconds after 3:30PM.
29
Declaring Keys
An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE.Either says that no two tuples of theEither says that no two tuples of the relation may agree in all the attribute(s) on the liston the list.There are a few distinctions to be mentioned later.
30
Declaring Single-Attribute Keys
Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute.Example:Example:
CREATE TABLE Lemonades (name CHAR(20) UNIQUE,manf CHAR(20)manf CHAR(20)
);
31
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 attributeof more than one attribute.
May be used even for one-attribute keys.
32
Example: Multiattribute Key
The bar and lemonade together are the key for S llSells:
CREATE TABLE Sells (bar CHAR(20),lemonade VARCHAR(20),lemonade VARCHAR(20),price REAL,PRIMARY KEY (b l d )PRIMARY KEY (bar, lemonade)
);
33
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 Butever be NULL in any tuple. But attributes declared UNIQUE may have NULL’s, and there may be several tuples with NULL.
34
p
Semistructured Data
Another data model, based on trees.Motivation: flexible representation of datadata.Motivation: sharing of documents
d d bamong systems and databases.
35
Graphs of Semistructured Data
Nodes = objects.Labels on arcs (like attribute names).Atomic values at leaf nodes (nodes withAtomic values at leaf nodes (nodes with no arcs out).Flexibility: no restriction on:
Labels out of a nodeLabels out of a node.Number of successors with a given label.
36
E l D t G hExample: Data Graphroot Notice a
Lemonade Lemonadebar
rootnew kindof data.
A.B.
manfmanf
name
prize
BudGold1995M’lob
servedAtname year award
MapleJoe’s
name addr
The lemonade objectf B d
The bar objectfor Joe’s Bar
for Bud
37
XML
XML = Extensible Markup Language.While HTML uses tags for formatting (e g “italic”) XML uses tags for(e.g., italic ), XML uses tags for semantics (e.g., “this is an address”).K id f d iKey idea: create tag sets for a domain (e.g., genomics), and translate all data ( g g )into properly tagged XML documents.
38
XML Documents
Start the document with a declaration, surrounded by <?xml … ?> .Typical:Typical:
<?xml version = “1.0” encoding “ f 8”= “utf-8” ?>
Balance of document is a root tagBalance of document is a root tagsurrounding nested tags.
39
Tags
Tags, as in HTML, are normally matched pairs, as <FOO> … </FOO>.
Optional single tag <FOO/>.Optional single tag FOO/ .
Tags may be nested arbitrarily.XML tags are case sensitive.
40
Example: an XML Document
<?xml version = “1.0” encoding = “utf-8” ?><BARS>
A NAMEsubobject<BARS>
<BAR><NAME>Joe’s Bar</NAME>< LEMONADE ><NAME>Bud</NAME>
subobject
< LEMONADE ><NAME>Bud</NAME><PRICE>2.50</PRICE></ LEMONADE >
< LEMONADE ><NAME>Miller</NAME>< LEMONADE ><NAME>Miller</NAME><PRICE>3.00</PRICE></ LEMONADE >
</BAR>
A LEMONADEsubobject/
<BAR> … </BARS>
41
Attributes
Like HTML, the opening tag in XML can have atttribute = value pairs.Attributes also allow linking amongAttributes also allow linking among elements (discussed later).
42
Bars, Using Attributes
<?xml version = “1.0” encoding = “utf-8” ?><BARS>
<BAR name = “Joe’s Bar”>< LEMONADE name = “Bud” price = 2.50 />
< LEMONADE name = “Miller” price = 3 00 />< LEMONADE name = Miller price = 3.00 /></BAR><BAR> …
</BARS>Notice Lemonade elementshave only opening tags
name andprice areattributes
43
/with attributes.attributes
DTD’s (Document Type Definitions)
A grammatical notation for describing ll d f tallowed use of tags.
Definition form:<!DOCTYPE <root tag> [
t<!ELEMENT <name>(<components>)>. . . more elements . . .. . . more elements . . .
]>
44
Example: DTD
<!DOCTYPE BARS [A BARS object haszero or more BAR’snested within
<!ELEMENT BARS (BAR*)><!ELEMENT BAR (NAME, LEMONADE+)>
nested within.
A BAR has one( , )<!ELEMENT NAME (#PCDATA)><!ELEMENT LEMONADE (NAME PRICE)>
A BAR has oneNAME and oneor more
O<!ELEMENT LEMONADE (NAME, PRICE)><!ELEMENT PRICE (#PCDATA)>
LEMONADEsubobjects.
h]> A LEMONADE has aNAME and aPRICE.
NAME and PRICEare HTML text.
45
Attributes
Opening tags in XML can have attributes.In a DTDIn a DTD,
<!ATTLIST E . . . >declares an attribute for element E, along with its datatype.along with its datatype.
46
Example: Attributes No closingtag or
<!ELEMENT LEMONADE EMPTY>tag orsubelements
<!ATTLIST name CDATA #REQUIRED,manf CDATA #IMPLIED>manf CDATA #IMPLIED>
Characterstring
Required = “must occur”;Implied = “optional
Example use:< LEMONADE name=“Bud” />
47
< LEMONADE name= Bud />
Architecture of WebArchitecture of Web ApplicationsApplications
Relational Database(MySQL, Oracle, MS SQL)( y Q Q )
Middleware(PHP, ASP, JSP, AJAX)Web Server(Apache, IIS)
Internet
Web Browser(Internet Explorer, Netscape, Mozilla)
48
Apache MySQL PHP onApache, MySQL, PHP on Windows (WAMP)Windows (WAMP)
49