如何使用 SQL 分析 Oracle 数据库列值以了解列中存储的值类型

发布于 2024-10-18 22:57:17 字数 116 浏览 2 评论 0原文

我有一个要求,我必须分析/检查 Oracle 数据库表中的特定列是否仅包含字符(城市等列中不允许包含数字)。类似地,我必须检查列是否可以包含字母数字值,但不能包含特殊字符。如何使用 oracle sql 来实现这一点?

I have an requirement where i have to profile / check that a paricular column in an oracle database table contains only characters or not (numbers are not allowed in a column like city etc). Similary i have to check that a column can contain alpha numeric values but not special characters. How can this be accomplished using oracle sql.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぇ气 2024-10-25 22:57:17

如果您使用的是 10g+ 版本,您可以尝试使用 regexp_instr 函数并搜索所需 posix 字符类的出现。

示例:

create table test1(onlyletters varchar2(100))
/
insert into test1
values('a')
/
insert into test1
values('b')
/
insert into test1
values('a1')
/
insert into test1
values('22')
/

select *
from  test1
where REGEXP_INSTR(onlyletters, '[[:digit:]]') = 0

这应该仅返回“onlyletters”列没有数字的行,在本例中是那些具有值“a”和“b”的行。

如果您想查找包含数字的内容,请搜索 REGEXP_INSTR(...) > 0.

我不确定特殊字符是什么意思,但是您可以通过这种方式使用不同的posix字符类,我用它来检测带有“REGEXP_INSTR(onlyletters, '[[:cntrl:]]'的控制字符)”。

有关字符类的更多信息,请参见:http://www.regular-expressions.info/posixbrackets.html

If you are on version 10g+ you could try with the regexp_instr function and search for occurrences of the desired posix character classes.

Example:

create table test1(onlyletters varchar2(100))
/
insert into test1
values('a')
/
insert into test1
values('b')
/
insert into test1
values('a1')
/
insert into test1
values('22')
/

select *
from  test1
where REGEXP_INSTR(onlyletters, '[[:digit:]]') = 0

This should then return only the rows where the column 'onlyletters' have no digits, in this case those with values 'a' and 'b'.

If you want to find those that contains digits, search for REGEXP_INSTR(...) > 0.

I'm not sure what you mean by special characters, but you can use different posix character classes in this way, I've used it to detect control characters with "REGEXP_INSTR(onlyletters, '[[:cntrl:]]')".

More info about the character classes here: http://www.regular-expressions.info/posixbrackets.html

め可乐爱微笑 2024-10-25 22:57:17

这是使用 TRANSLATE 的示例。您可能需要对您希望允许的字符和您不允许的字符进行一些调整,但这会给您一个想法:

SQL> create table cities
  2  ( name varchar2(30)
  3  , constraint city_name_alphanumeric
  4    check (translate(upper(name),'1ABCDEFGHIJKLMNOPQRSTUVWXYZ','1') is null)
  5  )
  6  /

Table created.

SQL> insert into cities values ('Utrecht')
  2  /

1 row created.

SQL> insert into cities values ('Utr3cht')
  2  /
insert into cities values ('Utr3cht')
*
ERROR at line 1:
ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated


SQL> insert into cities values ('UtrΘcht')
  2  /
insert into cities values ('UtrΘcht')
*
ERROR at line 1:
ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated


SQL> insert into cities values ('|Utrecht')
  2  /
insert into cities values ('|Utrecht')
*
ERROR at line 1:
ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated

这是一个使用正则表达式的字符:

SQL> delete cities
  2  /

1 row deleted.

SQL> alter table cities
  2    drop constraint city_name_alphanumeric
  3  /

Table altered.

SQL> alter table cities
  2    add constraint city_name_alphanumeric
  3    check (regexp_like(name,'^[[:alpha:]]+

问候,
抢。

)) 4 / Table altered. SQL> insert into cities values ('Utrecht') 2 / 1 row created. SQL> insert into cities values ('Utr3cht') 2 / insert into cities values ('Utr3cht') * ERROR at line 1: ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated SQL> insert into cities values ('UtrΘcht') 2 / insert into cities values ('UtrΘcht') * ERROR at line 1: ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated SQL> insert into cities values ('|Utrecht') 2 / insert into cities values ('|Utrecht') * ERROR at line 1: ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated

问候,
抢。

Here is an example using TRANSLATE. You might have to tweak a little with the characters you wish to allow and which you you don't, but it'll give you an idea:

SQL> create table cities
  2  ( name varchar2(30)
  3  , constraint city_name_alphanumeric
  4    check (translate(upper(name),'1ABCDEFGHIJKLMNOPQRSTUVWXYZ','1') is null)
  5  )
  6  /

Table created.

SQL> insert into cities values ('Utrecht')
  2  /

1 row created.

SQL> insert into cities values ('Utr3cht')
  2  /
insert into cities values ('Utr3cht')
*
ERROR at line 1:
ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated


SQL> insert into cities values ('UtrΘcht')
  2  /
insert into cities values ('UtrΘcht')
*
ERROR at line 1:
ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated


SQL> insert into cities values ('|Utrecht')
  2  /
insert into cities values ('|Utrecht')
*
ERROR at line 1:
ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated

And here is one using regular expressions:

SQL> delete cities
  2  /

1 row deleted.

SQL> alter table cities
  2    drop constraint city_name_alphanumeric
  3  /

Table altered.

SQL> alter table cities
  2    add constraint city_name_alphanumeric
  3    check (regexp_like(name,'^[[:alpha:]]+

Regards,
Rob.

)) 4 / Table altered. SQL> insert into cities values ('Utrecht') 2 / 1 row created. SQL> insert into cities values ('Utr3cht') 2 / insert into cities values ('Utr3cht') * ERROR at line 1: ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated SQL> insert into cities values ('UtrΘcht') 2 / insert into cities values ('UtrΘcht') * ERROR at line 1: ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated SQL> insert into cities values ('|Utrecht') 2 / insert into cities values ('|Utrecht') * ERROR at line 1: ORA-02290: check constraint (RWIJK.CITY_NAME_ALPHANUMERIC) violated

Regards,
Rob.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文