August 12th, 2008

NULL

Posted by admin in C. SELECT Statement

NULL is a marker used when
the value does not exist,
the value exists but we don’t know what the value is,
the value is out of range, or
the value is not appropriate for this column.

> Null values are shown as hyphens in the result of the SELECT statement.
> If a null is returned, it means that there is no value.
> A null is not the same as a zero (0), a blank, or an empty string (”).
> When testing for a specific value in the field, the result can be: TRUE (equal), FALSE (unequal), or UNKNOWN (null).
> If someone refers to a value as being NULL, that is incorrect because null means there is no value.
> In SQL, the keyword NULL represents a null.
> A null does not belong to any data type and can be inserted into any columns that allow a null.
> The capability of not having a null is established by using the keywords NOT NULL in a CREATE TABLE column definition. This will be covered in the DDL table definition course.
> If you do not specify a NOT NULL constraint, the column will accept a null by default.
> One extra byte is used in the SQL file for each field that can have a null value, whether the field is NULL or not.
> In most table definitions, a null is not allowed. This can help data integrity because some valid value must be inserted.
> Multiple null values within a column are not considered equal.
> Null values are considered equal with SELECT DISTINCT.
> A unique index will allow only one null entry for each column.
> Null values are considered greater than or less than non-null values in the collating sequence depending on the manufacturer’s implemented sequence of your RDBMS.
> Nulls are not included in the result of column functions.
> Nulls are handled in a special way in a batch program and will be covered in batch programming.
> Oracle treats an empty string (”) as a null. This can cause conversion problems when converting to another RDBMS. Oracle may change this convention in a future release.

August 11th, 2008

Using Constants

Posted by admin in C. SELECT Statement

Constants can be embedded within a SELECT statement. This feature can be used with an interactive SQL environment to produce simple reports. The ANSI/ISO standard specifies that SQL constants be enclosed in single quotes (‘…’). If a single quote is to be included in the constant text, it is written within the constant as two consecutive single-quote characters (‘It”s done’). Some SQL products such as Informix and SQL Server use constants enclosed in double quotes (“…”). Other SQL engines may have difficulty with the double quote character.

SELECT 'constant1', column1,
       'constant2', column2, ...
  FROM table1, table2, ...;

Notes:

> Constant values are enclosed within quotes and “nested” within column names of the
SELECT statement.

> The constant values are positional and repeated within each row that is returned.

August 8th, 2008

DISTINCT Values

Posted by admin in C. SELECT Statement

> The DISTINCT option eliminates all duplicate column values for the column names following the word DISTINCT.
> Several columns can be considered for the DISTINCT operator.
> If the DISTINCT clause contains multiple columns, each unique combination of these columns is returned.

> DISTINCT is used in the SELECT clause. UNIQUE is used in the table definition.
> The DISTINCT predicate is similar to the UNIQUE predicate, except in the way that it treats a null value.
> If all non-null values in a result table are DISTINCT, then they are also UNIQUE.
> Multiple null values are not considered DISTINCT from each other, but they are considered to be UNIQUE. See UNIQUE.

SELECT DISTINCT column1, column2, ...
  FROM          table1, table2, ...;

> If a query includes the primary key of a table in its SELECT clause, then every row of the query result table will be unique, because the primary key has a different value in each row.
> If the primary key is not included in the query, duplicate rows can occur.
> In Microsoft Access, DISTINCT can not be used inside a column function parameter.
> In MySQL, DISTINCT can be used inside a COUNT column function, but not inside a SUM or AVG.

« Previous PageNext Page »