May 15th, 2009

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

Posted by admin in P. Table Creation

The UNIQUE constraint makes sure that a column or columns have no duplicate values.> A table can have zero, one, or many UNIQUE constraints. It uses the reserved word UNIQUE in the CREATE TABLE statement.

> A simple constraint (one column) can be defined as a column constraint or a table constraint. As a simple column constraint, UNIQUE is placed in the column definition.

> A composite constraint (multiple columns) can only be defined as a table constraint. As a table constraint, the UNIQUE definition has the column names specified.

> A UNIQUE column can forbid nulls with NOT NULL.

> General practice is to use a CONSTRAINT clause to explicitly name all constraints. This gives a constraint name to make maintenance easier.

May 13th, 2009

How do Constraints Work?, FOREIGN KEY (Part 5 of 5)

Posted by admin in P. Table Creation

You can define the actions performed by SQL when you UPDATE or DELETE a key value in the parent table. This ON UPDATE or ON DELETE clause is placed in the FOREIGN KEY constraint.

          [CONSTRAINT  column_constraint_name]
            REFERENCES parent_table[(parent_columns)]
            [ON UPDATE {NO ACTION | CASCADE | RESTRICT | SET NULL | SET DEFAULT}]
            [ON DELETE {NO ACTION | CASCADE | RESTRICT | SET NULL | SET DEFAULT}]

                                                                              or

          [CONSTRAINT  table_constraint_name]
            FOREIGN KEY (key_columns)
            REFERENCES parent_table[(parent_columns)]
            [ON UPDATE {NO ACTION | CASCADE | RESTRICT | SET NULL | SET DEFAULT}]
            [ON DELETE {NO ACTION | CASCADE | RESTRICT | SET NULL | SET DEFAULT}]
ON UPDATE:
NO ACTION   Generates an error on a foreign key violation. This is the default.
CASCADE     Updates the dependent foreign key value to the parent key value.
RESTRICT    Does not allow the dependent foreign key to change value.
SET NULL    Sets the dependent foreign key values to null.
SET DEFAULT Sets the dependent foreign key values to their default values.
ON DELETE:
NO ACTION   Generates an error on a foreign key violation. This is the default.
CASCADE     Deletes the rows that contain foreign key values that match the deleted parent key value.
RESTRICT    Does not allow the dependent foreign key to change value.
SET NULL    Sets the dependent foreign key values to null.
SET DEFAULT Sets the dependent foreign key values to their default values.
May 11th, 2009

How do Constraints Work?, FOREIGN KEY (Part 4 of 5)

Posted by admin in P. Table Creation

The format is:

          [CONSTRAINT  column_constraint_name]
            REFERENCES parent_table[(parent_columns)]

or

          [CONSTRAINT    table_constraint_name]
            FOREIGN KEY  (key_columns)
              REFERENCES parent_table[(parent_columns)]

You can omit the parent_column if the parent_column is the primary key of the parent_table. However, this is not recommended.
The foreign key can be defined as a column constraint:

          CREATE TABLE     PAYROLL_TBL (
                 EMPL_ID   CHAR(4) NOT NULL
                           REFERENCES EMPLOYEE_TBL(EMPL_ID),
                 ...       );

The foreign key can be defined as a named table constraint:

          CREATE TABLE PAYROLL_TBL (
                 EMPL_ID        CHAR(4) NOT NULL,
                 ...
                 CONSTRAINT     PAYROLL_FK1
                    FOREIGN KEY (EMPL_ID)
                     REFERENCES EMPLOYEE_TBL(EMPL_ID),
                 ...            );

                                                            or

          CREATE TABLE SAMPLE_TBL (
                 SAMPLE1_ID     CHAR(4) NOT NULL,
                 SAMPLE2_ID     CHAR(4) NOT NULL,
                 ...
                 CONSTRAINT     SAMPLE_FK
                    FOREIGN KEY (SAMPLE1_ID, SAMPLE2_ID)
                     REFERENCES PARENT_TBL(PARENT1_ID, PARENT2_ID),
                 ...            );
« Previous PageNext Page »