Chapter 8A Relational Set Operators UNION UNION

Chapter 8A Relational Set Operators UNION UNION

Chapter 8a Relational Set Operators Objectives: to learn Outline • Based on relational algebra/ set operations • the relational set • Relational Set operators Operators • Work properly if relations are union-compatible – UNION, • SEQUENCE Objects – Names of relation attributes must be the same and – UNION ALL, • SQL Built-in Functions their data types must be identical – INTERSECT, and – Numeric • SQL-’99 ANSI Standard commands – MINUS – String – UNION • How to create and use – Date Sequence objects as – Misc – INTERSECT an ‘autonumber’ – Pseudo Columns – MINUS (called ‘EXCEPT’ in the 99 standards) • SQL functions to • Some DMS’s may not implement these. manipulate dates, strings, and other data Fall 2010 - CS275 1 Fall 2010 - CS275 2 UNION UNION ALL • Combines tables eliminating duplicate rows. • Combines tables retaining duplicate rows. • Can be used to unite more than two queries SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE CUS_AREACODE, CUS_PHONE FROM CUSTOMER FROM CUSTOMER UNION ALL UNION SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, SELECT CUS_LNAME, CUS_FNAME,CUS_INITIAL, CUS_AREACODE, CUS_PHONE CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2; FROM CUSTOMER_2; Fall 2010 - CS275 3 Fall 2010 - CS275 4 1 INTERSECT INTERSECT • Combines rows from two queries, returning only SELECT CUS_CODE the rows that appear in both sets. FROM CUSTOMER WHERE CUS_AREACODE = ‘615’ INTERSECT SELECT DISTINCT CUS_CODE • Example: FROM CUSTOMER; SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, FROM CUSTOMER INTERSECT SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2; Fall 2010 - CS275 5 Fall 2010 - CS275 6 Syntax Alternatives - Intersect MINUS • Generalized Syntax: • Combines rows from two queries, returning only Select <columns> the rows that appear in the first set but not in the from t1 where t1.fieldA in (select t2.fieldA from t2); second • Example: solving the previous problem: – Example: Select cus_code SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, from customer CUS_AREACODE, FROM CUSTOMER where cus_areacode= ‘615’ and MINUS cus-code in (select distinct cus_code from SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, Invoice) CUS_AREACODE, FROM CUSTOMER_2 Fall 2010 - CS275 8 Fall 2010 - CS275 7 2 MINUS Syntax Alternatives -Minus Example reversing the tables: SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, • Generalized Syntax CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2 Select <columns> from t1 w here t1.key not in (select MINUS SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, t2.key from t2); CUS_AREACODE, CUS_PHONE FROM CUSTOMER • Example starting with previous problem, but using another table(non-union compatiable): Select cus_code from customer where cus_areacode= ‘615’ and cus_code not in (select distinct cus_code from Invoice); • IN and NOT IN subqueries can be used in place of INTERSECT & MINUS Fall 2010 - CS275 9 Fall 2010 - CS275 10 Oracle Sequences Oracle Sequence • Sequence Numbers: allows establishing an • Create sequence <seq_name> start with <n> “autonumber” sequence Increment by <n>; • Oracle sequences – Examples: – Independent object in the database Create sequence cust_code start with 100 – Are named and used anywhere a numeric value is increment by 1; expected Create sequence product_num NOCACHE; – Not tied to a table or column – Generates new numeric values that can be • Viewing your sequences assigned to any column in any table Select * from user_sequences; – A named sequence can be created and deleted at any time Fall 2010 - CS275 11 Fall 2010 - CS275 12 3 Oracle Sequence Oracle Sequence Create sequence cus_code_seq start with 20010 • Sequence numbers are used via a keyword nocache; – NEXTVAL Create sequence inv_number_seq start with 4010 • used to generate a new number • Often used as a primary key for a table row nocache; • Example: Insert into table invoice values (inv_sequence.NEXTVAL, 20010, sysdate); – CURRVAL • Used to retrieve the current value of the sequence. • Cannot directly retrieve attributes from a sequence. • Example: Select inv_sequence.CURRVAL from dual; Fall 2010 - CS275 13 Fall 2010 - CS275 14 Oracle Sequences SQL (non-aggregate) Functions inserting a series of rows with auto sequence • Functions manipulate data items and return a numbers. Insert into customer values ( single result . ( i.e. SQL functions are similar to cus_code_seq.nextval, functions in programming languages.) ‘Connery’, ‘Sean’, NULL, ‘615’, ‘898-2007’, 0.00); • Oracle has more than 150 different functions. (See Insert into Invoice values ( link on our resource page to the Oracle inv_number_seq.nextval, 20010, sysdate); documentation ) Insert into Line values (inv_number_seq.currval, 1, • Functions use numerical, date, or string values, ’13-Q2/P2’, 1, 14.99); which may be part of a command or a table Insert into line values( attribute. inv_number_seq.currval, 2, ‘23109-HB’, 1, 9.95); • The Function may appear anywhere in an SQL statement Fall 2010 - CS275 15 Fall 2010 - CS275 16 4 Numeric Functions SQL Functions - Numeric • Grouped in different ABS(n) -Absolute value of n ways CEIL(n) -Smallest integer greater or equal to n – Algebraic, FLOOR(n) -Largest integer equal to or less than n trigonometric, MOD(m,n) -Remainder of m divided by n logarithmic, etc. POWER(m,n) -m raised to the nth power • Do not confuse with ROUND(n,[m]) -n rounded to m decimal places. aggregate functions SIGN(n) -If n<0 returns -1, if n= 0 returns 0, if – Aggregate functions n>0 returns 1 operate over sets SQRT(n) -Square root of n, if n<0 returns NULL – Numeric functions TRUNC(c[m]) -n truncated to m decimal places operate over single row Fall 2010 - CS275 17 Fall 2010 - CS275 18 String Functions SQL Functions - String • The most used ASCII(str) – Returns ASCII value of first character in str functions are the CHR(n) – Character with ASCII value n String functions CONCAT(str1,str2) – Returns the combined data items. • Examples: Upper/Lower INITCAP(str) – Capitalizes the first letter of each word Substr INSTR(str1,str2[,n[m]]) – Returns the position of the mth occurrence Length of str2, begins search at position n. || - or Concat SUBSTR(str, start, length) – Returns substring of str LENGTH(str) – Returns the length of str LOWER(str) – Forces all letters to lower case L/RPAD(str1,n[,str2]) – Left or Right pads str1 to length n with sequence of characters in str2 (if unspecified, pads with blanks) Fall 2010 - CS275 19 Fall 2010 - CS275 20 5 SQL Functions – String SQL Functions – Date & Time L/RTRIM(str,set) – Removes characters starting from the Left or Right in str up to the first character not in • All SQL-standard DBMSs support date and time set. functions REPLACE(str,’literal1’,’literal2’) • Date functions take one parameter – Search str for 1st string and replace w/2nd • Commonly used Functions: string. Similar to translate. ADD_MONTHS(d,n) – Date d plus n months SOUNDEX(str) – Returns a str value representing the sound of the word(s) in str. LAST_DAY(d) – Date of last day of month containing d SUBSTR(str,m[,n]) – Returns substring of str, beginning at MONTHS_BETWEEN(d,e) – Number of months between dates d & e character m, for length of n. TRANSLATE(str,frm,to) – str is translated from the character set frm NEW_TIME(d,a,b) – Date and time in timezone b when d is to the character set to. from timezone a. – Forces all letters to upper case. NEXT_DAY(d,char) – Date of first day of week named by char UPPER(str) => d USERENV(str) – Returns user information for writing audit trails. TRUNC(date) – Date with time of day removed. Fall 2010 - CS275 21 Fall 2010 - CS275 22 SQL Functions - Data Conversions Data Conversion Examples CHARTOROWID(char) – Converts char to a row ID TO_DATE :using character HEXTORAW(char) – Converts char, which must contain a formats to return a date. hexadecimal number to a binary value TO_CHAR: using a date and RAWTOHEX(raw) – Converts raw to char value containing a hex number. returning pieces of the date – Converts a row ID to a char value as character values. ROWIDTOCHAR(rowid) – Converts expr, a number or date value to TO_CHAR(expr[,fmt]) a char value. Format may be used to convert dates. TO_DATE(char[,fmt]) – Converts from char value to a date value. fmt specifies the format of the parameter char TO_DATE(n, fmt) – Converts a number into a date TO_NUMBER(char) – Converts a char value to a number . Fall 2010 - CS275 23 Fall 2010 - CS275 24 6 Data Conversion Examples SQL Functions - Miscellaneous TO_CHAR: converting a number to a • Misc. Functions- data type on item returned is character string variable TO_NUMBER: converting a character DECODE (expr, search1, return1, search2, return2,…[default]) string to a number If expr equals any search, returns the NVL: converts a null to a string following return; DECODE: returns associated value if not, returns default when matched or a default value DUMP (expr[, radix[,start-positions[,bytes]]]) Display the contents of internal data areas GREATEST (expr,expr,..) Returns greatest of a list of values LEAST (expr,expr,…) Returns the least of a list of values NVL (expr1,expr2) If expr1 is null, returns expr2; if expr1 is not null, returns expr1 VSIZE (expr) Returns the number of bytes in ORACLE’s internal representation of expr Fall 2010 - CS275 26 Fall 2010 - CS275 25 SQL Functions -Pseudo-Columns Chapter 8A Summary Level - 1 for root node, 2 for child - used in • Relational set operators combine output of two queries to generate new relation ..Select...Connect By • Set operations using the key words UNION, Null - Returns a null value - used in Insert UNION ALL, INTERSECT, and MINUS are part of statements for blank data the new SQL-’99 extensions. RowID - ID for each row in a table • Oracle sequences may be used to generate values RowNum - Order in which the row was extracted to be assigned to a record Sysdate - Current date and time • SQL functions are used to extract or transform UID - User ID data USER - Name of current user (Login name) • SQL has Numeric, String (Character), Date, Misc./Conversion functions, and Pseudo columns 28 Fall 2010 - CS275 27 Fall 2010 - CS275 7.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us