5

I am trying to create a Basic pay (BP) table with

CREATE TABLE bp (
   bpid       VARCHAR(5), 
      FOREIGN KEY (bpid) REFERENCES designation(desigid), 
   upperlimit DECIMAL(10,2) NOT NULL, 
   lowerlimit DECIMAL(10,2) NOT NULL, 
   increment  DECIMAL(10,2) NOT NULL 
      CONSTRAINT llvalid CHECK (upperlimit > lowerlimit)
 );

As you can see near the ending, I want to check if upperlimit is greater than lowerlimit, how can I do that?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Unknown
  • 71
  • 1
  • 1
  • 4

3 Answers3

6

It might (probably does) depend on the data base you use.

Comparing to the oracle syntax (e.g. here: http://www.techonthenet.com/oracle/check.php), what you are missing might be a ',' between NULL and CONSTRAINT

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
1

Here's proper the SQL query...

CREATE TABLE bp (bpid VARCHAR(5),
FOREIGN KEY (bpid) REFERENCES designation(desigid), 
upperlimit DECIMAL(10,2) NOT NULL,
lowerlimit DECIMAL(10,2) NOT NULL,
increment DECIMAL(10,2) NOT NULL,
CONSTRAINT llvalid CHECK (upperlimit > lowerlimit));

Note the comma after NOT NULL and CONSTRAINT in the last line.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Unknown
  • 71
  • 1
  • 1
  • 4
1

The problem is that you have defined it as a column level constraint but it references other columns. You must define a constraint at the table level.

ALTER TABLE bp
    ADD CONSTRAINT CK_limit CHECK ( upperlimit > lowerlimit)
MikeW
  • 5,702
  • 1
  • 35
  • 43