tags:

views:

321

answers:

2

I'm trying to create a table with a field that has a starting date, I want to put in a check to mkae sure that a date before today cannot be entered in. This is the code i have so far for that table

CREATE TABLE client_service (
    NHS_num Varchar(10) NOT NULL,
    service_id Integer NOT NULL,
    starting_date Date NOT NULL CHECK(starting_date >= CURDATE()),
    num_weeks Integer NOT NULL CHECK(num_weeks > 0),
    client_contribution Decimal(10,2) NOT NULL CHECK(client_contribution >= 0),
    CONSTRAINT PrimaryKey PRIMARY KEY (
            NHS_num,
            service_id,
            starting_date
    )
);
+1  A: 

I think the problem is the use of the CURDATE function within the check. I ran this example:
CREATE TABLE EMP
(ID SMALLINT NOT NULL,
NAME VARCHAR(9),
DEPT SMALLINT CHECK (DEPT BETWEEN 10 AND 100),
JOB CHAR(5) CHECK (JOB IN ('Sales', 'Mgr', 'Clerk')),
HIREDATE DATE,
SALARY DECIMAL(7,2),
COMM DECIMAL(7,2),
PRIMARY KEY (ID),
CONSTRAINT YEARSAL CHECK (YEAR(HIREDATE) >= 1986 OR SALARY > 40500) )

and it worked just fine. I changed it to this:
CREATE TABLE landrews.EMP
(ID SMALLINT NOT NULL,
NAME VARCHAR(9),
DEPT SMALLINT CHECK (DEPT BETWEEN 10 AND 100),
JOB CHAR(5) CHECK (JOB IN ('Sales', 'Mgr', 'Clerk')),
HIREDATE DATE,
SALARY DECIMAL(7,2),
COMM DECIMAL(7,2),
PRIMARY KEY (ID),
CONSTRAINT YEARSAL CHECK (HIREDATE >= CURDATE() ) )

and it rejected it with an error message that includes the following: The CHECK condition uses a column function or a UDF

Leslie
then how do I get the current date?
Chris
maybe do a Before Insert trigger that checks the date?
Leslie
would CURDATE() work in a trigger? I'm not really sure what the syntax for a trigger like that would be
Chris
I've been looking in my books and on line to try and find out how you would abort the insert if the condition failed but haven't had any success...
Leslie
is a tricky one, maybe that may be best left to next level rather than the database level
Chris
See an example of an before insert trigger here: http://www.ibm.com/developerworks/data/library/techarticle/yip/0111yip.html Basically you have to throw an exception
Peter Schuetze
+2  A: 

Per the documentation for CREATE TABLE:

The search-condition cannot contain any of the following (SQLSTATE 42621):
* Subqueries
* XMLQUERY or XMLEXISTS expressions
* Dereference operations or DEREF functions where the scoped reference argument is other than the object identifier (OID) column
* CAST specifications with a SCOPE clause
* Column functions
* Functions that are not deterministic
* Functions defined to have an external action
* User-defined functions defined with either CONTAINS SQL or READS SQL DATA
* Host variables
* Parameter markers
* Special registers
* Global variables
* References to generated columns other than the identity column
* References to columns of type XML (except in a VALIDATED predicate)
* An error tolerant nested-table-expression

So, as Leslie suggested, the correct way to do this is with a BEFORE INSERT trigger.

Ian Bjorhovde