MUCH ADO ABOUT NOTHING Walter Schenk SoluTech Consulting Services Inc.

Slides:



Advertisements
Similar presentations
Oracle 10g & 11g for Dev Virtual Columns DML error logging
Advertisements

Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
SAGE Computing Services Customised Oracle Training Workshops and Consulting Are you making the most of PL/SQL? Hints and tricks and things you may have.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
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.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
SQL (DDL & DML Commands)
Program with PL/SQL. Interacting with the Oracle Server.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Application Development using PL/SQL Programming.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Advanced SQL: Triggers & Assertions
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
1 Writing Control Structures Part D. 2 PL/SQL Decision Control Structures Sequential processing Sequential processing Processes statements one after another.
Oracle Data Integrator User Functions, Variables and Advanced Mappings
Declaring PL/SQL Variables
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
SQL. Originally developed by IBM Standardized in 80’s by ANSI and ISO Language to access relational database and English-like non-procedural Predominant.
Aggregator Stage : Definition : Aggregator classifies data rows from a single input link into groups and calculates totals or other aggregate functions.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
 CONACT UC:  Magnific training   
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Program with PL/SQL Lesson 4.
Relational Database Design
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
SQL Creating and Managing Tables
Interacting with the Oracle Server
SQL PL/SQL Presented by: Dr. Samir Tartir
Database Management Systems 2
Aggregating Data Using Group Functions
Writing Correlated Subqueries
(SQL) Aggregating Data Using Group Functions
SQL Creating and Managing Tables
Working with Composite Datatypes
Python Primer 2: Functions and Control Flow
SQL Creating and Managing Tables
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
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
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Database Programming Using Oracle 11g
Aggregating Data Using Group Functions
Presentation transcript:

MUCH ADO ABOUT NOTHING Walter Schenk SoluTech Consulting Services Inc

AGENDA What it is and what is not NULL in functions, expressions, comparisons and conditional control NULL in Indexes NULL in programming languages

WHAT A NULL IS NOT! A NULL is NOT 0! A NULL is NOT Nothing! A NULL is NOT an empty string (although Oracle does treat it as such but that may change in the future!)

THEN WHAT IS A NULL? When a column in a row has no value The value is not known or meaningful

WHY DO WE NEED NULL VALUES? Often real-world information is incomplete It is a way of handling this unknown

NULL IN FUNCTIONS “Normal” scalar functions will return NULL when given a NULL argument All aggregate functions except COUNT(*) and GROUPING ignore nulls. You can use the NVL in the argument to an aggregate function to substitute a value for a null. If a query with an aggregate function returns no rows or only rows with nulls for the argument to the aggregate function, the aggregate function returns null.

FUNCTIONS THAT HANDLE NULLS – NVL – CONCAT – REPLACE – NULLIF (9i) – COALESCE (9i) – DECODE

NVL If expr1 is null, returns expr2; if expr1 is not null, returns expr1. Can be any datatype NVL ( expr1, expr2 )

CONCAT Returns char1 concatenated with char2. CONCAT ( char1, char2 ) SELECT CONCAT(‘job’,NULL) "Job" FROM DUAL; Job job

REPLACE Returns char with every occurrence of search_string replaced with replacement_string. If replacement_string is omitted or null, all occurrences of search_string are removed. If search_string is null, char is returned. REPLACE ( char, search_string, replacement_string)

REPLACE SELECT REPLACE(‘Hello there’,’l’,NULL) "Changes" FROM DUAL; Changes Heo there SELECT REPLACE(‘Hello there’,NULL,’xxx’) "Changes" FROM DUAL; Changes Hello there

NULLIF If the values match, then the result is NULL A := NULLIF(B,C);

COALESCE COALESCE returns the first non-null expr in the expression list. At least one expr must not be the literal NULL. If all occurrences of expr evaluate to null, then the function returns null. COALESCE (expr1, expr2,..., expr n)

DECODE NULL = NULL !!!! DECODE (deptno, 10, ’ACCOUNTING’, 20, ’RESEARCH’, 30, ’SALES’, NULL, ’OPERATION’, ’NONE’)

NULLS AND COMPARISONS Only compare NULLs with IS NULL or IS NOT NULL Use of any other operator and the result will be NULL! NULL <> NULL (except in the DECODE expression and compound keys)

NULLS IN CONDITIONS Always use: variable IS NULL Never user: variable = NULL

NULLS IN LOGICAL EXPRESSIONS

HANDLING NULLS Avoid common mistakes by keeping the following in mind: – Comparisons involving NULLs always yield NULL – Applying the logical operator NOT to a NULL yields NULL – In conditional control statements, if the condition yields NULL, its associated sequence of statements is NOT executed!

NULL AND CONDITIONAL CONTROL MOST COMMON MISTAKE: In conditional control statements, if the condition yields NULL, its associated sequence of statements is NOT executed!

NULL AND CONDITIONAL CONTROL IF x > y THEN high := x; ELSE high := y; END IF; IF NOT x > y THEN high := Y; ELSE high := x; END IF;

PROGRAMMING GUIDELINE Always account for NULL in applications even if the underlying database objects are defined as NOT NULL.

NULL AND INDEX ENTRIES Oracle does NOT enter an index value if the ENTIRE key is NULL Consequences: – An index can NOT be used in a search criteria for NULL values – A UNIQUE constraint on a column that can be NULL will allow multiple NULL values

NULLS IN PROGRAMMING LANGUAGES PL/SQL C/C++ VB.NET and C#.NET

PL/SQL Full support of NULL

C/C++ C/C++ does NOT support NULL Variables are passed on to a C/C++ application through host variables for both input as well as output Host variables are prefixed with a colon (“:”) to set them apart from Oracle objects

C/C++ Any host variable can be associated with an indicator variable An indicator variable is a short integer variable that indicates the condition of its host variable

C/C++ host variable on Input If indicator variable = –1 then the variable is a NULL and Oracle ignores the value of the host variable If indicator variable >=0 Oracle will assign the value of the host variable to the column

C/C++ host variable on Output If indicator variable = -1 then the column is NULL and the value of the host variable is indeterminate If indicator variable = 0 then value of the host variable is assigned

C/C++ EXEC SQL SELECT SAL, COMM INTO :salary,:commission:ind_com FROM EMP WHERE EMPNO = :emp_number; if (ind_comm == -1) pay = salary else pay=salary + commission;

C/C++ Set ind_comm = -1; EXEC SQL INSERT INTO emp (empno,comm) VALUES (:emp_number,:commision:ind_comm);

VB In VB6 only Variant data types could support NULL The NULL keyword indicated that a variable contained the NULL value The IsNull function was used to test for NULL

VB.NET During a migration from VB6 to VB.NET: – Null is converted to DBNull – IsNull is converted to IsDBNull – The Variant data type is converted to Object In VB6 Null could be used in functions and assignments; DBNull cannot! Consider using the Nothing keyword in.NET instead of Null.

VB.NET IsDBNull function Returns TRUE if the expression evaluates to the DBNull type; otherwise returns FALSE The System.DBNull value indicates that the object represents missing or nonexistent data It is NOT the same as Nothing which indicates that a variable has not yet been initialized

VB.NET DBNull class The DBNull class is used to indicate the absence of a known value The class differentiates between a null value and an uninitialized value

PROGRAMMING GUIDELINE Do not circumvent the use of NULLs by assigning “meaningless” or “out-of-range” values Example: a column “EndDate” is often assigned a far fetched date in the future to avoid use of NULL

SQL STANDARDS AND NULLS FIPS (1993) The following features have "preliminary" syntax and semantics available in Working Draft form as part of an on-going ANSI and ISO/IEC standardization effort for further development of the SQL language. Features specified in preliminary form include: 17. Multiple null states. A facility that allows user definitions for an arbitrary number of application specific Null values, such as "Unknown", "Missing", "Not Applicable", "Pending", etc. Each such Null value would have a different representation in the database so that they could be distinguished during retrieval or update.

SQL STANDARDS AND NULLS FIPS (1995) If an SQL/ERI Server implementation at the Minimal SDL level or below chooses not to provide support for null values (see item 4 of Section 4.1), then it may raise an implementation-defined exception in any SQL statement that attempts to process null values. If an SQL/ERI Server implementation at the Minimal SDL level or below chooses not to provide support for null values (see item 4 of Section 4.1), then it shall provide an implementation-defined conversion of would-be null values in Information Schema tables to an appropriate non-null value. If an SQL/ERI Server implementation at the Minimal SDL level or below chooses not to provide support for null values (see item 4 of Section 4.1), then it may raise an implementation- defined exception in any SQL statement that attempts to process null values.

SQL STANDARDS AND NULLS The concept of NULL is subject to change! Various implementations may vary.

DEPARTING WORDS Never ignore NULL Use NULL properly

QUESTIONS? NoCOUG, February 20, 2003