39
Relational Databases Lecture 2 Robb T. Koether Hampden-Sydney College Fri, Jan 20, 2012 Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 1 / 36

Relational Databases Lecture 2 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...relational databases. Robb T. Koether (Hampden-Sydney College) Relational

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

  • Relational DatabasesLecture 2

    Robb T. Koether

    Hampden-Sydney College

    Fri, Jan 20, 2012

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 1 / 36

  • 1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 2 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 3 / 36

  • Benefits of Databases

    A few of the benefits of using a database:The user does not need to have knowledge of how the data isstored. He works through a user-friendly interface.Different users can be given different views of the data. Each usermay be unaware of the views that other users have.Data can be easily shared among users.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 4 / 36

  • Benefits of Databases

    OnlineCatalog

    SalesRecords Inventory

    Customer ManagerSalesman

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 5 / 36

  • Benefits of Databases

    Customer ManagerSalesman

    ComprehensiveDatabase

    (Catalog, Sales,Inventory)

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 5 / 36

  • Various Roles

    Roles of various individuals involved.The database administrator (DBA) – Oversees the entire system.The database designer – Designs a database to meet the specs.The user – Queries the database according to his needs.

    At various times in this course, we will be the database designerand the database user.

    One other person involved is the person who built the DBMS.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 6 / 36

  • Various Roles

    Roles of various individuals involved.The database administrator (DBA) – Oversees the entire system.The database designer – Designs a database to meet the specs.The user – Queries the database according to his needs.

    At various times in this course, we will be the database designerand the database user.One other person involved is the person who built the DBMS.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 6 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 7 / 36

  • Concurrency

    Multiple users accessing the very same data at the very sametime leads to the concurrency problem.For example, Alice, a customer, requests the price of item #123.At the very same time, Bob, an employee, is updating the price ofitem #123.Which price will Alice see?

    It depends on who goes first, Alice or Bob.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 8 / 36

  • Concurrency

    Multiple users accessing the very same data at the very sametime leads to the concurrency problem.For example, Alice, a customer, requests the price of item #123.At the very same time, Bob, an employee, is updating the price ofitem #123.Which price will Alice see?It depends on who goes first, Alice or Bob.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 8 / 36

  • Concurrency

    More significantly, Alice, an airline employee goes online to selecta list of passengers on flight #123.At the very same time, Bob, another airline employee, goes onlineto change the flight number from #123 to #456.Alice selects the first 50 names of passengers on flight #123.At that moment, the flight number is changed to #456.The last 50 names are not selected because the flight numberwas no longer #123.This type of conflict must be avoided.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 9 / 36

  • OLAP

    Most database applications fall into one of two categories: OLAPor OLTP.

    Definition (Database)Online analytical processing (OLAP) involves querying a database toobtain summary data, typical retrievals and aggregation.

    A typical OLAP application would be to get the total daily salesfrom online purchases.OLAP is computationally intensive (e.g., summing over millions ofrecords).This is more like what we will do when we first study databases.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 10 / 36

  • OLTP

    Definition (Database)Online transaction processing (OLTP) involves many small queries,typically insertions.

    A typical OLTP application would be a database of onlinepurchases. Each purchase is inserted into the database.OLTP is not computationally intensive.This is more like what we will do with PHP and Javascript.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 11 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 12 / 36

  • Types of Databases

    Originally, “databases” were simply collections of data, arrangedin whatever way the programmer saw fit.The “DBMS” was simply a program that could process the data.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 13 / 36

  • Types of Databases

    In the early 1970s, the concept of a relational database (RDB)was introduced.Its data were organized into tables of rows and columns.Queries were organized into select, insert, delete, and update.Given the standardized forms, queries could be highly optimizedby the DBMS.Today, SQL (Structured Query Language) is the standard forrelational databases.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 14 / 36

  • Types of Databases

    Catalog of Items

    Item Price Stock

    Sales

    Cust Item Qty

    Bob

    Alice

    123

    123

    4

    2

    456

    123

    6.99

    3.99

    1500

    1000

    : : : :::

    A Relational Database (SQL)

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 15 / 36

  • Types of Databases

    More recently, other forms have appeared, most notablyExtensible Markup Language (XML).An XML database is structured hierarchically.The underlying theory is less mature.The supporting software is less extensive.The queries are less optimized.On the other hand, the structure is much more flexible.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 16 / 36

  • Types of Databases

    Catalog

    Item2Item1

    ...

    StockPrice

    ...

    Sales

    Cust2Cust1

    ...

    QtyItem

    ...

    Company

    A Hierarchical Database (XML)

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 17 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 18 / 36

  • Relational Databases

    A relational database is a database in which the data are stored intabular form, arranged in rows and columns.Traditionally, rows are called records and columns are calledfields.In modern terminology, rows are called tuples and columns arecalled attributes.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 19 / 36

  • Relational Databases

    Consider a database of courses.

    Courses table

    Dept Number Title CreditCOMS 480 Databases 3MATH 121 Statistics 4CHEM 110 Chemical Concepts 3COMS 331 Computer Graphics 3

    ......

    ......

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 20 / 36

  • Relational Databases

    In the example, the tuples are(’COMS’, 480, ’Databases’, 3)(’MATH’, 121, ’Statistics’, 4)etc.

    The attributes areDeptNumberTitleCredit

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 21 / 36

  • Relational Databases

    The table expresses a relation, namely“The department dept offers a course numbered number with the

    title title for credit hours of credit.”For example, the first row represents the statement

    “The department COMS offers a course numbered 480 with thetitle Databases for 3 hours of credit.”

    Thus, each tuple of the database represents a true statement, aninstance of the relation.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 22 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 23 / 36

  • Relational Databases

    A query is a request that is sent to the DBMS.The DBMS parses the query, optimizes it, and sends it to thestorage engine, which executes the query on the database.There are four basic types of query.

    SelectInsertDeleteUpdate

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 24 / 36

  • Relational Databases

    A selection query might request all COMS courses.The result would be

    Dept Number Title CreditCOMS 480 Databases 3COMS 331 Computer Graphics 3

    ......

    ......

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 25 / 36

  • Relational Databases

    An insertion query might add the tuple(’COMS’, 261, ’Computer Science I’, 4).

    The result would be

    Dept Number Title CreditCOMS 480 Databases 3MATH 121 Statistics 4CHEM 110 Chemical Concepts 3COMS 331 Computer Graphics 3COMS 261 Computer Science I 4

    ......

    ......

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 26 / 36

  • Relational Databases

    A deletion query might delete the tuple(’COMS’, 261, ’Computer Science I’, 4).

    The result would be

    Dept Number Title CreditCOMS 480 Databases 3MATH 121 Statistics 4CHEM 110 Chemical Concepts 3COMS 331 Computer Graphics 3

    ......

    ......

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 27 / 36

  • Relational Databases

    An update query might change ’COMS’ to ’CSCI’.The result would be

    Dept Number Title CreditCSCI 480 Databases 3MATH 121 Statistics 4CHEM 110 Chemical Concepts 3CSCI 331 Computer Graphics 3

    ......

    ......

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 28 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 29 / 36

  • Schemas

    Definition (Schema)A relation schema consists of the relation’s name, a list of its attributes,and their domains, i.e., the set of legitimate values of each attribute. Adatabase schema is a set of relation schemas.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 30 / 36

  • Schemas

    The schema of the Courses relation isCourses(Dept string, Number integer, Title string, Credit integer)

    More specifically,100 ≤ Number ≤ 499

    0 ≤ Credit ≤ 4

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 31 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 32 / 36

  • Primary Keys

    Every table must have a primary key.The primary key is an attribute whose value is unique for eachtuple. . .Or it is a set of attributes whose combination of values is uniquefor each tuple.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 33 / 36

  • Primary Keys

    In the Courses table, the key would be the (Dept, Number)combination.In a relation schema, we underline the attributes that comprise theprimary key.We represent the schema asCourses(Dept string, Number integer, Title string, Credit integer)

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 34 / 36

  • Outline

    1 Databases Systems

    2 The Concurrency Problem

    3 Types of Databases

    4 Relational Databases

    5 Queries

    6 Schemas

    7 Primary Keys

    8 Assignment

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 35 / 36

  • Assignment

    Read Chapter 2 - Database System Concepts and Architecture.Look over the Review Questions and Exercises at the end thechapter.

    Robb T. Koether (Hampden-Sydney College) Relational DatabasesLecture 2 Fri, Jan 20, 2012 36 / 36

    Databases SystemsThe Concurrency ProblemTypes of DatabasesRelational DatabasesQueriesSchemasPrimary KeysAssignment