Top Banner
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL
47

Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Jan 09, 2016

Download

Documents

tahlia

Chapter 3 Selected Single-Row Functions and Advanced DML & DDL. Chapter Objectives. Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character strings Extract a substring using the SUBSTR function - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Chapter 3Selected Single-Row

Functionsand Advanced DML & DDL

Page 2: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Chapter Objectives

• Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character strings

• Extract a substring using the SUBSTR function

• Determine the length of a character string using the LENGTH function

Page 3: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Chapter Objectives

• Use the LPAD and RPAD functions to pad a string to a desired width

• Use the LTRIM and RTRIM functions to remove specific characters strings

• Round and truncate numeric data using the ROUND and TRUNC functions

• Calculate the number of months between two dates using the MONTHS_BETWEEN function

Page 4: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Chapter Objectives

• Identify and correct problems associated with calculations involving null values using the NVL function

• Display dates and numbers in a specific format with the TO_CHAR function

• Determine the current date setting using the SYSDATE keyword

• Nest functions inside other functions• Identify when to use the DUAL table

Page 5: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Terminology

• Function – predefined block of code that accepts arguments

• Single-row Function – returns one row of results for each record processed

• Multiple-row Function – returns one result per group of data processed

Page 6: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Types of Functions

Page 7: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Case Conversion Functions

Alter the case of data stored in a column or character string

Page 8: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

LOWER Function

Used to convert characters to lower-case letters

Page 9: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

UPPER Function

Used to convert characters to upper-case letters

Page 10: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

INITCAP Function

Used to convert characters to mixed-case

Page 11: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Character Manipulation Functions

Manipulates data by extracting substrings, counting number of characters, replacing strings, etc.

Page 12: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

SUBSTR Function

Used to return a substring, or portion of a string

Page 13: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

LENGTH Function

Used to determine the number of characters in a string

Page 14: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

LPAD and RPAD Functions

Used to pad, or fill in, a character string to a fixed width

Page 15: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

LTRIM and RTRIM Functions

Used to remove a specific string of characters

Page 16: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

REPLACE Function

Substitutes a string with another specified string

Page 17: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

CONCAT Function

Used to concatenate two character strings

Page 18: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Number Functions

Allows for manipulation of numeric data

Page 19: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

ROUND Function

Used to round numeric columns to a stated precision

Page 20: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

TRUNC Function

Used to truncate a numeric value to a specific position

Page 21: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Date Functions

Used to perform date calculations or format date values

Page 22: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

MONTHS_BETWEEN Function

Determines the number of months between two dates

Page 23: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

ADD_MONTHS Function

Adds a specified number of months to a date

Page 24: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

NEXT_DAY Function

Determines the next occurrence of a specified day of the week after a given date

Page 25: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

TO_DATE Function

Converts various date formats to the internal format (DD-MON-YYYY) used by Oracle9i

Page 26: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Format Model Elements - Dates

Page 27: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

NVL Function

Substitutes a value for a NULL value

Page 28: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

NVL2 Function

Allows different actions based on whether a value is NULL

Page 29: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

TO_CHAR Function

Converts dates and numbers to a formatted character string

Page 30: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Format Model Elements – Time and Number

Page 31: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Other Functions

• NVL

• NVL2

• TO_CHAR

• DECODE

• SOUNDEX

Page 32: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

DECODE Function

Determines action based upon values in a list

Page 33: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

SOUNDEX Function

References phonetic representation of words

Page 34: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Nesting Functions

• One function is used as an argument inside another function

• Must include all arguments for each function • Inner function is resolved first, then outer

function

Page 35: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Summary of functions

Single-Row Functions• Text Functions

lpad, rpad, lower, upper, initcap, length, substr, instr, trim, concat

• Arithmetic Functionsabs, round, ceil, floor, mod, sign, sqrt, trunc, vsize

• List Functionsgreatest, least, decode

• Date Functionsadd_months, last_day, months_between, new_time, next_day, round, trunc

• Conversion Functionsto_char, to_number, to_date

Page 36: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

DUAL Table

• Dummy table• Consists of one column and one row• Can be used for table reference in the FROM

clause

Ex: select sysdate from dual

Page 37: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Advanced Data Selection in Oracle

Using a Subquery

Multiple-Row Subqueries

SELECT ename, job, salFROM empWHERE sal = (SELECT MIN(sal)

FROM emp);

SELECT empno, ename, job, salFROM empWHERE sal < ANY (SELECT sa FROM emp WHERE job = 'SALESMAN')AND job <> 'SALESMAN';

multiple-row comparison operators – IN, ANY,ALL

Page 38: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Manipulating Oracle Data

Inserting Rows with Null Values and Special Values

Copying Rows from Another Table

INSERT INTO emp (empno, ename, hiredate, job, sal,comm, mgr, deptno) VALUES (113,'Louis', SYSDATE, 'MANAGER', 6900,NULL, NULL, 30);

INSERT INTO sales_reps(id, name, salary, com)SELECT empno, ename, sal, commFROM empWHERE job LIKE '%SALES%';

Page 39: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Creating and Managing Database Objects (Tables)

Creating a Table by Using a Subquery

CREATE TABLE dept30AS

SELECT empno, ename, sal*12 ANNSAL, hiredate

FROM empWHERE deptno = 30;

Page 40: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Creating and Managing Database Objects (Tables)

Common Datatypes

Datatype Description

VARCHAR2(size)

Variable-length character data, can up to 4,000 bytes.

CHAR(size) Fixed-length character data, can up to 2,000 bytes.

NUMBER(p.s) Variable-length numeric data, can up to 38 digits. E.g. Number(5.2) 999.99

DATE Date and time values, 7 bytes.

Other data types included: LONG, CLOB, RAW, LONG RAW, BLOB, BFILE, ROWID … etc.

Page 41: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Creating and Managing Database Objects (Tables)

The Drop Table Statement

DROP TABLE table;

Dropping a Table

DROP TABLE dept30;

Page 42: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Other Database Objects (Views, Sequences) Views

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view[(alias[, alias]...)]AS subquery[WITH CHECK OPTION [CONSTRAINT constraint]][WITH READ ONLY [CONSTRAINT constraint]];

DROP VIEW view;

To restrict data accessTo make complex queries easyTo provide data independenceTo present different views of the same dataYou can retrieve data from a view as from a table.

Page 43: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Other Database Objects (Views, Sequences)

Modifying a ViewCREATE OR REPLACE VIEW empv30

(id_no, name, salary)AS SELECT empno, ename, sal

FROM empWHERE deptno = 30;

Drop a View

DROP VIEW empv30

Creating a ViewCREATE VIEW empv30AS SELECT empno, ename, sal

FROM empWHERE deptno = 30;

Page 44: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Other Database Objects (Views, Sequences)

Sequences

CREATE SEQUENCE sequence[INCREMENT BY n][START WITH n][{MAXVALUE n | NOMAXVALUE}][{MINVALUE n | NOMINVALUE}][{CYCLE | NOCYCLE}][{CACHE n | NOCACHE}];

Automatically generates unique numbersIs typically used to create a primary keyReplaces application code

Page 45: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Other Database Objects (Views, Sequences)

Creating a Sequence

NEXTVAL and CURRVAL Pseudocolumns

CREATE SEQUENCE deptid_seqINCREMENT BY 10START WITH 5MAXVALUE 9999;

NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different usersCURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value

Page 46: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Other Database Objects (Views, Sequences)

Using a Sequence

View the current value

Removing a Sequence

INSERT INTO dept(deptno, dname, loc)VALUES (deptid_seq.NEXTVAL,'Support',

' HONG KONG' );

SELECT deptid_seq.CURRVALFROM dual;

DROP SEQUENCE deptid_seq;

Page 47: Chapter 3 Selected Single-Row Functions and Advanced DML & DDL

Appendix B: Useful link

• Try these with SQL

http://www.cse.cuhk.edu.hk/~csc3170/tutorial/index.html• http://db00.cse.cuhk.edu.hk• http://www.db.cs.ucdavis.edu/teaching/sqltutorial• http://www.w3schools.com/sql