May 22nd, 2009

How do Constraints Work?, CHECK (Part 2 of 2)

Posted by admin in P. Table Creation

The format is:

          [CONSTRAINT table_constraint_name]
            CHECK (condition)

The CHECK can be defined as a column constraint:

          CREATE TABLE      EMPLOYEE_TBL (
                 ...
                 SEX        CHAR(1)   CHECK (IN ('M', 'F')),
                 ... );

The CHECK can be defined as a named table constraint:

          CREATE TABLE EMPLOYEE_TBL (
                 ...
                 SEX        CHAR(1)    NOT NULL,
                 ...
                 CONSTRAINT SEX_CHK
                      CHECK (SEX IN ('M', 'F')),
                 ... );

                                            or

          CREATE TABLE EMPLOYEE_TBL (
                 ...
                 BADGE      CHAR(4) NOT NULL,
                 ...
                 CONSTRAINT BADGE_CHK
                      CHECK (
                      (SUBSTRING(BADGE FROM 1 FOR 1) = 'E')
                      AND
                      (CAST(SUBSTRING(BADGE FROM 2 FOR 3)
                      AS INTEGER(BETWEEN 0 AND 999))
                 ...        );

 

> If the CHECK condition is violated, the INSERT or UPDATE will not happen and an error condition will be returned.

> The SQL standard says that CHECK can not be used with CURRENT_DATETIME and CURRENT_USER.
Access, Postgre SQL, and SQL Server can CHECK these values.

> MySQL only handles CHECK constraints, but it can not handle named CHECK constraints.

May 20th, 2009

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

Posted by admin in P. Table Creation

A CHECK constraint makes sure that a column or columns have limited values.

> A table can have zero, one, or many CHECK constraints. It uses the reserved word CHECK 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, CHECK is placed in the column definition.

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

> A CHECK column constraint works just like a WHERE clause with BETWEEN, IN, IS NULL, or LIKE.
It can allow only specific values, minimum or maximum values, or a range of values.
If you apply a CHECK constraint after the table has been populated with rows, you must ensure that the values in the table fall within the CHECK constraint values.
If the CHECK constraint is in place before the table is populated, the constraint will ensure proper values.

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

May 18th, 2009

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

Posted by admin in P. Table Creation

The format is:

          [CONSTRAINT table_constraint_name]
            UNIQUE (unique_columns)

The UNIQUE can be defined as a column constraint:

          CREATE TABLE      EMPLOYEE_TBL (
                 ...
                 BADGE_ID   CHAR(4)   NOT NULL UNIQUE,
                 ...        );

The UNIQUE can be defined as a named table constraint:

          CREATE TABLE EMPLOYEE_TBL (
                 ...
                 BADGE_ID   CHAR(4)   NOT NULL,
                 ...
                 CONSTRAINT EMPLOYEE_UNQ1
                     UNIQUE (BADGE_ID),
                 ... );

                                               or

          CREATE TABLE      SAMPLE_TBL (
                 SAMPLE1    CHAR(4)   NOT NULL,
                 SAMPLE2    CHAR(4)   NOT NULL,
                 ...
                 CONSTRAINT SAMPLE_UNQ2
                     UNIQUE (SAMPLE1, SAMPLE2)
                 ... );
Next Page »

Store

Start your SQL Training now by ordering the Computer Based Training SQL Course
for $24.95.

Purchase Software For
$24.95