SlideShare uma empresa Scribd logo
1 de 42
Baixar para ler offline
CS145 Introduction

          About CS145
Relational Model, Schemas, SQL
  Semistructured Model, XML

                                 1
Content of CS145
Design of databases.
  E/R model, relational model,
  semistructured model, XML, UML, ODL.
Database programming.
  SQL, XPath, XQuery, Relational algebra,
  Datalog.
Not DBMS implementation (that’s
CS245, 346, 347, sometimes CS345).
                                            2
Textbook “Situation”
The closest text for the course is First
Course in Database Systems/3rd Edition.
  But it won’t be available until Friday.
  First 2 chapters available on-line.
You may prefer Database Systems:
Complete Book (also used in CS245) or
have FCDB/2nd E.
  If so, we’ll give you a free copy of the major
  additions in FCDB/3rd E.
                                              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
FROM R;                              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. Project: a little eBay supported by a
   database.
     Individual.
     Uses Stanford Oracle system.
2. Homeworks: Gradiance (automated)
   and “challenge problems” (written).
3. Midterm and final.

                                           6
Gradiance Homework System
Automatic, fast-feedback system for
taking you through standard homework
problems and verifying your knowledge.
Unusual: goal is to get 100% and learn.
  Homework in CS145 is not a “mini-test.”
  You try as many times as you like and get
  help with each wrong answer.

                                              7
Gradiance (GOAL) Access
To get your account, you need:
1. “Value-Pak” with any of the class texts, or
   purchase on-line.
2. Class token: For FCDB/3e use 1B8B815E;
   for other books use A5DDE704.
Details in the intro.html file.
Advice on using Gradiance:
www.gradiance.com/info.html
                                             8
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.
                                            9
More Interesting Stuff
 Database programming centers around
limited programming languages.
  Only area where non-Turing-complete
  languages make sense.
  Leads to very succinct programming, but
  also to unique query-optimization problems
  (CS346).


                                           10
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.




                                     11
And More…
Databases often have unique
concurrency-control problems (CS245,
CS347).
  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.

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



                                            13
A Relation is a Table
Attributes
(column
headers)         name         manf
 Tuples        Winterbrew   Pete’s
 (rows)        Bud Lite     Anheuser-Busch

                       Beers
          Relation
           name
                                         14
Schemas
Relation schema = relation name and
attribute list.
  Optionally: types of attributes.
  Example: Beers(name, manf) or
  Beers(name: string, manf: string)
Database schema = set of all relation
schemas in the database.
Database = collection of relations.
                                        15
Why Relations?
Very simple model.
Often matches how we think about
data.
Abstract model that underlies SQL, the
most important database language
today.


                                         16
Our Running Example
   Beers(name, manf)                       Bars
   Bars(name, addr, license)       Sells           Frequents
                                           Likes
   Drinkers(name, addr, phone)
                                  Beers            Drinkers
   Likes(drinker, beer)
   Sells(bar, beer, price)
   Frequents(drinker, bar)
Underline = key (tuples cannot have
the same value in all key attributes).
  Excellent example of a constraint.
                                                    17
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.



                                          18
Creating (Declaring) a Relation
 Simplest form is:
    CREATE TABLE <name> (
         <list of elements>
    );
 To delete a relation:
    DROP TABLE <name>;

                              19
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.
                                             20
Example: Create Table
CREATE TABLE Sells (
    bar    CHAR(20),
    beer   VARCHAR(20),
    price REAL
);



                          21
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 Bar’.
Any value can be NULL.

                                          22
Dates and Times

DATE and TIME are types in SQL.
The form of a date value is:
  DATE ’yyyy-mm-dd’
 Example: DATE ’2007-09-30’ for Sept.
 30, 2007.



                                        23
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.


                                       24
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.

                                         25
Declaring Single-Attribute Keys
 Place PRIMARY KEY or UNIQUE after the
 type in the declaration of the attribute.
 Example:
    CREATE TABLE Beers (
          name      CHAR(20) UNIQUE,
          manf      CHAR(20)
    );
                                        26
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.



                                             27
Example: Multiattribute Key
The bar and beer together are the key for Sells:
   CREATE TABLE Sells (
         bar        CHAR(20),
         beer       VARCHAR(20),
         price      REAL,
         PRIMARY KEY (bar, beer)
   );

                                              28
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.
                                     29
Semistructured Data
A data model based on trees.
Motivation: flexible representation of
data.
Motivation: sharing of documents
among systems and databases.



                                         30
Graphs of Semistructured Data
 Nodes = objects.
 Arc labels (properties of objects).
 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.

                                              31
Example: Data Graph
                                      root                           Notice a
                                                                     new kind
                          beer               beer                    of data.
        bar
                               manf      manf              prize
                          name    A.B.
                                          name
              servedAt                                 year        award
                         Bud
                                             M’lob     1995        Gold
name          addr

Joe’s     Maple                                 The beer object
                                                for Bud
                          The bar object
                          for Joe’s Bar

                                                                           32
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”).




                                          33
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.

                                           34
Tags
Tags, as in HTML, are normally open-
close pairs, as <FOO> … </FOO>.
  Optional single tag <FOO/>.
Tags may be nested arbitrarily.
XML tags are case sensitive.



                                       35
Example: an XML Document
<?xml version = “1.0” encoding = “utf-8” ?>   A NAME
<BARS>                                        subobject
  <BAR><NAME>Joe’s Bar</NAME>
      <BEER><NAME>Bud</NAME>
             <PRICE>2.50</PRICE></BEER>
      <BEER><NAME>Miller</NAME>
             <PRICE>3.00</PRICE></BEER>       A BEER
                                              subobject
  </BAR>
  <BAR> …
</BARS>
                                                   36
Attributes
Like HTML, the opening tag in XML can
have attribute = value pairs.
Attributes also allow linking among
elements (discussed later).




                                    37
Bars, Using Attributes
<?xml version = “1.0” encoding = “utf-8” ?>
<BARS>
  <BAR name = “Joe’s Bar”>
     <BEER name = “Bud” price = 2.50 />
     <BEER name = “Miller” price = 3.00 />
  </BAR>
  <BAR> … name and          Notice Beer elements
              price are
</BARS>                     have only opening tags
              attributes
                              with attributes.
                                                 38
DTD’s (Document Type Definitions)
  A grammatical notation for describing
 allowed use of tags.
  Definition form:
<!DOCTYPE <root tag> [
 <!ELEMENT <name>(<components>)>
 . . . more elements . . .
]>
                                      39
Example: DTD
                                     A BARS object has
<!DOCTYPE BARS [                     zero or more BAR’s
                                     nested within.
  <!ELEMENT BARS (BAR*)>
  <!ELEMENT BAR (NAME, BEER+)> A BAR has one
  <!ELEMENT NAME (#PCDATA)>        NAME and one
                                   or more BEER
  <!ELEMENT BEER (NAME, PRICE)> subobjects.
  <!ELEMENT PRICE (#PCDATA)>
                              A BEER has a
]>                            NAME and a
            NAME and PRICE
            are HTML text (“parsed    PRICE.
            character data”).
                                                     40
Attributes
  Opening tags in XML can have
 attributes.
  In a DTD,
<!ATTLIST E . . . >
 declares an attribute for element E,
 along with its datatype.


                                        41
Example: Attributes               No closing
                                          tag or
                                          subelements
<!ELEMENT BEER EMPTY>
 <!ATTLIST name CDATA #REQUIRED,
    manf CDATA #IMPLIED>

Character              Required = “must occur”;
string                 Implied = “optional

 Example use:
 <BEER name=“Bud” />
                                                  42

Mais conteúdo relacionado

Mais procurados (18)

Sql
SqlSql
Sql
 
Diving into MARC 2007
Diving into MARC 2007Diving into MARC 2007
Diving into MARC 2007
 
Marc format
Marc formatMarc format
Marc format
 
Diving into MARC
Diving into MARCDiving into MARC
Diving into MARC
 
Sql
SqlSql
Sql
 
Les09
Les09Les09
Les09
 
MARC21
MARC21MARC21
MARC21
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
 
BCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfBCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdf
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginners
 
Marc 21
Marc 21Marc 21
Marc 21
 
NOTES ON "FOXPRO"
NOTES ON "FOXPRO" NOTES ON "FOXPRO"
NOTES ON "FOXPRO"
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
 
Varraysandnestedtables
VarraysandnestedtablesVarraysandnestedtables
Varraysandnestedtables
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4
 
TIK RIBBON GROUP MS.EXCEL 2007 BILINGUAL
TIK RIBBON GROUP MS.EXCEL 2007 BILINGUALTIK RIBBON GROUP MS.EXCEL 2007 BILINGUAL
TIK RIBBON GROUP MS.EXCEL 2007 BILINGUAL
 
SQL
SQLSQL
SQL
 

Destaque

The relational database model
The relational database modelThe relational database model
The relational database modelDhani Ahmad
 
Entity Matching for Semistructured Data in the Cloud
Entity Matching for Semistructured Data in the CloudEntity Matching for Semistructured Data in the Cloud
Entity Matching for Semistructured Data in the CloudMarcus Paradies
 
GRATIN: Accelerating Graph Traversals in Main-Memory Column Stores
GRATIN: Accelerating Graph Traversals in Main-Memory Column StoresGRATIN: Accelerating Graph Traversals in Main-Memory Column Stores
GRATIN: Accelerating Graph Traversals in Main-Memory Column StoresMarcus Paradies
 
osm.cs.byu.edu
osm.cs.byu.eduosm.cs.byu.edu
osm.cs.byu.edubutest
 
Semistructured Data Seach
Semistructured Data SeachSemistructured Data Seach
Semistructured Data Seachkrisztianbalog
 
Database Management Systems Tutorial
Database Management Systems TutorialDatabase Management Systems Tutorial
Database Management Systems TutorialSachin MK
 
SysDB — The system management and inventory collection service
SysDB — The system management and inventory collection serviceSysDB — The system management and inventory collection service
SysDB — The system management and inventory collection serviceSysDB Project
 
Database Management Systems 1
Database Management Systems 1Database Management Systems 1
Database Management Systems 1Nickkisha Farrell
 
Introduction to project management
Introduction to project managementIntroduction to project management
Introduction to project managementDhani Ahmad
 
Entity relationship (er) modeling
Entity relationship (er) modelingEntity relationship (er) modeling
Entity relationship (er) modelingDhani Ahmad
 
Project integration management
Project integration managementProject integration management
Project integration managementDhani Ahmad
 
Business intelligence and data warehouses
Business intelligence and data warehousesBusiness intelligence and data warehouses
Business intelligence and data warehousesDhani Ahmad
 
Advanced data modeling
Advanced data modelingAdvanced data modeling
Advanced data modelingDhani Ahmad
 
Strategic planning
Strategic planningStrategic planning
Strategic planningDhani Ahmad
 
Types of islamic institutions and records
Types of islamic institutions and recordsTypes of islamic institutions and records
Types of islamic institutions and recordsDhani Ahmad
 
Database design, implementation, and management -chapter04
Database design, implementation, and management -chapter04Database design, implementation, and management -chapter04
Database design, implementation, and management -chapter04Beni Krisbiantoro
 
Information system
Information systemInformation system
Information systemDhani Ahmad
 
Database administration and security
Database administration and securityDatabase administration and security
Database administration and securityDhani Ahmad
 
Database design, implementation, and management -chapter02
Database design, implementation, and management -chapter02Database design, implementation, and management -chapter02
Database design, implementation, and management -chapter02Beni Krisbiantoro
 

Destaque (20)

The relational database model
The relational database modelThe relational database model
The relational database model
 
Entity Matching for Semistructured Data in the Cloud
Entity Matching for Semistructured Data in the CloudEntity Matching for Semistructured Data in the Cloud
Entity Matching for Semistructured Data in the Cloud
 
GRATIN: Accelerating Graph Traversals in Main-Memory Column Stores
GRATIN: Accelerating Graph Traversals in Main-Memory Column StoresGRATIN: Accelerating Graph Traversals in Main-Memory Column Stores
GRATIN: Accelerating Graph Traversals in Main-Memory Column Stores
 
osm.cs.byu.edu
osm.cs.byu.eduosm.cs.byu.edu
osm.cs.byu.edu
 
Semistructured Data Seach
Semistructured Data SeachSemistructured Data Seach
Semistructured Data Seach
 
Database Management Systems Tutorial
Database Management Systems TutorialDatabase Management Systems Tutorial
Database Management Systems Tutorial
 
SysDB — The system management and inventory collection service
SysDB — The system management and inventory collection serviceSysDB — The system management and inventory collection service
SysDB — The system management and inventory collection service
 
Database Management Systems 1
Database Management Systems 1Database Management Systems 1
Database Management Systems 1
 
Introduction to project management
Introduction to project managementIntroduction to project management
Introduction to project management
 
Entity relationship (er) modeling
Entity relationship (er) modelingEntity relationship (er) modeling
Entity relationship (er) modeling
 
Project integration management
Project integration managementProject integration management
Project integration management
 
Database
DatabaseDatabase
Database
 
Business intelligence and data warehouses
Business intelligence and data warehousesBusiness intelligence and data warehouses
Business intelligence and data warehouses
 
Advanced data modeling
Advanced data modelingAdvanced data modeling
Advanced data modeling
 
Strategic planning
Strategic planningStrategic planning
Strategic planning
 
Types of islamic institutions and records
Types of islamic institutions and recordsTypes of islamic institutions and records
Types of islamic institutions and records
 
Database design, implementation, and management -chapter04
Database design, implementation, and management -chapter04Database design, implementation, and management -chapter04
Database design, implementation, and management -chapter04
 
Information system
Information systemInformation system
Information system
 
Database administration and security
Database administration and securityDatabase administration and security
Database administration and security
 
Database design, implementation, and management -chapter02
Database design, implementation, and management -chapter02Database design, implementation, and management -chapter02
Database design, implementation, and management -chapter02
 

Semelhante a Database Model

Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebraguest20b0b3
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of dataDimara Hakim
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data ModelSmriti Jain
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Ankit Dubey
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Ankit Dubey
 
Diving into MARC: What's MARC got to do with it?
Diving into MARC:  What's MARC got to do with it?Diving into MARC:  What's MARC got to do with it?
Diving into MARC: What's MARC got to do with it?Johan Koren
 
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C..."MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...CAPSiDE
 
Relational Algebra Various Operations
Relational Algebra Various OperationsRelational Algebra Various Operations
Relational Algebra Various Operationsriteshjha1267
 
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdfRelational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdfAPMRETAIL
 
Diving into MARC: What's MARC got to do with it?
Diving into MARC:  What's MARC got to do with it?Diving into MARC:  What's MARC got to do with it?
Diving into MARC: What's MARC got to do with it?Johan Koren
 
Understanding about relational database m-square systems inc
Understanding about relational database m-square systems incUnderstanding about relational database m-square systems inc
Understanding about relational database m-square systems incMuthu Natarajan
 
CIS/336 ilab 1 of 7
CIS/336 ilab 1 of 7CIS/336 ilab 1 of 7
CIS/336 ilab 1 of 7ashhadiqbal
 

Semelhante a Database Model (20)

04.SQL.ppt
04.SQL.ppt04.SQL.ppt
04.SQL.ppt
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Sql
SqlSql
Sql
 
Ra
RaRa
Ra
 
uniT 4 (1).pptx
uniT 4 (1).pptxuniT 4 (1).pptx
uniT 4 (1).pptx
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
 
Structured Query Language
Structured Query LanguageStructured Query Language
Structured Query Language
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01
 
er.ppt
er.ppter.ppt
er.ppt
 
Diving into MARC: What's MARC got to do with it?
Diving into MARC:  What's MARC got to do with it?Diving into MARC:  What's MARC got to do with it?
Diving into MARC: What's MARC got to do with it?
 
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C..."MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...
 
Boosting MySQL (for starters)
Boosting MySQL (for starters)Boosting MySQL (for starters)
Boosting MySQL (for starters)
 
Relational Algebra Various Operations
Relational Algebra Various OperationsRelational Algebra Various Operations
Relational Algebra Various Operations
 
Sql server building a database ppt 12
Sql server building a database ppt 12Sql server building a database ppt 12
Sql server building a database ppt 12
 
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdfRelational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdf
 
Diving into MARC: What's MARC got to do with it?
Diving into MARC:  What's MARC got to do with it?Diving into MARC:  What's MARC got to do with it?
Diving into MARC: What's MARC got to do with it?
 
Understanding about relational database m-square systems inc
Understanding about relational database m-square systems incUnderstanding about relational database m-square systems inc
Understanding about relational database m-square systems inc
 
CIS/336 ilab 1 of 7
CIS/336 ilab 1 of 7CIS/336 ilab 1 of 7
CIS/336 ilab 1 of 7
 

Database Model

  • 1. CS145 Introduction About CS145 Relational Model, Schemas, SQL Semistructured Model, XML 1
  • 2. Content of CS145 Design of databases. E/R model, relational model, semistructured model, XML, UML, ODL. Database programming. SQL, XPath, XQuery, Relational algebra, Datalog. Not DBMS implementation (that’s CS245, 346, 347, sometimes CS345). 2
  • 3. Textbook “Situation” The closest text for the course is First Course in Database Systems/3rd Edition. But it won’t be available until Friday. First 2 chapters available on-line. You may prefer Database Systems: Complete Book (also used in CS245) or have FCDB/2nd E. If so, we’ll give you a free copy of the major additions in FCDB/3rd E. 3
  • 4. 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 FROM R; R 4
  • 5. 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
  • 6. Course Requirements 1. Project: a little eBay supported by a database. Individual. Uses Stanford Oracle system. 2. Homeworks: Gradiance (automated) and “challenge problems” (written). 3. Midterm and final. 6
  • 7. Gradiance Homework System Automatic, fast-feedback system for taking you through standard homework problems and verifying your knowledge. Unusual: goal is to get 100% and learn. Homework in CS145 is not a “mini-test.” You try as many times as you like and get help with each wrong answer. 7
  • 8. Gradiance (GOAL) Access To get your account, you need: 1. “Value-Pak” with any of the class texts, or purchase on-line. 2. Class token: For FCDB/3e use 1B8B815E; for other books use A5DDE704. Details in the intro.html file. Advice on using Gradiance: www.gradiance.com/info.html 8
  • 9. 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. 9
  • 10. More Interesting Stuff Database programming centers around limited programming languages. Only area where non-Turing-complete languages make sense. Leads to very succinct programming, but also to unique query-optimization problems (CS346). 10
  • 11. 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. 11
  • 12. And More… Databases often have unique concurrency-control problems (CS245, CS347). 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. 12
  • 13. What is a Data Model? 1. Mathematical representation of data. Examples: relational model = tables; semistructured model = trees/graphs. 2. Operations on data. 3. Constraints. 13
  • 14. A Relation is a Table Attributes (column headers) name manf Tuples Winterbrew Pete’s (rows) Bud Lite Anheuser-Busch Beers Relation name 14
  • 15. Schemas Relation schema = relation name and attribute list. Optionally: types of attributes. Example: Beers(name, manf) or Beers(name: string, manf: string) Database schema = set of all relation schemas in the database. Database = collection of relations. 15
  • 16. Why Relations? Very simple model. Often matches how we think about data. Abstract model that underlies SQL, the most important database language today. 16
  • 17. Our Running Example Beers(name, manf) Bars Bars(name, addr, license) Sells Frequents Likes Drinkers(name, addr, phone) Beers Drinkers Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) Underline = key (tuples cannot have the same value in all key attributes). Excellent example of a constraint. 17
  • 18. 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. 18
  • 19. Creating (Declaring) a Relation Simplest form is: CREATE TABLE <name> ( <list of elements> ); To delete a relation: DROP TABLE <name>; 19
  • 20. 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. 20
  • 21. Example: Create Table CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL ); 21
  • 22. 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 Bar’. Any value can be NULL. 22
  • 23. Dates and Times DATE and TIME are types in SQL. The form of a date value is: DATE ’yyyy-mm-dd’ Example: DATE ’2007-09-30’ for Sept. 30, 2007. 23
  • 24. 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. 24
  • 25. 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. 25
  • 26. Declaring Single-Attribute Keys Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. Example: CREATE TABLE Beers ( name CHAR(20) UNIQUE, manf CHAR(20) ); 26
  • 27. 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. 27
  • 28. Example: Multiattribute Key The bar and beer together are the key for Sells: CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY (bar, beer) ); 28
  • 29. 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. 29
  • 30. Semistructured Data A data model based on trees. Motivation: flexible representation of data. Motivation: sharing of documents among systems and databases. 30
  • 31. Graphs of Semistructured Data Nodes = objects. Arc labels (properties of objects). 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. 31
  • 32. Example: Data Graph root Notice a new kind beer beer of data. bar manf manf prize name A.B. name servedAt year award Bud M’lob 1995 Gold name addr Joe’s Maple The beer object for Bud The bar object for Joe’s Bar 32
  • 33. 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”). 33
  • 34. 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. 34
  • 35. Tags Tags, as in HTML, are normally open- close pairs, as <FOO> … </FOO>. Optional single tag <FOO/>. Tags may be nested arbitrarily. XML tags are case sensitive. 35
  • 36. Example: an XML Document <?xml version = “1.0” encoding = “utf-8” ?> A NAME <BARS> subobject <BAR><NAME>Joe’s Bar</NAME> <BEER><NAME>Bud</NAME> <PRICE>2.50</PRICE></BEER> <BEER><NAME>Miller</NAME> <PRICE>3.00</PRICE></BEER> A BEER subobject </BAR> <BAR> … </BARS> 36
  • 37. Attributes Like HTML, the opening tag in XML can have attribute = value pairs. Attributes also allow linking among elements (discussed later). 37
  • 38. Bars, Using Attributes <?xml version = “1.0” encoding = “utf-8” ?> <BARS> <BAR name = “Joe’s Bar”> <BEER name = “Bud” price = 2.50 /> <BEER name = “Miller” price = 3.00 /> </BAR> <BAR> … name and Notice Beer elements price are </BARS> have only opening tags attributes with attributes. 38
  • 39. DTD’s (Document Type Definitions) A grammatical notation for describing allowed use of tags. Definition form: <!DOCTYPE <root tag> [ <!ELEMENT <name>(<components>)> . . . more elements . . . ]> 39
  • 40. Example: DTD A BARS object has <!DOCTYPE BARS [ zero or more BAR’s nested within. <!ELEMENT BARS (BAR*)> <!ELEMENT BAR (NAME, BEER+)> A BAR has one <!ELEMENT NAME (#PCDATA)> NAME and one or more BEER <!ELEMENT BEER (NAME, PRICE)> subobjects. <!ELEMENT PRICE (#PCDATA)> A BEER has a ]> NAME and a NAME and PRICE are HTML text (“parsed PRICE. character data”). 40
  • 41. Attributes Opening tags in XML can have attributes. In a DTD, <!ATTLIST E . . . > declares an attribute for element E, along with its datatype. 41
  • 42. Example: Attributes No closing tag or subelements <!ELEMENT BEER EMPTY> <!ATTLIST name CDATA #REQUIRED, manf CDATA #IMPLIED> Character Required = “must occur”; string Implied = “optional Example use: <BEER name=“Bud” /> 42