Presentation is loading. Please wait.

Presentation is loading. Please wait.

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:

Similar presentations


Presentation on theme: "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:"— Presentation transcript:

1 Databases 1 First lecture

2 Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course: http://people.inf.elte.hu/balcsi4/databases http://people.inf.elte.hu/balcsi4/databases Book: H. G. Molinaa, J. Ullman, J. Widom: Database Systems: The Complete BookDatabase Systems: The Complete Book

3 Topics of the semester Relational data model, relational algebra Introduction to SQL Complex SQL queries Entity-relationship model Design of relational databases Datalog Oracle PL/SQL

4 Relational data model A relation is a table BeerManufacturer WinterbrewPete’s Bud LiteAnheuser-Busch Tuples (rows) Attributes (column headers)

5 Types and schemas Relation schema = relation name + attributes, in order (+ types of attributes). ▫Example: Beers(name, manf) or Beers(name: string, manf: string) Database = collection of relations. Database schema = set of all relation schemas in the database.

6 Why relations? Very simple model. Often matches how we think about data. Abstract model that underlies SQL, the most important database language today.

7 Relational model Logical level: ▫The relations are considered as tables. ▫The tables has unique names ▫The colums address the attributes ▫The rows represent the records ▫Rows can be interchanged, the order of rows is irrelevant Physical level: ▫The relations are stored in a file structure

8 Examples ABC abc daa cbd BCA bca aad ddc ABC cbd daa abc ABC cbd cbd abc Example 1Example 2 Example 3Example 4 In ex. 1 and ex. 2 the columns are interchanged but the same relation In ex. 1 and ex. 3 the same tuples are represented in different orders but these are the same relations too. Ex. 4 is not a relation

9 Keys A set of attributes called key if there are no two different tuples having the same values in these attributes R(A 1,…,A n ) is a relation, X={A 1,…,A k } is key if there are no two different tuples t 1, t 2 fulfill t 1 [X]= t 2 [X]. Foreign keys will be discussed later

10 Relational Algebra What is an “Algebra”? Mathematical system consisting of: ▫Operands --- variables or values from which new values can be constructed. ▫Operators --- symbols denoting procedures that construct new values from given values.

11 Core Relational Algebra Union, intersection, and difference. ▫Usual set operations, but require both operands have the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes.

12 Union, intersection, difference To apply these operators the relations must have the same atrributes. Union (R1  R2): all tuples from R1 or R2 Intersection (R1  R2): common tuples from R1 and R2 Difference (R1\R2): tuples occuring in R1 but not in R2

13 Example Relation Sells1: Relation Sells2: BarBeerPrice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 BarBeerPrice Joe’sBud2.50 Jack’sBud2.75 Sells1  Sells2: Sells1  Sells2: Sells2 \ Sells1: BarBeerPrice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Jack’sBud2.75 BarBeerPrice Joe’sBud2.50 BarBeerPrice Jack’sBud2.75

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

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

16 Projection R1 := π L (R2) ▫L is a list of attributes from the schema of R2. ▫R1 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. ▫Eliminate duplicate tuples, if any.

17 Example Relation Sells: Prices := π beer,price (Sells): BarBeerPrice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 BeerPrice Bud2.50 Miller2.75 Miller3.00

18 Product R3 := R1 x R2 (or R1 * R2 ) ▫Pair each tuple t1 of R1 with each tuple t2 of R2. ▫Concatenation t1t2 is a tuple of R3. ▫Schema of R3 is the attributes of R1 and R2, in order. ▫But beware attribute A of the same name in R1 and R2: use R1.A and R2.A.

19 Example: R3=R1 x R2 R1 R2 AB 12 34 BC 56 78 910 R3=R1 x R2 AR1.BR2.BC 1256 1278 12910 3456 3478 349

20 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. ▫Historic versions of this operator allowed only A theta B, where theta was =, <, etc.; hence the name “theta-join.”

21 Example Sells: Bars: Barinfo= Sells Θ Sells.bar = Bars.name Bars BarBeerPrice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 NameAddress Joe’sMaple st. Sue’sRiver rd. BarBeerPriceNameAddress Joe’sBud2.50Joe’sMaple st. Joe’sMiller2.75Joe’sMaple st. Sue’sBud2.50Sue’sRiver rd. Sue’sMiller3.00Sue’sRiver rd.

22 Natural Join A frequent type of join connects two relations by: ▫Equating attributes of the same name, and ▫Projecting out one copy of each pair of equated attributes. Called natural join. Denoted R3 := R1 Θ R2.

23 Example Sells: Bars: Barinfo= Sells Θ Bars BarBeerPrice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 BarAddress Joe’sMaple st. Sue’sRiver rd. BarBeerPriceAddress Joe’sBud2.50Maple st. Joe’sMiller2.75Maple st. Sue’sBud2.50River rd. Sue’sMiller3.00River rd.

24 Renaming The RENAME operator gives a new schema to a relation. R1 := ρ 1(A1,…,An) (R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2. Simplified notation: R1(A1,…,An) := R2.

25 Example Bars: R(Bar, Address) := Bars NameAddress Joe’sMaple st. Sue’sRiver rd. BarAddress Joe’sMaple st. Sue’sRiver rd.

26 Building Complex Expressions Algebras allow us to express sequences of operations in a natural way. ▫Example: in arithmetic --- (x + 4)*(y - 3). Relational algebra allows the same. Three notations, just as in arithmetic: 1.Sequences of assignment statements. 2.Expressions with several operators. 3.Expression trees.

27 Sequences of Assignments Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 Θ C R2 can be written: R4 := R1 x R2 R3 := σ C (R4)

28 Expressions in a Single Assignment Example: the theta-join R3 := R1 Θ C R2 can be written: R3 := σ C (R1 x R2) Precedence of relational operators: 1.Unary operators --- select, project, rename --- have highest precedence, bind first. 2.Then come products and joins. 3.Then intersection. 4.Finally, union and set difference bind last. wBut you can always insert parentheses to force the order you desire.

29 Expression Trees Leaves are operands --- either variables standing for relations or particular, constant relations. Interior nodes are operators, applied to their child or children.

30 Example Using the relations Bars(name, address) and Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3.

31 31 As a Tree: BarsSells σ addr = “Maple St.” σ price<3 AND beer=“Bud” π name ρ R(name) π bar U

32 32 Example Using Sells(bar, beer, price), find the bars that sell two different beers at the same price. Strategy: by renaming, define a copy of Sells, called S(bar, beer1, price). The natural join of Sells and S consists of quadruples (bar, beer, beer1, price) such that the bar sells both beers at this price.

33 33 The Tree Sells ρ S(bar, beer1, price) Θ π bar σ beer != beer1

34 34 Schemas for Interior Nodes An expression tree defines a schema for the relation associated with each interior node. Similarly, a sequence of assignments defines a schema for each relation on the left of the := sign.

35 35 Schema-Defining Rules 1 For union, intersection, and difference, the schemas of the two operands must be the same, so use that schema for the result. Selection: schema of the result is the same as the schema of the operand. Projection: list of attributes tells us the schema.

36 36 Schema-Defining Rules 2 Product: the schema is the attributes of both relations. ▫Use R.A, etc., to distinguish two attributes named A. Theta-join: same as product. Natural join: use attributes of both relations. ▫Shared attribute names are merged. Renaming: the operator tells the schema.


Download ppt "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:"

Similar presentations


Ads by Google