RELATIONAL DATA MODEL 1. 2 What is a Data Model? 1.Mathematical representation of data. wExamples: relational model = tables; semistructured model = trees/graphs.

Slides:



Advertisements
Similar presentations
1 More SQL Database Modification Defining a Database Schema Views.
Advertisements

1 Relational Algebra Operators Expression Trees Bag Model of Data Source: Slides by Jeffrey Ullman.
1 Relational Algebra Basic Operations Algebra of Bags.
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
1 Relational Algebra 1 Basic Operations. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which new values.
1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
1 Relational Algebra Lecture #9. 2 Querying the Database Goal: specify what we want from our database Find all the employees who earn more than $50,000.
1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
1 CS145 Introduction About CS145 Relational Model, Schemas, SQL Semistructured Model, XML.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
1 Relational Algebra Basic operators Relational algebra expression tree.
1 Relational Algebra Operators Expression Trees Bag Model of Data.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Relational Algebra Basic Operations Algebra of Bags.
1 CS351 Introduction to Database systems
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
Databases 1 First lecture. Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course:
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
1 Relational Algebra & SQL. 2 Why SQL & Relational Algebra? uSQL is a very-high-level language. wSay “what to do” rather than “how to do it.” wAvoid a.
1 CE223 Database Systems Introduction DBMS Overview, Relational Model, Schemas, SQL Semistructured Model, XML.
1 Lecture 1 Introduction Based on
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
1 CS1368 Introduction* Relational Model, Schemas, SQL Semistructured Model, XML * The slides in this lecture are adapted from slides used in Standford's.
1 Introduction Relational Model, Schemas, SQL Semistructured Model, XML The slides were made by Jeffrey D. Ullman for the Introduction to Databases course.
1 Relational Algebra Basic Operations Algebra of Bags.
Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
From Professor Ullman, Relational Algebra.
Database Management Systems Chapter 5 The Relational Algebra Instructor: Li Ma Department of Computer Science Texas Southern University, Houston October,
1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which.
1 Lecture 2 Relational Algebra Based on
1 Database Systems Defining Database Schema Views.
Databases 1 Second lecture.
Lu Chaojun, SJTU Relational Data Model 1. Lu Chaojun, SJTU What’s a Data Model? A notation (collection of conceptual tools) for describing data as seen.
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Relational Algebra BASIC OPERATIONS 1 DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA.
Relational Algebra Database Systems.
1 Relational Algebra Basic Operations Algebra of Bags.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
1 Introduction to Database Systems, CS420 SQL Constraints.
1 Introduction to Database Systems, CS420 Relational Algebra.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
Database Design and Programming Jan Baumbach Adopted from previous slides of Peter Schneider-Kamp.
1. 2 Design of databases. E/R model (entity-relationship model) Relational model (tables) UML (unified modeling language) Database programming. SQL, Relational.
CPSC-310 Database Systems
Database Design and Programming
Basic Operations Algebra of Bags
CPSC-310 Database Systems
CPSC-608 Database Systems
CS 440 Database Management Systems
Database Models Relational Model
CPSC-310 Database Systems
Operators Expression Trees Bag Model of Data
CPSC-310 Database Systems
Basic Operations Algebra of Bags
CPSC-608 Database Systems
CE223 Database Systems Introduction
Instructor: Zhe He Department of Computer Science
Presentation transcript:

RELATIONAL DATA MODEL 1

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

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

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

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

6 An 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.

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

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

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

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

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

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

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

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

15 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) );

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

17 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) );

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

SQL Demo 19

20 Relational Algebra Basic Operations Algebra of Bags

21 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which new values can be constructed. wOperators --- symbols denoting procedures that construct new values from given values.

22 What is Relational Algebra? uAn algebra whose operands are relations or variables that represent relations. uOperators are designed to do the most common things that we need to do with relations in a database. wThe result is an algebra that can be used as a query language for relations.

23 Core Relational Algebra uUnion, intersection, and difference. wUsual set operations, but both operands must have the same relation schema. uSelection: picking certain rows. uProjection: picking certain columns. uProducts and joins: compositions of relations. uRenaming of relations and attributes.

24 Selection  R1 := σ C (R2) wC is a condition (as in “if” statements) that refers to attributes of R2. wR1 is all those tuples of R2 that satisfy C.

25 Example: Selection Relation Sells: barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 JoeMenu := σ bar=“Joe’s” (Sells): barbeerprice Joe’sBud2.50 Joe’sMiller2.75

26 Projection  R1 := π L (R2) wL is a list of attributes from the schema of R2. wR1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. wEliminate duplicate tuples, if any.

27 Example: Projection Relation Sells: barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 Prices := π beer,price (Sells): beerprice Bud2.50 Miller2.75 Miller3.00

28 Extended Projection  Using the same π L operator, we allow the list L to contain arbitrary expressions involving attributes: 1.Arithmetic on attributes, e.g., A+B->C. 2.Duplicate occurrences of the same attribute.

29 Example: Extended Projection R = ( AB ) π A+B->C,A,A (R) =CA1A

30 Product  R3 := R1 Χ R2 wPair each tuple t1 of R1 with each tuple t2 of R2. wConcatenation t1t2 is a tuple of R3. wSchema of R3 is the attributes of R1 and then R2, in order. wBut beware attribute A of the same name in R1 and R2: use R1.A and R2.A.

31 Example: R3 := R1 Χ R2 R1(A,B ) R2(B,C ) R3(A,R1.B,R2.B,C )

32 Theta-Join  R3 := R1 ⋈ C R2  Take the product R1 Χ R2.  Then apply σ C to the result.  As for σ, C can be any boolean-valued condition. wHistoric versions of this operator allowed only A  B, where  is =, <, etc.; hence the name “theta-join.”

33 Example: Theta Join Sells(bar,beer,price )Bars(name,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 BarInfo := Sells ⋈ Sells.bar = Bars.name Bars BarInfo(bar,beer,price,name,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Joe’sMaple St. Sue’sBud2.50Sue’sRiver Rd. Sue’sCoors3.00Sue’sRiver Rd.

34 Natural Join uA useful join variant (natural join) connects two relations by: wEquating attributes of the same name, and wProjecting out one copy of each pair of equated attributes.  Denoted R3 := R1 ⋈ R2.

35 Example: Natural Join Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 BarInfo := Sells ⋈ Bars Note: Bars.name has become Bars.bar to make the natural join “work.” BarInfo(bar,beer,price,addr ) Joe’sBud2.50Maple St. Joe’sMilller2.75Maple St. Sue’sBud2.50River Rd. Sue’sCoors3.00River Rd.