简体   繁体   中英

Constraint on a character varying using numbers

I've got this code (just an excerpt)...

CREATE TABLE "Degree"
(
  ippp_code character varying(5) CONSTRAINT four_or_five_chars_only CHECK (ippp_code >= 4 OR ippp_code <= 5) NOT NULL
)

What i'm try do is make the ippp_code a maximum of 5 characters or a mininum of 4 characters. So you can only enter 4 or 5 characters.

When I run the code I get an error message saying...

ERROR: operator does not exist: character varying >= integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

*** Error ** *

ERROR: operator does not exist: character varying >= integer SQL state: 42883 Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

I wondering can I use the constraint I set using character varying? Also can I get a correction on the code?

CREATE TABLE "Degree" 
( 
  ippp_code character varying(5) CONSTRAINT four_or_five_chars_only CHECK (length(ippp_code) >= 4 OR length(ippp_code) <= 5) NOT NULL 
) 

You were trying to check that the value of ippp_code was between 4 and 5, not the length.

Try this:

CREATE TABLE Degree
(
  ippp_code varchar2(5) not null, 
  CONSTRAINT four_or_five_chars_only 
  CHECK (length(ippp_code) between 4 and 5)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM