The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.

Slides:



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

SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
Relational Algebra.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Subqueries Example Find the name of the producer of ‘Star Wars’.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
CMSC424: Database Design Instructor: Amol Deshpande
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Chapter 4 Rational data model. Chapter 5 Rational data model Chapter 4 Rational data model.
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.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Relational Algebra Spring 2012 Instructor: Hassan Khosravi.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
Relational Algebra (Chapter 7)
Lecture 11: Query processing and optimization Jose M. Peña
Database Systems Relational Algebra assoc. prof., dr. Vladimir Dimitrov web: is.fmi.uni-sofia.bg.
1 Chapter 6 Constraints uForeign Keys uConstraints.
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.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
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.
CS 157B Database Systems Dr. T Y Lin. Updates 1.Red color denotes updated data (ppt) 2.Class participation will be part of “extra” credits to to “quiz.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1.
SQL Exercises – Part I April
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
CS 157B Database Systems Dr. T Y Lin. 1.2 Overview of a Database Management System Data-Definition Language Commands –Illustrated by three examples.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
© D. Wong Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations.
High-level Database Models Prof. Yin-Fu Huang CSIE, NYUST Chapter 4.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
CPSC-310 Database Systems
Relational Data Model Lu Chaojun, SJTU.
Chap 5. The DB Language (SQL)
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
THE RELATIONAL MODEL OF DATA
SQL OVERVIEW DEFINING A SCHEMA
2018, Fall Pusan National University Ki-Joune Li
SQL This presentation will cover: View in database MySQL installation
SQL – Constraints & Triggers
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
CMSC-461 Database Management Systems
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2

Database Systems Yin-Fu Huang 2.1An Overview of Data Models What is a Data model Data model: a notation for describing data or information Three parts: 1) Structure of the data a. Conceptual model 2) Operations on the data a. Queries and modifications b. By limiting operations, it is possible for programmers to describe database operations at a very high level, yet have the DBMS implement the operations efficiently. 3) Constraints on the data

Database Systems Yin-Fu Huang Two important data models: 1) The relational model, including object-relational extensions 2) The semistructured-data model, including XML and related standards 2.1.2Important Data Models

Database Systems Yin-Fu Huang (See Fig. 2.1) This physical implementation is only one possible way the table could be implemented in physical data structures The Relational Model in Brief

Database Systems Yin-Fu Huang 2.1.3The Relational Model in Brief

Database Systems Yin-Fu Huang (See Fig. 2.2) XML: a way to represent data by hierarchically nested tagged elements The operations usually involve following paths in the implied tree from an element to one or more of its nested subelements, then to subelements nested within those, and so on. The constraints often involve the data type of values associated with a tag The Semistructured Model in Brief

Database Systems Yin-Fu Huang 2.1.4The Semistructured Model in Brief

Database Systems Yin-Fu Huang Object-relational model 1) Values can have structure, rather than being elementary types such as integer or strings. 2) Relations can have associated methods. Object-oriented model Hierarchical model Network model 2.1.5Other Data Models

Database Systems Yin-Fu Huang It appear that semistructured models have more flexibility than relations. However, 1) SQL enables the programmer to express their wishes at a very high level. 2) The short SQL programs can be optimized to run as fast, or faster than the code written in alternative languages Comparison of Modeling Approaches

Database Systems Yin-Fu Huang 2.2Basics of the Relational Model (See Fig. 2.3) Attributes Schemas Movies(title, year, length, genre) Database schema Tuples (Gone With the Wind, 1939, 231, drama)

Database Systems Yin-Fu Huang 2.2Basics of the Relational Model

Database Systems Yin-Fu Huang 2.2Basics of the Relational Model Domains Each component of each tuple should be atomic Equivalent Representations of a Relation Relations are sets of tuples, not lists of tuples (See Fig. 2.4) Relation Instances A relation about movies is not static; rather, relations change over time. It is less common for the schema of a relation to change. The current instance

Database Systems Yin-Fu Huang 2.2Basics of the Relational Model

Database Systems Yin-Fu Huang 2.2Basics of the Relational Model Keys of Relations A set of attributes forms a key for a relation if we do not allow two tuples in a relation instance to have the same values in all the attributes of the key An Example Database Schema (See Fig. 2.5)

Database Systems Yin-Fu Huang 2.2Basics of the Relational Model

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Current standard for SQL: SQL-99 1) Data-definition sublanguage 2) Data-manipulation sublanguage Relations in SQL Stored relations called tables Views defined by a computation Temporary tables

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Data Types CHAR(n), VARCHAR(n) BIT(n), BIT VARYING(n) BOOLEAN INT or INTEGER, SHORTINT FLOAT or REAL, DOUBLE PRECISION, DECIMAL(n,d) DATE, TIME

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Simple Table Declarations CREATE TABLE (See Fig. 2.7 and Fig. 2.8) Modifying Relation Schemas DROP TABLE ALTER TABLE 1) ADD followed by an attribute name and its data type ALTER TABLE MovieStar ADD phone CHAR(16); 2) DROP followed by an attribute name ALTER TABLE MovieStar DROP birthdate;

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Default Values gender CHAR(1) DEFAULT ‘?’, birthdate DATE DEFAULT DATE ‘ ’ ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT ‘unlisted’; Declaring Keys We may declare one attribute to be a key when that attribute is listed in the relation schema. We may add to the list of items declared in the schema an additional declaration that says a particular attribute or set of attributes forms the key.

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Two declarations: 1) PRIMARY KEY 2) UNIQUE Two tuples in R cannot agree on all of the attributes in set S (i.e., key), unless one of them is NULL. If PRIMARY KEY is used, then attributes in S are not allowed to have NULL as a value for their components. (See Fig. 2.9, Fig. 2.10, and Fig. 2.11)

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL

Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Relational algebra Why Do We Need a Special Query Language? Relational algebra is useful because it is less powerful than C or Java. Two huge rewards: 1) Ease of programming 2) Producing highly optimized code

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language What Is an Algebra? Atomic operands: 1) Variables that stand for relations 2) Constants, which are finite relations Overview of Relational Algebra Operations: Four broad classes 1) The usual set operations – union, intersection, and difference 2) Operations that remove parts of a relation – selection and projection

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language 3) Operations that combine the tuples of two relations – Cartesian product and join 4) An operation called renaming We generally shall refer to expressions of relational algebra as queries Set Operations on Relations R ∪ S, R∩S, R-S Some conditions on R and S: 1) Schemas with identical sets of attributes, and the types (domains) for each attribute must be the same

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language 2) The order of attributes is the same for both relations. Different names ⇒ renaming operator (See Fig. 2.12) Projection π A1, A2, …, An (R) (See Fig. 2.13) π title, year, length (Movies), π genre (Movies) Selection σ C (R) σ length ≧ 100 (Movies), σ length ≧ 100 AND studioName=’Fox’ (Movies)

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Cartesian Product R×S The components from R precede the components from S in the attribute order for the result. If R and S should happen to have some attributes in common, then we need to invent new names for at least one of each pair of identical attributes. (See Fig. 2.14) Natural Joins R∞S (See Fig. 2.15)

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Joined tuple vs. dangling tuple (See Fig. 2.16) Theta-Joins R∞ C S Constructed as follows: 1) Take the product of R and S. 2) Select from the product only those tuples that satisfy the condition C. U∞ A<D V (See Fig. 2.17) U∞ A<D AND U.B≠V.B V

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Combining Operations to Form Queries One can construct expressions of relational algebra by applying operators to subexpressions, using parentheses when necessary to indicate grouping of operands. “What are the titles and years of movies made by Fox that are at least 100 minutes long?” Four steps: (See Fig. 2.18) π title, year (σ length ≧ 100 (Movies)∩σ studioName=’Fox’ (Movies)) π title, year (σ length ≧ 100 AND studioName=’Fox’ (Movies))

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Naming and Renaming ρ S(A1, A2, …, An) (R) R×ρ S(X, C, D) (S), ρ RS(A, B, X, C, D) (R×S) (See Fig. 2.19) Relationships among Operations Some of the operations can be expressed in terms of other relational-algebra operations. R∩S=R-(R-S) R∞ C S=σ C (R×S)

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language R∞S=π L (σ C (R×S)) 1) C: R.A1=S.A1 AND R.A2=S.A2 AND … AND R.An=S.An 2) L: Attributes of R followed by those attributes in S that are not in R. U∞V ⇒ π A, U.B, U.C, D (σ U.B=V.B AND U.C=V.C (U×V)) U∞ A<D AND U.B≠V.B V ⇒ σ A<D AND U.B≠V.B (U×V) The six remaining operations – union, difference, selection, projection, product, and renaming – form an independent set.

Database Systems Yin-Fu Huang 2.4An Algebraic Query Language A Linear Notation for Algebraic Expressions The notation: 1) A relation name and parenthesized list of attributes for that relation 2) The assignment symbol := 3) Any algebraic expression on the right R(t, y, l, i, s, p):=σ length ≧ 100 (Movies) S(t, y, l, i, s, p):=σ studioName=’Fox’ (Movies) T(t, y, l, i, s, p):=R∩S Answer(title, year):=π t, y (T)

Database Systems Yin-Fu Huang 2.5Constraints on Relations Many kinds of constraints can be expressed in relational algebra Relational Algebra as a Constraint Language Two ways: 1) Equal-to-the-emptyset; e.g., R =  2) Set-containment; e.g., R ⊆ S Referential Integrity Constraints Movies(title, year, length, genre, studioName, producerC#) MovieExec(name, address, cert#, netWorth) π producerC# (Movies) ⊆ π certC# (MovieExec)

Database Systems Yin-Fu Huang 2.5Constraints on Relations StarsIn(movieTitle, movieYear, starName) Movies(title, year, length, genre, studioName, producerC#) π movieTitle, movieYear (StarsIn) ⊆ π title, year (Movies) Key Constraints If two tuples agree on name, then they must also agree on address. σ MS1.name=MS2.name AND MS1.address≠MS2.address (MS1×MS2) =  Additional Constraint Examples Domain constraint σ gender≠’F’ AND gender≠’M’ (MovieStar) = 

Database Systems Yin-Fu Huang 2.5Constraints on Relations MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#) σ netWorth< (Studio∞ presC#=cert# MovieExec) =  π presC# (Studio) ⊆ π cert# (σ netWorth ≧ (MovieExec))

Database Systems Yin-Fu Huang The End.