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));

Comments are closed.

Sorry, the comment form is closed at this time.