Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 RELATIONAL DATA MODEL 1

2 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 3 A Relation is a Table name manf WinterbrewPete’s Bud LiteAnheuser-Busch Beers Attributes (column headers) Tuples (rows) Relation name

4 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 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 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 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 8 Creating (Declaring) a Relation uSimplest form is: CREATE TABLE ( ); uTo delete a relation: DROP TABLE ;

9 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 10 Example: Create Table CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL );

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

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

19 SQL Demo 19

20 20 Relational Algebra Basic Operations Algebra of Bags

21 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 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 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 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 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 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 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 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 29 Example: Extended Projection R = ( AB ) 12 34 π A+B->C,A,A (R) =CA1A2 311 733

30 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 31 Example: R3 := R1 Χ R2 R1(A,B ) 12 34 R2(B,C ) 56 78 9 10 R3(A,R1.B,R2.B,C ) 1256 1278 129 10 3456 3478 349 10

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


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

Similar presentations


Ads by Google