Sql 如何在oracle数据库中存储枚举字段类型的对象
我的程序中有以下类,我希望这个类存储在Oracle11c数据库中,我知道如何存储大部分信息,但如何存储类型为enum的usertype?对于数据模型,我有以下方案: 这是mu用户类Sql 如何在oracle数据库中存储枚举字段类型的对象,sql,oracle,enumeration,Sql,Oracle,Enumeration,我的程序中有以下类,我希望这个类存储在Oracle11c数据库中,我知道如何存储大部分信息,但如何存储类型为enum的usertype?对于数据模型,我有以下方案: 这是mu用户类 public class User { @Id protected Identity<User> id; protected String identifier; protected UserType type; protected String pass
public class User {
@Id
protected Identity<User> id;
protected String identifier;
protected UserType type;
protected String password;
public static enum UserType {
CUSTOMER, COURIER;
}
}
公共类用户{
@身份证
受保护的身份标识;
受保护字符串标识符;
受保护的用户类型;
受保护的字符串密码;
公共静态枚举用户类型{
客户、快递;
}
}
只有两个固定值,最简单的方法是:
检查(输入('CUSTOMER'、'COURIER'))
非常简单;Oracle检查为该列提供的值是否为这两个值之一。假设您希望始终设置它,notnull
确保它不能为空
insert into users (id, identifier, password, type)
values (1, 'Bob', null, 'CUSTOMER');
1 row inserted.
insert into users (id, identifier, password, type)
values (2, 'Alice', null, null);
ORA-01400: cannot insert NULL into ("SCHEMA"."USERS"."TYPE")
insert into users (id, identifier, password, type)
values (2, 'Alice', null, 'INVALID');
ORA-02290: check constraint (SCHEMA.SYS_C00113048) violated
最好使用命名约束:
create table users (
id number,
identifier varchar2(30),
password raw(64),
type varchar2(8) not null,
constraint users_pk primary key (id),
constraint users_type_ck check (type in ('CUSTOMER', 'COURIER'))
);
更一般地说,特别是对于可以更改的值,您将拥有一个查找表和一个外键关系:
create table user_types (
id number,
description varchar2(8),
constraint user_types_pk primary key (id),
constraint user_types_desc_uk unique (description)
);
create table users (
id number,
identifier varchar2(30),
password raw(64),
type_id number not null,
constraint users_pk primary key (id),
constraint users_type_fk foreign key (type_id) references user_types(id)
);
insert into user_types (id, description) values (1, 'CUSTOMER');
insert into user_types (id, description) values (2, 'COURIER');
insert into users (id, identifier, password, type_id)
values (1, 'Bob', null, 1);
1 row inserted.
insert into users (id, identifier, password, type_id)
values (2, 'Alice', null, null);
ORA-01400: cannot insert NULL into ("SCHEMA"."USERS"."TYPE_ID")
insert into users (id, identifier, password, type_id)
values (2, 'Alice', null, 3);
ORA-02291: integrity constraint (SCHEMA.USERS_TYPE_FK) violated - parent key not found
你真的是男人吗;或者希望用户表中的字符串
type
列限制为这两个值;或者您是否有另一个包含这两个值(但可能会添加其他值)的表,并且希望它们之间具有引用完整性?编辑问题以指定我的问题
create table user_types (
id number,
description varchar2(8),
constraint user_types_pk primary key (id),
constraint user_types_desc_uk unique (description)
);
create table users (
id number,
identifier varchar2(30),
password raw(64),
type_id number not null,
constraint users_pk primary key (id),
constraint users_type_fk foreign key (type_id) references user_types(id)
);
insert into user_types (id, description) values (1, 'CUSTOMER');
insert into user_types (id, description) values (2, 'COURIER');
insert into users (id, identifier, password, type_id)
values (1, 'Bob', null, 1);
1 row inserted.
insert into users (id, identifier, password, type_id)
values (2, 'Alice', null, null);
ORA-01400: cannot insert NULL into ("SCHEMA"."USERS"."TYPE_ID")
insert into users (id, identifier, password, type_id)
values (2, 'Alice', null, 3);
ORA-02291: integrity constraint (SCHEMA.USERS_TYPE_FK) violated - parent key not found