Date post: | 22-Dec-2015 |
Category: |
Documents |
View: | 214 times |
Download: | 0 times |
1
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Week 4Week 4September 19September 19
• Relational Data ModelRelational Data Model• ViewsViews
2
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
CriteriaCriteria
Relational ModelRelational Model
• ObjectivesObjectives
– A degree of data independenceA degree of data independence
– Address data semantic, consistency and redundancy Address data semantic, consistency and redundancy problemsproblems
– Set-oriented data manipulation languageSet-oriented data manipulation language
• Structured Query Language (SQL)Structured Query Language (SQL)
Data SetData Set
Presen-Presen-tation tation methodmethod
DatabaseDatabase
InformationInformation
3
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Data SetData Set
Presen-Presen-tation tation methodmethod
InformationInformation
Data SetData Set
InformationInformation
Data SetData Set
InformationInformation
DatabaseDatabase
CriteriaCriteria
Crit
eria
Crit
eria
Criteria
Criteria
Presen-Presen-tation tation methodmethod
Presen-Presen-tation tation methodmethod
4
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Tuples Tuples (rows)(rows)• CardinalitiyCardinalitiy = =
number of number of tuplestuples
RelationRelation
AttributesAttributes (columns) (columns)• Degree of a relation = number of attributesDegree of a relation = number of attributes
Attrib
ute-
1
Attrib
ute-
1Attr
ibut
e-2
Attrib
ute-
2
Attrib
ute-
n
Attrib
ute-
n
EntityEntity
Domain = all values an attribute can assumeDomain = all values an attribute can assume
5
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Domain of an AttributeDomain of an Attribute
• Set of allowable values for one or more attributesSet of allowable values for one or more attributes
DomainDomain DomainDomainUnionUnion
ororIntersectionIntersection
InformationInformation
Attribute 1Attribute 1 Attribute 2Attribute 2
6
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Properties of RelationsProperties of Relations
• Distinct (i.e., unique) relation nameDistinct (i.e., unique) relation name
• Each cell contains exactly one atomic (single) value Each cell contains exactly one atomic (single) value
– No repeating groupsNo repeating groups
• Distinct attribute nameDistinct attribute name
• The values of an attribute come from the same domainThe values of an attribute come from the same domain
• Order of attributes has no significanceOrder of attributes has no significance
• Each tuple is distinct (i.e., unique)Each tuple is distinct (i.e., unique)
– No duplicate tuplesNo duplicate tuples
• Order of tuples has no significanceOrder of tuples has no significance
7
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Unique Identification of a RelationUnique Identification of a Relation
RelationRelation
keykey
SuperkeySuperkey
Candidate keyCandidate key
Primary keyPrimary key
Foreign keyForeign key??
8
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Identifying a TupleIdentifying a Tuple
• SuperkeySuperkeyAn attribute or a set of attributes that uniquely identifies a tuple An attribute or a set of attributes that uniquely identifies a tuple within a relationwithin a relation
• Candidate keyCandidate keyA super key such that no proper subset is a superkey within the A super key such that no proper subset is a superkey within the relationrelation– Uniquely identifies the tuple (uniqueness)Uniquely identifies the tuple (uniqueness)– Contains no unique subset (irreducibility)Contains no unique subset (irreducibility)
• Primary keyPrimary keyThe candidate key that is selected to identify tuples uniquely The candidate key that is selected to identify tuples uniquely within a relationwithin a relation– Should remain constant over the life of the tupleShould remain constant over the life of the tuple– Most Most efficient efficient way of identifying a tupleway of identifying a tuple
9
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Finding the Primary KeyFinding the Primary Key
Super KeySuper Key
Candidate KeyCandidate Key
Primary keyPrimary key
10
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
KeysKeys
AttributesAttributes• Catalog numberCatalog number• Record titleRecord title• Artist nameArtist name• Record labelRecord label• UPCUPC
CDs RelationCDs Relation
129341129341
129342129342
129343129343
129344129344
Help!Help!
Hard Day’s NightHard Day’s Night
Sergeant Pepper’sSergeant Pepper’s
Magical Mystery TourMagical Mystery Tour
BeatlesBeatles
BeatlesBeatles
BeatlesBeatles
BeatlesBeatles
ColumbiaColumbia
ColumbiaColumbia
ColumbiaColumbia
ColumbiaColumbia
129345129345 Abbey RoadAbbey Road BeatlesBeatles AppleApple
1-29150-8384-01-29150-8384-0
1-29150-7115-01-29150-7115-0
1-29150-2484-01-29150-2484-0
1-29150-7515-01-29150-7515-0
1-15700-9510-01-15700-9510-0
Superkey?Superkey?Candidate key?Candidate key?Primary key?Primary key?
11
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Selecting a KeySelecting a Key
• CriteriaCriteria
– An An efficientefficient way of identifying an entity way of identifying an entity
– The attribute (value) remains constant over the life of The attribute (value) remains constant over the life of the entitythe entity
• Never changesNever changes
12
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Identifying a TupleIdentifying a Tuple
• Foreign keyForeign keyAn attribute or set of attributes within one relation that An attribute or set of attributes within one relation that matches the candidate key of some (possibly the same) matches the candidate key of some (possibly the same) relationrelation
RelationRelation
keykey
keykey
RelationRelation
foreign keyforeign key
13
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Foreign KeyForeign Key
CDs RelationCDs Relation
129341129341
129342129342
129343129343
129344129344
Help!Help!
Hard Day’s NightHard Day’s Night
Sergeant Pepper’sSergeant Pepper’s
Magical Mystery TourMagical Mystery Tour
BeatlesBeatles
BeatlesBeatles
BeatlesBeatles
BeatlesBeatles
COLCOL
COLCOL
COLCOL
COLCOL
129345129345 Abbey RoadAbbey Road BeatlesBeatles APPAPP
1-29150-8384-01-29150-8384-0
1-29150-7115-01-29150-7115-0
1-29150-2484-01-29150-2484-0
1-29150-7515-01-29150-7515-0
1-15700-9510-01-15700-9510-0
COLCOL
APPAPP
Columbia RecordsColumbia Records
Apple RecordsApple RecordsRecording Label Recording Label
RelationRelation(home relation)(home relation)
Must match!Must match!
14
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Relational IntegrityRelational Integrity
Constraints placed on the set of values allowed for the Constraints placed on the set of values allowed for the attributes of a relation.attributes of a relation.• Entity integrityEntity integrity
– No attribute of a primary key can be null (every tuple No attribute of a primary key can be null (every tuple must be uniquely identified)must be uniquely identified)
• Referential integrityReferential integrity– If a foreign key exists in a relation, either the foreign If a foreign key exists in a relation, either the foreign
key value must match a candidate key value of some key value must match a candidate key value of some tuple in its home relation, or the foreign key value must tuple in its home relation, or the foreign key value must be wholly null (i.e., no key exists in the home relation)be wholly null (i.e., no key exists in the home relation)
• General constraints (reflect business practices)General constraints (reflect business practices)
15
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Null ValueNull Value
• Absence of any value (i.e., unknown or nonapplicable to a Absence of any value (i.e., unknown or nonapplicable to a tuple)tuple)
16
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
ViewsViews
• A view is a A view is a virtual relation virtual relation or one that does not actually or one that does not actually exist, but exist, but dynamically deriveddynamically derived– Can be constructed by performing operations (i.e., select, Can be constructed by performing operations (i.e., select,
project, join, etc.) on values of existing base relations project, join, etc.) on values of existing base relations • Base relation - a named relation, corresponding to an Base relation - a named relation, corresponding to an
entity in the conceptual schema, whose tuples are entity in the conceptual schema, whose tuples are physically stored in the databasephysically stored in the database
• View - a dynamic result of one or more relational View - a dynamic result of one or more relational operations operating on the base relations to produce operations operating on the base relations to produce anotheranother
17
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Schema and SubschemasSchema and Subschemas
DBMS
Schema Conceptual LevelConceptual Level
External External LevelLevel
Internal LevelInternal LevelPhysical DatabasePhysical Database
DBMS SoftwareDBMS Software
User User User User User User
Subschema
SomeSome end-user applications end-user applications can be supported by can be supported by viewsviews
Subschema Subschema
18
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
ViewsViews
KeyKeyForeign Foreign
KeyKey KeyKey
CriterionCriterion
Base Relation RBase Relation R Base Relation SBase Relation S
ViewView
19
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Purpose of ViewsPurpose of Views
• Provides a powerful and flexible security mechanism by Provides a powerful and flexible security mechanism by hiding parts of the database from certain usershiding parts of the database from certain users
• Permits user access in a way that is customized to their Permits user access in a way that is customized to their needsneeds
• Simplify complex operations on the base relationsSimplify complex operations on the base relations
• Designed to support the external model Designed to support the external model
• Provides logical independenceProvides logical independence
20
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Updating ViewsUpdating Views
• Allowed on views Allowed on views
– Derived from a single base relation, andDerived from a single base relation, and
– Containing the primary key or a candidate keyContaining the primary key or a candidate key
• NOT allowed on viewsNOT allowed on views
– Derived from multiple base relationsDerived from multiple base relations
– Involving aggregations (i.e., summations) or groups Involving aggregations (i.e., summations) or groups operationsoperations
• Vendors may have other constraints on updating viewsVendors may have other constraints on updating views
21
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Relational LanguagesRelational LanguagesRelational Calculus vs. Relational AlgebraRelational Calculus vs. Relational Algebra
• Relational calculusRelational calculusNonprocedural formal relational data manipulation Nonprocedural formal relational data manipulation language in which the user specifies what data should be language in which the user specifies what data should be retrieved, but retrieved, but notnot how to retrieve it. how to retrieve it.
• Relational algebraRelational algebraA theoretical language with operators that work on one or A theoretical language with operators that work on one or two relations to produce another relation.two relations to produce another relation.
Ricardo, 1990Ricardo, 1990
22
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Relational AlgebraRelational Algebra
• Fundamental operations:Fundamental operations:
– Selection (Selection ())
– Projection (Projection ())
– Union (Union ())
– Set differenceSet difference
– Intersection (Intersection ())
– Cartesian productCartesian product
– Join ( )Join ( )
– DivisionDivision
Unary operations (applied to a Unary operations (applied to a single relation)single relation)
23
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Aggregating and Grouping OperationsAggregating and Grouping Operations
• CountCount
• SumSum
• Average (value)Average (value)
• Minimum (value)Minimum (value)
• Maximum (value)Maximum (value)
24
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SelectionSelection
• Selection operator works on a Selection operator works on a singlesingle relation and defines a relation and defines a relation that contains only those tuples that satisfy the relation that contains only those tuples that satisfy the specified condition (specified condition (predicatepredicate))
predicatepredicate(R) (R) predicatepredicate(R) (R)
PredicatePredicate
25
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
ProjectionProjection
• Defines a relation that contains a vertical subset, extracting Defines a relation that contains a vertical subset, extracting the values of specified attributes and eliminating the values of specified attributes and eliminating duplicates.duplicates.
attribute-1, ..., attribute-attribute-1, ..., attribute-nn(R)(R)attribute-1, ..., attribute-attribute-1, ..., attribute-nn(R)(R)
26
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
• The selection extracts a subset of tuples from the original The selection extracts a subset of tuples from the original relation R based on the predicate. The project further relation R based on the predicate. The project further reduces the number of attributes. reduces the number of attributes.
Projection on a SelectionProjection on a Selection
attribute-1, ..., attribute-attribute-1, ..., attribute-nn((predicatepredicate(R))(R))attribute-1, ..., attribute-attribute-1, ..., attribute-nn((predicatepredicate(R))(R))
Original relationOriginal relation
27
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Project on a SelectionProject on a Selection
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
Carslicense, makelicense, make((color=‘Silver’color=‘Silver’(Cars))(Cars))license, makelicense, make((color=‘Silver’color=‘Silver’(Cars))(Cars))
3ABC1233ABC123 Acura TLAcura TL SilverSilver4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver3ABC1233ABC123 Acura TLAcura TL SilverSilver4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver
Step 1Step 1Step 1Step 1
color=‘Silver’color=‘Silver’
3ABC1233ABC123 Acura TLAcura TL4LLP3944LLP394 Lexus GS400Lexus GS4003ABC1233ABC123 Acura TLAcura TL4LLP3944LLP394 Lexus GS400Lexus GS400
Step 2Step 2Step 2Step 2license, license,
makemake
28
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
UnionUnion
• The union of two relations The union of two relations RR and and SS with with II and and JJ tuples, tuples, receptively, is obtained by concatenating them into one receptively, is obtained by concatenating them into one relation with a maximum of (relation with a maximum of (II + + JJ) tuples, duplicate tuples ) tuples, duplicate tuples being eliminated.being eliminated.
– RR and and SS must be union compatible (i.e., same attributes) must be union compatible (i.e., same attributes)
R R S S R R S S RR SS
attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) )
29
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
UnionUnion
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
Set 1
Set 2
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
Common
R R S S R R S S
30
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Set DifferenceSet Difference
• Defines a relation consisting of the tuples that are in Defines a relation consisting of the tuples that are in relation relation RR, but not in , but not in SS..
– RR and and SS must be union-compatible (i.e., same attributes) must be union-compatible (i.e., same attributes)
R R S S R R S S RR SS
attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) )
31
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Set DifferenceSet Difference
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
Set 1
Set 2
license, model, colorlicense, model, color(Set 1) (Set 1) license, model, colorlicense, model, color(Set 2) (Set 2) license, model, colorlicense, model, color(Set 1) (Set 1) license, model, colorlicense, model, color(Set 2) (Set 2)
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
Not unique to Set 1
R R S S R R S S Set 1Set 1 Set 2Set 2
32
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Set DifferenceSet Difference
R R S S R R S S RR SS
attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) )
ReversedReversed
33
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Set DifferenceSet Difference
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
Set 1
Set 2
license, model, colorlicense, model, color(Set 2) (Set 2) license, model, colorlicense, model, color(Set 1) (Set 1) license, model, colorlicense, model, color(Set 2) (Set 2) license, model, colorlicense, model, color(Set 1) (Set 1)
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
R R S S R R S S Set 1Set 1 Set 2Set 2
34
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
IntersectionIntersection
• An intersection consists of the set of all tuples that are in An intersection consists of the set of all tuples that are in both both RR and and SS..
– RR and and SS must be union-compatible (i.e., same attributes) must be union-compatible (i.e., same attributes)
R R S S R R S S RR SS
attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) )
35
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
IntersectionIntersection
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
3ABC1233ABC123 Acura TLAcura TL SilverSilver4MIS3454MIS345 Honda Accord DX Honda Accord DX WhiteWhite4MIS8374MIS837 Honda Accord LXHonda Accord LX BlackBlack4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4NOP9344NOP934 Acura CLAcura CL MetBlueMetBlue
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
4TLC3944TLC394 Mercedes CLK320Mercedes CLK320 RedRed4MSE2914MSE291 BMW 525i BMW 525i TealTeal5AMD0425AMD042 BMW M RoadsterBMW M Roadster BlueMetBlueMet4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4GAS2944GAS294 Lexus LS430Lexus LS430 GoldMetGoldMet
4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver4LLP3944LLP394 Lexus GS400Lexus GS400 SilverSilver
Set 1
Set 2
R R S S R R S S Set 1Set 1 Set 2Set 2
36
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Cartesian ProductCartesian Product
• The Cartesian product defines a relation that is the The Cartesian product defines a relation that is the concatenation of every tuple of relation concatenation of every tuple of relation RR with every tuple with every tuple of relation of relation SS
– II JJ tuples tuples
– N N ++ M M attributes attributes
attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((RR) ) attribute-1, ..., attribute-attribute-1, ..., attribute-nn((SS) )
37
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Cartesian ProductCartesian Product
a, b, u, va, b, u, va, b, w, xa, b, w, xa, b, y, za, b, y, zc, d, u ,vc, d, u ,vc, d, w, xc, d, w, xc, d, y, zc, d, y, ze, f, u, ve, f, u, ve, f, w, xe, f, w, xe, f, y, ze, f, y, z
Cartesian Cartesian ProductProduct
a, ba, bc, dc, de, fe, f
Relation Relation RR
u, vu, vw, xw, xy, zy, z
Relation Relation SS
}}
}}}}
RR SS
38
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
JoinsJoins
• Theta joinTheta join
• Equi-joinEqui-join
• Natural joinNatural join
• Outer joinOuter join
• Semi-joinSemi-join
Selection performed on two Selection performed on two relations, relations, RR and and SS, that share at , that share at least a single common attributeleast a single common attribute
39
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Theta-Join vs. Equi-JoinTheta-Join vs. Equi-Join
• Theta-joinTheta-joinA resulting relation that contains tuples satisfying the A resulting relation that contains tuples satisfying the predicate predicate FF from the Cartesian product of from the Cartesian product of RR and and SS– The predicate The predicate FF is of the form is of the form
where where may be one of the comparison operators <, <=, may be one of the comparison operators <, <=, >, >=, <>>, >=, <>
• Equi-joinEqui-join– Where Where is ‘=‘ is ‘=‘
RR.a.ajj SS.b.bj j RR.a.ajj SS.b.bj j
RR FFSSRR FFSS
RR RR.aj .aj SS.bj .bj SSRR RR.aj .aj SS.bj .bj SS
40
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Theta JoinTheta Join
CarsLS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMW
LS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMW
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.com
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.com
Manufacturers(( model model ((CarsCars)) )) cars.make < ‘MB’cars.make < ‘MB’ (( URLURL((ManufacturersManufacturers))))(( model model ((CarsCars)) )) cars.make < ‘MB’cars.make < ‘MB’ (( URLURL((ManufacturersManufacturers))))
LS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.com
LS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.com
modelmodel URLURL
model
URL
41
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Equi-JoinEqui-Join
CarsLS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMWS8S8 AudiAudi
LS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMWS8S8 AudiAudi
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.comPorschePorsche www.porsche.comwww.porsche.com
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.comPorschePorsche www.porsche.comwww.porsche.com
Manufacturers(( model model((CarsCars)) )) cars.make=manufacturers .makecars.make=manufacturers .make(( URLURL((ManufacturersManufacturers))))(( model model((CarsCars)) )) cars.make=manufacturers .makecars.make=manufacturers .make(( URLURL((ManufacturersManufacturers))))
modelmodel URLURLLS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.comC320C320 www.mercedesbenz.comwww.mercedesbenz.comE320E320 www.mercedesbenz.comwww.mercedesbenz.comS430S430 www.mercedesbenz.comwww.mercedesbenz.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.com
LS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.comC320C320 www.mercedesbenz.comwww.mercedesbenz.comE320E320 www.mercedesbenz.comwww.mercedesbenz.comS430S430 www.mercedesbenz.comwww.mercedesbenz.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.com
model
URL
No match
No match
42
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Natural JoinNatural Join
• Equi-join of two relations Equi-join of two relations RR and and SS over all common over all common attributes attributes xx
– One occurrence of each common attribute is eliminated One occurrence of each common attribute is eliminated from the resultfrom the result
R R SSR R SS
43
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Natural JoinNatural Join
CarsLS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMW
LS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMW
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.com
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.com
Manufacturers
(( model, make model, make ((CarsCars)) )) cars.make=manufacturers .makecars.make=manufacturers .make
(( make, URLmake, URL((ManufacturersManufacturers))))
(( model, make model, make ((CarsCars)) )) cars.make=manufacturers .makecars.make=manufacturers .make
(( make, URLmake, URL((ManufacturersManufacturers))))
““Make” not repeatedMake” not repeated
LS340LS340 LexusLexus www.lexus.comwww.lexus.comGS300GS300 LexusLexus www.lexus.comwww.lexus.comGS430GS430 LexusLexus www.lexus.comwww.lexus.comC320C320 MBMB www.mercedesbenz.comwww.mercedesbenz.comE320E320 MBMB www.mercedesbenz.comwww.mercedesbenz.comS430S430 MBMB www.mercedesbenz.comwww.mercedesbenz.com325i325i BMWBMW www.bmw.comwww.bmw.com523i523i BMWBMW www.bmw.comwww.bmw.com530i530i BMWBMW www.bmw.comwww.bmw.com
LS340LS340 LexusLexus www.lexus.comwww.lexus.comGS300GS300 LexusLexus www.lexus.comwww.lexus.comGS430GS430 LexusLexus www.lexus.comwww.lexus.comC320C320 MBMB www.mercedesbenz.comwww.mercedesbenz.comE320E320 MBMB www.mercedesbenz.comwww.mercedesbenz.comS430S430 MBMB www.mercedesbenz.comwww.mercedesbenz.com325i325i BMWBMW www.bmw.comwww.bmw.com523i523i BMWBMW www.bmw.comwww.bmw.com530i530i BMWBMW www.bmw.comwww.bmw.com
44
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Outer JoinOuter Join
• A join in which tuples from A join in which tuples from RR that do not have matching that do not have matching values in the common attributes of values in the common attributes of SS are also included in are also included in the result relation.the result relation.
– Missing values in the second relation are set to nullMissing values in the second relation are set to null
– Right outer join (all tuples on right kept) Right outer join (all tuples on right kept) vs. left outer join (all tuples on the left kept) vs. left outer join (all tuples on the left kept) vs. full outer join (all tuples on both sides kept)vs. full outer join (all tuples on both sides kept)
RR SSRR SS
45
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Left Outer JoinLeft Outer Join
CarsLS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMWS8S8 AudiAudi
LS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMWS8S8 AudiAudi
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.comPorschePorsche www.porsche.comwww.porsche.com
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.comPorschePorsche www.porsche.comwww.porsche.com
Manufacturers model model ((CarsCars) ) cars.make=manufacturers.makecars.make=manufacturers.make URL URL((ManufacturersManufacturers)) model model ((CarsCars) ) cars.make=manufacturers.makecars.make=manufacturers.make URL URL((ManufacturersManufacturers))
LS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.comC320C320 www.mercedesbenz.comwww.mercedesbenz.comE320E320 www.mercedesbenz.comwww.mercedesbenz.comS430S430 www.mercedesbenz.comwww.mercedesbenz.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.comS8S8 nullnull
LS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.comC320C320 www.mercedesbenz.comwww.mercedesbenz.comE320E320 www.mercedesbenz.comwww.mercedesbenz.comS430S430 www.mercedesbenz.comwww.mercedesbenz.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.comS8S8 nullnull
Match all the models in Cars to the URLs in Manufacturers
No match found
46
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Right Outer JoinRight Outer Join
CarsLS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMWS8S8 AudiAudi
LS340LS340 LexusLexusGS300GS300 LexusLexusGS430GS430 LexusLexusC320C320 MBMBE320E320 MBMBS430S430 MBMB325i325i BMWBMW523i523i BMWBMW530i530i BMWBMWS8S8 AudiAudi
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.comPorschePorsche www.porsche.comwww.porsche.com
LexusLexus www.lexus.comwww.lexus.comMBMB www.mercedesbenz.comwww.mercedesbenz.comBMWBMW www.bmw.comwww.bmw.comPorschePorsche www.porsche.comwww.porsche.com
Manufacturers model model ((CarsCars) ) cars.make=manufacturers.makecars.make=manufacturers.make URL URL((ManufacturersManufacturers)) model model ((CarsCars) ) cars.make=manufacturers.makecars.make=manufacturers.make URL URL((ManufacturersManufacturers))
LS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.comC320C320 www.mercedesbenz.comwww.mercedesbenz.comE320E320 www.mercedesbenz.comwww.mercedesbenz.comS430S430 www.mercedesbenz.comwww.mercedesbenz.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.comnullnull www.porsche.comwww.porsche.com
LS340LS340 www.lexus.comwww.lexus.comGS300GS300 www.lexus.comwww.lexus.comGS430GS430 www.lexus.comwww.lexus.comC320C320 www.mercedesbenz.comwww.mercedesbenz.comE320E320 www.mercedesbenz.comwww.mercedesbenz.comS430S430 www.mercedesbenz.comwww.mercedesbenz.com325i325i www.bmw.comwww.bmw.com523i523i www.bmw.comwww.bmw.com530i530i www.bmw.comwww.bmw.comnullnull www.porsche.comwww.porsche.com
Match all the URLs in Manufacturers to the models in Cars
No match found
47
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
DivisionDivision
• Consists of the set of tuples from Consists of the set of tuples from RR defined over the defined over the attributes attributes CC that match the combination of every tuple in that match the combination of every tuple in SS
attribute-xattribute-x((RR) ) attribute-xattribute-x(( predicatepredicate((SS)) )) attribute-xattribute-x((RR) ) attribute-xattribute-x(( predicatepredicate((SS)) ))
SelectSelect
Project of Select Project of Select T2T2Project R Project R T1T1
Occurrences in project T1 that match the pattern in T2Occurrences in project T1 that match the pattern in T2
48
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
rno,pnorno,pno((ViewingViewing) ) pnopno((rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Identify all renters who have viewed all properties with 3 rooms)(Identify all renters who have viewed all properties with 3 rooms)
rno,pnorno,pno((ViewingViewing) ) pnopno((rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Identify all renters who have viewed all properties with 3 rooms)(Identify all renters who have viewed all properties with 3 rooms)
RnoRno PnoPno DateDate CommentCommentCR56 CR56 PA14 5/24/95 PA14 5/24/95 Too smallToo smallCR76CR76 PG4PG4 4/20/954/20/95 Too remoteToo remoteCR56CR56 PG4PG4 5/26/955/26/95CR62CR62 PA14PA14 5/14/955/14/95 No dining roomNo dining roomCR56CR56 PG36PG36 4/28/954/28/95
RnoRno PnoPno DateDate CommentCommentCR56 CR56 PA14 5/24/95 PA14 5/24/95 Too smallToo smallCR76CR76 PG4PG4 4/20/954/20/95 Too remoteToo remoteCR56CR56 PG4PG4 5/26/955/26/95CR62CR62 PA14PA14 5/14/955/14/95 No dining roomNo dining roomCR56CR56 PG36PG36 4/28/954/28/95
ViewingViewing
Property_for_RentProperty_for_Rent
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPA14PA14 6 6PL94PL94 4 4PG4PG4 33PG36PG36 33PG21PG21 5 5PG16PG16 4 4
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPA14PA14 6 6PL94PL94 4 4PG4PG4 33PG36PG36 33PG21PG21 5 5PG16PG16 4 4
Rno = Renter NumberRno = Renter Number
49
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Select Property_for_Rent with rooms = 3)(Select Property_for_Rent with rooms = 3)
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Select Property_for_Rent with rooms = 3)(Select Property_for_Rent with rooms = 3)
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPG4PG4 3 3PG36PG36 3 3
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPG4PG4 3 3PG36PG36 3 3
Property_for_RentProperty_for_Rent
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPA14PA14 6 6PL94PL94 4 4PG4PG4 3 3PG36PG36 3 3PG21PG21 5 5PG16PG16 4 4
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPA14PA14 6 6PL94PL94 4 4PG4PG4 3 3PG36PG36 3 3PG21PG21 5 5PG16PG16 4 4
(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))
50
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Project proper number)(Project proper number)
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Project proper number)(Project proper number)
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPG4PG4 3 3PG36PG36 3 3
PnoPno StreetStreet AreaArea CityCity PcodePcode TypeType RoomsRooms RentRent OnoOno Sno Sno BnoBnoPG4PG4 3 3PG36PG36 3 3
Select(Property_for_Rent)Select(Property_for_Rent)
PnoPnoPG4PG4PG36PG36
PnoPnoPG4PG4PG36PG36 All properties for All properties for
rent with 3 rooms.rent with 3 rooms.
51
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Project renter number and property number)(Project renter number and property number)
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Project renter number and property number)(Project renter number and property number)
RnoRno PnoPno DateDate CommentCommentCR56 CR56 PA14 5/24/95 PA14 5/24/95 Too smallToo smallCR76CR76 PG4PG4 4/20/954/20/95 Too remoteToo remoteCR56CR56 PG4PG4 5/26/955/26/95CR62CR62 PA14PA14 5/14/955/14/95 No dining roomNo dining roomCR56CR56 PG36PG36 4/28/954/28/95
RnoRno PnoPno DateDate CommentCommentCR56 CR56 PA14 5/24/95 PA14 5/24/95 Too smallToo smallCR76CR76 PG4PG4 4/20/954/20/95 Too remoteToo remoteCR56CR56 PG4PG4 5/26/955/26/95CR62CR62 PA14PA14 5/14/955/14/95 No dining roomNo dining roomCR56CR56 PG36PG36 4/28/954/28/95
ViewingViewing
rno,pnorno,pno
52
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
PnoPnoPG4PG4PG36PG36
PnoPnoPG4PG4PG36PG36
Who has viewed Who has viewed allall rental properties with rental properties with 3 rooms?3 rooms?
Renters who have Renters who have viewed propertiesviewed properties
All rental All rental properties with properties with 3 rooms3 rooms
53
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
PnoPnoPG4PG4PG36PG36
PnoPnoPG4PG4PG36PG36
Who has viewed Who has viewed allall rental properties with rental properties with 3 rooms?3 rooms?
Renter CR76 has viewed PG4, but not PG36Renter CR76 has viewed PG4, but not PG36
Renters who have Renters who have viewed propertiesviewed properties
All rental All rental properties with properties with 3 rooms3 rooms
54
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
RnoRnoCR56CR56
RnoRnoCR56CR56
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Identify all renters who have viewed all properties with three (Identify all renters who have viewed all properties with three
rooms)rooms)
rno,pnorno,pno((ViewingViewing) ) pnopno(( rooms=3rooms=3((Property_for_RentProperty_for_Rent))))(Identify all renters who have viewed all properties with three (Identify all renters who have viewed all properties with three
rooms)rooms)
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
RnoRno PnoPnoCR56 CR56 PA14PA14CR76CR76 PG4PG4CR56CR56 PG4PG4CR62CR62 PA14PA14CR56CR56 PG36PG36
PnoPnoPG4PG4PG36PG36
PnoPnoPG4PG4PG36PG36{{
Renters who have Renters who have viewed propertiesviewed properties
Renter number CR56 has Renter number CR56 has viewed viewed allall properties properties with 3 roomswith 3 rooms
All rental All rental properties with properties with 3 rooms3 rooms
Who has viewed all Who has viewed all rental properties with rental properties with 3 rooms?3 rooms?
55
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Relational CalculusRelational Calculus
• Specifies Specifies whatwhat is to be retrieved rather than is to be retrieved rather than howhow to retrieve to retrieve itit
– Predicate (truth-value function arguments)Predicate (truth-value function arguments)
– Proposition (arguments replaced with values including Proposition (arguments replaced with values including the predicate)the predicate)
For all For all xx, if , if PP is true (e.g., for all is true (e.g., for all xx if predicate ( if predicate (xx > 100) > 100) is true)is true)
{{xx | P( | P(xx)} )} {{xx | P( | P(xx)} )} Where Where PP is the predicate is the predicate
56
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Tuple Oriented vs. Domain Oriented Tuple Oriented vs. Domain Oriented Relational CalculusRelational Calculus
• Tuple oriented (entities or rows)Tuple oriented (entities or rows)Finding tuples for which a predicate is trueFinding tuples for which a predicate is true
– Tuple variables (ranges over a named relation)Tuple variables (ranges over a named relation)
– Quantifiers: Quantifiers:
• Existential (Existential (): there exists (one instance)): there exists (one instance)
• Universal (Universal (): for all): for all
• Domain oriented (attributes or columns)Domain oriented (attributes or columns)Test for membership conditionTest for membership condition
– Variables that take their values from domainsVariables that take their values from domains
Number of Number of tuples to be tuples to be applied toapplied to
57
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL (pronounced ‘sequel’)SQL (pronounced ‘sequel’)
• ProjectionProjection
• SelectionSelection
58
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Structured Query Language (SQL)Structured Query Language (SQL)
• Transform-oriented language:Transform-oriented language:– DDL (data definition language)DDL (data definition language)– DML (data manipulation language)DML (data manipulation language)
• Non-procedural (no logic structure required)Non-procedural (no logic structure required)– Specify Specify whatwhat information is required, rather than information is required, rather than howhow
that information should be retrievedthat information should be retrieved– Format-free (not bound to columns)Format-free (not bound to columns)– English-like (English-like (although abbreviatedalthough abbreviated))
• First standard database language to gain wide acceptanceFirst standard database language to gain wide acceptance– ISO 1992 standards (today ISO 2003)ISO 1992 standards (today ISO 2003)
59
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL TerminologySQL Terminology
• ISO terminologyISO terminology
– Relations (entity type)Relations (entity type) TablesTables
– Attributes (properties)Attributes (properties) ColumnsColumns
– Tuples (entities)Tuples (entities) RowsRows
columncolumn
Row Row {{
60
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SELECT: Basic SyntaxSELECT: Basic Syntax
SELECT SELECT
[DISTINCT | ALL | {* | [column-list [AS alias]] [, ...]}[DISTINCT | ALL | {* | [column-list [AS alias]] [, ...]}
FROM table-name [alias] [, ...]FROM table-name [alias] [, ...]
[WHERE condition][WHERE condition]
[GROUP BY column-list] [HAVING condition][GROUP BY column-list] [HAVING condition]
[ORDER BY column-list][ORDER BY column-list]
[ ] = optional[ ] = optional| = select one option| = select one option
61
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Examples Using Products TableExamples Using Products Table
ProductsProducts ( (product_codeproduct_code ( (keykey), product_description, ), product_description, product_cost, product_MSRP, product_retail_price, product_cost, product_MSRP, product_retail_price, retail_unit, manufacturer_code (retail_unit, manufacturer_code (foreign keyforeign key), active_date, ), active_date, number_on_hand, number_on_order, number_committed)number_on_hand, number_on_order, number_committed)
62
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
ProjectionProjectionPerformed on a Single TablePerformed on a Single Table
product_code, product_descriptionproduct_code, product_description((ProductsProducts))
SELECT product_code, product_descriptionSELECT product_code, product_description
FROM productsFROM products
Result: Result: AllAll rows with only the product_code and rows with only the product_code and product_description columns are retrievedproduct_description columns are retrieved
Column-listColumn-listColumn-listColumn-list
63
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> SQL> select product_code, product_description from products;select product_code, product_description from products;
PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_CO PRODUCT_DESCRIPTION ---------- ------------------------------ ---------- ------------------------------ 301-III Direct/Reflecting Speakers 301-III Direct/Reflecting Speakers 3800 Three-way Speaker System 3800 Three-way Speaker System 4312 Studio Monitors 4312 Studio Monitors 901Classic Direct/Reflecting Spkr System 901Classic Direct/Reflecting Spkr System AM3 Acoustimass Speaker System AM3 Acoustimass Speaker System AM5 Acoustimass 5 Speaker System AM5 Acoustimass 5 Speaker System AM7 Acoustimass 7 Speaker System AM7 Acoustimass 7 Speaker System AT-10 Loudspeakers AT-10 Loudspeakers AT-15 Three-way Speaker AT-15 Three-way Speaker CCS-350 Compact System w/CD Player CCS-350 Compact System w/CD Player CCS-450 Compact System w/CD Player CCS-450 Compact System w/CD Player CCS-550 75-watt System w/CD Changer CCS-550 75-watt System w/CD Changer CD-1000C Compact Disc Changer CD-1000C Compact Disc Changer CDP-297 Compact Disc Player CDP-297 Compact Disc Player CDP-397 Compact Disc Player CDP-397 Compact Disc Player CDP-C225 Disc Jockey CD ChangerCDP-C225 Disc Jockey CD Changer
..
..
..100 rows selected.100 rows selected.
64
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SelectionSelectionPerformed on a Single TablePerformed on a Single Table
manufacturer_code = ‘SON’ manufacturer_code = ‘SON’ ((ProductsProducts))
SELECT *SELECT *
FROM productsFROM products
WHERE manufacturer_code = ‘SON’WHERE manufacturer_code = ‘SON’
Result: Only those rows with SON for their Result: Only those rows with SON for their manufacturer_code are retrieved along with all columns.manufacturer_code are retrieved along with all columns.
All columnsAll columnsProposition Proposition (predicate)(predicate)
65
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Four Ways to Build a SelectionFour Ways to Build a Selection
• Relational operator (=, <, <=, >, >=, <>)Relational operator (=, <, <=, >, >=, <>)– Logical AND and ORLogical AND and OR
WHEREWHERE column-namecolumn-name == xx AND AND column-namecolumn-name = = yy• Specific range using BETWEENSpecific range using BETWEEN
WHEREWHERE column-namecolumn-name BETWEENBETWEEN xx ANDAND yy• Specific values using INSpecific values using IN
WHERE WHERE column-namecolumn-name ININ ( (list-of-valueslist-of-values))• Character match using LIKE and wildcards (%, _)Character match using LIKE and wildcards (%, _)
– UPPER and LOWER functionsUPPER and LOWER functions
WHERE LOWER(WHERE LOWER(column-namecolumn-name) ) LIKELIKE ‘%‘%character-stringcharacter-string%’%’
66
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Four ways to build a SELECTION:Four ways to build a SELECTION:
1. Relational and Logical Operators1. Relational and Logical Operators
• Relational operators:Relational operators:
== equalsequals
>, < >, < greater than, less thangreater than, less than
<=<= less than or equal toless than or equal to
>=>= greater than or equal togreater than or equal to
<><> not equal tonot equal to
• Logical operatorsLogical operators
AND, ORAND, OR AND evaluated before ORAND evaluated before OR
NOTNOT NOT evaluated before AND and ORNOT evaluated before AND and OR
67
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
WHERE (Condition)WHERE (Condition)
SELECT *SELECT *
FROM productsFROM products
WHERE manufacturer_code = ‘SON’ WHERE manufacturer_code = ‘SON’
OROR manufacturer_code = ‘PAN’ manufacturer_code = ‘PAN’
SELECT *SELECT *
FROM productsFROM products
WHERE product_msrp >= 100 WHERE product_msrp >= 100 ANDAND product_msrp <= 500 product_msrp <= 500
ANDAND product_code = ‘SON’ product_code = ‘SON’ OROR product_code = ‘PAN’ product_code = ‘PAN’
All columnsAll columns
68
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
select * from products where manufacturer_code='SON';select * from products where manufacturer_code='SON';
PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_COST PRODUCT_MSRP PRODUCT_RETAIL_PRICE RE MAN PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_COST PRODUCT_MSRP PRODUCT_RETAIL_PRICE RE MAN ---------- ------------------------------ ------------ ------------ -------------------- -- --- ---------- ------------------------------ ------------ ------------ -------------------- -- --- ACTIVE_DA NUMBER_ON_HAND NUMBER_ON_ORDER NUMBER_COMMITTED ACTIVE_DA NUMBER_ON_HAND NUMBER_ON_ORDER NUMBER_COMMITTED --------- -------------- --------------- ---------------- --------- -------------- --------------- ---------------- CDP-297 Compact Disc Player 84.47 129.95 116.96 EA SON CDP-297 Compact Disc Player 84.47 129.95 116.96 EA SON 25-AUG-96 4 0 0 25-AUG-96 4 0 0 CDP-397 Compact Disc Player 97.47 149.95 134.96 EA SON CDP-397 Compact Disc Player 97.47 149.95 134.96 EA SON 25-AUG-96 11 0 0 25-AUG-96 11 0 0 CDP-C225 Disc Jockey CD Changer 129.97 199.95 179.96 EA SON CDP-C225 Disc Jockey CD Changer 129.97 199.95 179.96 EA SON 25-AUG-96 10 0 0 25-AUG-96 10 0 0 CDP-C325 Disc Jockey CD Changer 149.47 229.95 206.96 EA SON CDP-C325 Disc Jockey CD Changer 149.47 229.95 206.96 EA SON 25-AUG-96 1 0 0 25-AUG-96 1 0 0 CDP-C425 Disc Jockey CD Changer 162.47 249.95 224.96 EA SON CDP-C425 Disc Jockey CD Changer 162.47 249.95 224.96 EA SON 25-AUG-96 5 0 0 25-AUG-96 5 0 0 CDP-C525 Disc Jockey CD Changer 194.97 299.95 269.96 EA SON CDP-C525 Disc Jockey CD Changer 194.97 299.95 269.96 EA SON 25-AUG-96 6 0 0 25-AUG-96 6 0 0 SL-S600 Super-Beta Video Recorder 214.47 329.95 296.96 EA SON SL-S600 Super-Beta Video Recorder 214.47 329.95 296.96 EA SON 25-AUG-96 13 0 0 25-AUG-96 13 0 0 TC-W490 Double Cassette Deck 110.47 169.95 152.96 EA SON TC-W490 Double Cassette Deck 110.47 169.95 152.96 EA SON 25-AUG-96 6 0 0 25-AUG-96 6 0 0
12 rows selected.12 rows selected.
69
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Projection on a SelectionProjection on a SelectionPerformed on a Single TablePerformed on a Single Table
SELECT product_code, product_descriptionSELECT product_code, product_description
FROM productsFROM products
WHERE manufacturer_code = ‘SON’ WHERE manufacturer_code = ‘SON’
OR manufacturer_code = ‘PAN’OR manufacturer_code = ‘PAN’
product_code, product_descriptionproduct_code, product_description manufacturer_code = ‘SON’ or ‘PAN’ manufacturer_code = ‘SON’ or ‘PAN’ ((ProductsProducts))))
Column list Predicate (condition)
Only one condition has to be true
70
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> SQL> select product_code, product_description from products whereselect product_code, product_description from products where 2 2 manufacturer_code='SON' or manufacturer_code='PAN';manufacturer_code='SON' or manufacturer_code='PAN';
PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_CO PRODUCT_DESCRIPTION ---------- ------------------------------ ---------- ------------------------------ CDP-297 Compact Disc Player CDP-297 Compact Disc Player CDP-397 Compact Disc Player CDP-397 Compact Disc Player CDP-C225 Disc Jockey CD Changer CDP-C225 Disc Jockey CD Changer CDP-C325 Disc Jockey CD Changer CDP-C325 Disc Jockey CD Changer CDP-C425 Disc Jockey CD Changer CDP-C425 Disc Jockey CD Changer CDP-C525 Disc Jockey CD Changer CDP-C525 Disc Jockey CD Changer PV-2201 HQ VHS Video Cassette Recorde PV-2201 HQ VHS Video Cassette Recorde PV-4210 4-Head VHS Video Cass Recorde PV-4210 4-Head VHS Video Cass Recorde PV-4250 HiFi VHS Video Cass Recorder PV-4250 HiFi VHS Video Cass Recorder SC-T095 Compact Stereo System SC-T095 Compact Stereo System SC-TC430 Compact System w/CD Changer SC-TC430 Compact System w/CD Changer SL-S600 Super-Beta Video Recorder SL-S600 Super-Beta Video Recorder TC-W490 Double Cassette Deck TC-W490 Double Cassette Deck TC-WR590 Double Cassette Deck TC-WR590 Double Cassette Deck TC-WR690 Double Cassette Deck TC-WR690 Double Cassette Deck TC-WR790 Double Cassette Deck TC-WR790 Double Cassette Deck TC-WR875 Double Cassette Deck TC-WR875 Double Cassette Deck
17 rows selected.17 rows selected.
71
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> SQL> select product_code, product_description, product_msrpselect product_code, product_description, product_msrp 22 from productsfrom products 3 3 where manufacturer_code='SON' or manufacturer_code='PAN'where manufacturer_code='SON' or manufacturer_code='PAN' 4 4 and product_msrp >= 100 and product_msrp <=500;and product_msrp >= 100 and product_msrp <=500;
PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_MSRP PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_MSRP ---------- ------------------------------ ------------ ---------- ------------------------------ ------------ CDP-297 Compact Disc Player 129.95 CDP-297 Compact Disc Player 129.95 CDP-397 Compact Disc Player 149.95 CDP-397 Compact Disc Player 149.95 CDP-C225 Disc Jockey CD Changer 199.95 CDP-C225 Disc Jockey CD Changer 199.95 CDP-C325 Disc Jockey CD Changer 229.95 CDP-C325 Disc Jockey CD Changer 229.95 CDP-C425 Disc Jockey CD Changer 249.95 CDP-C425 Disc Jockey CD Changer 249.95 CDP-C525 Disc Jockey CD Changer 299.95 CDP-C525 Disc Jockey CD Changer 299.95 PV-2201 HQ VHS Video Cassette Recorde 229.95 PV-2201 HQ VHS Video Cassette Recorde 229.95 PV-4210 4-Head VHS Video Cass Recorde 299.95 PV-4210 4-Head VHS Video Cass Recorde 299.95 PV-4250 HiFi VHS Video Cass Recorder 349.95 PV-4250 HiFi VHS Video Cass Recorder 349.95 SC-T095 Compact Stereo System 139.95 SC-T095 Compact Stereo System 139.95 SC-TC430 Compact System w/CD Changer 429.95 SC-TC430 Compact System w/CD Changer 429.95 SL-S600 Super-Beta Video Recorder 329.95 SL-S600 Super-Beta Video Recorder 329.95 TC-W490 Double Cassette Deck 169.95 TC-W490 Double Cassette Deck 169.95 TC-WR590 Double Cassette Deck 199.95 TC-WR590 Double Cassette Deck 199.95 TC-WR690 Double Cassette Deck 249.95 TC-WR690 Double Cassette Deck 249.95 TC-WR790 Double Cassette Deck 329.95 TC-WR790 Double Cassette Deck 329.95 TC-WR875 Double Cassette Deck 429.95 TC-WR875 Double Cassette Deck 429.95
17 rows selected.17 rows selected.
Both conditions Both conditions must be truemust be true
72
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Four ways to build a SELECTION:Four ways to build a SELECTION: 2. Range Search with BETWEEN2. Range Search with BETWEEN
SELECT product_code, product_description, product_msrpSELECT product_code, product_description, product_msrp
FROM productsFROM products
WHERE product_msrp BETWEEN 100 and 500WHERE product_msrp BETWEEN 100 and 500
AND manufacturer_code = ‘SON’ OR manufacturer_code = AND manufacturer_code = ‘SON’ OR manufacturer_code = ‘PAN’‘PAN’
InclusiveInclusive
73
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> SQL> select product_code, product_description, product_msrp,select product_code, product_description, product_msrp, 2 2 manufacturer_codemanufacturer_code 3 3 from productsfrom products 4 4 where manufacturer_code = 'SON' or manufacturer_code = 'PAN' where manufacturer_code = 'SON' or manufacturer_code = 'PAN' 5 5 and product_msrp between 100 and 500;and product_msrp between 100 and 500;
PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_MSRP MAN PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_MSRP MAN ---------- ------------------------------ ------------ --- ---------- ------------------------------ ------------ --- CDP-297 Compact Disc Player 129.95 SON CDP-297 Compact Disc Player 129.95 SON CDP-397 Compact Disc Player 149.95 SON CDP-397 Compact Disc Player 149.95 SON CDP-C225 Disc Jockey CD Changer 199.95 SON CDP-C225 Disc Jockey CD Changer 199.95 SON CDP-C325 Disc Jockey CD Changer 229.95 SON CDP-C325 Disc Jockey CD Changer 229.95 SON CDP-C425 Disc Jockey CD Changer 249.95 SON CDP-C425 Disc Jockey CD Changer 249.95 SON CDP-C525 Disc Jockey CD Changer 299.95 SON CDP-C525 Disc Jockey CD Changer 299.95 SON PV-2201 HQ VHS Video Cassette Recorde 229.95 PAN PV-2201 HQ VHS Video Cassette Recorde 229.95 PAN PV-4210 4-Head VHS Video Cass Recorde 299.95 PAN PV-4210 4-Head VHS Video Cass Recorde 299.95 PAN PV-4250 HiFi VHS Video Cass Recorder 349.95 PAN PV-4250 HiFi VHS Video Cass Recorder 349.95 PAN SC-T095 Compact Stereo System 139.95 PAN SC-T095 Compact Stereo System 139.95 PAN SC-TC430 Compact System w/CD Changer 429.95 PAN SC-TC430 Compact System w/CD Changer 429.95 PAN SL-S600 Super-Beta Video Recorder 329.95 SON SL-S600 Super-Beta Video Recorder 329.95 SON TC-W490 Double Cassette Deck 169.95 SON TC-W490 Double Cassette Deck 169.95 SON TC-WR590 Double Cassette Deck 199.95 SON TC-WR590 Double Cassette Deck 199.95 SON TC-WR690 Double Cassette Deck 249.95 SON TC-WR690 Double Cassette Deck 249.95 SON TC-WR790 Double Cassette Deck 329.95 SON TC-WR790 Double Cassette Deck 329.95 SON TC-WR875 Double Cassette Deck 429.95 SON TC-WR875 Double Cassette Deck 429.95 SON
17 rows selected.17 rows selected.
74
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Four ways to build a SELECTION:Four ways to build a SELECTION: 3. Search for Specific Values Using IN3. Search for Specific Values Using IN
SELECT product_code, product_descriptionSELECT product_code, product_description
FROM productsFROM products
WHERE manufacturer_code IN ('SON', 'PAN', 'BOS');WHERE manufacturer_code IN ('SON', 'PAN', 'BOS');
List of valuesList of values
75
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> SQL> select product_code, product_descriptionselect product_code, product_description 2 2 from productsfrom products 3 3 where manufacturer_code in ('SON', 'PAN', 'BOS');where manufacturer_code in ('SON', 'PAN', 'BOS');
PRODUCT_CO PRODUCT_DESCRIPTIONPRODUCT_CO PRODUCT_DESCRIPTION---------- --------------------------------------------- -----------------------------------DVD-A110 DVD/CD PlayerDVD-A110 DVD/CD PlayerDVP-S7000 DVD/CD PlayerDVP-S7000 DVD/CD PlayerDVP-S3000 DVD/CD PlayerDVP-S3000 DVD/CD PlayerDVP-S500D DVD/CD PlayerDVP-S500D DVD/CD PlayerKV-20S40 20" Trinitron TVKV-20S40 20" Trinitron TVKV-20V80 20" Digital Comb Filter TVKV-20V80 20" Digital Comb Filter TVKV-27V22 27" Trinitron TVKV-27V22 27" Trinitron TVKV-27V26 27" Trinitron TVKV-27V26 27" Trinitron TVKV-27V36 27" Picture-in-Picture TVKV-27V36 27" Picture-in-Picture TVKV-32V36 32" 1-Tuner PIP TVKV-32V36 32" 1-Tuner PIP TVKV-35V36 35" 1-Tuner PIP TVKV-35V36 35" 1-Tuner PIP TVKV-32XBR48 32" Trinitron XBR TVKV-32XBR48 32" Trinitron XBR TVKV-35XBR48 35" Trinitron XBR TVKV-35XBR48 35" Trinitron XBR TVKV-35XBR88 35" Trinitron XBR TVKV-35XBR88 35" Trinitron XBR TV
..
..
..TC-KE500S Cassette DeckTC-KE500S Cassette Deck
95 rows selected.95 rows selected.
76
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Four ways to build a SELECTION:Four ways to build a SELECTION: 4. Pattern Match with LIKE4. Pattern Match with LIKE
• Wildcard charactersWildcard characters– % = any sequence of zero or more characters% = any sequence of zero or more characters– _ (underscore) = any single character_ (underscore) = any single character
SELECT product_code, product_descriptionSELECT product_code, product_description
FROM productsFROM products
WHERE product_code LIKE ‘C%’WHERE product_code LIKE ‘C%’
Result:Result: All rows with product codes beginning with C and All rows with product codes beginning with C and their corresponding product_description will be retrieved.their corresponding product_description will be retrieved.
77
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> SQL> select product_code, product_description from productsselect product_code, product_description from products 2 2 where product_code like 'C%';where product_code like 'C%';
PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_CO PRODUCT_DESCRIPTION ---------- ------------------------------ ---------- ------------------------------ CCS-350 Compact System w/CD Player CCS-350 Compact System w/CD Player CCS-450 Compact System w/CD Player CCS-450 Compact System w/CD Player CCS-550 75-watt System w/CD Changer CCS-550 75-watt System w/CD Changer CD-1000C Compact Disc Changer CD-1000C Compact Disc Changer CDP-297 Compact Disc Player CDP-297 Compact Disc Player CDP-397 Compact Disc Player CDP-397 Compact Disc Player CDP-C225 Disc Jockey CD Changer CDP-C225 Disc Jockey CD Changer CDP-C325 Disc Jockey CD Changer CDP-C325 Disc Jockey CD Changer CDP-C425 Disc Jockey CD Changer CDP-C425 Disc Jockey CD Changer CDP-C525 Disc Jockey CD Changer CDP-C525 Disc Jockey CD Changer CS-13RX 13" Color Television CS-13RX 13" Color Television CS-13SX1 13" Stereo Monitor/Television CS-13SX1 13" Stereo Monitor/Television CS-20SX1 20" Stereo Monitor/Television CS-20SX1 20" Stereo Monitor/Television CT-WN70R 6+1 Cassette Changer CT-WN70R 6+1 Cassette Changer
14 rows selected.14 rows selected.
78
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Pattern Match with LIKE and UPPER FunctionPattern Match with LIKE and UPPER Function
• SELECT product_code, product_descriptionSELECT product_code, product_descriptionFROM productsFROM productsWHERE UPPER(product_description) LIKE UPPER('%casset%');WHERE UPPER(product_description) LIKE UPPER('%casset%');
Changes to upper caseChanges to upper case
79
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> SQL> select product_code, product_descriptionselect product_code, product_description 2 2 from productsfrom products 3 3 where where upperupper(product_description) like (product_description) like upperupper('%casset%');('%casset%');
PRODUCT_CO PRODUCT_DESCRIPTIONPRODUCT_CO PRODUCT_DESCRIPTION---------- --------------------------------------------- -----------------------------------TC-KE400S Cassette DeckTC-KE400S Cassette DeckTC-KE500S Cassette DeckTC-KE500S Cassette DeckCT-W606DR Double Cassette DeckCT-W606DR Double Cassette DeckCT-W616DR Double Cassette DeckCT-W616DR Double Cassette DeckTD-W254 Double Auto-reverse Cassette DeckTD-W254 Double Auto-reverse Cassette DeckTD-W354 Double Auto-reverse Cassette DeckTD-W354 Double Auto-reverse Cassette DeckTD-W718 Dual Auto-reverse Rec Cassette DeckTD-W718 Dual Auto-reverse Rec Cassette DeckRS-TR373 Double Auto-reverse Cassette DeckRS-TR373 Double Auto-reverse Cassette DeckRS-TR575 Double Auto-reverse Cassette DeckRS-TR575 Double Auto-reverse Cassette DeckK-903 Dual Electronic Cassette DeckK-903 Dual Electronic Cassette DeckTC-WE405 Dual Cassette DeckTC-WE405 Dual Cassette DeckTC-WE605S Dual Auto-reverse Cassette DeckTC-WE605S Dual Auto-reverse Cassette Deck ••• •••K-90 Double Cassette DeckK-90 Double Cassette Deck
42 rows selected.42 rows selected.
80
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
SQL> select product_code, product_descriptionSQL> select product_code, product_description 2 from products2 from products 3 where product_description like '%casset%';3 where product_description like '%casset%';
no rows selectedno rows selected
Like WithLike Withoutout Matching the Case Matching the Case
CCassette assette ccassetteassetteCCassette assette ccassetteassette
81
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
ORDER BY: Sorting the ResultsORDER BY: Sorting the Results
SELECT manufacturer_code, product_code, SELECT manufacturer_code, product_code, product_descriptionproduct_description
FROM productsFROM products
ORDER BY manufacturer_code, product_codeORDER BY manufacturer_code, product_code
Major (sort) keyMajor (sort) key Minor (sort) keyMinor (sort) key
82
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
select manufacturer_code, product_code, product_descriptionselect manufacturer_code, product_code, product_description 2 2 from products order by manufacturer_code, product_code;from products order by manufacturer_code, product_code;
MAN PRODUCT_CO PRODUCT_DESCRIPTION MAN PRODUCT_CO PRODUCT_DESCRIPTION --- ---------- ------------------------------ --- ---------- ------------------------------ AIW NSX-D2 Mini Component System AIW NSX-D2 Mini Component System AIW XK-S9000 Cassette Deck AIW XK-S9000 Cassette Deck BOS 301-III Direct/Reflecting Speakers BOS 301-III Direct/Reflecting Speakers BOS 901Classic Direct/Reflecting Spkr System BOS 901Classic Direct/Reflecting Spkr System BOS AM3 Acoustimass Speaker System BOS AM3 Acoustimass Speaker System BOS AM5 Acoustimass 5 Speaker System BOS AM5 Acoustimass 5 Speaker System BOS AM7 Acoustimass 7 Speaker System BOS AM7 Acoustimass 7 Speaker System BOS VS-100 Center Channel Mini Speaker BOS VS-100 Center Channel Mini Speaker CRV AT-10 Loudspeakers CRV AT-10 Loudspeakers CRV AT-15 Three-way Speaker CRV AT-15 Three-way Speaker CRV SW-12B Subwoofer System CRV SW-12B Subwoofer System DA PS-6a Point Source Speaker System DA PS-6a Point Source Speaker System DA PS-8c Point Source Speaker Sytem DA PS-8c Point Source Speaker Sytem DA PS-9 Point Source Speaker System DA PS-9 Point Source Speaker System GMI PVX-31 Single Ch 31/3rd Octave Bands GMI PVX-31 Single Ch 31/3rd Octave Bands GMI XL-1800QII Prof Manual DJ Turntable GMI XL-1800QII Prof Manual DJ Turntable GMI XL-BD10 Semi-Auto Belt-Dr Turntable GMI XL-BD10 Semi-Auto Belt-Dr Turntable GMI XL-DD20 Semi-Automatic Turntable GMI XL-DD20 Semi-Automatic Turntable
......
100 rows selected.100 rows selected.
83
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Using DISTINCTUsing DISTINCT
SELECT DISTINCT(manfuacturer_code)SELECT DISTINCT(manfuacturer_code)
FROM productsFROM products
Result:Result: The non-duplicated manufacturer_codes will be The non-duplicated manufacturer_codes will be retrieved.retrieved.
84
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
select distinct(manufacturer_code) from products;select distinct(manufacturer_code) from products;
MAN MAN --- --- AIW AIW BOS BOS CRV CRV DA DA GMI GMI HVC HVC JBL JBL JVC JVC MIT MIT PAN PAN PIN PIN PIO PIO SAM SAM SHE SHE SON SON TEA TEA TEC TEC THN THN YAM YAM
19 rows selected.19 rows selected.
85
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento