开发者

Create IBM DB2 CHECK for date greater or equal current date

I'm trying to create a table wit开发者_运维知识库h 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
    )
);


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜