Sql 如何检查检查约束中的大写和小写是否相等?

Sql 如何检查检查约束中的大写和小写是否相等?,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我有一个限制: ALTER TABLE account ADD CONSTRAINT chk_account_type CHECK (Type IN ('saving', 'credit', 'HOME LOAN', 'PERSONAL LOAN', 'TERM DEPOSIT', 'CHECK', 'iSaver', 'SHARE' ) ); 当我尝试插入 INSERT INTO Account VALUES ('012878', 123456, 22345678, 'Credit', -1

我有一个限制:

ALTER TABLE account
ADD CONSTRAINT chk_account_type CHECK (Type IN ('saving', 'credit', 'HOME LOAN', 'PERSONAL LOAN', 'TERM DEPOSIT', 'CHECK', 'iSaver', 'SHARE' ) );
当我尝试插入

INSERT INTO Account VALUES ('012878', 123456, 22345678, 'Credit', -1534.52);
它不起作用。因为信用是从大写字母开始的。
如何在某种程度上设计它我如何接受保存、保存、保存。

您可以在检查时更改案例:

ALTER TABLE account
ADD CONSTRAINT chk_account_type CHECK (lower(Type) IN ('saving', 'credit',
  'home loan', 'personal loan', 'term deposit', 'check', 'isaver', 'share' ));
但允许变化似乎相当奇怪,通过检查约束强制执行这些限制也是如此。将帐户类型放在一个单独的表中,使用主键,并将类型列作为主键的外键,这将更正常、更灵活。然后,您可以通过添加到该表来添加新的帐户类型,而不必修改检查约束,并且您可以在必要时允许从帐户类型表进行不区分大小写的查找,但始终一致地显示它们

比如:

create table account_types(account_type_id number, description varchar2(30),
  constraint account_type_pk primary key (account_type_id));

insert into account_types (account_type_id, description) values (1, 'Saving');
insert into account_types (account_type_id, description) values (2, 'Credit');
insert into account_types (account_type_id, description) values (3, 'Home loan');
...

create table account(account_number number, account_type_id number,
  -- other columns...
  constraint account_pk primary key (account_number),
  constraint account_fk_account_type foreign key (account_type_id)
    references account_types(account_type_id));
然后创建“储蓄”帐户:

insert into account values (123, 1);

1 rows inserted.
如果您的值无效,则会得到:

insert into account values (124, 42);

SQL Error: ORA-02291: integrity constraint (MYSCHEMA.ACCOUNT_FK_ACCOUNT_TYPE) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"

您可以通过在检查时更改案例来执行此操作:

ALTER TABLE account
ADD CONSTRAINT chk_account_type CHECK (lower(Type) IN ('saving', 'credit',
  'home loan', 'personal loan', 'term deposit', 'check', 'isaver', 'share' ));
但允许变化似乎相当奇怪,通过检查约束强制执行这些限制也是如此。将帐户类型放在一个单独的表中,使用主键,并将类型列作为主键的外键,这将更正常、更灵活。然后,您可以通过添加到该表来添加新的帐户类型,而不必修改检查约束,并且您可以在必要时允许从帐户类型表进行不区分大小写的查找,但始终一致地显示它们

比如:

create table account_types(account_type_id number, description varchar2(30),
  constraint account_type_pk primary key (account_type_id));

insert into account_types (account_type_id, description) values (1, 'Saving');
insert into account_types (account_type_id, description) values (2, 'Credit');
insert into account_types (account_type_id, description) values (3, 'Home loan');
...

create table account(account_number number, account_type_id number,
  -- other columns...
  constraint account_pk primary key (account_number),
  constraint account_fk_account_type foreign key (account_type_id)
    references account_types(account_type_id));
然后创建“储蓄”帐户:

insert into account values (123, 1);

1 rows inserted.
如果您的值无效,则会得到:

insert into account values (124, 42);

SQL Error: ORA-02291: integrity constraint (MYSCHEMA.ACCOUNT_FK_ACCOUNT_TYPE) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"

为什么要为相同的值接受不同的案例?当然,它们应该是一致的?为什么要为相同的值接受不同的情况?它们肯定应该是一致的吗?