April 22nd, 2009

How do Constraints Work?, NOT NULL (Part 2 of 2)

Posted by admin in P. Table Creation

> NOT NULL is a column constraint in a CREATE TABLE statement and can not be a table constraint.

> NULL is the default column constraint and is implied if it is not specified.

> It is best if you do not allow nulls by using NOT NULL.

> If a column does not have NOT NULL specified, the column accepts nulls by default.

> If you prevent nulls with NOT NULL, you force a value to be entered and you have better data integrity.

> Nulls can not be used in a PRIMARY KEY because every primary key must be unique.

> Nulls can be in a FOREIGN KEY, but it creates referential integrity problems.

> If a column is not associated with a PRIMARY KEY and it does not have NOT NULL, and no value is given, a null will be set for that column.

> If a column is not associated with a PRIMARY KEY and it does not have NOT NULL, a null can be explicitly set for this column regardless of the data type.

> Nulls complicate the results of SELECT, INSERT, and UPDATE statements.

See IS NULL (Unknown Values).

       CREATE TABLE          PAYROLL_TBL
             (EMPL_ID        CHAR(4)      NOT NULL,
              STOR_ID        CHAR(4)      NOT NULL,
              DUE_DATE       DATE         NOT NULL,
              HRSTHIS_PAYPRD DECIMAL(4,1) NOT NULL DEFAULT,
              GROSS          DECIMAL(8,2) NOT NULL DEFAULT,
              TAX            DECIMAL(8,2) NOT NULL DEFAULT,
              CONSTRAINT     PAYROLL_PK   PRIMARY KEY (EMPL_ID, STOR_ID, DUE_DATE),
              CONSTRAINT     PYRL_EMPL_FK FOREIGN KEY (EMPL_ID)
                                          REFERENCES EMPLOYEE_TBL(EMPL_ID),
              CONSTRAINT     PYRL_STOR_FK FOREIGN KEY (STOR_ID)
                                          REFERENCES STORE_TBL(STOR_ID));
April 20th, 2009

How do Constraints Work?, NOT NULL (Part 1 of 2)

Posted by admin in P. Table Creation

Let us review: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 row.

> Nulls 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 talks about a “null value”, that is wrong because null means there is no value.

> In SQL, the keyword NULL represents a null. When setting a column to NULL, do not specify ‘NULL’ between quotes, or the value will be the literal ‘NULL’.

> Nulls don’t belong to any data type and can be inserted into any column that allows nulls.

> The capability of not allowing nulls is established by using the keywords NOT NULL in a CREATE TABLE column definition. This will be covered in the Data Definition Language topic.

> If you don’t specify a NOT NULL constraint, the column will accept nulls by default.

> One extra byte is used in the SQL file for each field that can have a null, whether the field is NULL or not.

April 17th, 2009

How do Constraints Work?

Posted by admin in P. Table Creation

Constraints allow you to write rules for the allowable values in columns and enforce the integrity of the data. As you can see, there are two types of constraints in the format of a CREATE TABLE statement:

          CREATE TABLE table_name
                (column_name1 data_type1 [col_constraint1],
                 column_name2 data_type2 [col_constraint2],
                 ...
                [CONSTRAINT constraint_name1 table_constraint1,]
                [CONSTRAINT constraint_name2 table_constraint2,]
                 ...);

A column constraint is in the column definition and sets conditions only on the values in this column.
A column constraint does not have an explicit constraint name and can not apply to more than one column.
(The DBMS system assigns a column constraint name consisting of random characters and numbers.)

A table constraint is defined separately from the column definitions and can set conditions on one or many columns in the table.

Only table constraints have constraint names, but a column constraint (one column) can be stated as a table constraint and given a name. This allows you to ALTER or DROP the constraint by name.
Many company standards only allow named table constraints, not column constraints, so that the constraint can be identified in warning and error messages.
The exception is NOT NULL, which is nearly always an unnamed column constraint.

A primary key can be defined as a column constraint (if it is one column) or a table constraint.

« Previous PageNext Page »