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:

Slides:



Advertisements
Similar presentations
1 Relational Algebra Operators Expression Trees Bag Model of Data Source: Slides by Jeffrey Ullman.
Advertisements

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.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
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.
SQL CSET 3300.
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.
Winter 2002Arthur Keller – CS 1805–1 Schedule Today: Jan. 17 (TH) u Relational Algebra. u Read Chapter 5. Project Part 1 due. Jan. 22 (T) u SQL Queries.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
Fall 2001Arthur Keller – CS 1805–1 Schedule Today Oct. 9 (T) Multivalued Dependencies, Relational Algebra u Read Sections 3.7, Assignment 2 due.
1 Relational Algebra Basic operators Relational algebra expression tree.
1 Relational Algebra Operators Expression Trees Bag Model of Data.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
Relational Model 2015, Fall Pusan National University Ki-Joune Li.
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.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
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.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
RELATIONAL DATA MODEL 1. 2 What is a Data Model? 1.Mathematical representation of data. wExamples: relational model = tables; semistructured model = trees/graphs.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra (Chapter 7)
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
The Relational Algebra and Calculus
1 Relational Algebra Basic Operations Algebra of Bags.
“Core” Relational Algebra A small set of operators that allow us to manipulate relations in limited but useful ways. The operators are: 1.Union, intersection,
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
Winter 2002Judy Cushing5–1 Schedule Today: Jan. 23 (Wed) u Relational Algebra. u Read Chapter 5. u Project Part 2 due Friday 5pm, Judy’s mailbox in Lab.
Databases 1 Second lecture.
SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections And then u Subqueries,
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
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.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
Relational Algebra Database Systems.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
1 Relational Algebra Basic Operations Algebra of Bags.
1 Introduction to Database Systems, CS420 Relational Algebra.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
Summary of Relational Algebra
Database Design and Programming
Basic Operations Algebra of Bags
CPSC-310 Database Systems
Relational Database Systems 1
Slides are reused by the approval of Jeffrey Ullman’s
COP4710 Database Systems Relational Algebra.
CS 440 Database Management Systems
Database Models Relational Model
IST 210: Organization of Data
Operators Expression Trees Bag Model of Data
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
Basic Operations Algebra of Bags
Schedule Today: Next And then Relational Algebra.
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

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: Book: H. G. Molinaa, J. Ullman, J. Widom: Database Systems: The Complete BookDatabase Systems: The Complete Book

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

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

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.

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

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

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

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

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.

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.

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

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

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.

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

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.

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

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.

Example: R3=R1 x R2 R1 R2 AB BC R3=R1 x R2 AR1.BR2.BC

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

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.

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.

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.

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.

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

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.

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)

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.

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

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 As a Tree: BarsSells σ addr = “Maple St.” σ price<3 AND beer=“Bud” π name ρ R(name) π bar U

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 The Tree Sells ρ S(bar, beer1, price) Θ π bar σ beer != beer1

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