Lecture 2 Relational Database

Slides:



Advertisements
Similar presentations
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Advertisements

Chapter 3 : Relational Model
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Formal Relational Query Languages Chapter 6
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
ICOM 5016 – Introduction to Database Systems Lecture 5b Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
CSc340 1b1 The Relational Model Chapter 2 Database Schema Keys Schema Diagrams Relational Query Languages Relational Operations.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
Computing & Information Sciences Kansas State University Friday, 26 Jan 2008CIS 560: Database System Concepts Lecture 2 of 42 Friday, 29 August 2008 William.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
Chapter 2: Introduction to Relational Model
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
Database System Concepts, 6 th Ed. Chapter 6: Formal Relational Query Languages.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
COP Introduction to Database Structures
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Midlands State University Topic: Relational DB Model
Module 2: Intro to Relational Model
Introduction to Relational Model
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Chapter 2: Relational Model
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Lecture 2 The Relational Model
Chapter 3: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Relational Model
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Introduction to Database
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Session 3 Welcome: To session 3-the second learning sequence
Chapter 6: Formal Relational Query Languages
Instructor: Mohamed Eltabakh
The Relational Model Transparencies
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Presentation transcript:

Lecture 2 Relational Database Reason of RDB's popularity: simplicity, easy to use, properties of rob, tables for entity and their relation Concepts, relational database

Outline Relational Data Model Schema and Instance Mathematica Fundament select: s project: Õ union: È set difference: – Cartesian product: x rename: r

Relational Data Model R

Example of a Relation attributes (or columns) tuples (or rows) Relations, attributes, tuples, ids, the semantics of a row as well as a column, take the table as an example A tuple is a sequence of values Similar to instructors, departments….

Relationship between two entities Related tuples(tables)

Attribute Types The set of allowed values for each attribute is called the domain of the attribute Attribute values are (normally) required to be atomic; that is, indivisible The special value null is a member of every domain. Indicated that the value is “unknown” or does not exist The null value causes complications in the definition of many operations domain: Permitted values for a column Atomic: depends the real application, for example, Address Null: causes a lot of difficulties from operations tables, should be eliminated

Relation Schema and Instance A1, A2, …, An are attributes R = (A1, A2, …, An ) is a relation schema Example: instructor = (ID, name, dept_name, salary) Formally, given sets D1, D2, …. Dn a relation r is a subset of D1 x D2 x … x Dn Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai Î Di Database Schema: the logical design of the database and consists of a list of attributes and their corresponding domains ,does not generally change database instance, which is a snapshot of the data in the database at a given instant in time, change with time Relation between tuples The current values (relation instance) of a relation are specified by a table An element t of r is a tuple, represented by a row in a table

Relations are Unordered Order of tuples is irrelevant (tuples may be stored in an arbitrary order) Example: instructor relation with unordered tuples Tuples is in set, the properties of a set the order in which the tuples appear in a relation is irrelevant, since a relation is a set of tuples. Any two relations, if they have the same schema and same tuples, they are the same as long as they have the same set of tuples whatever their order is.

Keys Let K Í R K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R) Example: {ID} and {ID,name} are both superkeys of instructor. Superkey K is a candidate key if K is minimal Example: {ID} is a candidate key for Instructor One of the candidate keys is selected to be the primary key which one? Foreign key constraint: Value in one relation must appear in another referencing relation referenced relation example – dept_name in instructor is a foreign key from instructor referencing department referential integrity constraint; Tuples in the same tables should be distinguishable. A superkey is a set of one or more attributes that, taken collectively, allow us to identify uniquely a tuple in the relation, maybe have extraneous attributes (ID, name) vs. Id candidate keys: minimal superkeys primary key to denote a candidate key that is chosen by the database designer as the principal means of identifying tuples within a relation attribute values are never, or very rarely, changed Primary key attributes are also underlined , no duplication, unique

Schema Diagram for University Database schema diagrams depicted, A database schema, along with primary key and foreign key dependencies, can be depicted by schema diagrams The parts of a table The underlined attributes are the PK The arrow from… to… E-R model

Expression of relation schema

Mathematica Fundament: Relational Algebra

Relational Query Languages Procedural vs .non-procedural, or declarative “Pure” languages: Relational algebra Tuple relational calculus Domain relational calculus The above 3 pure languages are equivalent in computing power We will concentrate in this chapter on relational algebra Not turning-machine equivalent consists of 6 basic operations In a procedural language, the user instructs the system to perform a sequence of operations on the database to compute the desired result. In a nonprocedural language, the user describes the desired information without giving a specific procedure for obtaining that information There are a number of “pure” query languages: The relational algebra is procedural(fundamental techniques for extracting data from the database ),whereas the tuple relational calculus and domain relational calculus are nonprocedural.

Relational Algebra Procedural language Six basic operators select: s project: Õ union: È set difference: – Cartesian product: x rename: r The operators take one or two relations as inputs and produce a new relation as a result.

Select Operation Notation: s p(r) p is called the selection predicate Defined as: sp(r) = {t | t Î r and p(t)} Where p is a formula in propositional calculus consisting of terms connected by : Ù (and), Ú (or), Ø (not) Each term is one of: <attribute> op <attribute> or <constant> where op is one of: =, ¹, >, ³. <. £

Select Operation – selection of rows (tuples) Relation r sA=B ^ D > 5 (r)

Example of Selection s dept_name=“Physics”(instructor)

Project Operation Notation: where A1, A2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets

Example of Selecting columns (Attributes) Relation r: ÕA,C (r) To eliminate the dept_name attribute of instructor ÕID, name, salary (instructor)

Union Operation Notation: r È s Defined as: r È s = {t | t Î r or t Î s} For r È s to be valid. r, s must have the same arity (same number of attributes) The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2nd column of s)

Example of Union Operation Relations r, s: r È s:

Example of Union Operation Example: to find all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or in both Õcourse_id (s semester=“Fall” Λ year=2009 (section)) È Õcourse_id (s semester=“Spring” Λ year=2010 (section))

Set Difference Operation Notation r – s Defined as: r – s = {t | t Î r and t Ï s} Set differences must be taken between compatible relations. r and s must have the same arity attribute domains of r and s must be compatible

Example of Set difference of two relations Relations r, s: r – s:

Example of Set difference of two relations Example: to find all courses taught in the Fall 2009 semester, but not in the Spring 2010 semester Õcourse_id (s semester=“Fall” Λ year=2009 (section)) − Õcourse_id (s semester=“Spring” Λ year=2010 (section))

Set-Intersection Operation Notation: r Ç s Defined as: r Ç s = { t | t Î r and t Î s } Assume: r, s have the same arity attributes of r and s are compatible Note: r Ç s = r – (r – s)

Example of Set intersection of two relations Relation r, s: r Ç s Note: r Ç s = r – (r – s)

Cartesian-Product Operation Notation r x s Defined as: r x s = {t q | t Î r and q Î s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R Ç S = Æ). If attributes of r(R) and s(S) are not disjoint, then renaming must be used.

Example of Cartesian-Product Operation Relations r, s: r x s:

Cartesian-product – naming issue Relations r, s: B r.B s.B r x s:

Rename Operation Allows us to name, and therefore to refer to, the results of relational- algebra expressions. Allows us to refer to a relation by more than one name. Example: r x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A1 , A2 , …., An .

Renaming a Table Allows us to refer to a relation, (say E) by more than one name r x (E) returns the expression E under the name X Relations r r.A r.B s.A s.B r x r s (r) α β 1 2 α β 1 2

Composition of Operations Can build expressions using multiple operations Example: sA=C (r x s) r x s sA=C (r x s)

Natural Join Let r and s be relations on schemas R and S respectively. Then, the “natural join” of relations R and S is a relation on schema R È S obtained as follows: Consider each pair of tuples tr from r and ts from s. If tr and ts have the same value on each of the attributes in R Ç S, add a tuple t to the result, where t has the same value as tr on r t has the same value as ts on s

Natural Join Example Relations r, s: Natural Join r s Õ A, r.B, C, r.D, E (s r.B = s.B ˄ r.D = s.D (r x s)))

Notes about Relational Languages Each Query input is a table (or set of tables) Each query output is a table. All data in the output table appears in one of the input tables Relational Algebra is not Turning complete Can we compute: SUM AVG MAX MIN

Summary of Relational Algebra Operators Symbol (Name) Example of Use (Selection) σ salary > = 85000 (instructor) σ Return rows of the input relation that satisfy the predicate. Π (Projection) Π ID, salary (instructor) Output specified attributes from all rows of the input relation. Remove duplicate tuples from the output. x (Cartesian Product) instructor x department Output pairs of rows from the two input relations that have the same value on all attributes that have the same name. ∪ (Union) Π name (instructor) ∪ Π name (student) Output the union of tuples from the two input relations. - (Set Difference) Π name (instructor) -- Π name (student) Output the set difference of tuples from the two input relations. ⋈ (Natural Join) instructor ⋈ department Output pairs of rows from the two input relations that have the same value on all attributes that have the same name.

Quiz Sno Sname Ssex Sage Sdept Course Student Cno Cname Cpqo Ccredit 1 database 5 4 2    math 4 2 information system  1 4 4  Operating System 6 3 5  Data structure 7 4 6  Data Processing 2 4 7    Pascal    6 4 Sno Sname Ssex Sage Sdept 95001 Yong Li    male  20   CS    95002 Chen liu    female 19 IS 95003   Ming Wang   female 18 MA 95004  li Zhang     male 19 IS 1.Give the names and departments of all students Sno Cno Grade 95001 1 92 95001 2 85 95001 3 88 95002 2 90 95002 3 80 SC 2.Give the students whose department is ‘IS’ 3.Give the student number who study the course ‘1’ 4.Give the student names who study the course ‘1’ or ‘3’ 5. Give the students’ name, name and the grades of the courses they took

• Table • Relation • Tuple • Attribute • Domain • Atomicdomain Nullvalue Database schema Database instance Relation schema Relation instance Keys Superkey ◦ Candidate key Primary key Foreign key Referencing relation Referenced relation Referential integrity constraint Schema diagram • Querylanguage ◦ Procedural language Nonprocedural language Operations on relations Selection of tuples Selection of attributes ◦ Natural join Cartesian product Set operations Relational algebra

What have you learned from this lecture? Any Questions?