1 Week 5 September 26 • Oracle: Control Files • Basic Data Types • SQL: Creating and Altering a Table, Imposing Integrity Constraints, DML R. Ching, Ph.D. • MIS • California State University, Sacramento SQL Terminology • ISO terminology – Relations (entity type) Tables – Attributes (properties) Columns – Tuples (entities) Rows column Row { R. Ching, Ph.D. • MIS • California State University, Sacramento 2 Oracle Control Files • Loads data into tables in batch mode • Two parts – Control language – Data R. Ching, Ph.D. • MIS • California State University, Sacramento 3 Anatomy of a Control File Using Displacement (i.e., Column Position) 4 Data contained in control file Table name load data infile * into table videos (category_code position(1:2) stock_number position(5:10) video_title position(12:57) retail_price position(59:66) running_time position(67:70) rating position(71:75) distributor_code position(77:79) year_released position(82:91) active_date position(92:101) stock_on_hand position(102:103) stock_on_order position(104:105) begindata SF 000025 Kronos DC 000036 Alice in Wonderland DC 000100 Little Mermaid, The SF 000101 Navy vs. the Night Monsters, The SF 000102 Monster Mania Column names Data char, char, char, decimal external, integer external, char, char, date(10) "mm-dd-yyyy", date(10) "mm-dd-yyyy", integer external, integer external) 19.95 22.95 26.95 19.95 9.99 78NR 75NR 82NR 90NR 60NR Data types and format BBE DIS DIS BBE MCA 01-01-195908-19-1995 01-01-195103-11-1995 01-01-198901-12-1991 01-01-196609-29-1992 01-01-199806-01-1998 412 410 317 6 0 2 6 1 2 3 4 5 6 7 8 9 10 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345 R. Ching, Ph.D. • MIS • California State University, Sacramento Anatomy of a Control File Using Displacement (i.e., Column Position) 5 Data contained in control file TableOptional name append char, load data infile * append into table videos position(1:2) load data infile * into table videos (category_code stock_number position(5:10) video_title position(12:57) retail_price position(59:66) running_time position(67:70) rating position(71:75) distributor_code position(77:79) year_released position(82:91) LOAD DATA active_date position(92:101) INFILE 'manufac.dat' stock_on_hand position(102:103) INTO TABLE manufacturers stock_on_order position(104:105) begindata SF 000025 Kronos DC 000036 Alice in Wonderland DC 000100 Little Mermaid, The SF 000101 Navy vs. the Night Monsters, The SF 000102 Monster Mania Column names Reading data from a file Data char, char, decimal external, integer external, char, char, date(10) "mm-dd-yyyy", date(10) "mm-dd-yyyy", integer external, integer external) 19.95 22.95 26.95 19.95 9.99 78NR 75NR 82NR 90NR 60NR Data types and format BBE DIS DIS BBE MCA 01-01-195908-19-1995 01-01-195103-11-1995 01-01-198901-12-1991 01-01-196609-29-1992 01-01-199806-01-1998 412 410 317 6 0 2 6 1 2 3 4 5 6 7 8 9 10 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345 R. Ching, Ph.D. • MIS • California State University, Sacramento Anatomy of a Control File Using a Delimiter 6 Data contained in control file Table name Declared delimiter Column names LOAD DATA INFILE * INTO TABLE video_categories FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' (category_code, category_title) begindata AC,Action BB,Big Book Best Sellers Data BC,British Comedy and Drama R. Ching, Ph.D. • MIS • California State University, Sacramento Basic Data Types in Oracle 7 String Data Types • Fixed length char(length) char(30) • Variable length varchar2(maximum-length) Varchar(30) • Fixed point number(precision,scale) dec(precision,scale) Where precision = total number length, scale = number of decimal places number(8,2) dec(6,2) • Integer int, smallint • Floating point number, dec, float Numeric Data Types Date Data Types • Date date Other data types exist R. Ching, Ph.D. • MIS • California State University, Sacramento Anatomy of a CREATE TABLE SQL> 2 3 4 5 6 7 8 9 10 create table computer_products (model_number varchar2(12) product_description varchar2(50) list_price dec(6,2) retail_price dec(6,2) retail_unit char(2) stock_on_hand int stock_on_order int last_shipment_received date, manufacturer_code varchar2(3)); Table created. Column name R. Ching, Ph.D. • MIS • California State University, Sacramento Data type 8 primary default default default default default default key, 'N/A', 0, 0, 'EA', 0, 0, Constraint Integrity Constraints • Imposed to protect the database from becoming inconsistent • Types: – Required data – Domain constraints – Entity constraints – Referential integrity – Enterprise constraints R. Ching, Ph.D. • MIS • California State University, Sacramento 9 Integrity Constraints • Required data – A column cannot be null (not null) • Domain constraints – Values assigned to a column must be from a defined domain SQL> 2 3 4 5 CREATE TABLE REVENUES (TRANSACTION_NUMBER ROWID PRIMARY KEY, TRANSACTION_DATE DATE NOT NULL, TRANSACTION_TYPE CHAR(1) CONSTRAINT CK_TRANS_TYPE CHECK (TRANSACTION_TYPE IN('R','S','E','A','X'))); Table created. R. Ching, Ph.D. • MIS • California State University, Sacramento 10 Domain Constraint • Using BETWEEN SQL> 2 3 4 5 6 create table limousines (fleet_number varchar2(10) primary key, vehichle_type varchar2(15) not null, seat_capacity smallint constraint ck_seat_capacity check (seat_capacity between 1 and 12)); Table created. R. Ching, Ph.D. • MIS • California State University, Sacramento 11 Integrity Constraints • Entity constraints – Primary key must contain a unique, no null value for each row SQL> 2 3 4 CREATE TABLE VIDEO_REVENUES (TRANSACTION_NUMBER ROWID PRIMARY KEY, TRANSACTION_TYPE CHAR(1) CONSTRAINT CK_TRANS_TYPE CHECK (TRANSACTION_TYPE IN('R','S','E','A','X'))); Table created. R. Ching, Ph.D. • MIS • California State University, Sacramento 12 Entity Integrity Composite Key SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 13 create table invoice_items Neither column is unique by itself (invoice_number int, item_number int constraint ck_invoice_number check (item_number in (1,2,3,4,5,6,7,8,9,10)), product_code varchar2(10), quantity int default 1, price float not null, constraint pk_invoice_items primary key Composite key (invoice_number, item_number), constraint fk_invoice_number foreign key (invoice_number) references invoices(invoice_number), constraint fk_product_code foreign key (product_code) references products(product_code)); Table created. R. Ching, Ph.D. • MIS • California State University, Sacramento Integrity Constraints • Referential integrity – The value of a foreign key must exist in another table (i.e., parent) as at least a candidate key – Normally, when a row in the parent is deleted and a child exists, one of fours actions can be taken: • Cascade • Set null* • Set default* • No action *Supported through triggers R. Ching, Ph.D. • MIS • California State University, Sacramento 14 Integrity Constraints SQL> 2 3 4 5 6 7 8 15 CREATE TABLE REVENUES (TRANSACTION_NUMBER ROWID PRIMARY KEY, TRANSACTION_TYPE CHAR(1) CONSTRAINT CK_TRANS_TYPE CHECK (TRANSACTION_TYPE IN('R','S','E','A','X')), PRODUCT_CODE, CONSTRAINT FK_PRODUCT_CODE FOREIGN KEY (PRODUCT_CODE) REFERENCES PRODUCTS(PRODUCT_CODE) ON DELETE CASCADE); Table created. Referential integrity CONSTRAINT index-name FOREIGN KEY (column-name) REFERENCES table-name(key-name) ON DELETE CASADE R. Ching, Ph.D. • MIS • California State University, Sacramento Integrity Constraints • Enterprise constraints – Organizational constraints (i.e., business rules) R. Ching, Ph.D. • MIS • California State University, Sacramento 16 Another Example of Creating a Table with Referential Integrity Constraints Table name Column names, data types and constraints Table name Column names, data types and constraints R. Ching, Ph.D. • MIS • California State University, Sacramento 17 Example of Creating a Table with Referential Integrity Constraints 18 Two ways of imposing constraints Table name Column name Referential integrity constraints Constraint name (index) R. Ching, Ph.D. • MIS • California State University, Sacramento Example of Creating a Table with Referential Integrity Constraints 19 Two ways of imposing constraints Constraint name (index) Column name Table name R. Ching, Ph.D. • MIS • California State University, Sacramento Refers to column name within the table In Case of Error… ALTER Table • Alter table SQL command – Add a column – Modify a column – Delete a column • A column cannot be renamed – Drop the column – Add the column R. Ching, Ph.D. • MIS • California State University, Sacramento 20 Customers table defined R. Ching, Ph.D. • MIS • California State University, Sacramento 21 Adding a Column 22 ALTER TABLE table-name ADD (column-name data-type) Table name Optional constraint Data type Column name R. Ching, Ph.D. • MIS • California State University, Sacramento Modifying a Column Definition 23 ALTER TABLE table-name MODIFY (column-name data-type) Table name New definition Column name R. Ching, Ph.D. • MIS • California State University, Sacramento Deleting a Column 24 ALTER TABLE table-name DROP (column-name) Table name Column name R. Ching, Ph.D. • MIS • California State University, Sacramento Basic SQL: Data Retrieval From Relational Algebra to SQL 25 license, make(color=‘Silver’(Cars)) Columns (attributes) to retrieve (projection) SELECT column-list FROM table-name WHERE condition Select license, make from Cars where color = ‘Silver’ Table (relation) specification Row (tuple) specification (selection) R. Ching, Ph.D. • MIS • California State University, Sacramento 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 • California State University, Sacramento 26 Projection 27 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 • California State University, Sacramento 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 • California State University, Sacramento 28 Selection 29 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 • California State University, Sacramento Four Ways to Build a Selection 30 • 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 • 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 • California State University, Sacramento 31 WHERE (Condition) SELECT * 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 • California State University, Sacramento 32 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 33 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 0 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 162.47 0 194.97 0 214.47 0 0 110.47 R. Ching, Ph.D. • MIS • California State University, Sacramento 199.95 0 149.47 12 rows selected. 134.96 EA SON 0 129.97 0 149.95 0 Projection on a Selection 34 product_code, product_description ( manufacturer_code = ‘SON’ or ‘PAN’ (Products)) SELECT product_code, product_description FROM products WHERE manufacturer_code = ‘SON’ OR manufacturer_code = ‘PAN’ R. Ching, Ph.D. • MIS • 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 • California State University, Sacramento 35 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 • California State University, Sacramento 36 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 • California State University, Sacramento 37 SQL> 2 3 4 5 select product_code, product_description, product_msrp, manufacturer_code from products where manufacturer_code = 'SON' or manufacturer_code = 'PAN' 38 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 • 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 • California State University, Sacramento 39 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 • California State University, Sacramento 40 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 • California State University, Sacramento 41 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 • California State University, Sacramento 42 Pattern Match with LIKE and UPPER Function 43 • SELECT product_code, product_description FROM products WHERE UPPER(product_description) LIKE UPPER('%casset%'); Changes to upper case R. Ching, Ph.D. • MIS • California State University, Sacramento SQL> select product_code, product_description 2 from products 3 where upper(product_description) like upper('%casset%') 44 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 • 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 • California State University, Sacramento 45 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 • California State University, Sacramento 46 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 • California State University, Sacramento 47 Using DISTINCT SELECT DISTINCT(manfuacturer_code) FROM products Result: The non-duplicated manufacturer_codes will be retrieved. R. Ching, Ph.D. • MIS • California State University, Sacramento 48 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 • California State University, Sacramento 49 Equi-Join 50 R R.aj S.bj S • A resulting relation that contains tuples satisfying the predicate of equality between two attributes of the same domain from the Cartesian product of R and S R. Ching, Ph.D. • MIS • California State University, Sacramento Equi-Join SQL> 2 3 4 51 select product_code, p.manufacturer_code, m.manufacturer_code from products p, manufacturers m where p.manufacturer_code = m.manufacturer_code; PRODUCT_CO ---------RS1B SM165 CC1M 100 201-IV VS-100 MAN --INF INF INF BOS BOS BOS MAN --INF INF INF BOS BOS BOS Products RS1B SM165 CC1M 100 201-IV VS-100 6 rows selected. R. Ching, Ph.D. • MIS • California State University, Sacramento Manufacturers INF INF INF BOS BOS BOS ADV Advent BOS Bose INF Infinity Outer Joins in Oracle • An outer join returns all rows that satisfy the join condition and those rows from one table for which no rows from the other satisfy the join condition. – Such rows are not returned by a simple join. – To perform an outer join of tables A and B and return all rows from A, apply the outer join operator (+) to all columns of B in the join condition. – For all rows in A that have no matching rows in B, a NULL is returned for any select list expressions containing columns of B. R. Ching, Ph.D. • MIS • California State University, Sacramento 52 Left Outer Join 53 • All rows on the left table (i.e., products) are kept SQL> 2 3 4 select product_code, product_description, p.manufacturer_code, m.manufacturer_code from products p, manufacturers m where p.manufacturer_code = m.manufacturer_code(+); PRODUCT_CO ---------100 201-IV VS-100 RS1B SM165 CC1M PRODUCT_DESCRIPTION ----------------------------------Compact Speakers Direct/Reflecting Speakers Center Channel Mini Speaker 2-way Bookshelf Speakers Bookshelf-sized Speakers Center Channel Speaker 6 rows selected. R. Ching, Ph.D. • MIS • California State University, Sacramento MAN --BOS BOS BOS INF INF INF MAN --BOS BOS BOS INF INF INF Right Outer Join 54 • All rows on the right table (i.e., manufacturers) are kept SQL> 2 3 4 select product_code, product_description, p.manufacturer_code, m.manufacturer_code from products p, manufacturers m where p.manufacturer_code(+) = m.manufacturer_code; PRODUCT_CO PRODUCT_DESCRIPTION MAN MAN ---------- ----------------------------------- --- --ADV 100 Compact Speakers BOS BOS 201-IV Direct/Reflecting Speakers BOS BOS VS-100 Center Channel Mini Speaker BOS BOS RS1B 2-way Bookshelf Speakers INF INF SM165 Bookshelf-sized Speakers INF INF CC1M Center Channel Speaker INF INF 7 rows selected. R. Ching, Ph.D. • MIS • California State University, Sacramento Cartesian Product 55 SQL> select * from products, manufacturers; RS1B SM165 CC1M 100 201-IV VS-100 RS1B SM165 CC1M 100 201-IV VS-100 RS1B SM165 CC1M 100 201-IV VS-100 2-way Bookshelf Speakers Bookshelf-sized Speakers Center Channel Speaker Compact Speakers Direct/Reflecting Speakers Center Channel Mini Speaker 2-way Bookshelf Speakers Bookshelf-sized Speakers Center Channel Speaker Compact Speakers Direct/Reflecting Speakers Center Channel Mini Speaker 2-way Bookshelf Speakers Bookshelf-sized Speakers Center Channel Speaker Compact Speakers Direct/Reflecting Speakers Center Channel Mini Speaker 18 rows selected. R. Ching, Ph.D. • MIS • California State University, Sacramento INF 188 PR ADV Advent INF 205 PR ADV Advent INF 164 EA ADV Advent BOS 205 PR ADV Advent BOS 205 PR ADV Advent BOS 116.96 EA ADV Advent INF 188 PR INF Infinity INF 205 PR INF Infinity INF 164 EA INF Infinity BOS 205 PR INF Infinity BOS 205 PR INF Infinity BOS 116.96 EA INF Infinity INF 188 PR BOS Bose INF 205 PR BOS Bose INF 164 EA BOS Bose BOS 205 PR BOS Bose BOS 205 PR BOS Bose BOS 116.96 EA BOS Bose Cartesian Product 56 SQL> select * from products, manufacturers; RS1B 2-way Bookshelf Speakers INF 188 PR ADV Advent SM165 Bookshelf-sized Speakers INF 205 PR ADV Advent I J rows (tuples) , N + M columns (attributes) CC1M Center Channel Speaker INF 164 EA ADV Advent 100 Compact Speakers BOS 205 PR ADV Advent 201-IV Products Direct/Reflecting Speakers BOS 205 PR ADV Advent VS-100 RS1B Center 2-way Channel Mini Speaker BOS 116.96 Bookshelf Speakers INF EA ADV 188 Advent PR RS1B SM1652-way Bookshelf Speakers INF 188 Bookshelf-sized Speakers INF PR INF 205 Infinity PR SM165 CC1M Bookshelf-sized Speakers INF 205 PR INF Infinity Center Channel Speaker INF 164 EA CC1M 100 Center Compact Channel Speakers Speaker INF 164 EA INF BOS 205 Infinity PR 100 Compact Speakers BOS 205 PR INF Infinity 201-IV Direct/Reflecting Speakers BOS 205 PR 201-IV VS-100 Direct/Reflecting Speakers BOS 205 PR INF Center Channel Mini Speaker BOS 116.96 Infinity EA VS-100 Center Channel Mini Speaker BOS 116.96 EA INF Infinity RS1B 2-way Bookshelf Speakers INF 188 PR BOS Bose Manufacturers SM165 Bookshelf-sized Speakers INF 205 PR BOS Bose ADV Advent CC1M Center Channel Speaker INF 164 EA BOS Bose INF Infinity 3 x 6 = 18 100 Compact Speakers BOS 205rows PR BOS Bose 201-IV BOS Bose Direct/Reflecting Speakers BOS 205 PR BOS Bose VS-100 Center Channel Mini Speaker BOS 116.96 EA BOS Bose R. Ching, Ph.D. • MIS • California State University, Sacramento 18 rows selected. Union, Intersection, Set Difference attribute-1, ..., attribute-n(R) { | |-}attribute-1, ..., attribute-n(S) SELECT colunn-name1, ..., column-namen FROM table1 {UNION | INTERSECT | MINUS} SELECT column-name1, ..., column-namen FROM table2 Columns must correspond to one another R RS S R. Ching, Ph.D. • MIS • California State University, Sacramento 57 Example Tables 58 TEAC_200_products; W-410C W-525R V-370 V-390CHX EQA-3 PD-555 PD-C400 Double Cassette Deck Auto-Reverse Double Cassette Cassette Deck Cassette Deck Stereo 10-Band Equalizer CD Player Remote CD Player TEA TEA TEA TEA TEA TEA TEA 89.96 170.96 63.86 89.96 62.96 116.96 152.96 EA EA EA EA EA EA EA TEA TEA TEA 170.96 EA 224.96 EA 152.96 EA 7 Rows TEAC_150_250_products W-525R W-585R PD-C400 Auto-Reverse Double Cassette Double Cassette Deck Remote CD Player 3 Rows R. Ching, Ph.D. • MIS • California State University, Sacramento Union 59 attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) SQL> select * from teac_200_products 2 union 3 select * from teac_150_250_products; EQA-3 PD-555 PD-C400 V-370 V-390CHX W-410C W-525R W-585R Stereo 10-Band Equalizer CD Player Remote CD Player Cassette Deck Cassette Deck Double Cassette Deck Auto-Reverse Double Cassette Double Cassette Deck 8 rows selected. R. Ching, Ph.D. • MIS • California State University, Sacramento TEA TEA TEA TEA TEA TEA TEA TEA 62.96 116.96 152.96 63.86 89.96 89.96 170.96 224.96 EA EA EA EA EA EA EA EA Intersection 60 attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) SQL> select * from teac_200_products 2 intersect 3 select * from teac_150_250_products; PD-C400 W-525R Remote CD Player Auto-Reverse Double Cassette R. Ching, Ph.D. • MIS • California State University, Sacramento TEA TEA 152.96 EA 170.96 EA Set Difference attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) • Performed on two union compatible tables (i.e., same columns) • Displays the rows unique to one of the two tables (i.e., found in one but not the other) – The order of the tables matters! R. Ching, Ph.D. • MIS • California State University, Sacramento 61 Set Difference 62 attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) SQL> select * from teac_200_products 2 minus 3 select * from teac_150_250_products; EQA-3 PD-555 V-370 V-390CHX W-410C Stereo 10-Band Equalizer CD Player Cassette Deck Cassette Deck Double Cassette Deck TEA TEA TEA TEA TEA 62.96 116.96 63.86 89.96 89.96 EA EA EA EA EA Why? SQL> select * from teac_150_250_products 2 minus 3 select * from teac_200_products; W-585R Double Cassette Deck R. Ching, Ph.D. • MIS • California State University, Sacramento TEA 224.96 EA Set Difference 63 attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) SQL> select * from teac_200_products TEAC_200_products; 2 minus W-410C 3 select * from teac_150_250_products; W-525R V-370 EQA-3 Stereo 10-Band Equalizer TEA 62.96 EA 7 Rows V-390CHX116.96 PD-555 CD Player TEA EA EQA-3 V-370 Cassette Deck TEA 63.86 EA PD-555 89.96 EA V-390CHX Cassette Deck TEA Duplicates PD-C400 89.96 W-410C Double Cassette Deck TEA EA These rows are unique to TEAC_200_products R. Ching, Ph.D. • MIS • California State University, Sacramento TEAC_150_250_products W-525R W-585R PD-C400 3 Rows Set Difference 64 attribute-1, ..., attribute-n(R) attribute-1, ..., attribute-n(S) SQL> select * from teac_150_250_productsTEAC_150_250_products 2 minus W-525R 3 select * from teac_200_products; W-585R 3 Rows PD-C400 W-585R Double Cassette Deck TEA 224.96 EA TEAC_200_products; This rows is unique to TEAC_150_250_products R. Ching, Ph.D. • MIS • California State University, Sacramento W-410C W-525R V-370 V-390CHX EQA-3 PD-555 PD-C400 Duplicates 7 Rows Calculation and Format Models (Masks) 65 Concatenation SQL> 2 3 4 5 6 7 8 9 10 select product_code, Alias manufacturer_name || ' - ' || product_description as "Description", number_on_hand as "Stock on Hand", to_char(product_retail_price,'$9,990.00') as "Selling Price", to_char(number_on_hand * product_retail_price,'$9,990.00') as "Inventory Value" from products p, manufacturers m where p.manufacturer_code = m.manufacturer_code and product_retail_price < 100; Format model Converts numeric or date data type to character (required for a format mask) R. Ching, Ph.D. • MIS • California State University, Sacramento Calculation and Format Models (Masks) (Results) PRODUCT_CO ---------XL-BD10 V-370 V-390CHX W-410C SH-8017 SL-BD20 EQA-3 CD-1000C 66 Description Stock on Hand Selling Pr Inventory --------------------------------------------- ------------Gemini - Semi-Auto Belt-Dr Turntable 1 $80.96 $80.96 Teac - Cassette Deck 1 $63.86 $63.86 Teac - Cassette Deck 1 $89.96 $89.96 Teac - Double Cassette Deck 1 $89.96 $89.96 Technics - Graphic Equalizer 1 $62.96 $62.96 Technics - Belt-Drive Semi-Auto Turntabl 1 $89.96 $89.96 Teac - Stereo 10-Band Equalizer 1 $62.96 $62.96 Sherwood - Compact Disc Changer 1 $89.96 $89.96 8 rows selected. (Edited to fit the slide) R. Ching, Ph.D. • MIS • California State University, Sacramento Applicable Columns 67 SQL> describe products; Name Null? --------------------------------------- -------PRODUCT_CODE NOT NULL PRODUCT_DESCRIPTION PRODUCT_COST PRODUCT_MSRP PRODUCT_RETAIL_PRICE RETAIL_UNIT MANUFACTURER_CODE ACTIVE_DATE NUMBER_ON_HAND NUMBER_ON_ORDER NUMBER_COMMITTED INACTIVE_DATE Type ------------VARCHAR2(10) VARCHAR2(35) NUMBER(8,2) NUMBER(8,2) NUMBER(8,2) CHAR(2) CHAR(3) DATE NUMBER(6) NUMBER(6) NUMBER(6) DATE SQL> describe manufacturers; Name --------------------------------------MANUFACTURER_CODE MANUFACTURER_NAME Type ------------CHAR(3) VARCHAR2(30) R. Ching, Ph.D. • MIS • California State University, Sacramento Null? -------NOT NULL NOT NULL SQL> 2 3 4 5 6 7 8 select manufacturer_name, sum(number_on_hand), max(product_retail_price),min(product_retail_price), avg(product_retail_price),count(product_code) from products p, manufacturers m where p.manufacturer_code = m.manufacturer_code group by manufacturer_name having sum(number_on_hand) > 25 order by manufacturer_name; 68 MANUFACTURER_NAME SUM(NUMBER_ON_HAND) MAX(PRODUCT_RETAIL_PRICE) -------------------- ------------------- ------------------------MIN(PRODUCT_RETAIL_PRICE) AVG(PRODUCT_RETAIL_PRICE) COUNT(PRODUCT_CODE) ------------------------- ------------------------- ------------------JVC 30 1266 116.96 417.31867 30 Polk 26 135 Sony 1614 427.07692 68 116.96 Technics 2474 561.34426 32 62.96 26 68 629.96 200.97625 Do not follow this query too closely for your homework assignment R. Ching, Ph.D. • MIS • California State University, Sacramento 32 SQL> 2 3 4 5 6 7 8 9 10 11 12 select manufacturer_name manufacturer, to_char(sum(number_on_hand),'9,990')"Total on Hand", to_char(max(product_retail_price),'$9,990.00') "Max Price", 69 to_char(min(product_retail_price),'$9,990.00') "Min Price", to_char(avg(product_retail_price),'$9,990.00') "Average Price", to_char(count(product_code),'9,990') "Total Products" from products p, manufacturers m where p.manufacturer_code = m.manufacturer_code group by manufacturer_name having sum(number_on_hand) > 25 order by manufacturer_name; MANUFACTURER Total Max Price Min Price Average Pr Total ----------------- ------ ---------- ---------- ---------- -----JVC 30 $1,266.00 $116.96 $417.32 30 Polk 26 $1,614.00 $135.00 $427.08 26 Sony 68 $2,474.00 $116.96 $561.34 68 Technics 32 $629.96 $62.96 $200.98 32 Output edited to fit slide Do not follow this query too closely for your homework assignment R. Ching, Ph.D. • MIS • California State University, Sacramento 70 R. Ching, Ph.D. • MIS • California State University, Sacramento