dr. t. y. lin | sjsu | cs 157a | fall 2011 chapter 2 the relational model of data 1

105
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 Chapter 2 THE RELATIONAL MODEL THE RELATIONAL MODEL OF DATA OF DATA 1

Upload: arabella-francis

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

Chapter 2Chapter 2

THE RELATIONAL THE RELATIONAL MODEL OF DATAMODEL OF DATA

11

Page 2: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

Chapter 2Chapter 2The Relational Model of DataThe Relational Model of Data

2.1 An Overview of Data Models2.1 An Overview of Data Models

2.2 Basics of Relational Model2.2 Basics of Relational Model

2.3 Defining a Relation Schema in SQL2.3 Defining a Relation Schema in SQL

2.4 An Algebraic Query Language2.4 An Algebraic Query Language

2.5 Constraints on Relations2.5 Constraints on Relations

2.6 Summary of Chapter 22.6 Summary of Chapter 2

2.7 References for Chapter 22.7 References for Chapter 2

22

Page 3: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

AN OVERVIEW OF DATA AN OVERVIEW OF DATA MODELSMODELS

Section 2.1 Section 2.1

33

Page 4: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1 An Overview of Data 2.1 An Overview of Data ModelsModels

2.1.1 What is Data Model?2.1.1 What is Data Model?

2.1.2 Important Data Models2.1.2 Important Data Models

2.1.3 The Relational Data Model in Brief2.1.3 The Relational Data Model in Brief

2.1.4 The Semi-structured Model in Brief2.1.4 The Semi-structured Model in Brief

2.1.5 Other Data Models2.1.5 Other Data Models

2.1.6 Comparison of Modeling Approaches2.1.6 Comparison of Modeling Approaches

44

Page 5: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.1 What is Data Model?2.1.1 What is Data Model?

A data model is a “small set of A data model is a “small set of notations/mathematics” (mathematical notations/mathematics” (mathematical model – see Definition in Discrete model – see Definition in Discrete Mathematics) for describing data.Mathematics) for describing data.

The description generally consists of 3 parts:The description generally consists of 3 parts:1.1. StructureStructure: it can be imagined as 'object' in Java or : it can be imagined as 'object' in Java or

'struct' in C but in database world, the structure 'struct' in C but in database world, the structure of data is higher level than of data is higher level than physical data modelphysical data model. . That's why we refer to it as That's why we refer to it as conceptual modelconceptual model..

2.2. OperationsOperations: a limited set of queries (retrieving : a limited set of queries (retrieving data) and modifications (changing data)data) and modifications (changing data)

3.3. ConstraintsConstraints: applying some limitations on what : applying some limitations on what the data can be.the data can be.

55

Page 6: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

StructureStructure

External view External view E.g. CSE.g. CS

conceptual viewconceptual view E.g KnowledgeE.g Knowledge

Internal viewInternal view Congress libarayCongress libaray

66

Page 7: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.2 Important Data Models2.1.2 Important Data Models

Currently, there are two important data Currently, there are two important data models as follows:models as follows:

1.1. The The Relational Data Model Relational Data Model (including Object-(including Object-Relational Extension) which is present in all Relational Extension) which is present in all commercial DBMS'scommercial DBMS's

2.2. The The Semi-Structured Data Model Semi-Structured Data Model (including (including XML) which is an added feature of most XML) which is an added feature of most DBMS'sDBMS's

77

Page 8: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.3 The Relational Data 2.1.3 The Relational Data Model in BriefModel in Brief

The relational model is based on tables.The relational model is based on tables. For instance, the following table shows 3 For instance, the following table shows 3

movies but you can imagine that there movies but you can imagine that there are many more rows.are many more rows.

88

Title Year Length

Genre

Gone with the wind

1939 231 Drama

Star Wars 1977 124 Sci-fi

Wayne World 1992 95 Comedy

Page 9: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.3 The Relational Data 2.1.3 The Relational Data Model in Brief (cont'd)Model in Brief (cont'd)

We are not going to talk about how to We are not going to talk about how to implement the implement the structurestructure of the tables of the tables here and it will be postponed to higher here and it will be postponed to higher courses in database.courses in database.

There are some There are some operationsoperations that we can that we can do on the tables. For example we can do on the tables. For example we can query the rows where the genre is query the rows where the genre is 'comedy'.'comedy'.

As an example for the As an example for the constraintsconstraints, we , we may decide there could never be two may decide there could never be two movies with the same title and year in movies with the same title and year in this table.this table.

99

Page 10: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.4 The Semi-structured 2.1.4 The Semi-structured Model in BriefModel in Brief

Semi-structured data resembles Semi-structured data resembles treestrees or or graphsgraphs, rather than tables or arrays., rather than tables or arrays.

The principal manifestation of this The principal manifestation of this viewpoint today is viewpoint today is XMLXML, a way to , a way to represent data by hierarchically nested represent data by hierarchically nested tagged elements.tagged elements.

TYLIN: IBM ARC abandom it TYLIN: IBM ARC abandom it The tags, similar to those used in HTML.The tags, similar to those used in HTML. You can imagine tags as the column You can imagine tags as the column

headers do in the relational model.headers do in the relational model. You can see an example of XML in the next You can see an example of XML in the next

slide which is the same as movies data.slide which is the same as movies data.1010

Page 11: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.4 The Semi-structured 2.1.4 The Semi-structured Model in Brief (cont'd)Model in Brief (cont'd)

<Movies><Movies>

<Movie title=“Gone with <Movie title=“Gone with the wind”>the wind”>

<Year>1939</Year><Year>1939</Year>

<Length>281</Length><Length>281</Length>

<Genre>drama</Genre><Genre>drama</Genre>

</Movie></Movie>

<Movie title=“Star Wars”><Movie title=“Star Wars”>

<Year>1977</Year><Year>1977</Year>

<Length>124</Length><Length>124</Length>

<Genre>Scifi</Genre><Genre>Scifi</Genre>

</Movie></Movie>

<Movie title=“Wayne’s <Movie title=“Wayne’s World”>World”>

<Year>1992</Year><Year>1992</Year>

<Length>95</Length><Length>95</Length>

<Genre>Comedy</Genre><Genre>Comedy</Genre>

</Movie></Movie>

</Movies></Movies>

1111

Page 12: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.5 Other Data Models2.1.5 Other Data Models

A modern trend is to add A modern trend is to add object-orientedobject-oriented features to the relational model.features to the relational model.

There are two effects of object-There are two effects of object-orientation on relations:orientation on relations:1.1. Values can have Values can have structurestructure, rather than being , rather than being

elementary types such as integer or strings.elementary types such as integer or strings.

2.2. Relations can have associated Relations can have associated methodsmethods.. These extensions are called These extensions are called object-object-

relationalrelational model. model.

1212

Page 13: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.5 Other Data Models 2.1.5 Other Data Models (cont'd)(cont'd)

In earlier DBMS's, there were several In earlier DBMS's, there were several other models like other models like hierarchicalhierarchical model or model or networknetwork model. model.

HierarchicalHierarchical model was a model was a tree-orientedtree-oriented model that unlike the modern DBMS's, it model that unlike the modern DBMS's, it really operated at the physical level.really operated at the physical level.

NetworkNetwork model was a model was a graph-orientedgraph-oriented and and also physical level model as well.also physical level model as well.

1313

Page 14: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.1.6 Comparison of Modeling 2.1.6 Comparison of Modeling Approaches (IBM ARC; no moreApproaches (IBM ARC; no more

Semi-structured models have more flexibility Semi-structured models have more flexibility but relational model is still preferred.but relational model is still preferred.

In large databases, In large databases, efficiency of access to efficiency of access to data and modifying datadata and modifying data are of great are of great importance.importance.

Ease of use Ease of use is another factor of using DBMS's.is another factor of using DBMS's. Both of these features can found in relational Both of these features can found in relational

DBMS's.DBMS's. Moreover, Moreover, SQLSQL, the structured query , the structured query

language, in spite of its simplicity, is a language, in spite of its simplicity, is a powerful language for database operations.powerful language for database operations.

1414

Page 15: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

BASICS OF RELATIONAL BASICS OF RELATIONAL MODELMODEL

Section 2.2 Section 2.2

1515

Page 16: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2 Basics of Relational Model2.2 Basics of Relational Model

2.2.1 Attributes2.2.1 Attributes

2.2.2 Schemas2.2.2 Schemas

2.2.3 Tuples2.2.3 Tuples

2.2.4 Domains2.2.4 Domains

2.2.5 Equivalent Representations of a 2.2.5 Equivalent Representations of a RelationRelation

2.2.6 Relation Instances2.2.6 Relation Instances

2.2.7 Keys of Relations2.2.7 Keys of Relations

2.2.8 An Example Database Schema2.2.8 An Example Database Schema

2.2.9 Exercises for Section 2.22.2.9 Exercises for Section 2.2

1616

Page 17: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2 Basics of Relational Model2.2 Basics of Relational Model

The relational model gives us a single The relational model gives us a single way to represent data: as a two-way to represent data: as a two-dimensional table called a dimensional table called a relationrelation..

MoviesMovies

relationrelation

Each row (Each row (tupletuple) represents a movie and ) represents a movie and each column (each column (attributeattribute) represents a ) represents a property of the movie.property of the movie.

1717

Title Year Length

Genre

Gone with the wind

1939 231 Drama

Star Wars 1977 124 Sci-fi

Wayne World 1992 95 Comedy

Page 18: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.1 Attributes2.2.1 Attributes

The columns of a relation are called The columns of a relation are called attributesattributes..

In the Movies relation (in previous slide), In the Movies relation (in previous slide), title, year, length, and genre are title, year, length, and genre are attributes.attributes.

Attributes appear at the top of the Attributes appear at the top of the columns.columns.

Like choosing descriptive names for Like choosing descriptive names for variables in regular programming variables in regular programming languages, attributes names should be languages, attributes names should be chosen in such a way that describe the chosen in such a way that describe the contents.contents.

1818

Page 19: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.2 Schemas2.2.2 Schemas

The names of a relation and the set of The names of a relation and the set of attributes for the relation is called the attributes for the relation is called the schemaschema of the relation. of the relation.

We show the schema for the relation with We show the schema for the relation with the relation name followed by the the relation name followed by the parenthesized list of its attributesparenthesized list of its attributes

For instance, the following is the schema For instance, the following is the schema of relation Movies: of relation Movies:

Movies(title, year, length, genre)Movies(title, year, length, genre)

1919

Page 20: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.2 Schemas (cont'd)2.2.2 Schemas (cont'd)

Note that the attributes are Note that the attributes are a set, not a a set, not a list list but when we talk about relations, we but when we talk about relations, we often specify an order for the attributes.often specify an order for the attributes.

A database consists of one ore more A database consists of one ore more relations. The set of schemas in the relations. The set of schemas in the database is called a database is called a relational database relational database schemaschema, or just a , or just a database schemadatabase schema..

2020

Page 21: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.3 Tuples2.2.3 Tuples

The rows of a relation, other than the The rows of a relation, other than the header row containing the attributes header row containing the attributes names, are called names, are called tuplestuples..

A tuple has one component for each A tuple has one component for each attribute of the relation.attribute of the relation.

For example, in the Movies relation, the For example, in the Movies relation, the first tuple has four components: 'Gone first tuple has four components: 'Gone with the wind', 1939, 231, and drama for with the wind', 1939, 231, and drama for attributes title, year, length, and genre attributes title, year, length, and genre respectively.respectively.

2121

Page 22: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.3 Tuples (cont'd)2.2.3 Tuples (cont'd)

When we wish to write a tuple in When we wish to write a tuple in isolation, not as a part of a relation, we isolation, not as a part of a relation, we normally use commas to separate normally use commas to separate components like this:components like this:('Gone with the wind', 1939, 231, 'drama')('Gone with the wind', 1939, 231, 'drama')

Note that we always use the same order Note that we always use the same order of the attributes to show the tuple in of the attributes to show the tuple in isolation.isolation.

2222

Page 23: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.4 Domains2.2.4 Domains

Tylin 8/29: Past, present, future dataTylin 8/29: Past, present, future data The relational model requires that each The relational model requires that each

component of each tuple be component of each tuple be atomicatomic.. That is, it must be of some That is, it must be of some elementary elementary

type type such as such as integerinteger or or stringstring.. It is not allowed for a value to be a It is not allowed for a value to be a setset, ,

listlist, , arrayarray or any other type that or any other type that reasonably can be broken into smaller reasonably can be broken into smaller components.components.

It's further assumed that associated with It's further assumed that associated with each attribute of a relation is a each attribute of a relation is a domaindomain..

2323

Page 24: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.4 Domains (cont'd)2.2.4 Domains (cont'd)

We can include the domain for each We can include the domain for each attributes in a schema as follows:attributes in a schema as follows:

Movies(title:string, year:integer, Movies(title:string, year:integer, length:integer, genre:string)length:integer, genre:string)

2424

Page 25: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.5 Equivalent 2.2.5 Equivalent Representations of a RelationRepresentations of a Relation

Relations are sets of tuples, not lists of Relations are sets of tuples, not lists of tuples. In other words, tuples. In other words, the order of the order of tuples in a relation has no significancetuples in a relation has no significance..

Moreover, Moreover, we can reorder the attributeswe can reorder the attributes of a relation as well.of a relation as well.

Note that, when we change the order of Note that, when we change the order of the attributes, we should change the the attributes, we should change the order of the contents as well.order of the contents as well.

2525

Page 26: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.6 Relation Instances2.2.6 Relation Instances Relations change over time. In other Relations change over time. In other

words, relations are words, relations are not staticnot static.. For example, we insert tuples in the For example, we insert tuples in the

Movies relation over time, and also, we Movies relation over time, and also, we may delete or update some tuples as well.may delete or update some tuples as well.

Even the schema can be changed. In other Even the schema can be changed. In other words, we may add/delete an attribute words, we may add/delete an attribute to/from the schema.to/from the schema.

We call a set of tuples for a given relation We call a set of tuples for a given relation an an instanceinstance of that relation. of that relation.

Current instanceCurrent instance, is the set of tuples that , is the set of tuples that exists now.exists now.

2626

Page 27: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.7 Keys of Relations2.2.7 Keys of Relations

Relational model allows us to place some Relational model allows us to place some constraintsconstraints on a schema. on a schema.

One important constraint is called One important constraint is called key key constraint constraint or simply or simply a keya key..

A set of attributes (one or more) forms a A set of attributes (one or more) forms a keykey if two tuples in the relation cannot if two tuples in the relation cannot have the same values in all the attributes have the same values in all the attributes of the key.of the key.

2727

Page 28: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.7 Keys of Relations 2.2.7 Keys of Relations (cont'd)(cont'd)

Example 2.1Example 2.1

For the Movies relation, we can assign the For the Movies relation, we can assign the attributes title and year be the key of the attributes title and year be the key of the relation.relation.

In this way, the relation cannot have two In this way, the relation cannot have two tuples with the same title and year.tuples with the same title and year.

Note that the title by itself does not form a Note that the title by itself does not form a key because there are many movies over key because there are many movies over the years that have the same name. In the years that have the same name. In other words, the title by itself is not other words, the title by itself is not unique and cannot identify a movie unique and cannot identify a movie uniquely.uniquely.

2828

Page 29: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.7 Keys of Relations 2.2.7 Keys of Relations (cont'd)(cont'd)

We indicate the attribute(s) contributing We indicate the attribute(s) contributing in the key by in the key by underlyingunderlying them as follows: them as follows:Movies(Movies(titletitle, , yearyear, length, genre), length, genre)

Note that the Note that the key is a constraint for all key is a constraint for all possible instancespossible instances of the relation, and of the relation, and not for a specific instance.not for a specific instance.

Most of the time, we use an Most of the time, we use an artificial keys artificial keys for a relation. For example, for the for a relation. For example, for the Movies relation, we could add a new Movies relation, we could add a new attributes movie_id and assign it as the attributes movie_id and assign it as the key. In this way, we could make sure it key. In this way, we could make sure it was unique for all possible tuples.was unique for all possible tuples.

2929

Page 30: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.8 An Example Database 2.2.8 An Example Database SchemaSchema

The database schema that are used during The database schema that are used during this book is as follows:this book is as follows:

Movies(Movies(titletitle:string, :string, yearyear:integer, :integer, length:integer, genre:string, length:integer, genre:string, studioName:string, producerC#:integer)studioName:string, producerC#:integer)

Moviestar(Moviestar(namename:string, address:string, :string, address:string, gender:char, birthdate:date)gender:char, birthdate:date)

3030

Page 31: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.8 An Example Database 2.2.8 An Example Database Schema (cont'd)Schema (cont'd)

StarsIn(StarsIn(movieTitlemovieTitle:string, :string, movieYearmovieYear:integer, :integer, starNamestarName:string):string)

MovieExec (name:string, addres:string, MovieExec (name:string, addres:string, cert#cert#:integer, netWorth:integer):integer, netWorth:integer)

Studio (Studio (namename:string, address:string, :string, address:string, presC#:integer)presC#:integer)

3131

Page 32: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.2.9 Exercises for Section 2.22.2.9 Exercises for Section 2.2

3232

Page 33: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

DEFINING A RELATION DEFINING A RELATION SCHEMA IN SQLSCHEMA IN SQL

Section 2.3 Section 2.3

3333

Page 34: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3 Defining a Relation 2.3 Defining a Relation Schema in SQLSchema in SQL

2.3.1 Relations in SQL2.3.1 Relations in SQL

2.3.2 Data Types2.3.2 Data Types

2.3.3 Simple Table Declaration2.3.3 Simple Table Declaration

2.3.4 Modifying Relations Schemas2.3.4 Modifying Relations Schemas

2.3.5 Default Values2.3.5 Default Values

2.3.6 Declaring Keys2.3.6 Declaring Keys

2.3.7 Exercises for Section 2.32.3.7 Exercises for Section 2.3

3434

Page 35: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3 Defining a Relation 2.3 Defining a Relation Schema in SQLSchema in SQL

SQL, Structured Query Language, SQL, Structured Query Language, pronounced "sequel", is the principal pronounced "sequel", is the principal language to language to describedescribe, and , and manipulatemanipulate relational database.relational database.

There is a standard called SQL-99 that There is a standard called SQL-99 that most commercial databases implemented most commercial databases implemented something similar, but not identical to, something similar, but not identical to, the standard.the standard.

There are two sub-languages for SQL:There are two sub-languages for SQL: DDL: Data Definition LanguageDDL: Data Definition Language DML: Data Manipulation LanguageDML: Data Manipulation Language

3535

Page 36: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.1 Relations in SQL2.3.1 Relations in SQL

SQL makes a distinction between three SQL makes a distinction between three kinds of relations:kinds of relations: Stored relationsStored relations: are called tables. These : are called tables. These

relations exists in database and usually we deal relations exists in database and usually we deal with them.with them.

ViewsViews: are relations that do not exist but are : are relations that do not exist but are constructed when needed.constructed when needed.

Temporary tablesTemporary tables: are constructed temporarily by : are constructed temporarily by SQL processor when it executes queries or other SQL processor when it executes queries or other tasks.tasks.

We are going to discuss about tables in this We are going to discuss about tables in this chapter. Views will be covered in chapter 8 chapter. Views will be covered in chapter 8 and temporary tables are never declared.and temporary tables are never declared.

3636

Page 37: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.2 Data Types2.3.2 Data Types

All attributes must have a data type.All attributes must have a data type. The primitive data types supported by SQL The primitive data types supported by SQL

are:are: Character stringCharacter string

CHAR(n)CHAR(n): fixed length string of length n; short : fixed length string of length n; short strings will be padded with trailing blank to make n strings will be padded with trailing blank to make n characters.characters.

VARCHAR(n)VARCHAR(n): variable length string up to n : variable length string up to n character; an character; an end-markerend-marker or or string-lengthstring-length is used to is used to show the end of the string; the purpose is to save show the end of the string; the purpose is to save space.space.Note that longer values will be truncated to fit.Note that longer values will be truncated to fit.

Bit stringBit string BIT(n)BIT(n): fixed bit string of length n;: fixed bit string of length n; BIT VARYING(n)BIT VARYING(n): bit string of length up to n;: bit string of length up to n;

3737

Page 38: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.2 Data Types (cont'd)2.3.2 Data Types (cont'd)

The primitive data types (cont'd):The primitive data types (cont'd): BOOLEAN: BOOLEAN: a logical value of TRUE, FALSE, or a logical value of TRUE, FALSE, or

UNKNOWN (NULL)UNKNOWN (NULL) INTINT or or INTEGERINTEGER: integer value: integer value SHORTINTSHORTINT: short integer; usually the lower : short integer; usually the lower

bound and the upper bound of SHORTINT is bound and the upper bound of SHORTINT is half of INTEGER's.half of INTEGER's.

FLOATFLOAT or or REALREAL: floating point number: floating point number DOUBLEDOUBLE: double precision real number: double precision real number DECIMAL(n, d)DECIMAL(n, d): customized real number; : customized real number; NUMERIC(n, d)NUMERIC(n, d): a synonym for DECIMAL: a synonym for DECIMAL

3838

Page 39: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.2 Data Types (cont'd)2.3.2 Data Types (cont'd)

The primitive data types (cont'd):The primitive data types (cont'd): DATEDATE : represents a date value of the form : represents a date value of the form

'yyyy-mm-dd''yyyy-mm-dd' TIMETIME: represents a time value of the form: represents a time value of the form

'HH:mm:ss''HH:mm:ss' or or 'HH:mm:ss.d''HH:mm:ss.d' (d is a fraction (d is a fraction of seconds)of seconds)

You can create a date constant like this:You can create a date constant like this:DATE '2011-08-24' DATE '2011-08-24'

You can create a time constant like this:You can create a time constant like this: TIME '16:09:25' TIME '16:09:25' or or TIME '16:09:25.378' TIME '16:09:25.378'

Most databases have Most databases have TIMESTAMPTIMESTAMP data data type of the form type of the form 'yyyy-mm-dd HH:mm:ss''yyyy-mm-dd HH:mm:ss'

3939

Page 40: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.3 Simple Table Declaration2.3.3 Simple Table Declaration

The simplest form of relation declaration:The simplest form of relation declaration:

CREATE TABLE tabName(CREATE TABLE tabName(attribattrib11 type, type,attribattrib22 type, type,......attribattribnn type); type);

4040

Page 41: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.3 Simple Table Declaration 2.3.3 Simple Table Declaration (cont'd)(cont'd)

Example 2.2Example 2.2

The relation Movies can be declared as The relation Movies can be declared as follows:follows:

CREATE TABLE Movies(CREATE TABLE Movies(

title CHAR(100),title CHAR(100),

year INT,year INT,

length INT,length INT,

genre genre CHAR(10), CHAR(10),

studioName CHAR(30),studioName CHAR(30),

producerC# INT);producerC# INT);4141

Page 42: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.3 Simple Table Declaration 2.3.3 Simple Table Declaration (cont'd)(cont'd)

Example 2.3Example 2.3

The relation MovieStar can be declared as The relation MovieStar can be declared as follows:follows:

CREATE TABLE MovieStar(CREATE TABLE MovieStar(

name CHAR(30),name CHAR(30),

address address VARCHAR(255), VARCHAR(255),

gender CHAR(1),gender CHAR(1),

birthdate DATE);birthdate DATE);

The gender attribute can be 'M' or 'F'.The gender attribute can be 'M' or 'F'.4242

Page 43: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.4 Modifying Relations 2.3.4 Modifying Relations SchemasSchemas

To drop a relation R, execute the To drop a relation R, execute the following SQL statement:following SQL statement:DROP TABLE R;DROP TABLE R;

To alter the schema, we have several To alter the schema, we have several options.options. To add attributes:To add attributes:ALTER TABLE R ALTER TABLE R ADD attribADD attrib11 type, ..., attrib type, ..., attribnn type; type;

To drop attributes:To drop attributes:ALTER TABLE RALTER TABLE RDROP attribDROP attrib11, ..., attrib, ..., attribnn;;

4343

Page 44: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.4 Modifying Relations 2.3.4 Modifying Relations Schemas (cont'd)Schemas (cont'd)

Example 2.4Example 2.4

Add an attribute to MoviesStar for phone Add an attribute to MoviesStar for phone data.data.

ALTER TABLE MovieStar ALTER TABLE MovieStar

ADD phone CHAR(16);ADD phone CHAR(16);

Note that phone attribute will be Note that phone attribute will be NULL for NULL for all existing tuplesall existing tuples..

Drop birthdate attributeDrop birthdate attribute

ALTER TABLE MovieStar ALTER TABLE MovieStar

DROP bithdate;DROP bithdate;4444

Page 45: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.5 Default Values2.3.5 Default Values

When we insert or modify a tuple, we When we insert or modify a tuple, we sometimes do not have values for some sometimes do not have values for some attributes and we wish to assign a attributes and we wish to assign a default values for them. default values for them.

To assign a default value for attribute1, To assign a default value for attribute1, we use the following syntax:we use the following syntax:

CREATE TABLE tabName(CREATE TABLE tabName(attribattrib11 type type DEFAULT defaultValueDEFAULT defaultValue,,......attribattribnn type); type);

4545

Page 46: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.4 Modifying Relations 2.3.4 Modifying Relations Schemas (cont'd)Schemas (cont'd)

Example 2.5Example 2.5Assign default value '?' for gender and Assign default value '?' for gender and

default value '0000-00-00' for birthdate.default value '0000-00-00' for birthdate.CREATE TABLE MovieStar(CREATE TABLE MovieStar(

name CHAR(30),name CHAR(30),

address VARCHAR(255),address VARCHAR(255),

gender CHAR(1) DEFAULT '?',gender CHAR(1) DEFAULT '?',

birthdate DATE DEFAULT DATE '0000-00-00');birthdate DATE DEFAULT DATE '0000-00-00');

Note that we can assign a default value when Note that we can assign a default value when altering a schema as well:altering a schema as well:

ALTER TABLE MovieStar ALTER TABLE MovieStar

ADD phone CHAR(16) ADD phone CHAR(16) DEFAULT 'unlisted'DEFAULT 'unlisted';;4646

Page 47: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.6 Declaring Keys2.3.6 Declaring Keys

There are two ways to declare an attribute There are two ways to declare an attribute or a set of attributes to be a key:or a set of attributes to be a key: Method 1:Method 1:CREATE TABLE tabName(CREATE TABLE tabName(attribattrib11 type type PRIMARY KEYPRIMARY KEY,,......attribattribnn type); type);

Method 2:Method 2:CREATE TABLE tabName(CREATE TABLE tabName(attribattrib11 type, type,......attribattribnn type, type,PRIMARY KEY(attribPRIMARY KEY(attrib11,...,attrib,...,attribkk))););

4747

Page 48: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.6 Declaring Keys (cont'd)2.3.6 Declaring Keys (cont'd)

Note that if the key is a set of attributes, Note that if the key is a set of attributes, then we have to use method 2 but if the then we have to use method 2 but if the key is just one attribute, then either key is just one attribute, then either methods can be used.methods can be used.

There are two declarations that may be There are two declarations that may be used to indicate key:used to indicate key: PRIMARY KEYPRIMARY KEY UNIQUEUNIQUE

Both have the same effect but in Both have the same effect but in PRIMARY KEY case, none of the PRIMARY KEY case, none of the attributes of the key can be NULL but in attributes of the key can be NULL but in UNIQUE case, it's possible.UNIQUE case, it's possible.

4848

Page 49: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.6 Declaring Keys (cont'd)2.3.6 Declaring Keys (cont'd)

Example 2.6Example 2.6

Declare name attribute as primary key in Declare name attribute as primary key in MovieStar relation.MovieStar relation.

CREATE TABLE MovieStar(CREATE TABLE MovieStar(

name CHAR(30) name CHAR(30) PRIMARY KEYPRIMARY KEY,,

address VARCHAR(255),address VARCHAR(255),

gender CHAR(1) DEFAULT '?',gender CHAR(1) DEFAULT '?',

birthdate DATE DEFAULT DATE '0000-00-00');birthdate DATE DEFAULT DATE '0000-00-00');

4949

Page 50: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.6 Declaring Keys (cont'd)2.3.6 Declaring Keys (cont'd)

Example 2.6 (cont'd)Example 2.6 (cont'd)

Alternatively, we can use the following Alternatively, we can use the following syntax:syntax:

CREATE TABLE MovieStar(CREATE TABLE MovieStar(

name CHAR(30),name CHAR(30),

address VARCHAR(255),address VARCHAR(255),

gender CHAR(1) DEFAULT '?',gender CHAR(1) DEFAULT '?',

birthdate DATE DEFAULT DATE '0000-00-00'),birthdate DATE DEFAULT DATE '0000-00-00'),

PRIMARY KEY (name)PRIMARY KEY (name);;

Note that UNIQUE can replace PRIMARY Note that UNIQUE can replace PRIMARY KEY.KEY.

5050

Page 51: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.6 Declaring Keys (cont'd)2.3.6 Declaring Keys (cont'd)

Example 2.7Example 2.7Declare title and year attributes as primary Declare title and year attributes as primary

key in Movies relation. key in Movies relation.

CREATE TABLE Movies(CREATE TABLE Movies(

title CHAR(100),title CHAR(100),

year INT,year INT,

length INT,length INT,

genre CHAR(10),genre CHAR(10),

studioName CHAR(30),studioName CHAR(30),

producerC# INT,producerC# INT,

PRIMARY KEY (title, year)PRIMARY KEY (title, year);;5151

Page 52: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.3.7 Exercises for Section 2.32.3.7 Exercises for Section 2.3

5252

Page 53: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

AN ALGEBRAIC QUERY AN ALGEBRAIC QUERY LANGUAGELANGUAGE

Section 2.4 Section 2.4

5353

Page 54: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4 An Algebraic Query 2.4 An Algebraic Query LanguageLanguage

2.4.1 Why Do We Need a Special Query 2.4.1 Why Do We Need a Special Query Language?Language?

2.4.2 What is an Algebra?2.4.2 What is an Algebra?

2.4.3 Overview of an Relational Algebra2.4.3 Overview of an Relational Algebra

2.4.4 Set Operations on Relations2.4.4 Set Operations on Relations

2.4.5 Projection2.4.5 Projection

2.4.6 Selection2.4.6 Selection

2.4.7 Cartesian Product2.4.7 Cartesian Product

2.4.8 Natural Joins2.4.8 Natural Joins

2.4.9 Theta-Joins2.4.9 Theta-Joins

5454

Page 55: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4 An Algebraic Query 2.4 An Algebraic Query Language (cont'd)Language (cont'd)

2.4.10 Combining Operations to Form 2.4.10 Combining Operations to Form QueriesQueries

2.4.11 Naming and Renaming2.4.11 Naming and Renaming

2.4.12 Relationships Among Operations2.4.12 Relationships Among Operations

2.4.13 A linear Notation for Algebraic 2.4.13 A linear Notation for Algebraic ExpressionsExpressions

2.4.14 Exercises for Section 2.42.4.14 Exercises for Section 2.4

5555

Page 56: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4 An Algebraic Query 2.4 An Algebraic Query LanguageLanguage

A DBMS needs a way to query the data A DBMS needs a way to query the data and to modify the data.and to modify the data.

We begin our study of operations on We begin our study of operations on relations with a special algebra called relations with a special algebra called relational algebrarelational algebra..

Relational algebra was used by some Relational algebra was used by some early DBMS's prototypes but early DBMS's prototypes but is not used is not used in current commercial DBMS'sin current commercial DBMS's..

The real query language, SQL, uses The real query language, SQL, uses relational algebra internally to relational algebra internally to optimize optimize the process of retrievingthe process of retrieving the data. the data.

5656

Page 57: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.1 Why Do We Need a 2.4.1 Why Do We Need a Special Query Language?Special Query Language?

Why we don't use Java or C to retrieve the Why we don't use Java or C to retrieve the needed data?needed data?

For example, we could represent a tuple For example, we could represent a tuple with an object in Java and we could with an object in Java and we could represent a relation with an array of the represent a relation with an array of the objects! What would be the problem? objects! What would be the problem?

Surprisingly, relational algebra is useful Surprisingly, relational algebra is useful because it is because it is less powerful less powerful than Java or C!than Java or C!

Ease of programming Ease of programming and producing highly and producing highly optimized code optimized code by compiler are two by compiler are two important advantages of being less important advantages of being less powerful.powerful.

5757

Page 58: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.2 What is an Algebra?2.4.2 What is an Algebra?

In general, algebra consists of In general, algebra consists of operatorsoperators and and atomic operandsatomic operands..

In arithmetic, the atomic operand are In arithmetic, the atomic operand are variables like x and y and constants like 10 variables like x and y and constants like 10 and the operators are the simple arithmetic and the operators are the simple arithmetic operators like: +, -, /, *.operators like: +, -, /, *.

Any algebra allows us to build Any algebra allows us to build expressionsexpressions by combining operators and atomic by combining operators and atomic operands.operands.

Relational algebra Relational algebra is another example of is another example of algebra. Variables are relations and algebra. Variables are relations and constants are finite relations. Operators will constants are finite relations. Operators will be covered in next sub-sections.be covered in next sub-sections.

5858

Page 59: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.3 Overview of an 2.4.3 Overview of an Relational AlgebraRelational Algebra

The operations fall into four classes:The operations fall into four classes: Set operationsSet operations: union, intersection, difference: union, intersection, difference Operations that remove some parts of a relation: Operations that remove some parts of a relation:

SelectionSelection – eliminates some tuples – eliminates some tuples ProjectionProjection – eliminates some attributes – eliminates some attributes

Operations that combine the tuples of two Operations that combine the tuples of two relations:relations:

Cartesian productCartesian product: pairs the tuples of two relations : pairs the tuples of two relations in all possible waysin all possible ways

Various kinds of joinsVarious kinds of joins: will be covered later: will be covered later RenamingRenaming: changes the schema without changing : changes the schema without changing

the tuples.the tuples. We refer to expressions of relational algebra We refer to expressions of relational algebra

as as queriesqueries..5959

Page 60: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.4 Set Operations on 2.4.4 Set Operations on RelationsRelations

The three most common operations on sets The three most common operations on sets are:are: UnionUnion: R : R S, is the set of elements that are in R S, is the set of elements that are in R

or S or both. or S or both. IntersectionIntersection: R : R S, is the set of elements that S, is the set of elements that

are in both R and S.are in both R and S. DifferenceDifference: R – S, is the set of elements that are : R – S, is the set of elements that are

in R but not in S.in R but not in S. Note that an element appears in a set once Note that an element appears in a set once

and duplicated values are not allowed.and duplicated values are not allowed. When we apply these operations to When we apply these operations to

relations, we need to put some conditions relations, we need to put some conditions on R and S.on R and S.

6060

Page 61: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.4 Set Operations on 2.4.4 Set Operations on Relations (cont'd)Relations (cont'd)

The conditions of R and SThe conditions of R and S R and S must have the same schema. R and S must have the same schema. The order of attributes is important here and The order of attributes is important here and

must be the same.must be the same. If the name of the attributes are different If the name of the attributes are different

but the types are the same, we can but the types are the same, we can rename the attributes temporarily by rename the attributes temporarily by renaming operator.renaming operator.

6161

Page 62: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.4 Set Operations on 2.4.4 Set Operations on Relations (cont'd)Relations (cont'd)

Example 2.8Example 2.8

Given relations R and S as follows, compute:Given relations R and S as follows, compute:R R S, R S, R S, and R – S S, and R – S

Relation RRelation R

Relation SRelation S

6262

Name Gender Birthdate

Carrie Fisher F 9/9/99

Mark Hamill M 8/8/88

Name Gender Birthdate

Carrie Fisher F 9/9/99

Harrison Ford M 7/7/77

Page 63: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.4 Set Operations on 2.4.4 Set Operations on Relations (cont'd)Relations (cont'd)

Example 2.8 (cont'd)Example 2.8 (cont'd)

R R S S

R R S S

R - SR - S

6363

Name Gender Birthdate

Carrie Fisher F 9/9/99

Mark Hamill M 8/8/88

Harrison Ford M 7/7/77

Name Gender Birthdate

Carrie Fisher F 9/9/99

Name Gender Birthdate

Mark Hamill M 8/8/88

Page 64: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.5 Projection2.4.5 Projection

The projection operator produces a new The projection operator produces a new relation that has only some of the attributes.relation that has only some of the attributes.

Projection operator in relational algebra is:Projection operator in relational algebra is:

ππA1, A2, ..., An A1, A2, ..., An (R)(R) This operator applies on the relation R and This operator applies on the relation R and

produces a new relation with only attributes produces a new relation with only attributes AA11, A, A22, …, A, …, An n from relation R. In other words, from relation R. In other words, the schema of the new relation would have the schema of the new relation would have the following set of attributes:the following set of attributes:

{A{A11, A, A22, …, A, …, Ann}}

6464

Page 65: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.5 Projection (cont'd)2.4.5 Projection (cont'd)

Example 2.9Example 2.9

Given the relation Movies. Project the first Given the relation Movies. Project the first three attributes.three attributes.

The resultThe result

relationrelation

6565

Title Year

Length

Genre studioName

ProducerC#

Star Wars 1977 124 sciFi Fox 12345

Galaxy Quest

1999 104 Comedy

Dreamworks

67890

Wayne’s World

1992 95 Comedy

Paramount 99999Title Year

Length

Star Wars 1977 124

Galaxy Quest

1999 104

Wayne’s World

1992 95

Page 66: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.5 Projection (cont'd)2.4.5 Projection (cont'd)

Example 2.9 (cont'd)Example 2.9 (cont'd)

Project the Genre attribute.Project the Genre attribute.

The resultThe result

relationrelation

Note that in the Note that in the relational algebra of setsrelational algebra of sets, , duplicate tuples are always eliminated. duplicate tuples are always eliminated. That's why 'Comedy' tuple is one instead That's why 'Comedy' tuple is one instead of two.of two.

6666

Genre

sciFi

Comedy

Page 67: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.6 Selection2.4.6 Selection

The selection operator, applies to a The selection operator, applies to a relation R, and produces a new relation relation R, and produces a new relation with with a subset of R's tuplesa subset of R's tuples..

The tuples in the resulting relation are The tuples in the resulting relation are those that satisfy some condition C that those that satisfy some condition C that involves the attributes of R.involves the attributes of R.

Selection operator is denoted by:Selection operator is denoted by:

σσCC (R) (R) The The schemaschema for the resulting relation is for the resulting relation is

the same as R's schema. the same as R's schema.

6767

Page 68: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.6 Selection (cont'd)2.4.6 Selection (cont'd)

The operands in condition C are either The operands in condition C are either constants or attributes of R.constants or attributes of R.

We apply C to each tuple t of R by We apply C to each tuple t of R by substituting.substituting.

6868

Page 69: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.6 Selection (cont'd)2.4.6 Selection (cont'd)

Example 2.10Example 2.10

Given the Movies relation as follows: Given the Movies relation as follows:

Find Find σσlength >= 100length >= 100 (Movies). (Movies).

6969

Title Year

Length

Genre studioName

ProducerC#

Star Wars 1977 124 sciFi Fox 12345

Galaxy Quest 1999 104 Comedy

DreamWorks

67890

Wayne's World

1992 95 Comedy

Paramount 99999

Page 70: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.6 Selection (cont'd)2.4.6 Selection (cont'd)

Example 2.10 (cont'd)Example 2.10 (cont'd)

The first two tuples satisfy the condition. The first two tuples satisfy the condition. So, the result relation would be:So, the result relation would be:

7070

Title Year

Length

Genre studioName

ProducerC#

Star Wars 1977 124 sciFi Fox 12345

Galaxy Quest 1999 104 Comedy

DreamWorks

67890

Page 71: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.6 Selection (cont'd)2.4.6 Selection (cont'd)

Example 2.11Example 2.11

The Movies relation is given. The Movies relation is given.

Find the set of tuples that represent Fox Find the set of tuples that represent Fox movies at least 100 minutes long. movies at least 100 minutes long.

So, we are looking for:So, we are looking for:

σσlength >= 100 AND studioName = 'FOX'length >= 100 AND studioName = 'FOX' (Movies). (Movies).

7171

Title Year

Length

Genre studioName

ProducerC#

Star Wars 1977 124 sciFi Fox 12345

Galaxy Quest 1999 104 Comedy

DreamWorks

67890

Wayne's World

1992 95 Comedy

Paramount 99999

Page 72: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.6 Selection (cont'd)2.4.6 Selection (cont'd)

Example 2.11 (cont'd)Example 2.11 (cont'd)

The result would be:The result would be:

7272

Title Year

Length

Genre studioName

ProducerC#

Star Wars 1977 124 sciFi Fox 12345

Page 73: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.7 Cartesian Product2.4.7 Cartesian Product

The Cartesian Product (or The Cartesian Product (or cross product cross product or just product for simplicity) of two sets or just product for simplicity) of two sets R and S is the set of pairs that can be R and S is the set of pairs that can be formed by choosing the first element of formed by choosing the first element of the pair to be any element of R and the the pair to be any element of R and the second any element of S.second any element of S.

This product is denote by R X S.This product is denote by R X S. In relational algebra, the sets are the In relational algebra, the sets are the

relations and the members are the relations and the members are the tuples.tuples.

7373

Page 74: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.7 Cartesian Product 2.4.7 Cartesian Product (cont'd)(cont'd)

Example 2.12Example 2.12

Relation R Relation S Result R X SRelation R Relation S Result R X S

Note that attribute B is in both schemas, it Note that attribute B is in both schemas, it has been R.B and S.B in the result to has been R.B and S.B in the result to disambiguate them.disambiguate them.

7474

A B

1 2

3 4

B C D

2 5 6

4 7 8

9 10 11

A R.B

S.B

C D

1 2 2 5 6

1 2 4 7 8

1 2 9 10 11

3 4 2 5 6

3 4 4 7 8

3 4 9 10 11

Page 75: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.8 Natural Joins2.4.8 Natural Joins

More often the cross product is not what we More often the cross product is not what we want. Usually we want to pair only those want. Usually we want to pair only those tuples that match in some certain conditions.tuples that match in some certain conditions.

The simplest way is the The simplest way is the natural join natural join of two of two relation R and S.relation R and S.

In this join we pair those tuples that agree In this join we pair those tuples that agree with the common attributes in R and S.with the common attributes in R and S.

Natural join is denoted by:Natural join is denoted by:

R ∞ SR ∞ S A tuple that fails to pair with any tuple of the A tuple that fails to pair with any tuple of the

other relation is said to be a other relation is said to be a dangling tupledangling tuple..

7575

Page 76: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.8 Natural Joins (cont'd)2.4.8 Natural Joins (cont'd)

7676

Example 2.13Example 2.13

Relation R Relation S Result R ∞ SRelation R Relation S Result R ∞ S

Note that attribute B is in both schemas, Note that attribute B is in both schemas, and since they should be equal in the and since they should be equal in the result, therefore, one copy of it is result, therefore, one copy of it is enough in the result.enough in the result.

A B

1 2

3 4

B C D

2 5 6

4 7 8

9 10 11

A B C D

1 2 5 6

3 4 7 8

Page 77: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.8 Natural Joins (cont'd)2.4.8 Natural Joins (cont'd)

7777

Example 2.14Example 2.14

Relation U Relation V Result U ∞ VRelation U Relation V Result U ∞ V

Note that attribute B and C are in both Note that attribute B and C are in both schemas, and since they should be equal schemas, and since they should be equal in the result, therefore, one copy of in the result, therefore, one copy of them is enough in the result.them is enough in the result.

A B C

1 2 3

6 7 8

9 7 8

B C D

2 3 4

2 3 5

7 8 10

A B C D

1 2 3 4

1 2 3 5

6 7 8 10

9 7 8 10

Page 78: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.9 Theta-Joins2.4.9 Theta-Joins

Equating the shared attributes is just Equating the shared attributes is just one way that is used in natural join. one way that is used in natural join.

It is sometimes desirable to pair tuples It is sometimes desirable to pair tuples from two relations on some other basis.from two relations on some other basis.

Historically, the theta refers to an Historically, the theta refers to an arbitrary condition. We use C as the arbitrary condition. We use C as the condition rather than θ.condition rather than θ.

Theta-join is denoted by:Theta-join is denoted by:

R ∞R ∞CC S S

7878

Page 79: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.9 Theta-Joins (cont'd)2.4.9 Theta-Joins (cont'd)

7979

Example 2.15Example 2.15

Relation U Relation U

Result: U ∞Result: U ∞A<DA<D V V

Relation VRelation V

A B C

1 2 3

6 7 8

9 7 8

B C D

2 3 4

2 3 5

7 8 10

A U.B U.C V.B V.C D

1 2 3 2 3 4

1 2 3 2 3 5

1 2 3 7 8 10

6 7 8 7 8 10

9 7 8 7 8 10

Page 80: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.9 Theta-Joins (cont'd)2.4.9 Theta-Joins (cont'd)

8080

Example 2.16Example 2.16

Relation U Relation U

Result: Result: U ∞U ∞A<D AND U.B <> V.BA<D AND U.B <> V.B V V

Relation VRelation V

A B C

1 2 3

6 7 8

9 7 8

B C D

2 3 4

2 3 5

7 8 10

A U.B U.C V.B V.C D

1 2 3 7 8 10

Page 81: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.10 Combining Operations 2.4.10 Combining Operations to Form Queriesto Form Queries

Relational algebra like all other Relational algebra like all other algebras, allows us to form algebras, allows us to form complex complex expressions expressions by applying operations to by applying operations to the result of other operations.the result of other operations.

One can construct expressions of One can construct expressions of relational algebra by applying operators relational algebra by applying operators to sub expressions, using parenthesis to sub expressions, using parenthesis when necessary to indicate grouping of when necessary to indicate grouping of operands.operands.

It is also possible to represent It is also possible to represent expressions as expressions as expression treesexpression trees..

8181

Page 82: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.10 Combining Operations 2.4.10 Combining Operations to Form Queries (cont'd)to Form Queries (cont'd)

Example 2.17Example 2.17

What are the titles and years of Movies What are the titles and years of Movies made by fox that are at least 100 minutes made by fox that are at least 100 minutes long?long?

One solution would be:One solution would be:

1.1. Select those tuples that have length >= Select those tuples that have length >= 100.100.

2.2. Select those tuples that have studioName Select those tuples that have studioName = 'Fox'.= 'Fox'.

3.3. Compute the intersection of (1) and (2).Compute the intersection of (1) and (2).

4.4. Project the relation from (3) onto Project the relation from (3) onto attributes title and year.attributes title and year.

8282

Page 83: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.10 Combining Operations 2.4.10 Combining Operations to Form Queries (cont'd)to Form Queries (cont'd)

Example 2.17 (cont'd)Example 2.17 (cont'd)

Here is the Here is the

suggestedsuggested

expression tree!expression tree!

8383

σσlength >= 100length >= 100

ππ title, yeartitle, year

σσstudioName = 'Fox'studioName = 'Fox'

MoviesMovies MoviesMovies

Page 84: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.10 Combining Operations 2.4.10 Combining Operations to Form Queries (cont'd)to Form Queries (cont'd)

Example 2.17 (cont'd)Example 2.17 (cont'd)

Alternatively, we could represent the same Alternatively, we could represent the same expression in a linear notation as follows:expression in a linear notation as follows:

ππtitle,year title,year (σ(σlength>=100length>=100((MoviesMovies)) σσstudioName=‘Fox’studioName=‘Fox’((MoviesMovies))))

There are always more than one solution for a There are always more than one solution for a problem. For instance, the following expression problem. For instance, the following expression does the same job but more efficiently. Can you does the same job but more efficiently. Can you say why?say why?

ππtitle,yeartitle,year(σ(σlength>=100 AND studioName=‘Fox’length>=100 AND studioName=‘Fox’((MoviesMovies))))

8484

Page 85: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.11 Naming and Renaming2.4.11 Naming and Renaming

Sometimes we need to change the relation's Sometimes we need to change the relation's name or change its attributes names.name or change its attributes names.

The following operator renames the relation R The following operator renames the relation R to S and renames the attributes as well:to S and renames the attributes as well:

ρ ρ S(A1, A2, …, An ) S(A1, A2, …, An ) (R)(R) Note that the resulting relation has the same Note that the resulting relation has the same

tuples. In other words, the renaming operator tuples. In other words, the renaming operator does not change the relation's contents.does not change the relation's contents.

If we just want to rename the relation's name, If we just want to rename the relation's name, then we can eliminate the attributes as:then we can eliminate the attributes as:

ρ ρ S S (R)(R)

8585

Page 86: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

Example 2.18Example 2.18

This is the same as This is the same as example 2.12 but it uses example 2.12 but it uses the renaming operator the renaming operator to avoid ambiguity to avoid ambiguity between the attributes.between the attributes.

Relation R Relation SRelation R Relation S

8686

A B

1 2

3 4

B C D

2 5 6

4 7 8

9 10 11

A B X C D

1 2 2 5 6

1 2 4 7 8

1 2 9 10 11

3 4 2 5 6

3 4 4 7 8

3 4 9 10 11

R X ρ R X ρ S(X, C, D) S(X, C, D) (S) (S)

2.4.11 Naming and Renaming 2.4.11 Naming and Renaming (cont'd)(cont'd)

Page 87: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

Example 2.18 (cont'd)Example 2.18 (cont'd)

Alternatively, we could make the product Alternatively, we could make the product first and then rename the attributes as first and then rename the attributes as follows:follows:

ρ ρ RS(A, B, X, C, D)RS(A, B, X, C, D) (R X S) (R X S)

8787

2.4.11 Naming and Renaming 2.4.11 Naming and Renaming (cont'd)(cont'd)

Page 88: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.12 Relationships Among 2.4.12 Relationships Among OperationsOperations

Some operations can be expressed in Some operations can be expressed in terms of other operations.terms of other operations.

For instance, the following identity is For instance, the following identity is valid:valid:

R R S = R – (R – S) S = R – (R – S) Also, theta-join can be expressed by cross Also, theta-join can be expressed by cross

product and selection as follows:product and selection as follows:

R ∞R ∞CC S = σ S = σCC (R X S) (R X S) The other equality is between natural-join The other equality is between natural-join

and cross product as follows:and cross product as follows:

R ∞ S = R ∞ S = ππLL (σ (σCC (R X S)) (R X S))8888

Page 89: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.13 A linear Notation for 2.4.13 A linear Notation for Algebraic ExpressionsAlgebraic Expressions

8989

Page 90: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.4.14 Exercises for Section 2.4.14 Exercises for Section 2.42.4

9090

Page 91: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

CONSTRAINTS ON RELATIONSCONSTRAINTS ON RELATIONSSection 2.5Section 2.5

9191

Page 92: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5 Constraints on Relations2.5 Constraints on Relations

2.5.1 Relational Algebra as a Constraint 2.5.1 Relational Algebra as a Constraint LanguageLanguage

2.5.2 Referential Integrity Constraints2.5.2 Referential Integrity Constraints

2.5.3 Key Constraints2.5.3 Key Constraints

2.5.4 Additional Constraint Examples2.5.4 Additional Constraint Examples

2.5.5 Exercises for Section 2.52.5.5 Exercises for Section 2.5

9292

Page 93: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5 Constraints on Relations2.5 Constraints on Relations

A constraint is the ability to restrict the A constraint is the ability to restrict the data that may be stored in a database.data that may be stored in a database.

So far we have seen one kind of So far we have seen one kind of constraints, the key.constraints, the key.

Constraints can be expressed in Constraints can be expressed in relational algebra.relational algebra.

9393

Page 94: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.1 Relational Algebra as a 2.5.1 Relational Algebra as a Constraint LanguageConstraint Language

There are two ways in which we can use There are two ways in which we can use expressions of relational algebra to expressions of relational algebra to express constraints:express constraints:

1.1. If R is an expression of a relational algebra, If R is an expression of a relational algebra, then R=then R= is a constraint that says “the value is a constraint that says “the value of R must be empty”, or equivalently, “There of R must be empty”, or equivalently, “There are no tuples in R”.are no tuples in R”.

2.2. If R and S are expressions of relational If R and S are expressions of relational algebra, then R algebra, then R S is a constraint that says S is a constraint that says “Every tuple in R must also be in S.”“Every tuple in R must also be in S.”

9494

Page 95: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.2 Referential Integrity 2.5.2 Referential Integrity ConstraintsConstraints

Referential Integrity Referential Integrity constraint asserts constraint asserts that a value appearing in one relation that a value appearing in one relation must also appear in another related must also appear in another related relation.relation.

For instance, in the Movies database, For instance, in the Movies database, should we see a StarsIn tuple that has a should we see a StarsIn tuple that has a person p in the starName attribute, we person p in the starName attribute, we would expect that p appears as the would expect that p appears as the name of some star in the MovieStar name of some star in the MovieStar relation.relation.

9595

Page 96: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.2 Referential Integrity 2.5.2 Referential Integrity Constraints (cont'd)Constraints (cont'd)

In general, if we have any value v as the In general, if we have any value v as the component in attribute A of some tuple component in attribute A of some tuple in one relation R, then v must appear in in one relation R, then v must appear in a particular component, say for attribute a particular component, say for attribute B, of some tuple of another relation S.B, of some tuple of another relation S.

We can express this integrity constraint We can express this integrity constraint in relational algebra as:in relational algebra as:

ππAA (R) (R) ππBB(S) (S)

or equivalently as:or equivalently as:

ππAA (R) - (R) - ππBB(S) = (S) =

9696

Page 97: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.2 Referential Integrity 2.5.2 Referential Integrity Constraints (cont'd)Constraints (cont'd)

Example 2.21Example 2.21

Consider the following schemas:Consider the following schemas:Movies(title, year, length, genre, studioName, Movies(title, year, length, genre, studioName,

producerC#)producerC#)

MovieExec(name, address, cert#, netWorth)MovieExec(name, address, cert#, netWorth)

The producer of a movie should be an The producer of a movie should be an executive and should have a record in executive and should have a record in MovieExec.MovieExec.

Therefore, we must expect that producerC# Therefore, we must expect that producerC# in Movies relation should appear as cert# in Movies relation should appear as cert# in one tuple of MovieExec relation.in one tuple of MovieExec relation.

ππproducerC#producerC# (Movies) (Movies) ππcert#cert#(MovieExec) (MovieExec) 9797

Page 98: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.2 Referential Integrity 2.5.2 Referential Integrity Constraints (cont'd)Constraints (cont'd)

Example 2.22 (multi-value referential Example 2.22 (multi-value referential integrity)integrity)

Consider the following schemas:Consider the following schemas:

StarsIn(movieTitle, movieYear, starName)StarsIn(movieTitle, movieYear, starName)Movies(title, year, length, genre, studioName, Movies(title, year, length, genre, studioName,

producerC#)producerC#)

The combined movieTitle and movieYear in The combined movieTitle and movieYear in StarsIn relation must appear in one StarsIn relation must appear in one tuple of Movies relation.tuple of Movies relation.

ππmovieTitle, movieYearmovieTitle, movieYear (StarsIn) (StarsIn) ππtitle, year title, year (Movies) (Movies)

9898

Page 99: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.3 Key Constraints2.5.3 Key Constraints

Example 2.23Example 2.23

Consider the following schema:Consider the following schema:MovieStar(name, address, gender, birthdate)MovieStar(name, address, gender, birthdate)

The attribute 'name' is the key of this The attribute 'name' is the key of this relation.relation.

That is, if two tuples have the same name, That is, if two tuples have the same name, then they must have the same address, then they must have the same address, gender, and birthdate. gender, and birthdate.

To express this constraint in relational To express this constraint in relational algebra, we make Cartesian product of algebra, we make Cartesian product of the relation with itself as follows:the relation with itself as follows:

9999

Page 100: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.3 Key Constraints (cont'd)2.5.3 Key Constraints (cont'd)

Example 2.23 (cont'd)Example 2.23 (cont'd)σσMS1.name = MS2.name AND MS1.address <> MS2.addressMS1.name = MS2.name AND MS1.address <> MS2.address (MS1 X MS2) = (MS1 X MS2) =

Note that we renamed the MovieStar Note that we renamed the MovieStar relation to MS1 and MS2 to disambiguate relation to MS1 and MS2 to disambiguate the references to them.the references to them.

Here are the renaming operators:Here are the renaming operators:

ρρMS1MS1 (MovieStar) (MovieStar)

ρρMS2MS2 (MovieStar) (MovieStar)

100100

Page 101: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.4 Additional Constraint 2.5.4 Additional Constraint ExamplesExamples

Example 2.24Example 2.24

Consider the following schema:Consider the following schema:MovieStar(name, address, gender, birthdate)MovieStar(name, address, gender, birthdate)

Suppose we wish to specify that the only Suppose we wish to specify that the only legal values for the gender attribute are legal values for the gender attribute are 'F' and 'M'.'F' and 'M'.

We can express this constraint in We can express this constraint in relational algebra as:relational algebra as:

σσgender <> 'F' AND gender <> 'M'gender <> 'F' AND gender <> 'M' (MovieStar) = (MovieStar) =

101101

Page 102: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.4 Additional Constraint 2.5.4 Additional Constraint Examples (cont'd)Examples (cont'd)

Example 2.25Example 2.25

Consider the following schema:Consider the following schema:MovieExec(name, address, cert#, netWorth)MovieExec(name, address, cert#, netWorth)

Studio(name, address, presC#)Studio(name, address, presC#)

Suppose we wish to require that one must Suppose we wish to require that one must have a net worth of at least $10,000,000 have a net worth of at least $10,000,000 to be the president of a movie studio.to be the president of a movie studio.

The constraint can be expressed as:The constraint can be expressed as:σσnetWorth < 10000000netWorth < 10000000 (Studio ∞(Studio ∞presC# = cert#presC# = cert# MovieExec) = MovieExec) =

or equivalently as:or equivalently as:ππprecC#precC# (Studio) (Studio) ππcert# cert# (σ(σnetWorth < 10000000netWorth < 10000000 (MovieExec))(MovieExec))

102102

Page 103: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.5.5 Exercises for Section 2.52.5.5 Exercises for Section 2.5

103103

Page 104: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.6 Summary of Chapter 22.6 Summary of Chapter 2

104104

Page 105: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

2.7 References for Chapter 22.7 References for Chapter 2

105105