1 Week 4 September 19 • Relational Data Model • Views R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Relational Model 2 • Objectives – A degree of data independence – Address data semantic, consistency and redundancy problems – Set-oriented data manipulation language • Structured Query Language (SQL) Database Criteria Data Set R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Presentation method Information Data Set Presentation method 3 Information Criteria Data Set Presentation method Information Database Data Set R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Presentation method Information Domain = all values an attribute can assume 4 Entity Tuples (rows) • Cardinalitiy = number of tuples Relation Attributes (columns) • Degree of a relation = number of attributes R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Domain of an Attribute 5 • Set of allowable values for one or more attributes Attribute 1 Domain Attribute 2 Union or Intersection Information R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Domain Properties of Relations • Distinct (i.e., unique) relation name • Each cell contains exactly one atomic (single) value – No repeating groups • Distinct attribute name • The values of an attribute come from the same domain • Order of attributes has no significance • Each tuple is distinct (i.e., unique) – No duplicate tuples • Order of tuples has no significance R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 6 Unique Identification of a Relation Relation key Superkey Candidate key Primary key Foreign key ? R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 7 Identifying a Tuple • Superkey An attribute or a set of attributes that uniquely identifies a tuple within a relation • Candidate key A super key such that no proper subset is a superkey within the relation – Uniquely identifies the tuple (uniqueness) – Contains no unique subset (irreducibility) • Primary key The candidate key that is selected to identify tuples uniquely within a relation – Should remain constant over the life of the tuple – Most efficient way of identifying a tuple R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 8 Finding the Primary Key Super Key Candidate Key Primary key R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 9 Keys 10 CDs Relation 129341 Help! Beatles Columbia 1-29150-8384-0 129342 Hard Day’s Night Beatles Columbia 1-29150-7115-0 129343 Sergeant Pepper’s Beatles Columbia 1-29150-2484-0 129344 Magical Mystery Tour Beatles Columbia 1-29150-7515-0 129345 Abbey Road Beatles Apple 1-15700-9510-0 Attributes • Catalog number • Record title • Artist name • Record label • UPC R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Superkey? Candidate key? Primary key? Selecting a Key • Criteria – An efficient way of identifying an entity – The attribute (value) remains constant over the life of the entity • Never changes R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 11 Identifying a Tuple 12 • Foreign key An attribute or set of attributes within one relation that matches the candidate key of some (possibly the same) relation Relation key foreign key Relation key R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Foreign Key 13 CDs Relation 129341 Help! Beatles COL 1-29150-8384-0 129342 Hard Day’s Night Beatles COL 1-29150-7115-0 129343 Sergeant Pepper’s Beatles COL 1-29150-2484-0 129344 Magical Mystery Tour Beatles COL 129345 Abbey Road Beatles APP Recording Label Relation (home relation) Must match! 1-29150-7515-0 1-15700-9510-0 COL Columbia Records APP Apple Records R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Relational Integrity Constraints placed on the set of values allowed for the attributes of a relation. • Entity integrity – No attribute of a primary key can be null (every tuple must be uniquely identified) • Referential integrity – If a foreign key exists in a relation, either the foreign key value must match a candidate key value of some tuple in its home relation, or the foreign key value must be wholly null (i.e., no key exists in the home relation) • General constraints (reflect business practices) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 14 Null Value • Absence of any value (i.e., unknown or nonapplicable to a tuple) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 15 Views • A view is a virtual relation or one that does not actually exist, but dynamically derived – Can be constructed by performing operations (i.e., select, project, join, etc.) on values of existing base relations • Base relation - a named relation, corresponding to an entity in the conceptual schema, whose tuples are physically stored in the database • View - a dynamic result of one or more relational operations operating on the base relations to produce another R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 16 Schema and Subschemas 17 Internal Level Physical Database DBMS Some end-user applications can be supported by views External Subschema Level User User DBMS Software Schema Conceptual Level Subschema User R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento User Subschema User User Views 18 Base Relation R Key Base Relation S Foreign Key Key Criterion View R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Purpose of Views • Provides a powerful and flexible security mechanism by hiding parts of the database from certain users • Permits user access in a way that is customized to their needs • Simplify complex operations on the base relations • Designed to support the external model • Provides logical independence R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 19 Updating Views • Allowed on views – Derived from a single base relation, and – Containing the primary key or a candidate key • NOT allowed on views – Derived from multiple base relations – Involving aggregations (i.e., summations) or groups operations • Vendors may have other constraints on updating views R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 20 Relational Languages Relational Calculus vs. Relational Algebra • Relational calculus Nonprocedural formal relational data manipulation language in which the user specifies what data should be retrieved, but not how to retrieve it. • Relational algebra A theoretical language with operators that work on one or two relations to produce another relation. Ricardo, 1990 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 21 Relational Algebra • Fundamental operations: – Selection () Unary operations (applied to a – Projection () single relation) – Union () – Set difference – Intersection () – Cartesian product – Join ( ) – Division R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 22 Aggregating and Grouping Operations • • • • • Count Sum Average (value) Minimum (value) Maximum (value) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 23 Selection 24 • Selection operator works on a single relation and defines a relation that contains only those tuples that satisfy the specified condition (predicate) predicate(R) Predicate R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Projection 25 • Defines a relation that contains a vertical subset, extracting the values of specified attributes and eliminating duplicates. attribute-1, ..., attribute-n(R) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Projection on a Selection 26 • The selection extracts a subset of tuples from the original relation R based on the predicate. The project further reduces the number of attributes. attribute-1, ..., attribute-n(predicate(R)) Original relation R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Project on a Selection 27 license, make(color=‘Silver’(Cars)) Cars 3ABC123 4MIS345 4MIS837 4LLP394 4NOP934 4TLC394 4MSE291 5AMD042 4GAS294 Acura TL Honda Accord DX Honda Accord LX Lexus GS400 Acura CL Mercedes CLK320 BMW 525i BMW M Roadster Lexus LS430 color=‘Silver’ Silver White Black Silver MetBlue Red Teal BlueMet GoldMet Step 1 3ABC123 Acura TL 4LLP394 license, make Lexus GS400 Silver Silver Step 2 3ABC123 Acura TL R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 4LLP394 Lexus GS400 Union 28 • The union of two relations R and S with I and J tuples, receptively, is obtained by concatenating them into one relation with a maximum of (I + J) tuples, duplicate tuples being eliminated. – R and S must be union compatible (i.e., same attributes) attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) RS R S R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Union RS Set 1 3ABC123 4MIS345 4MIS837 4LLP394 4NOP934 Acura TL Silver Honda Accord DX White Honda Accord LX 3ABC123 Black Acura TL 4MIS345 Lexus GS400 Silver Honda Accord DX 4MIS837 Acura CL MetBlue Honda Accord LX 4LLP394 Lexus GS400 Common 4NOP934 Acura CL 4TLC394 Mercedes CLK320 Set 2 BMW 525i 4TLC394 Mercedes CLK320 4MSE291 Red 5AMD042 BMW M Roadster 4MSE291 BMW 525i Teal 5AMD042 BMW M Roadster 4GAS294 BlueMet Lexus LS430 4LLP394 Lexus GS400 Silver 4GAS294 Lexus LS430 GoldMet R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 29 Silver White Black Silver MetBlue Red Teal BlueMet GoldMet Set Difference 30 • Defines a relation consisting of the tuples that are in relation R, but not in S. – R and S must be union-compatible (i.e., same attributes) attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) RS R R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento S Set Difference license, model, color(Set 1) license, model, color(Set 2) 31 Set 1 3ABC123 4MIS345 4MIS837 4LLP394 4NOP934 Acura TL Honda Accord DX Honda Accord LX Lexus GS400 Acura CL Silver White Black Silver MetBlue 3ABC123 Acura TL 4MIS345 Honda Accord DX Not unique to Set 1 4MIS837 Honda Accord LX Set 2 4NOP934 Acura CL 4TLC394 Mercedes CLK320 Red 4MSE291 BMW 525i Teal 5AMD042 BMW M Roadster BlueMet RS 4LLP394 Lexus GS400 Silver Set 1 Set 2 4GAS294 Lexus LS430 GoldMet R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Silver White Black MetBlue Set Difference 32 Reversed attribute-1, ..., attribute-n(S) attribute-1, ..., attribute-n(R) RS R R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento S Set Difference license, model, color(Set 2) license, model, color(Set 1) 33 Set 1 3ABC123 4MIS345 4MIS837 4LLP394 4NOP934 Acura TL Honda Accord DX Honda Accord LX Lexus GS400 Acura CL Silver White Black Silver MetBlue 4TLC394 Mercedes CLK320 4MSE291 BMW 525i 5AMD042 BMW M Roadster Set 2 4GAS294 Lexus LS430 4TLC394 Mercedes CLK320 Red 4MSE291 BMW 525i Teal 5AMD042 BMW M Roadster BlueMet RS 4LLP394 Lexus GS400 Silver Set 1 Set 2 4GAS294 Lexus LS430 GoldMet R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Red Teal BlueMet GoldMet Intersection 34 • An intersection consists of the set of all tuples that are in both R and S. – R and S must be union-compatible (i.e., same attributes) attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) RS R S R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Intersection 35 Set 1 3ABC123 4MIS345 4MIS837 4LLP394 4NOP934 Acura TL Honda Accord DX Honda Accord LX Lexus GS400 Acura CL Silver White Black Silver MetBlue 4LLP394 Lexus GS400 Silver Set 2 4TLC394 4MSE291 5AMD042 4LLP394 4GAS294 Mercedes CLK320 BMW 525i BMW M Roadster Lexus GS400 Lexus LS430 Red Teal BlueMet Silver GoldMet R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento RS Set 1 Set 2 Cartesian Product • The Cartesian product defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) – I J tuples – N + M attributes R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 36 Cartesian Product 37 Cartesian Product Relation R Relation S a, b c, d e, f } u, v } w, x } y, z RS R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento a, b, u, v a, b, w, x a, b, y, z c, d, u ,v c, d, w, x c, d, y, z e, f, u, v e, f, w, x e, f, y, z Joins • • • • • Theta join Equi-join Natural join Outer join Semi-join 38 Selection performed on two relations, R and S, that share at least a single common attribute R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Theta-Join vs. Equi-Join R FS • Theta-join A resulting relation that contains tuples satisfying the predicate F from the Cartesian product of R and S – The predicate F is of the form R.aj S.bj where may be one of the comparison operators <, <=, >, >=, <> • Equi-join – Where is ‘=‘ R R.aj S.bj S R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 39 Theta Join 40 ( model (Cars)) Cars LS340 GS300 GS430 C320 E320 S430 325i 523i 530i cars.make < ‘MB’ ( URL(Manufacturers)) Manufacturers Lexus Lexus Lexus MB MB MB BMW BMW BMW Lexus www.lexus.com MB www.mercedesbenz.com BMW www.bmw.com URL model model URL LS340 www.lexus.com GS300 www.lexus.com GS430 www.lexus.com 325i www.bmw.com 523i www.bmw.com 530i www.bmw.com R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Equi-Join 41 ( model(Cars)) Cars LS340 GS300 GS430 C320 E320 S430 325i 523i 530i S8 cars.make=manufacturers .make( URL(Manufacturers)) Manufacturers Lexus Lexus Lexus MB MB MB BMW BMW BMW Audi Lexus MB BMW Porsche www.lexus.com www.mercedesbenz.com www.bmw.com www.porsche.com URL No match model model LS340 GS300 GS430 C320 E320 S430 325i 523i 530i R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento No match URL www.lexus.com www.lexus.com www.lexus.com www.mercedesbenz.com www.mercedesbenz.com www.mercedesbenz.com www.bmw.com www.bmw.com www.bmw.com Natural Join 42 • Equi-join of two relations R and S over all common attributes x – One occurrence of each common attribute is eliminated from the result R S R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Natural Join 43 ( model, make (Cars)) cars.make=manufacturers .make ( make, URL(Manufacturers)) Cars LS340 GS300 GS430 C320 E320 S430 325i 523i 530i Manufacturers Lexus Lexus Lexus MB MB MB BMW BMW BMW Lexus www.lexus.com MB www.mercedesbenz.com BMW www.bmw.com LS340 GS300 GS430 C320 E320 S430 325i 523i 530i Lexus Lexus Lexus MB MB MB BMW BMW BMW R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento www.lexus.com www.lexus.com www.lexus.com www.mercedesbenz.com www.mercedesbenz.com www.mercedesbenz.com www.bmw.com www.bmw.com www.bmw.com “Make” not repeated Outer Join 44 • A join in which tuples from R that do not have matching values in the common attributes of S are also included in the result relation. – Missing values in the second relation are set to null R S – Right outer join (all tuples on right kept) vs. left outer join (all tuples on the left kept) vs. full outer join (all tuples on both sides kept) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Left Outer Join model (Cars) Cars LS340 GS300 GS430 C320 E320 S430 325i 523i 530i S8 cars.make=manufacturers.make 45 URL(Manufacturers) Manufacturers Lexus Lexus Lexus MB MB MB BMW BMW BMW Audi Lexus MB BMW Porsche www.lexus.com www.mercedesbenz.com www.bmw.com www.porsche.com Match all the models in Cars to the URLs in Manufacturers LS340 GS300 GS430 C320 E320 S430 No match 325i found 523i 530i S8 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento www.lexus.com www.lexus.com www.lexus.com www.mercedesbenz.com www.mercedesbenz.com www.mercedesbenz.com www.bmw.com www.bmw.com www.bmw.com null Right Outer Join model (Cars) Cars LS340 GS300 GS430 C320 E320 S430 325i 523i 530i S8 cars.make=manufacturers.make 46 URL(Manufacturers) Manufacturers Lexus Lexus Lexus MB MB MB BMW BMW BMW Audi Lexus MB BMW Porsche www.lexus.com www.mercedesbenz.com www.bmw.com www.porsche.com Match all the URLs in Manufacturers to the models in Cars No match found LS340 GS300 GS430 C320 E320 S430 325i 523i 530i null R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento www.lexus.com www.lexus.com www.lexus.com www.mercedesbenz.com www.mercedesbenz.com www.mercedesbenz.com www.bmw.com www.bmw.com www.bmw.com www.porsche.com Division • Consists of the set of tuples from R defined over the attributes C that match the combination of every tuple in S attribute-x(R) attribute-x( predicate(S)) Select Project R T1 Project of Select T2 Occurrences in project T1 that match the pattern in T2 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 47 rno,pno(Viewing) pno( rooms=3(Property_for_Rent)) (Identify all renters who have viewed all properties with 3 rooms) Viewing Rno CR56 CR76 CR56 CR62 CR56 Pno PA14 PG4 PG4 PA14 PG36 Pno Street PA14 PL94 PG4 PG36 PG21 PG16 Date 5/24/95 4/20/95 5/26/95 5/14/95 4/28/95 Comment Too small Too remote Rno = Renter Number No dining room Area City Property_for_Rent Pcode Type Rooms Rent 6 4 3 3 5 4 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Ono Sno Bno 48 rno,pno(Viewing) pno( rooms=3(Property_for_Rent)) (Select Property_for_Rent with rooms = 3) 49 Property_for_Rent Pno Street PA14 PL94 PG4 PG36 PG21 PG16 Area City Pcode Type Rooms Rent 6 4 3 3 5 4 Ono Sno Bno ( rooms=3(Property_for_Rent) Pno Street PG4 PG36 Area City Pcode Type Rooms Rent 3 3 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Ono Sno Bno rno,pno(Viewing) pno( rooms=3(Property_for_Rent)) (Project proper number) 50 Select(Property_for_Rent) Pno Street PG4 PG36 Area City Pcode Type Rooms Rent 3 3 Ono Sno Bno pno( rooms=3(Property_for_Rent) Pno PG4 PG36 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento All properties for rent with 3 rooms. rno,pno(Viewing) pno( rooms=3(Property_for_Rent)) (Project renter number and property number) 51 Viewing Rno CR56 CR76 CR56 CR62 CR56 Pno PA14 PG4 PG4 PA14 PG36 Date 5/24/95 4/20/95 5/26/95 5/14/95 4/28/95 Comment Too small Too remote No dining room R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento rno,pno Rno CR56 CR76 CR56 CR62 CR56 Pno PA14 PG4 PG4 PA14 PG36 rno,pno(Viewing) pno( rooms=3(Property_for_Rent)) Renters who have viewed properties Rno CR56 CR76 CR56 CR62 CR56 Pno PA14 PG4 PG4 PA14 PG36 Pno PG4 PG36 All rental properties with 3 rooms Who has viewed all rental properties with 3 rooms? R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 52 rno,pno(Viewing) pno( rooms=3(Property_for_Rent)) Renters who have viewed properties Rno CR56 CR76 CR56 CR62 CR56 Pno PA14 PG4 PG4 PA14 PG36 Pno PG4 PG36 All rental properties with 3 rooms Who has viewed all rental properties with 3 rooms? Renter CR76 has viewed PG4, but not PG36 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 53 rno,pno(Viewing) pno( rooms=3(Property_for_Rent)) (Identify all renters who have viewed all properties with three rooms) Renters who have viewed properties Rno CR56 CR76 CR56 CR62 CR56 Pno PA14 PG4 PG4 PA14 PG36 { Pno PG4 PG36 All rental properties with 3 rooms Who has viewed all rental properties with 3 rooms? Rno CR56 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 54 Renter number CR56 has viewed all properties with 3 rooms Relational Calculus 55 • Specifies what is to be retrieved rather than how to retrieve it – Predicate (truth-value function arguments) – Proposition (arguments replaced with values including the predicate) {x | P(x)} Where P is the predicate For all x, if P is true (e.g., for all x if predicate (x > 100) is true) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Tuple Oriented vs. Domain Oriented Relational Calculus • Tuple oriented (entities or rows) Finding tuples for which a predicate is true – Tuple variables (ranges over a named relation) – Quantifiers: Number of • Existential (): there exists (one instance) tuples to be • Universal (): for all applied to • Domain oriented (attributes or columns) Test for membership condition – Variables that take their values from domains R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 56 57 SQL (pronounced ‘sequel’) • Projection • Selection R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Structured Query Language (SQL) • Transform-oriented language: – DDL (data definition language) – DML (data manipulation language) • Non-procedural (no logic structure required) – Specify what information is required, rather than how that information should be retrieved – Format-free (not bound to columns) – English-like (although abbreviated) • First standard database language to gain wide acceptance – ISO 1992 standards (today ISO 2003) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 58 SQL Terminology • ISO terminology – Relations (entity type) Tables – Attributes (properties) Columns – Tuples (entities) Rows column Row { R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 59 SELECT: Basic Syntax SELECT [DISTINCT | ALL | {* | [column-list [AS alias]] [, ...]} FROM table-name [alias] [, ...] [WHERE condition] [GROUP BY column-list] [HAVING condition] [ORDER BY column-list] [ ] = optional | = select one option R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 60 Examples Using Products Table Products (product_code (key), product_description, product_cost, product_MSRP, product_retail_price, retail_unit, manufacturer_code (foreign key), active_date, number_on_hand, number_on_order, number_committed) R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 61 Projection Performed on a Single Table product_code, product_description(Products) Column-list SELECT product_code, product_description FROM products Result: All rows with only the product_code and product_description columns are retrieved R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 62 SQL> select product_code, product_description from products; PRODUCT_CO PRODUCT_DESCRIPTION ---------- -----------------------------301-III Direct/Reflecting Speakers 3800 Three-way Speaker System 4312 Studio Monitors 901Classic Direct/Reflecting Spkr System AM3 Acoustimass Speaker System AM5 Acoustimass 5 Speaker System AM7 Acoustimass 7 Speaker System AT-10 Loudspeakers AT-15 Three-way Speaker CCS-350 Compact System w/CD Player CCS-450 Compact System w/CD Player CCS-550 75-watt System w/CD Changer CD-1000C Compact Disc Changer CDP-297 Compact Disc Player CDP-397 Compact Disc Player CDP-C225 Disc Jockey CD Changer . . . 100 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 63 Selection Performed on a Single Table 64 manufacturer_code = ‘SON’ (Products) SELECT * All columns FROM products WHERE manufacturer_code = ‘SON’ Proposition (predicate) Result: Only those rows with SON for their manufacturer_code are retrieved along with all columns. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Four Ways to Build a Selection 65 • Relational operator (=, <, <=, >, >=, <>) – Logical AND and OR WHERE column-name = x AND column-name = y • Specific range using BETWEEN WHERE column-name BETWEEN x AND y • Specific values using IN WHERE column-name IN (list-of-values) • Character match using LIKE and wildcards (%, _) – UPPER and LOWER functions WHERE LOWER(column-name) LIKE ‘%character-string%’ R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Four ways to build a SELECTION: 1. Relational and Logical Operators • Relational operators: = equals >, < greater than, less than <= less than or equal to >= greater than or equal to <> not equal to • Logical operators AND, OR AND evaluated before OR NOT NOT evaluated before AND and OR R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 66 WHERE (Condition) SELECT * All columns FROM products WHERE manufacturer_code = ‘SON’ OR manufacturer_code = ‘PAN’ SELECT * FROM products WHERE product_msrp >= 100 AND product_msrp <= 500 AND product_code = ‘SON’ OR product_code = ‘PAN’ R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 67 select * from products where manufacturer_code='SON'; PRODUCT_CO PRODUCT_DESCRIPTION PRODUCT_COST PRODUCT_MSRP PRODUCT_RETAIL_PRICE RE MAN ---------- ------------------------------ ------------ ------------ -------------------- -- --ACTIVE_DA NUMBER_ON_HAND NUMBER_ON_ORDER NUMBER_COMMITTED --------- -------------- --------------- ---------------CDP-297 Compact Disc Player 84.47 129.95 116.96 EA SON 25-AUG-96 4 0 0 68 CDP-397 25-AUG-96 Compact Disc Player 11 97.47 CDP-C225 25-AUG-96 Disc Jockey CD Changer 10 CDP-C325 25-AUG-96 Disc Jockey CD Changer 1 0 CDP-C425 25-AUG-96 Disc Jockey CD Changer 5 0 CDP-C525 25-AUG-96 Disc Jockey CD Changer 6 0 SL-S600 25-AUG-96 Super-Beta Video Recorder 13 TC-W490 25-AUG-96 Double Cassette Deck 6 0 149.95 134.96 EA SON 199.95 179.96 EA SON 229.95 206.96 EA SON 249.95 224.96 EA SON 299.95 269.96 EA SON 329.95 296.96 EA SON 169.95 152.96 EA SON 0 129.97 0 0 149.47 0 162.47 0 194.97 0 214.47 0 0 110.47 0 12 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 0 Projection on a Selection Performed on a Single Table 69 product_code, product_description ( manufacturer_code = ‘SON’ or ‘PAN’ (Products)) Column list Predicate (condition) SELECT product_code, product_description FROM products WHERE manufacturer_code = ‘SON’ OR manufacturer_code = ‘PAN’ Only one condition has to be true R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento SQL> select product_code, product_description from products where 2 manufacturer_code='SON' or manufacturer_code='PAN'; PRODUCT_CO ---------CDP-297 CDP-397 CDP-C225 CDP-C325 CDP-C425 CDP-C525 PV-2201 PV-4210 PV-4250 SC-T095 SC-TC430 SL-S600 TC-W490 TC-WR590 TC-WR690 TC-WR790 TC-WR875 PRODUCT_DESCRIPTION -----------------------------Compact Disc Player Compact Disc Player Disc Jockey CD Changer Disc Jockey CD Changer Disc Jockey CD Changer Disc Jockey CD Changer HQ VHS Video Cassette Recorde 4-Head VHS Video Cass Recorde HiFi VHS Video Cass Recorder Compact Stereo System Compact System w/CD Changer Super-Beta Video Recorder Double Cassette Deck Double Cassette Deck Double Cassette Deck Double Cassette Deck Double Cassette Deck 17 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 70 SQL> 2 3 4 select product_code, product_description, product_msrp from products where manufacturer_code='SON' or manufacturer_code='PAN' and product_msrp >= 100 and product_msrp <=500; PRODUCT_CO ---------CDP-297 CDP-397 CDP-C225 CDP-C325 CDP-C425 CDP-C525 PV-2201 PV-4210 PV-4250 SC-T095 SC-TC430 SL-S600 TC-W490 TC-WR590 TC-WR690 TC-WR790 TC-WR875 PRODUCT_DESCRIPTION PRODUCT_MSRP ------------------------------ -----------Compact Disc Player 129.95 Compact Disc Player 149.95 Disc Jockey CD Changer 199.95 Disc Jockey CD Changer 229.95 Disc Jockey CD Changer 249.95 Disc Jockey CD Changer 299.95 HQ VHS Video Cassette Recorde 229.95 4-Head VHS Video Cass Recorde 299.95 HiFi VHS Video Cass Recorder 349.95 Compact Stereo System 139.95 Compact System w/CD Changer 429.95 Super-Beta Video Recorder 329.95 Double Cassette Deck 169.95 Double Cassette Deck 199.95 Double Cassette Deck 249.95 Double Cassette Deck 329.95 Double Cassette Deck 429.95 17 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 71 Both conditions must be true Four ways to build a SELECTION: 2. Range Search with BETWEEN SELECT product_code, product_description, product_msrp FROM products WHERE product_msrp BETWEEN 100 and 500 Inclusive AND manufacturer_code = ‘SON’ OR manufacturer_code = ‘PAN’ R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 72 SQL> 2 3 4 5 select product_code, product_description, product_msrp, manufacturer_code from products where manufacturer_code = 'SON' or manufacturer_code = 'PAN' 73 and product_msrp between 100 and 500; PRODUCT_CO ---------CDP-297 CDP-397 CDP-C225 CDP-C325 CDP-C425 CDP-C525 PV-2201 PV-4210 PV-4250 SC-T095 SC-TC430 SL-S600 TC-W490 TC-WR590 TC-WR690 TC-WR790 TC-WR875 PRODUCT_DESCRIPTION PRODUCT_MSRP MAN ------------------------------ ------------ --Compact Disc Player 129.95 SON Compact Disc Player 149.95 SON Disc Jockey CD Changer 199.95 SON Disc Jockey CD Changer 229.95 SON Disc Jockey CD Changer 249.95 SON Disc Jockey CD Changer 299.95 SON HQ VHS Video Cassette Recorde 229.95 PAN 4-Head VHS Video Cass Recorde 299.95 PAN HiFi VHS Video Cass Recorder 349.95 PAN Compact Stereo System 139.95 PAN Compact System w/CD Changer 429.95 PAN Super-Beta Video Recorder 329.95 SON Double Cassette Deck 169.95 SON Double Cassette Deck 199.95 SON Double Cassette Deck 249.95 SON Double Cassette Deck 329.95 SON Double Cassette Deck 429.95 SON 17 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Four ways to build a SELECTION: 3. Search for Specific Values Using IN SELECT product_code, product_description FROM products WHERE manufacturer_code IN ('SON', 'PAN', 'BOS'); List of values R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 74 SQL> 2 3 select product_code, product_description from products where manufacturer_code in ('SON', 'PAN', 'BOS'); PRODUCT_CO ---------DVD-A110 DVP-S7000 DVP-S3000 DVP-S500D KV-20S40 KV-20V80 KV-27V22 KV-27V26 KV-27V36 KV-32V36 KV-35V36 KV-32XBR48 KV-35XBR48 KV-35XBR88 . . . TC-KE500S PRODUCT_DESCRIPTION ----------------------------------DVD/CD Player DVD/CD Player DVD/CD Player DVD/CD Player 20" Trinitron TV 20" Digital Comb Filter TV 27" Trinitron TV 27" Trinitron TV 27" Picture-in-Picture TV 32" 1-Tuner PIP TV 35" 1-Tuner PIP TV 32" Trinitron XBR TV 35" Trinitron XBR TV 35" Trinitron XBR TV Cassette Deck 95 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 75 Four ways to build a SELECTION: 4. Pattern Match with LIKE • Wildcard characters – % = any sequence of zero or more characters – _ (underscore) = any single character SELECT product_code, product_description FROM products WHERE product_code LIKE ‘C%’ Result: All rows with product codes beginning with C and their corresponding product_description will be retrieved. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 76 SQL> 2 select product_code, product_description from products where product_code like 'C%'; PRODUCT_CO ---------CCS-350 CCS-450 CCS-550 CD-1000C CDP-297 CDP-397 CDP-C225 CDP-C325 CDP-C425 CDP-C525 CS-13RX CS-13SX1 CS-20SX1 CT-WN70R PRODUCT_DESCRIPTION -----------------------------Compact System w/CD Player Compact System w/CD Player 75-watt System w/CD Changer Compact Disc Changer Compact Disc Player Compact Disc Player Disc Jockey CD Changer Disc Jockey CD Changer Disc Jockey CD Changer Disc Jockey CD Changer 13" Color Television 13" Stereo Monitor/Television 20" Stereo Monitor/Television 6+1 Cassette Changer 14 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 77 Pattern Match with LIKE and UPPER Function 78 • SELECT product_code, product_description FROM products WHERE UPPER(product_description) LIKE UPPER('%casset%'); Changes to upper case R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento SQL> select product_code, product_description 2 from products 3 where upper(product_description) like upper('%casset%') 79 PRODUCT_CO ---------TC-KE400S TC-KE500S CT-W606DR CT-W616DR TD-W254 TD-W354 TD-W718 RS-TR373 RS-TR575 K-903 TC-WE405 TC-WE605S ••• K-90 PRODUCT_DESCRIPTION ----------------------------------Cassette Deck Cassette Deck Double Cassette Deck Double Cassette Deck Double Auto-reverse Cassette Deck Double Auto-reverse Cassette Deck Dual Auto-reverse Rec Cassette Deck Double Auto-reverse Cassette Deck Double Auto-reverse Cassette Deck Dual Electronic Cassette Deck Dual Cassette Deck Dual Auto-reverse Cassette Deck Double Cassette Deck 42 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento Like Without Matching the Case SQL> select product_code, product_description 2 from products 3 where product_description like '%casset%'; no rows selected Cassette cassette R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 80 ORDER BY: Sorting the Results SELECT manufacturer_code, product_code, product_description FROM products ORDER BY manufacturer_code, product_code Major (sort) key Minor (sort) key R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 81 select manufacturer_code, product_code, product_description 2 from products order by manufacturer_code, product_code; MAN --AIW AIW BOS BOS BOS BOS BOS BOS CRV CRV CRV DA DA DA GMI GMI GMI GMI PRODUCT_CO ---------NSX-D2 XK-S9000 301-III 901Classic AM3 AM5 AM7 VS-100 AT-10 AT-15 SW-12B PS-6a PS-8c PS-9 PVX-31 XL-1800QII XL-BD10 XL-DD20 ... PRODUCT_DESCRIPTION -----------------------------Mini Component System Cassette Deck Direct/Reflecting Speakers Direct/Reflecting Spkr System Acoustimass Speaker System Acoustimass 5 Speaker System Acoustimass 7 Speaker System Center Channel Mini Speaker Loudspeakers Three-way Speaker Subwoofer System Point Source Speaker System Point Source Speaker Sytem Point Source Speaker System Single Ch 31/3rd Octave Bands Prof Manual DJ Turntable Semi-Auto Belt-Dr Turntable Semi-Automatic Turntable 100 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 82 Using DISTINCT SELECT DISTINCT(manfuacturer_code) FROM products Result: The non-duplicated manufacturer_codes will be retrieved. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 83 select distinct(manufacturer_code) from products; MAN --AIW BOS CRV DA GMI HVC JBL JVC MIT PAN PIN PIO SAM SHE SON TEA TEC THN YAM 19 rows selected. R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento 84 85 R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento