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

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Copyright س Oracle Corporation, All rights reserved. 13 Other Database Objects.
Using Single-Row Functions to Customize Output
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
3-1 Copyright  Oracle Corporation, All rights reserved. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
3 Single-Row Functions. 3-2 Objectives At the end of this lesson, you should be able to: Describe various types of functions available in SQL Use character,
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
13 Other Database Objects Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
Objectives After completing this lesson, you should be able to do the following: Categorize the main database objects Review the table structure List.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Chapter 5 Sequences.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
SQL (DDL & DML Commands)
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1 (SQL) Other Database Objects Asif Sohail University of the.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
Copyright س Oracle Corporation, All rights reserved. 3 Single-Row Functions.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 第三讲 Single-Row Functions. Objectives After completing this lesson, you should be able to do the following: Describe various types of functions available.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
Using Single-Row Functions to Customize Output
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
3 Copyright © 2007, Oracle. All rights reserved. Substitution Variables ra Oly l&On nase lce Int erU.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Defining a Column Alias
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
SQL 101 3rd Session.
Using Single-Row Functions to Customize Output
Chapter 5 Sequences.
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

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

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

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

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

Types of Functions

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

LOWER Function Used to convert characters to lower-case letters

UPPER Function Used to convert characters to upper-case letters

INITCAP Function Used to convert characters to mixed-case

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

SUBSTR Function Used to return a substring, or portion of a string

LENGTH Function Used to determine the number of characters in a string

LPAD and RPAD Functions Used to pad, or fill in, a character string to a fixed width

LTRIM and RTRIM Functions Used to remove a specific string of characters

REPLACE Function Substitutes a string with another specified string

CONCAT Function Used to concatenate two character strings

Number Functions Allows for manipulation of numeric data

ROUND Function Used to round numeric columns to a stated precision

TRUNC Function Used to truncate a numeric value to a specific position

Date Functions Used to perform date calculations or format date values

MONTHS _ BETWEEN Function Determines the number of months between two dates

ADD _ MONTHS Function Adds a specified number of months to a date

NEXT _ DAY Function Determines the next occurrence of a specified day of the week after a given date

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

Format Model Elements - Dates

NVL Function Substitutes a value for a NULL value

NVL2 Function Allows different actions based on whether a value is NULL

TO_CHAR Function Converts dates and numbers to a formatted character string

Format Model Elements – Time and Number

Other Functions NVL NVL2 TO_CHAR DECODE SOUNDEX

DECODE Function Determines action based upon values in a list

SOUNDEX Function References phonetic representation of words

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

Summary of functions Single-Row Functions Text Functions lpad, rpad, lower, upper, initcap, length, substr, instr, trim, concat Arithmetic Functions abs, round, ceil, floor, mod, sign, sqrt, trunc, vsize List Functions greatest, least, decode Date Functions add_months, last_day, months_between, new_time, next_day, round, trunc Conversion Functions to_char, to_number, to_date

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

Advanced Data Selection in Oracle Using a Subquery Multiple-Row Subqueries SELECT ename, job, sal FROM emp WHERE sal= (SELECT MIN(sal) FROM emp); SELECT empno, ename, job, sal FROM emp WHERE sal < ANY (SELECT sa FROM emp WHERE job = 'SALESMAN') AND job <> 'SALESMAN'; multiple-row comparison operators – IN, ANY,ALL

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, comm FROM emp WHERE job LIKE '%SALES%';

Creating and Managing Database Objects (Tables) Creating a Table by Using a Subquery CREATE TABLE dept30 AS SELECTempno, ename, sal*12 ANNSAL, hiredate FROM emp WHERE deptno = 30;

Creating and Managing Database Objects (Tables) Common Datatypes DatatypeDescription VARCHAR2(siz e) 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)  DATEDate and time values, 7 bytes. Other data types included: LONG, CLOB, RAW, LONG RAW, BLOB, BFILE, ROWID … etc.

Creating and Managing Database Objects (Tables) The Drop Table Statement DROP TABLE table; Dropping a Table DROP TABLE dept30;

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 access  To make complex queries easy  To provide data independence  To present different views of the same data  You can retrieve data from a view as from a table.

Other Database Objects (Views, Sequences) Modifying a View CREATE OR REPLACE VIEW empv30 (id_no, name, salary) AS SELECT empno, ename, sal FROM emp WHERE deptno = 30; Drop a View DROP VIEW empv30 Creating a View CREATE VIEW empv30 AS SELECT empno, ename, sal FROM emp WHERE deptno = 30;

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 numbers  Is typically used to create a primary key  Replaces application code

Other Database Objects (Views, Sequences) Creating a Sequence NEXTVAL and CURRVAL Pseudocolumns CREATE SEQUENCE deptid_seq INCREMENT BY 10 START WITH 5 MAXVALUE 9999;  NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users  CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value

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.CURRVAL FROM dual; DROP SEQUENCE deptid_seq;

Appendix B: Useful link Try these with SQL html