2012-03-20 90 views
0

我有了这个代码(只是节选)字符改变...约束下使用数字

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

我正在尝试做的是使ippp_code最多的5个字符或中的最小值4个字符。所以你只能输入4或5个字符。

当我运行代码,我得到一个错误信息说...

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.

我想我可以用我设置使用字符改变约束?也可以在代码上进行更正吗?

回答

2
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 
) 

您试图检查ippp_code的值是在4和5之间,而不是长度。

+0

感谢您的答复香港小轮。当我运行你的代码,我得到这个错误.... – 2012-03-20 03:01:57

+0

错误:函数len(字符变化)不存在 提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。 **********错误********** 错误:函数len(字符变化)不存在 SQL状态:42883 提示:没有函数与给定名称匹配和参数类型。您可能需要添加显式类型转换。 – 2012-03-20 03:02:39

+0

另外我刚刚意识到在我的其他代码中,我一直在检查值是在某个数字之间,而不是长度。感谢那。 所以我需要使用len(或length)关键字来检查字符或整数的长度吗? – 2012-03-20 03:05:53

1

试试这个:

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

谢谢sql_mommy,你的代码工作。只需要在“varchar2”中使用“2”即可。 – 2012-03-20 03:17:09

+0

所以我想用长度关键字检查字符的长度? – 2012-03-20 03:18:15

+0

是 - 确切地说。你没有指定你的数据库,所以我只是在我的,这是Oracle。在SQLServer中,数据类型将是varchar,在Oracle中它将是varchar2,而其他DB将使用不同的措辞。同样,每个数据库中的长度函数也是不同的。 – user158017 2012-03-21 00:45:12