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

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
1 Lecture 11: Basic SQL, Integrity constraints
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
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.
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
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.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
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.
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.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Relational Model 2015, Fall Pusan National University Ki-Joune Li.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Relational Algebra Spring 2012 Instructor: Hassan Khosravi.
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.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
1 Relational Algebra and Calculas Chapter 4, Part A.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
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.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
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.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
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.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
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.
CS 157B Database Systems Dr. T Y Lin. 1.2 Overview of a Database Management System Data-Definition Language Commands –Illustrated by three examples.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
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.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CPSC-310 Database Systems
Relational Data Model Lu Chaojun, SJTU.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
THE RELATIONAL MODEL OF DATA
SQL OVERVIEW DEFINING A SCHEMA
2018, Fall Pusan National University Ki-Joune Li
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CMSC-461 Database Management Systems
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

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 2011 Chapter 2 The Relational Model of Data 2.1 An Overview of Data Models 2.2 Basics of Relational Model 2.3 Defining a Relation Schema in SQL 2.4 An Algebraic Query Language 2.5 Constraints on Relations 2.6 Summary of Chapter References for Chapter 2 2

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 AN OVERVIEW OF DATA MODELS Section 2.1 3

Dr. T. Y. Lin | SJSU | CS 157A | Fall An Overview of Data Models What is Data Model? Important Data Models The Relational Data Model in Brief The Semi-structured Model in Brief Other Data Models Comparison of Modeling Approaches 4

Dr. T. Y. Lin | SJSU | CS 157A | Fall What is Data Model? A data model is a “small set of notations/mathematics” (mathematical model – see Definition in Discrete Mathematics) for describing data. A data model is a “small set of notations/mathematics” (mathematical model – see Definition in Discrete Mathematics) for describing data. The description generally consists of 3 parts: The description generally consists of 3 parts: 1. Structure: it can be imagined as 'object' in Java or 'struct' in C but in database world, the structure of data is higher level than physical data model. That's why we refer to it as conceptual model. 2. Operations: a limited set of queries (retrieving data) and modifications (changing data) 3. Constraints: applying some limitations on what the data can be. 5

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Structure External view External view E.g. CS E.g. CS conceptual view conceptual view E.g Knowledge E.g Knowledge Internal view Internal view Congress libaray Congress libaray 6

Dr. T. Y. Lin | SJSU | CS 157A | Fall Important Data Models Currently, there are two important data models as follows: Currently, there are two important data models as follows: 1. The Relational Data Model (including Object- Relational Extension) which is present in all commercial DBMS's 2. The Semi-Structured Data Model (including XML) which is an added feature of most DBMS's 7

Dr. T. Y. Lin | SJSU | CS 157A | Fall The Relational Data Model in Brief The relational model is based on tables. The relational model is based on tables. For instance, the following table shows 3 movies but you can imagine that there are many more rows. For instance, the following table shows 3 movies but you can imagine that there are many more rows. 8 TitleYearLengthGenre Gone with the wind Drama Star Wars Sci-fi Wayne World199295Comedy

Dr. T. Y. Lin | SJSU | CS 157A | Fall The Relational Data Model in Brief (cont'd) We are not going to talk about how to implement the structure of the tables here and it will be postponed to higher courses in database. We are not going to talk about how to implement the structure of the tables here and it will be postponed to higher courses in database. There are some operations that we can do on the tables. For example we can query the rows where the genre is 'comedy'. There are some operations that we can do on the tables. For example we can query the rows where the genre is 'comedy'. As an example for the constraints, we may decide there could never be two movies with the same title and year in this table. As an example for the constraints, we may decide there could never be two movies with the same title and year in this table. 9

Dr. T. Y. Lin | SJSU | CS 157A | Fall The Semi-structured Model in Brief Semi-structured data resembles trees or graphs, rather than tables or arrays. Semi-structured data resembles trees or graphs, rather than tables or arrays. The principal manifestation of this viewpoint today is XML, a way to represent data by hierarchically nested tagged elements. The principal manifestation of this viewpoint today is XML, a way to represent data by hierarchically nested tagged elements. 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 headers do in the relational model. You can imagine tags as the column headers do in the relational model. You can see an example of XML in the next slide which is the same as movies data. You can see an example of XML in the next slide which is the same as movies data. 10

Dr. T. Y. Lin | SJSU | CS 157A | Fall The Semi-structured Model in Brief (cont'd) <Movies> <Year>1939</Year><Length>281</Length><Genre>drama</Genre></Movie> <Year>1977</Year><Length>124</Length><Genre>Scifi</Genre></Movie> <Year>1992</Year><Length>95</Length><Genre>Comedy</Genre></Movie></Movies> 11

Dr. T. Y. Lin | SJSU | CS 157A | Fall Other Data Models A modern trend is to add object-oriented features to the relational model. A modern trend is to add object-oriented features to the relational model. There are two effects of object-orientation on relations: There are two effects of object-orientation on relations: 1. Values can have structure, rather than being elementary types such as integer or strings. 2. Relations can have associated methods. These extensions are called object-relational model. These extensions are called object-relational model. 12

Dr. T. Y. Lin | SJSU | CS 157A | Fall Other Data Models (cont'd) In earlier DBMS's, there were several other models like hierarchical model or network model. In earlier DBMS's, there were several other models like hierarchical model or network model. Hierarchical model was a tree-oriented model that unlike the modern DBMS's, it really operated at the physical level. Hierarchical model was a tree-oriented model that unlike the modern DBMS's, it really operated at the physical level. Network model was a graph-oriented and also physical level model as well. Network model was a graph-oriented and also physical level model as well. 13

Dr. T. Y. Lin | SJSU | CS 157A | Fall Comparison of Modeling Approaches (IBM ARC; no more Semi-structured models have more flexibility but relational model is still preferred. Semi-structured models have more flexibility but relational model is still preferred. In large databases, efficiency of access to data and modifying data are of great importance. In large databases, efficiency of access to data and modifying data are of great importance. Ease of use is another factor of using DBMS's. Ease of use is another factor of using DBMS's. Both of these features can found in relational DBMS's. Both of these features can found in relational DBMS's. Moreover, SQL, the structured query language, in spite of its simplicity, is a powerful language for database operations. Moreover, SQL, the structured query language, in spite of its simplicity, is a powerful language for database operations. 14

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 BASICS OF RELATIONAL MODEL Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall Basics of Relational Model Attributes Schemas Tuples Domains Equivalent Representations of a Relation Relation Instances Keys of Relations An Example Database Schema Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall Basics of Relational Model The relational model gives us a single way to represent data: as a two-dimensional table called a relation. The relational model gives us a single way to represent data: as a two-dimensional table called a relation.Moviesrelation Each row (tuple) represents a movie and each column (attribute) represents a property of the movie. Each row (tuple) represents a movie and each column (attribute) represents a property of the movie. 17 TitleYearLengthGenre Gone with the wind Drama Star Wars Sci-fi Wayne World199295Comedy

Dr. T. Y. Lin | SJSU | CS 157A | Fall Attributes The columns of a relation are called attributes. The columns of a relation are called attributes. In the Movies relation (in previous slide), title, year, length, and genre are attributes. In the Movies relation (in previous slide), title, year, length, and genre are attributes. Attributes appear at the top of the columns. Attributes appear at the top of the columns. Like choosing descriptive names for variables in regular programming languages, attributes names should be chosen in such a way that describe the contents. Like choosing descriptive names for variables in regular programming languages, attributes names should be chosen in such a way that describe the contents. 18

Dr. T. Y. Lin | SJSU | CS 157A | Fall Schemas The names of a relation and the set of attributes for the relation is called the schema of the relation. The names of a relation and the set of attributes for the relation is called the schema of the relation. We show the schema for the relation with the relation name followed by the parenthesized list of its attributes We show the schema for the relation with the relation name followed by the parenthesized list of its attributes For instance, the following is the schema of relation Movies: For instance, the following is the schema of relation Movies: Movies(title, year, length, genre) 19

Dr. T. Y. Lin | SJSU | CS 157A | Fall Schemas (cont'd) Note that the attributes are a set, not a list but when we talk about relations, we often specify an order for the attributes. Note that the attributes are a set, not a list but when we talk about relations, we often specify an order for the attributes. A database consists of one ore more relations. The set of schemas in the database is called a relational database schema, or just a database schema. A database consists of one ore more relations. The set of schemas in the database is called a relational database schema, or just a database schema. 20

Dr. T. Y. Lin | SJSU | CS 157A | Fall Tuples The rows of a relation, other than the header row containing the attributes names, are called tuples. The rows of a relation, other than the header row containing the attributes names, are called tuples. A tuple has one component for each attribute of the relation. A tuple has one component for each attribute of the relation. For example, in the Movies relation, the first tuple has four components: 'Gone with the wind', 1939, 231, and drama for attributes title, year, length, and genre respectively. For example, in the Movies relation, the first tuple has four components: 'Gone with the wind', 1939, 231, and drama for attributes title, year, length, and genre respectively. 21

Dr. T. Y. Lin | SJSU | CS 157A | Fall Tuples (cont'd) When we wish to write a tuple in isolation, not as a part of a relation, we normally use commas to separate components like this: ('Gone with the wind', 1939, 231, 'drama') When we wish to write a tuple in isolation, not as a part of a relation, we normally use commas to separate components like this: ('Gone with the wind', 1939, 231, 'drama') Note that we always use the same order of the attributes to show the tuple in isolation. Note that we always use the same order of the attributes to show the tuple in isolation. 22

Dr. T. Y. Lin | SJSU | CS 157A | Fall Domains Tylin 8/29: Past, present, future data The relational model requires that each component of each tuple be atomic. The relational model requires that each component of each tuple be atomic. That is, it must be of some elementary type such as integer or string. That is, it must be of some elementary type such as integer or string. It is not allowed for a value to be a set, list, array or any other type that reasonably can be broken into smaller components. It is not allowed for a value to be a set, list, array or any other type that reasonably can be broken into smaller components. It's further assumed that associated with each attribute of a relation is a domain. It's further assumed that associated with each attribute of a relation is a domain. 23

Dr. T. Y. Lin | SJSU | CS 157A | Fall Domains (cont'd) We can include the domain for each attributes in a schema as follows: Movies(title:string, year:integer, length:integer, genre:string) We can include the domain for each attributes in a schema as follows: Movies(title:string, year:integer, length:integer, genre:string) 24

Dr. T. Y. Lin | SJSU | CS 157A | Fall Equivalent Representations of a Relation Relations are sets of tuples, not lists of tuples. In other words, the order of tuples in a relation has no significance. Relations are sets of tuples, not lists of tuples. In other words, the order of tuples in a relation has no significance. Moreover, we can reorder the attributes of a relation as well. Moreover, we can reorder the attributes of a relation as well. Note that, when we change the order of the attributes, we should change the order of the contents as well. Note that, when we change the order of the attributes, we should change the order of the contents as well. 25

Dr. T. Y. Lin | SJSU | CS 157A | Fall Relation Instances Relations change over time. In other words, relations are not static. Relations change over time. In other words, relations are not static. For example, we insert tuples in the Movies relation over time, and also, we may delete or update some tuples as well. For example, we insert tuples in the Movies relation over time, and also, we may delete or update some tuples as well. Even the schema can be changed. In other words, we may add/delete an attribute to/from the schema. Even the schema can be changed. In other words, we may add/delete an attribute to/from the schema. We call a set of tuples for a given relation an instance of that relation. We call a set of tuples for a given relation an instance of that relation. Current instance, is the set of tuples that exists now. Current instance, is the set of tuples that exists now. 26

Dr. T. Y. Lin | SJSU | CS 157A | Fall Keys of Relations Relational model allows us to place some constraints on a schema. Relational model allows us to place some constraints on a schema. One important constraint is called key constraint or simply a key. One important constraint is called key constraint or simply a key. A set of attributes (one or more) forms a key if two tuples in the relation cannot have the same values in all the attributes of the key. A set of attributes (one or more) forms a key if two tuples in the relation cannot have the same values in all the attributes of the key. 27

Dr. T. Y. Lin | SJSU | CS 157A | Fall Keys of Relations (cont'd) Example 2.1 For the Movies relation, we can assign the attributes title and year be the key of the relation. In this way, the relation cannot have two tuples with the same title and year. Note that the title by itself does not form a key because there are many movies over the years that have the same name. In other words, the title by itself is not unique and cannot identify a movie uniquely. 28

Dr. T. Y. Lin | SJSU | CS 157A | Fall Keys of Relations (cont'd) We indicate the attribute(s) contributing in the key by underlying them as follows: Movies(title, year, length, genre) We indicate the attribute(s) contributing in the key by underlying them as follows: Movies(title, year, length, genre) Note that the key is a constraint for all possible instances of the relation, and not for a specific instance. Note that the key is a constraint for all possible instances of the relation, and not for a specific instance. Most of the time, we use an artificial keys for a relation. For example, for the Movies relation, we could add a new attributes movie_id and assign it as the key. In this way, we could make sure it was unique for all possible tuples. Most of the time, we use an artificial keys for a relation. For example, for the Movies relation, we could add a new attributes movie_id and assign it as the key. In this way, we could make sure it was unique for all possible tuples. 29

Dr. T. Y. Lin | SJSU | CS 157A | Fall An Example Database Schema The database schema that are used during this book is as follows: Movies(title:string, year:integer, length:integer, genre:string, studioName:string, producerC#:integer) Moviestar(name:string, address:string, gender:char, birthdate:date) 30

Dr. T. Y. Lin | SJSU | CS 157A | Fall An Example Database Schema (cont'd) StarsIn(movieTitle:string, movieYear:integer, starName:string) MovieExec (name:string, addres:string, cert#:integer, netWorth:integer) Studio (name:string, address:string, presC#:integer) 31

Dr. T. Y. Lin | SJSU | CS 157A | Fall Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 DEFINING A RELATION SCHEMA IN SQL Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall Defining a Relation Schema in SQL Relations in SQL Data Types Simple Table Declaration Modifying Relations Schemas Default Values Declaring Keys Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall Defining a Relation Schema in SQL SQL, Structured Query Language, pronounced "sequel", is the principal language to describe, and manipulate relational database. SQL, Structured Query Language, pronounced "sequel", is the principal language to describe, and manipulate relational database. There is a standard called SQL-99 that most commercial databases implemented something similar, but not identical to, the standard. There is a standard called SQL-99 that most commercial databases implemented something similar, but not identical to, the standard. There are two sub-languages for SQL: There are two sub-languages for SQL: DDL: Data Definition Language DDL: Data Definition Language DML: Data Manipulation Language DML: Data Manipulation Language 35

Dr. T. Y. Lin | SJSU | CS 157A | Fall Relations in SQL SQL makes a distinction between three kinds of relations: SQL makes a distinction between three kinds of relations: Stored relations: are called tables. These relations exists in database and usually we deal with them. Stored relations: are called tables. These relations exists in database and usually we deal with them. Views: are relations that do not exist but are constructed when needed. Views: are relations that do not exist but are constructed when needed. Temporary tables: are constructed temporarily by SQL processor when it executes queries or other tasks. Temporary tables: are constructed temporarily by SQL processor when it executes queries or other tasks. We are going to discuss about tables in this chapter. Views will be covered in chapter 8 and temporary tables are never declared. We are going to discuss about tables in this chapter. Views will be covered in chapter 8 and temporary tables are never declared. 36

Dr. T. Y. Lin | SJSU | CS 157A | Fall Data Types All attributes must have a data type. All attributes must have a data type. The primitive data types supported by SQL are: The primitive data types supported by SQL are: Character string Character string CHAR(n): fixed length string of length n; short strings will be padded with trailing blank to make n characters. CHAR(n): fixed length string of length n; short strings will be padded with trailing blank to make n characters. VARCHAR(n): variable length string up to n character; an end-marker or string-length is used to show the end of the string; the purpose is to save space. Note that longer values will be truncated to fit. VARCHAR(n): variable length string up to n character; an end-marker or string-length is used to show the end of the string; the purpose is to save space. Note that longer values will be truncated to fit. Bit string Bit string BIT(n): fixed bit string of length n; BIT(n): fixed bit string of length n; BIT VARYING(n): bit string of length up to n; BIT VARYING(n): bit string of length up to n; 37

Dr. T. Y. Lin | SJSU | CS 157A | Fall Data Types (cont'd) The primitive data types (cont'd): The primitive data types (cont'd): BOOLEAN: a logical value of TRUE, FALSE, or UNKNOWN (NULL) BOOLEAN: a logical value of TRUE, FALSE, or UNKNOWN (NULL) INT or INTEGER: integer value INT or INTEGER: integer value SHORTINT: short integer; usually the lower bound and the upper bound of SHORTINT is half of INTEGER's. SHORTINT: short integer; usually the lower bound and the upper bound of SHORTINT is half of INTEGER's. FLOAT or REAL: floating point number FLOAT or REAL: floating point number DOUBLE: double precision real number DOUBLE: double precision real number DECIMAL(n, d): customized real number; DECIMAL(n, d): customized real number; NUMERIC(n, d): a synonym for DECIMAL NUMERIC(n, d): a synonym for DECIMAL 38

Dr. T. Y. Lin | SJSU | CS 157A | Fall Data Types (cont'd) The primitive data types (cont'd): The primitive data types (cont'd): DATE : represents a date value of the form 'yyyy-mm-dd' DATE : represents a date value of the form 'yyyy-mm-dd' TIME: represents a time value of the form 'HH:mm:ss' or 'HH:mm:ss.d' (d is a fraction of seconds) TIME: represents a time value of the form 'HH:mm:ss' or 'HH:mm:ss.d' (d is a fraction of seconds) You can create a date constant like this: DATE ' ' You can create a date constant like this: DATE ' ' You can create a time constant like this: TIME '16:09:25' or TIME '16:09:25.378' You can create a time constant like this: TIME '16:09:25' or TIME '16:09:25.378' Most databases have TIMESTAMP data type of the form 'yyyy-mm-dd HH:mm:ss' Most databases have TIMESTAMP data type of the form 'yyyy-mm-dd HH:mm:ss' 39

Dr. T. Y. Lin | SJSU | CS 157A | Fall Simple Table Declaration The simplest form of relation declaration: CREATE TABLE tabName( attrib 1 type, attrib 2 type,... attrib n type); The simplest form of relation declaration: CREATE TABLE tabName( attrib 1 type, attrib 2 type,... attrib n type); 40

Dr. T. Y. Lin | SJSU | CS 157A | Fall Simple Table Declaration (cont'd) Example 2.2 The relation Movies can be declared as follows: CREATE TABLE Movies( title CHAR(100), year INT, length INT, genre CHAR(10), studioName CHAR(30), producerC# INT); 41

Dr. T. Y. Lin | SJSU | CS 157A | Fall Simple Table Declaration (cont'd) Example 2.3 The relation MovieStar can be declared as follows: CREATE TABLE MovieStar( name CHAR(30), address VARCHAR(255), gender CHAR(1), birthdate DATE); The gender attribute can be 'M' or 'F'. 42

Dr. T. Y. Lin | SJSU | CS 157A | Fall Modifying Relations Schemas To drop a relation R, execute the following SQL statement: DROP TABLE R; To drop a relation R, execute the following SQL statement: DROP TABLE R; To alter the schema, we have several options. To alter the schema, we have several options. To add attributes: ALTER TABLE R ADD attrib 1 type,..., attrib n type; To add attributes: ALTER TABLE R ADD attrib 1 type,..., attrib n type; To drop attributes: ALTER TABLE R DROP attrib 1,..., attrib n ; To drop attributes: ALTER TABLE R DROP attrib 1,..., attrib n ; 43

Dr. T. Y. Lin | SJSU | CS 157A | Fall Modifying Relations Schemas (cont'd) Example 2.4 Add an attribute to MoviesStar for phone data. ALTER TABLE MovieStar ADD phone CHAR(16); Note that phone attribute will be NULL for all existing tuples. Drop birthdate attribute ALTER TABLE MovieStar DROP bithdate; 44

Dr. T. Y. Lin | SJSU | CS 157A | Fall Default Values When we insert or modify a tuple, we sometimes do not have values for some attributes and we wish to assign a default values for them. When we insert or modify a tuple, we sometimes do not have values for some attributes and we wish to assign a default values for them. To assign a default value for attribute1, we use the following syntax: To assign a default value for attribute1, we use the following syntax: CREATE TABLE tabName( attrib 1 type DEFAULT defaultValue,... attrib n type); 45

Dr. T. Y. Lin | SJSU | CS 157A | Fall Modifying Relations Schemas (cont'd) Example 2.5 Assign default value '?' for gender and default value ' ' for birthdate. CREATE TABLE MovieStar( name CHAR(30), address VARCHAR(255), gender CHAR(1) DEFAULT '?', birthdate DATE DEFAULT DATE ' '); Note that we can assign a default value when altering a schema as well: ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT 'unlisted'; 46

Dr. T. Y. Lin | SJSU | CS 157A | Fall Declaring Keys There are two ways to declare an attribute or a set of attributes to be a key: There are two ways to declare an attribute or a set of attributes to be a key: Method 1: CREATE TABLE tabName( attrib 1 type PRIMARY KEY,... attrib n type); Method 1: CREATE TABLE tabName( attrib 1 type PRIMARY KEY,... attrib n type); Method 2: CREATE TABLE tabName( attrib 1 type,... attrib n type, PRIMARY KEY(attrib 1,...,attrib k )); Method 2: CREATE TABLE tabName( attrib 1 type,... attrib n type, PRIMARY KEY(attrib 1,...,attrib k )); 47

Dr. T. Y. Lin | SJSU | CS 157A | Fall Declaring Keys (cont'd) Note that if the key is a set of attributes, then we have to use method 2 but if the key is just one attribute, then either methods can be used. Note that if the key is a set of attributes, then we have to use method 2 but if the key is just one attribute, then either methods can be used. There are two declarations that may be used to indicate key: There are two declarations that may be used to indicate key: PRIMARY KEY PRIMARY KEY UNIQUE UNIQUE Both have the same effect but in PRIMARY KEY case, none of the attributes of the key can be NULL but in UNIQUE case, it's possible. Both have the same effect but in PRIMARY KEY case, none of the attributes of the key can be NULL but in UNIQUE case, it's possible. 48

Dr. T. Y. Lin | SJSU | CS 157A | Fall Declaring Keys (cont'd) Example 2.6 Declare name attribute as primary key in MovieStar relation. CREATE TABLE MovieStar( name CHAR(30) PRIMARY KEY, address VARCHAR(255), gender CHAR(1) DEFAULT '?', birthdate DATE DEFAULT DATE ' '); 49

Dr. T. Y. Lin | SJSU | CS 157A | Fall Declaring Keys (cont'd) Example 2.6 (cont'd) Alternatively, we can use the following syntax: CREATE TABLE MovieStar( name CHAR(30), address VARCHAR(255), gender CHAR(1) DEFAULT '?', birthdate DATE DEFAULT DATE ' '), PRIMARY KEY (name); PRIMARY KEY (name); Note that UNIQUE can replace PRIMARY KEY. 50

Dr. T. Y. Lin | SJSU | CS 157A | Fall Declaring Keys (cont'd) Example 2.7 Declare title and year attributes as primary key in Movies relation. CREATE TABLE Movies( title CHAR(100), year INT, length INT, genre CHAR(10), studioName CHAR(30), producerC# INT, PRIMARY KEY (title, year); PRIMARY KEY (title, year); 51

Dr. T. Y. Lin | SJSU | CS 157A | Fall Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 AN ALGEBRAIC QUERY LANGUAGE Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall An Algebraic Query Language Why Do We Need a Special Query Language? What is an Algebra? Overview of an Relational Algebra Set Operations on Relations Projection Selection Cartesian Product Natural Joins Theta-Joins 54

Dr. T. Y. Lin | SJSU | CS 157A | Fall An Algebraic Query Language (cont'd) Combining Operations to Form Queries Naming and Renaming Relationships Among Operations A linear Notation for Algebraic Expressions Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall An Algebraic Query Language A DBMS needs a way to query the data and to modify the data. A DBMS needs a way to query the data and to modify the data. We begin our study of operations on relations with a special algebra called relational algebra. We begin our study of operations on relations with a special algebra called relational algebra. Relational algebra was used by some early DBMS's prototypes but is not used in current commercial DBMS's. Relational algebra was used by some early DBMS's prototypes but is not used in current commercial DBMS's. The real query language, SQL, uses relational algebra internally to optimize the process of retrieving the data. The real query language, SQL, uses relational algebra internally to optimize the process of retrieving the data. 56

Dr. T. Y. Lin | SJSU | CS 157A | Fall Why Do We Need a Special Query Language? Why we don't use Java or C to retrieve the needed data? Why we don't use Java or C to retrieve the needed data? For example, we could represent a tuple with an object in Java and we could represent a relation with an array of the objects! What would be the problem? For example, we could represent a tuple with an object in Java and we could represent a relation with an array of the objects! What would be the problem? Surprisingly, relational algebra is useful because it is less powerful than Java or C! Surprisingly, relational algebra is useful because it is less powerful than Java or C! Ease of programming and producing highly optimized code by compiler are two important advantages of being less powerful. Ease of programming and producing highly optimized code by compiler are two important advantages of being less powerful. 57

Dr. T. Y. Lin | SJSU | CS 157A | Fall What is an Algebra? In general, algebra consists of operators and atomic operands. In general, algebra consists of operators and atomic operands. In arithmetic, the atomic operand are variables like x and y and constants like 10 and the operators are the simple arithmetic operators like: +, -, /, *. In arithmetic, the atomic operand are variables like x and y and constants like 10 and the operators are the simple arithmetic operators like: +, -, /, *. Any algebra allows us to build expressions by combining operators and atomic operands. Any algebra allows us to build expressions by combining operators and atomic operands. Relational algebra is another example of algebra. Variables are relations and constants are finite relations. Operators will be covered in next sub-sections. Relational algebra is another example of algebra. Variables are relations and constants are finite relations. Operators will be covered in next sub-sections. 58

Dr. T. Y. Lin | SJSU | CS 157A | Fall Overview of an Relational Algebra The operations fall into four classes: The operations fall into four classes: Set operations: union, intersection, difference Set operations: union, intersection, difference Operations that remove some parts of a relation: Operations that remove some parts of a relation: Selection – eliminates some tuples Selection – eliminates some tuples Projection – eliminates some attributes Projection – eliminates some attributes Operations that combine the tuples of two relations: Operations that combine the tuples of two relations: Cartesian product: pairs the tuples of two relations in all possible ways Cartesian product: pairs the tuples of two relations in all possible ways Various kinds of joins: will be covered later Various kinds of joins: will be covered later Renaming: changes the schema without changing the tuples. Renaming: changes the schema without changing the tuples. We refer to expressions of relational algebra as queries. We refer to expressions of relational algebra as queries. 59

Dr. T. Y. Lin | SJSU | CS 157A | Fall Set Operations on Relations The three most common operations on sets are: The three most common operations on sets are: Union: R  S, is the set of elements that are in R or S or both. Union: R  S, is the set of elements that are in R or S or both. Intersection: R  S, is the set of elements that are in both R and S. Intersection: R  S, is the set of elements that are in both R and S. Difference: R – S, is the set of elements that are in R but not in S. Difference: R – S, is the set of elements that are in R but not in S. Note that an element appears in a set once and duplicated values are not allowed. Note that an element appears in a set once and duplicated values are not allowed. When we apply these operations to relations, we need to put some conditions on R and S. When we apply these operations to relations, we need to put some conditions on R and S. 60

Dr. T. Y. Lin | SJSU | CS 157A | Fall Set Operations on Relations (cont'd) The conditions of R and S The 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 must be the same. The order of attributes is important here and must be the same. If the name of the attributes are different but the types are the same, we can rename the attributes temporarily by renaming operator. If the name of the attributes are different but the types are the same, we can rename the attributes temporarily by renaming operator. 61

Dr. T. Y. Lin | SJSU | CS 157A | Fall Set Operations on Relations (cont'd) Example 2.8 Given relations R and S as follows, compute: R  S, R  S, and R – S Relation R Relation S 62 NameGenderBirthdate Carrie FisherF9/9/99 Mark HamillM8/8/88 NameGenderBirthdate Carrie FisherF9/9/99 Harrison FordM7/7/77

Dr. T. Y. Lin | SJSU | CS 157A | Fall Set Operations on Relations (cont'd) Example 2.8 (cont'd) R  S R  S R  S R  S R - S R - S 63 NameGenderBirthdate Carrie FisherF9/9/99 Mark HamillM8/8/88 Harrison FordM7/7/77 NameGenderBirthdate Carrie FisherF9/9/99 NameGenderBirthdate Mark HamillM8/8/88

Dr. T. Y. Lin | SJSU | CS 157A | Fall Projection The projection operator produces a new relation that has only some of the attributes. The projection operator produces a new relation that has only some of the attributes. Projection operator in relational algebra is: Projection operator in relational algebra is: π A1, A2,..., An (R) π A1, A2,..., An (R) This operator applies on the relation R and produces a new relation with only attributes A 1, A 2, …, A n from relation R. In other words, the schema of the new relation would have the following set of attributes: This operator applies on the relation R and produces a new relation with only attributes A 1, A 2, …, A n from relation R. In other words, the schema of the new relation would have the following set of attributes: {A 1, A 2, …, A n } 64

Dr. T. Y. Lin | SJSU | CS 157A | Fall Projection (cont'd) Example 2.9 Given the relation Movies. Project the first three attributes. The result relation 65 TitleYearLengthGenrestudioNameProducerC# Star Wars sciFiFox12345 Galaxy Quest ComedyDreamworks67890 Wayne’s World199295ComedyParamount99999 TitleYearLength Star Wars Galaxy Quest Wayne’s World199295

Dr. T. Y. Lin | SJSU | CS 157A | Fall Projection (cont'd) Example 2.9 (cont'd) Project the Genre attribute. The result relation Note that in the relational algebra of sets, duplicate tuples are always eliminated. That's why 'Comedy' tuple is one instead of two. 66 Genre sciFi Comedy

Dr. T. Y. Lin | SJSU | CS 157A | Fall Selection The selection operator, applies to a relation R, and produces a new relation with a subset of R's tuples. The selection operator, applies to a relation R, and produces a new relation with a subset of R's tuples. The tuples in the resulting relation are those that satisfy some condition C that involves the attributes of R. The tuples in the resulting relation are those that satisfy some condition C that involves the attributes of R. Selection operator is denoted by: Selection operator is denoted by: σ C (R) σ C (R) The schema for the resulting relation is the same as R's schema. The schema for the resulting relation is the same as R's schema. 67

Dr. T. Y. Lin | SJSU | CS 157A | Fall Selection (cont'd) The operands in condition C are either constants or attributes of R. The operands in condition C are either constants or attributes of R. We apply C to each tuple t of R by substituting. We apply C to each tuple t of R by substituting. 68

Dr. T. Y. Lin | SJSU | CS 157A | Fall Selection (cont'd) Example 2.10 Given the Movies relation as follows: Find σ length >= 100 (Movies). 69 TitleYearLengthGenrestudioNameProducerC# Star Wars sciFiFox12345 Galaxy Quest ComedyDreamWorks67890 Wayne's World199295ComedyParamount99999

Dr. T. Y. Lin | SJSU | CS 157A | Fall Selection (cont'd) Example 2.10 (cont'd) The first two tuples satisfy the condition. So, the result relation would be: 70 TitleYearLengthGenrestudioNameProducerC# Star Wars sciFiFox12345 Galaxy Quest ComedyDreamWorks67890

Dr. T. Y. Lin | SJSU | CS 157A | Fall Selection (cont'd) Example 2.11 The Movies relation is given. Find the set of tuples that represent Fox movies at least 100 minutes long. So, we are looking for: σ length >= 100 AND studioName = 'FOX' (Movies). σ length >= 100 AND studioName = 'FOX' (Movies). 71 TitleYearLengthGenrestudioNameProducerC# Star Wars sciFiFox12345 Galaxy Quest ComedyDreamWorks67890 Wayne's World199295ComedyParamount99999

Dr. T. Y. Lin | SJSU | CS 157A | Fall Selection (cont'd) Example 2.11 (cont'd) The result would be: 72 TitleYearLengthGenrestudioNameProducerC# Star Wars sciFiFox12345

Dr. T. Y. Lin | SJSU | CS 157A | Fall Cartesian Product The Cartesian Product (or cross product or just product for simplicity) of two sets R and S is the set of pairs that can be formed by choosing the first element of the pair to be any element of R and the second any element of S. The Cartesian Product (or cross product or just product for simplicity) of two sets R and S is the set of pairs that can be formed by choosing the first element of the pair to be any element of R and the 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 relations and the members are the tuples. In relational algebra, the sets are the relations and the members are the tuples. 73

Dr. T. Y. Lin | SJSU | CS 157A | Fall Cartesian Product (cont'd) Example 2.12 Relation R Relation S Result R X S Note that attribute B is in both schemas, it has been R.B and S.B in the result to disambiguate them. 74 AB BCD AR.BS.BCD

Dr. T. Y. Lin | SJSU | CS 157A | Fall Natural Joins More often the cross product is not what we want. Usually we want to pair only those tuples that match in some certain conditions. More often the cross product is not what we want. Usually we want to pair only those tuples that match in some certain conditions. The simplest way is the natural join of two relation R and S. The simplest way is the natural join of two relation R and S. In this join we pair those tuples that agree with the common attributes in R and S. In this join we pair those tuples that agree with the common attributes in R and S. Natural join is denoted by: Natural join is denoted by: R ∞ S A tuple that fails to pair with any tuple of the other relation is said to be a dangling tuple. A tuple that fails to pair with any tuple of the other relation is said to be a dangling tuple. 75

Dr. T. Y. Lin | SJSU | CS 157A | Fall Natural Joins (cont'd) 76 Example 2.13 Relation R Relation S Result R ∞ S Relation R Relation S Result R ∞ S Note that attribute B is in both schemas, and since they should be equal in the result, therefore, one copy of it is enough in the result. AB BCD ABCD

Dr. T. Y. Lin | SJSU | CS 157A | Fall Natural Joins (cont'd) 77 Example 2.14 Relation U Relation V Result U ∞ V Relation U Relation V Result U ∞ V Note that attribute B and C are in both schemas, and since they should be equal in the result, therefore, one copy of them is enough in the result. ABC BCD ABCD

Dr. T. Y. Lin | SJSU | CS 157A | Fall Theta-Joins Equating the shared attributes is just one way that is used in natural join. Equating the shared attributes is just one way that is used in natural join. It is sometimes desirable to pair tuples from two relations on some other basis. It is sometimes desirable to pair tuples from two relations on some other basis. Historically, the theta refers to an arbitrary condition. We use C as the condition rather than θ. Historically, the theta refers to an arbitrary condition. We use C as the condition rather than θ. Theta-join is denoted by: Theta-join is denoted by: R ∞ C S 78

Dr. T. Y. Lin | SJSU | CS 157A | Fall Theta-Joins (cont'd) 79 Example 2.15 Relation U Relation U Result: U ∞ A<D V Result: U ∞ A<D V Relation V Relation V ABC BCD AU.BU.CV.BV.CD

Dr. T. Y. Lin | SJSU | CS 157A | Fall Theta-Joins (cont'd) 80 Example 2.16 Relation U Relation U Result: U ∞ A V.B V Result: U ∞ A V.B V Relation V Relation V ABC BCD AU.BU.CV.BV.CD

Dr. T. Y. Lin | SJSU | CS 157A | Fall Combining Operations to Form Queries Relational algebra like all other algebras, allows us to form complex expressions by applying operations to the result of other operations. Relational algebra like all other algebras, allows us to form complex expressions by applying operations to the result of other operations. One can construct expressions of relational algebra by applying operators to sub expressions, using parenthesis when necessary to indicate grouping of operands. One can construct expressions of relational algebra by applying operators to sub expressions, using parenthesis when necessary to indicate grouping of operands. It is also possible to represent expressions as expression trees. It is also possible to represent expressions as expression trees. 81

Dr. T. Y. Lin | SJSU | CS 157A | Fall Combining Operations to Form Queries (cont'd) Example 2.17 What are the titles and years of Movies made by fox that are at least 100 minutes long? One solution would be: 1. Select those tuples that have length >= Select those tuples that have studioName = 'Fox'. 3. Compute the intersection of (1) and (2). 4. Project the relation from (3) onto attributes title and year. 82

Dr. T. Y. Lin | SJSU | CS 157A | Fall Combining Operations to Form Queries (cont'd) Example 2.17 (cont'd) Here is the suggested expression tree! 83  σ length >= 100 π title, year σ studioName = 'Fox' Movies Movies

Dr. T. Y. Lin | SJSU | CS 157A | Fall Combining Operations to Form Queries (cont'd) Example 2.17 (cont'd) Alternatively, we could represent the same expression in a linear notation as follows: π title,year (σ length>=100 ( Movies )  σ studioName=‘Fox’ ( Movies )) There are always more than one solution for a problem. For instance, the following expression does the same job but more efficiently. Can you say why? π title,year (σ length>=100 AND studioName=‘Fox’ ( Movies )) 84

Dr. T. Y. Lin | SJSU | CS 157A | Fall Naming and Renaming Sometimes we need to change the relation's name or change its attributes names. Sometimes we need to change the relation's name or change its attributes names. The following operator renames the relation R to S and renames the attributes as well: The following operator renames the relation R to S and renames the attributes as well: ρ S(A1, A2, …, An ) (R) Note that the resulting relation has the same tuples. In other words, the renaming operator does not change the relation's contents. Note that the resulting relation has the same tuples. In other words, the renaming operator does not change the relation's contents. If we just want to rename the relation's name, then we can eliminate the attributes as: If we just want to rename the relation's name, then we can eliminate the attributes as: ρ S (R) 85

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Example 2.18 This is the same as example 2.12 but it uses the renaming operator to avoid ambiguity between the attributes. Relation R Relation S Relation R Relation S 86 AB BCD ABXCD R X ρ S(X, C, D) (S) Naming and Renaming (cont'd)

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Example 2.18 (cont'd) Alternatively, we could make the product first and then rename the attributes as follows: ρ RS(A, B, X, C, D) (R X S) Naming and Renaming (cont'd)

Dr. T. Y. Lin | SJSU | CS 157A | Fall Relationships Among Operations Some operations can be expressed in terms of other operations. Some operations can be expressed in terms of other operations. For instance, the following identity is valid: For instance, the following identity is valid: R  S = R – (R – S)R  S = R – (R – S) Also, theta-join can be expressed by cross product and selection as follows: Also, theta-join can be expressed by cross product and selection as follows: R ∞ C S = σ C (R X S)R ∞ C S = σ C (R X S) The other equality is between natural-join and cross product as follows: The other equality is between natural-join and cross product as follows: R ∞ S = π L (σ C (R X S))R ∞ S = π L (σ C (R X S)) 88

Dr. T. Y. Lin | SJSU | CS 157A | Fall A linear Notation for Algebraic Expressions 89

Dr. T. Y. Lin | SJSU | CS 157A | Fall Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 CONSTRAINTS ON RELATIONS Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall Constraints on Relations Relational Algebra as a Constraint Language Referential Integrity Constraints Key Constraints Additional Constraint Examples Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall Constraints on Relations A constraint is the ability to restrict the data that may be stored in a database. A constraint is the ability to restrict the data that may be stored in a database. So far we have seen one kind of constraints, the key. So far we have seen one kind of constraints, the key. Constraints can be expressed in relational algebra. Constraints can be expressed in relational algebra. 93

Dr. T. Y. Lin | SJSU | CS 157A | Fall Relational Algebra as a Constraint Language There are two ways in which we can use expressions of relational algebra to express constraints: There are two ways in which we can use expressions of relational algebra to express constraints: 1. If R is an expression of a relational algebra, then R=  is a constraint that says “the value of R must be empty”, or equivalently, “There are no tuples in R”. 2. If R and S are expressions of relational algebra, then R  S is a constraint that says “Every tuple in R must also be in S.” 94

Dr. T. Y. Lin | SJSU | CS 157A | Fall Referential Integrity Constraints Referential Integrity constraint asserts that a value appearing in one relation must also appear in another related relation. Referential Integrity constraint asserts that a value appearing in one relation must also appear in another related relation. For instance, in the Movies database, should we see a StarsIn tuple that has a person p in the starName attribute, we would expect that p appears as the name of some star in the MovieStar relation. For instance, in the Movies database, should we see a StarsIn tuple that has a person p in the starName attribute, we would expect that p appears as the name of some star in the MovieStar relation. 95

Dr. T. Y. Lin | SJSU | CS 157A | Fall Referential Integrity Constraints (cont'd) In general, if we have any value v as the component in attribute A of some tuple in one relation R, then v must appear in a particular component, say for attribute B, of some tuple of another relation S. In general, if we have any value v as the component in attribute A of some tuple in one relation R, then v must appear in a particular component, say for attribute B, of some tuple of another relation S. We can express this integrity constraint in relational algebra as: We can express this integrity constraint in relational algebra as: π A (R)  π B (S) or equivalently as:or equivalently as: π A (R) - π B (S) =  96

Dr. T. Y. Lin | SJSU | CS 157A | Fall Referential Integrity Constraints (cont'd) Example 2.21 Consider the following schemas: Movies(title, year, length, genre, studioName, producerC#) MovieExec(name, address, cert#, netWorth) The producer of a movie should be an executive and should have a record in MovieExec. Therefore, we must expect that producerC# in Movies relation should appear as cert# in one tuple of MovieExec relation. π producerC# (Movies)  π cert# (MovieExec) 97

Dr. T. Y. Lin | SJSU | CS 157A | Fall Referential Integrity Constraints (cont'd) Example 2.22 (multi-value referential integrity) Consider the following schemas: StarsIn(movieTitle, movieYear, starName) Movies(title, year, length, genre, studioName, producerC#) The combined movieTitle and movieYear in StarsIn relation must appear in one tuple of Movies relation. π movieTitle, movieYear ( StarsIn )  π title, year (Movies) π movieTitle, movieYear (StarsIn)  π title, year (Movies) 98

Dr. T. Y. Lin | SJSU | CS 157A | Fall Key Constraints Example 2.23 Consider the following schema: MovieStar(name, address, gender, birthdate) The attribute 'name' is the key of this relation. That is, if two tuples have the same name, then they must have the same address, gender, and birthdate. To express this constraint in relational algebra, we make Cartesian product of the relation with itself as follows: 99

Dr. T. Y. Lin | SJSU | CS 157A | Fall Key Constraints (cont'd) Example 2.23 (cont'd) σ MS1.name = MS2.name AND MS1.address <> MS2.address (MS1 X MS2) =  Note that we renamed the MovieStar relation to MS1 and MS2 to disambiguate the references to them. Here are the renaming operators:Here are the renaming operators: ρ MS1 (MovieStar)ρ MS1 (MovieStar) ρ MS2 (MovieStar)ρ MS2 (MovieStar) 100

Dr. T. Y. Lin | SJSU | CS 157A | Fall Additional Constraint Examples Example 2.24 Consider the following schema: MovieStar(name, address, gender, birthdate) Suppose we wish to specify that the only legal values for the gender attribute are 'F' and 'M'. We can express this constraint in relational algebra as: σ gender <> 'F' AND gender <> 'M' (MovieStar) =  101

Dr. T. Y. Lin | SJSU | CS 157A | Fall Additional Constraint Examples (cont'd) Example 2.25 Consider the following schema: MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#) Suppose we wish to require that one must have a net worth of at least $10,000,000 to be the president of a movie studio. The constraint can be expressed as: σ netWorth < (Studio ∞ presC# = cert# MovieExec) =  or equivalently as:or equivalently as: π precC# ( Studio )  π cert# (σ netWorth < (MovieExec)) π precC# (Studio)  π cert# (σ netWorth < (MovieExec)) 102

Dr. T. Y. Lin | SJSU | CS 157A | Fall Exercises for Section

Dr. T. Y. Lin | SJSU | CS 157A | Fall Summary of Chapter 2 104

Dr. T. Y. Lin | SJSU | CS 157A | Fall References for Chapter 2 105