Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction Relational Model, Schemas, SQL Semistructured Model, XML The slides were made by Jeffrey D. Ullman for the Introduction to Databases course.

Similar presentations


Presentation on theme: "1 Introduction Relational Model, Schemas, SQL Semistructured Model, XML The slides were made by Jeffrey D. Ullman for the Introduction to Databases course."— Presentation transcript:

1 1 Introduction Relational Model, Schemas, SQL Semistructured Model, XML The slides were made by Jeffrey D. Ullman for the Introduction to Databases course (CS145) in Stanford.

2 2 Content of the Course uDatabase programming. wSQL, PL/SQL, XPath, XQuery, Relational algebra. uDesign of databases. wE/R model, relational model, semistructured model, XML, UML.

3 3 Do You Know SQL? uExplain the difference between: SELECT b FROM R WHERE a =10; and SELECT b FROM R; ab 520 1030 2040… R

4 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 5 Interesting Stuff About Databases uIt used to be about boring stuff: employee records, bank records, etc. uToday, the field covers all the largest sources of data, with many new ideas. wWeb search. wData mining. wScientific and medical databases. wIntegrating information.

6 6 More Interesting Stuff uDatabase programming centers around limited programming languages. wOnly area where non-Turing-complete languages make sense. wLeads to very succinct programming, but also to unique query-optimization problems.

7 7 Still More … uYou may not notice it, but databases are behind almost everything you do on the Web. wGoogle searches. wQueries at Amazon, eBay, etc.

8 8 And More… uDatabases often have unique concurrency-control problems. wMany activities (transactions) at the database at all times. wMust not confuse actions, e.g., two withdrawals from the same account must each debit the account.

9 9 What is a Data Model? 1.Mathematical representation of data. wExamples: relational model = tables; semistructured model = trees/graphs. 2.Operations on data. 3.Constraints.

10 10 A Relation is a Table name manf WinterbrewPete’s Bud LiteAnheuser-Busch Beers Attributes (column headers) Tuples (rows) Relation name

11 11 Schemas uRelation schema = relation name and attribute list. wOptionally: types of attributes. wExample: Beers(name, manf) or Beers(name: string, manf: string) uDatabase = collection of relations. uDatabase schema = set of all relation schemas in the database.

12 12 Why Relations? uVery simple model. uOften matches how we think about data. uAbstract model that underlies SQL, the most important database language today.

13 13 Our Running Example Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) uUnderline = key (tuples cannot have the same value in all key attributes). wExcellent example of a constraint.

14 14 Database Schemas in SQL uSQL is primarily a query language, for getting information from a database. uBut SQL also includes a data-definition component for describing database schemas.

15 15 Creating (Declaring) a Relation uSimplest form is: CREATE TABLE ( ); uTo delete a relation: DROP TABLE ;

16 16 Elements of Table Declarations uMost basic element: an attribute and its type. uThe most common types are: wINT or INTEGER (synonyms). wREAL or FLOAT (synonyms). wCHAR(n ) = fixed-length string of n characters. wVARCHAR(n ) = variable-length string of up to n characters.

17 17 Example: Create Table CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL );

18 18 SQL Values uIntegers and reals are represented as you would expect. uStrings are too, except they require single quotes.  Two single quotes = real quote, e.g., ’Joe’’s Bar’. uAny value can be NULL.

19 19 Dates and Times uDATE and TIME are types in SQL. uThe form of a date value is: DATE ’yyyy-mm-dd’  Example: DATE ’2007-09-30’ for Sept. 30, 2007.

20 20 Times as Values uThe 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 21 Declaring Keys uAn attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. uEither says that no two tuples of the relation may agree in all the attribute(s) on the list. uThere are a few distinctions to be mentioned later.

22 22 Declaring Single-Attribute Keys uPlace PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. uExample: CREATE TABLE Beers ( nameCHAR(20) UNIQUE, manfCHAR(20) );

23 23 Declaring Multiattribute Keys uA key declaration can also be another element in the list of elements of a CREATE TABLE statement. uThis form is essential if the key consists of more than one attribute. wMay be used even for one-attribute keys.

24 24 Example: Multiattribute Key uThe bar and beer together are the key for Sells: CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL, PRIMARY KEY (bar, beer) );

25 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 26 Semistructured Data uAnother data model, based on trees. uMotivation: flexible representation of data. uMotivation: sharing of documents among systems and databases.

27 27 Graphs of Semistructured Data uNodes = objects. uLabels on arcs (like attribute names). uAtomic values at leaf nodes (nodes with no arcs out). uFlexibility: no restriction on: wLabels out of a node. wNumber of successors with a given label.

28 28 Example: Data Graph Bud A.B. Gold1995 MapleJoe’s M’lob beer bar manf servedAt name addr prize yearaward root The bar object for Joe’s Bar The beer object for Bud Notice a new kind of data.

29 29 XML uXML = Extensible Markup Language. uWhile HTML uses tags for formatting (e.g., “italic”), XML uses tags for semantics (e.g., “this is an address”). uKey idea: create tag sets for a domain (e.g., genomics), and translate all data into properly tagged XML documents.

30 30 XML Documents uStart the document with a declaration, surrounded by. uTypical: uBalance of document is a root tag surrounding nested tags.

31 31 Tags uTags, as in HTML, are normally matched pairs, as …. wOptional single tag. uTags may be nested arbitrarily. uXML tags are case sensitive.

32 32 Example: an XML Document Joe’s Bar Bud 2.50 Miller 3.00 … A NAME subobject A BEER subobject

33 33 Attributes uLike HTML, the opening tag in XML can have atttribute = value pairs. uAttributes also allow linking among elements (discussed later).

34 34 Bars, Using Attributes … Notice Beer elements have only opening tags with attributes. name and price are attributes

35 35 DTD’s (Document Type Definitions) uA grammatical notation for describing allowed use of tags.  Definition form: [ ( )>... more elements... ]>

36 36 Example: DTD <!DOCTYPE BARS [ ]> A BARS object has zero or more BAR’s nested within. A BAR has one NAME and one or more BEER subobjects. A BEER has a NAME and a PRICE. NAME and PRICE are HTML text.

37 37 Attributes uOpening tags in XML can have attributes. uIn a DTD, declares an attribute for element E, along with its datatype.

38 38 Example: Attributes <!ATTLIST name CDATA #REQUIRED, manf CDATA #IMPLIED> No closing tag or subelements Character string Required = “must occur”; Implied = “optional Example use:


Download ppt "1 Introduction Relational Model, Schemas, SQL Semistructured Model, XML The slides were made by Jeffrey D. Ullman for the Introduction to Databases course."

Similar presentations


Ads by Google