49
Introduction About course About course Relational Model, Schemas, SQL Semistructured Model, XML 1

intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Introduction

About courseAbout courseRelational Model, Schemas, SQL

Semistructured Model, XML

1

Page 2: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 3: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 4: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

ER Model

PerformsArtist SongArtist Song

4

Page 5: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Content

XPath (XML Path Language) is a language for selecting nodes from an XML document

5

Page 6: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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>

Page 7: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Content

XQuery is a query language (with some programming language features) that is designed to query collections of g q yXML data

7

Page 8: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

XQuery

for $s in doc(“mp3.xml”)/mp3/sanatçı return <isim> {$s/ad} </isim>

8

Page 9: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 10: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 11: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Course Requirements

1. Firstterm (30%, Classic)2. Midterm (20%, Test)3 Final (50% Classic)3. Final (50%, Classic)

11

Page 12: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Database Processing

Application

Database

Application Programs

DBMSDBMS

12

Page 13: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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.

Page 14: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

More Interesting Stuff

Database programming centers around limited programming languages.

14

Page 15: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 16: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 17: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Advantages of DBMS

Data IntegrityConcurrencyData SecurityData Security

17

Page 18: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 19: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

A Relation is a Table

Attributes(column

name manfWinterbrew Pete’s

(columnheaders)

Winterbrew Pete sBud Lite Anheuser-Busch

Tuples(rows)

LemonadesLemonadesRelationname

19

name

Page 20: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 21: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 22: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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.

Page 23: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 24: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 25: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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.

Page 26: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Example: Create Table

CREATE TABLE Sells (bar CHAR(20),lemonade VARCHAR(20)lemonade VARCHAR(20),price REAL

);

26

Page 27: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 28: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 29: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 30: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 31: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 32: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 33: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 34: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 35: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Semistructured Data

Another data model, based on trees.Motivation: flexible representation of datadata.Motivation: sharing of documents

d d bamong systems and databases.

35

Page 36: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 37: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 38: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 39: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 40: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 41: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 42: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 43: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 44: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 45: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 46: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 47: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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 />

Page 48: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

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

Page 49: intro [Uyumluluk Modu]bekirdizdaroglu.com/ceng/downloads/intro.pdf · Title: Microsoft PowerPoint - intro [Uyumluluk Modu] Author: Toshiba Created Date: 10/7/2010 1:11:19 PM

Apache MySQL PHP onApache, MySQL, PHP on Windows (WAMP)Windows (WAMP)

49