Oracle Data Types, Java Mappings and Database Design Best Practices

A Practical Guide for Java Backend Developers

One of the biggest mistakes made during schema design is choosing inappropriate data types. A poor datatype decision can impact:

  • Storage requirements
  • Query performance
  • Index size
  • JPA mappings
  • Application memory usage
  • Future scalability

This guide provides:

✅ Complete Oracle datatype reference
✅ Minimum and maximum sizes
✅ Java mappings
✅ When to use each datatype
✅ Primary key recommendations
✅ Date and timezone best practices
✅ Column sizing recommendations
✅ Constraints and schema design guidelines


Oracle Character Data Types

Oracle TypeMin SizeMax SizeJava TypeExampleRecommended Usage
CHAR(n)1 byte2000 bytesStringCHAR(2)Fixed length values
VARCHAR2(n)1 byte4000 bytesStringVARCHAR2(100)Most text columns
NCHAR(n)1 char2000 charsStringNCHAR(20)Unicode fixed text
NVARCHAR2(n)1 char4000 charsStringNVARCHAR2(200)Unicode variable text
LONG1 byte2 GBStringLONGDeprecated

CHAR

COUNTRY_CODE CHAR(2)

Good examples:

IN
US
UK

Use when:

  • ISO country codes
  • Status flags
  • Fixed-length values

Avoid for:

  • Names
  • Addresses
  • Descriptions

VARCHAR2

FIRST_NAME VARCHAR2(100)

Recommended for:

  • Names
  • Email
  • Address
  • Status
  • URLs

Oracle Numeric Data Types

Oracle TypePrecisionScaleJava TypeExampleUsage
NUMBER38 digitsVariableBigDecimalNUMBERGeneric numeric
NUMBER(p)1-380Integer/LongNUMBER(10)IDs
NUMBER(p,s)1-380-127BigDecimalNUMBER(10,2)Money
BINARY_FLOAT32-bitFloatingFloatBINARY_FLOATScientific values
BINARY_DOUBLE64-bitFloatingDoubleBINARY_DOUBLECalculations

NUMBER Recommendations

UsageOracle TypeJava Type
AgeNUMBER(3)Integer
CountNUMBER(10)Integer
IDNUMBER(19)Long
SalaryNUMBER(12,2)BigDecimal
PercentageNUMBER(5,2)BigDecimal

Monetary Values

Bad:

double salary;

Good:

BigDecimal salary;

Database:

SALARY NUMBER(12,2)

Reason:

  • Avoid floating-point errors.
  • Required for financial systems.

Oracle Date and Time Types

Oracle TypeStorageJava TypeUsage
DATE7 bytesLocalDateTimeLegacy timestamp
TIMESTAMP11 bytesLocalDateTimeRecommended
TIMESTAMP(6)11 bytesLocalDateTimeMicroseconds
TIMESTAMP WITH TIME ZONE13 bytesOffsetDateTimeGlobal applications
TIMESTAMP WITH LOCAL TIME ZONE11 bytesOffsetDateTimeSession timezone
INTERVAL YEAR TO MONTHVariablePeriodDurations
INTERVAL DAY TO SECONDVariableDurationTime intervals

Recommended Java Date Mapping

Business RequirementJava TypeDatabase Type
Birth dateLocalDateDATE
Joining dateLocalDateDATE
Audit timestampLocalDateTimeTIMESTAMP
Event timeInstantTIMESTAMP
Global eventOffsetDateTimeTIMESTAMP WITH TIME ZONE

Should Time Zones Be Stored?

Most enterprise applications should standardize on UTC.

Recommended architecture:

Database

2026-06-20T10:30:00Z

Java

Instant createdTime;

UI

Convert to local timezone.

Benefits:

  • Single storage format.
  • Easy comparisons.
  • Multi-region support.
  • No daylight saving issues.

Large Object Types

Oracle TypeMaximum SizeJava TypeUsage
CLOB128 TBStringArticles, XML
NCLOB128 TBStringUnicode text
BLOB128 TBbyte[]Images, PDFs
BFILEExternalFileExternal files

CLOB Example

DESCRIPTION CLOB

Java:

@Lob
private String description;

BLOB Example

DOCUMENT BLOB

Java:

@Lob
private byte[] document;

RAW Data Types

Oracle TypeSizeJava TypeUsage
RAW1–2000 bytesbyte[]Binary data
LONG RAW2 GBbyte[]Deprecated
RAW(16)16 bytesUUIDUUID storage

UUID Storage

EMP_ID RAW(16)

Java:

private UUID id;

Advantages:

  • Smaller than VARCHAR2(36).
  • Faster indexes.

Row Identifier Types

TypeSizeUsage
ROWID10 bytesPhysical row location
UROWIDVariableUniversal row ID

Used mainly by DBAs.


Recommended Column Sizes

ColumnRecommended Size
First NameVARCHAR2(50)
Last NameVARCHAR2(50)
EmailVARCHAR2(200)
PhoneVARCHAR2(20)
Country CodeCHAR(2)
StatusVARCHAR2(20)
AddressVARCHAR2(500)
URLVARCHAR2(500)
DescriptionCLOB
Currency CodeCHAR(3)

VARCHAR Best Practices

Bad:

NAME VARCHAR2(4000)

Problems:

  • Larger indexes.
  • Wasted storage.
  • Poor performance.

Good:

NAME VARCHAR2(100)

File Metadata Storage

Store metadata in database.

FILE_NAME VARCHAR2(255)
CONTENT_TYPE VARCHAR2(100)
FILE_SIZE NUMBER(12)
FILE_PATH VARCHAR2(500)

Store actual file:

  • S3
  • NAS
  • Object storage

Avoid storing large files in BLOB unless required.


Primary Key Strategies

TypeOracleJavaRecommended
SequenceNUMBER(19)LongYes
IdentityNUMBERLongYes
UUID StringVARCHAR2(36)UUIDGood
UUID BinaryRAW(16)UUIDExcellent
CompositeMultiple columnsEmbeddedIdSituational

Sequence Example

EMP_ID NUMBER(19)

Java:

Long id;

Best for:

  • Oracle applications
  • Monoliths
  • Banking systems

UUID Example

EMP_ID RAW(16)

Java:

UUID id;

Best for:

  • Microservices
  • Distributed systems

Composite Keys

Example:

PRIMARY KEY
(
    ORDER_ID,
    PRODUCT_ID
)

Use when:

  • Relationship tables
  • Junction tables

Avoid for:

  • Large entities
  • Frequently joined tables

Recommended Constraints

Primary Key

CONSTRAINT PK_EMP
PRIMARY KEY (EMP_ID)

Foreign Key

CONSTRAINT FK_DEPT
FOREIGN KEY(DEPT_ID)
REFERENCES DEPARTMENT(DEPT_ID)

Unique

EMAIL VARCHAR2(200)
UNIQUE

Check

CHECK(SALARY > 0)

Not Null

NAME VARCHAR2(100)
NOT NULL

Recommended Audit Columns

Every enterprise table:

CREATED_DATE TIMESTAMP
CREATED_BY VARCHAR2(50)

UPDATED_DATE TIMESTAMP
UPDATED_BY VARCHAR2(50)

VERSION NUMBER

Java:

private LocalDateTime createdDate;
private LocalDateTime updatedDate;
private Integer version;

Standard Enterprise Table

EMPLOYEE
---------
EMP_ID NUMBER(19)
FIRST_NAME VARCHAR2(50)
LAST_NAME VARCHAR2(50)
EMAIL VARCHAR2(200)
STATUS VARCHAR2(20)
SALARY NUMBER(12,2)

CREATED_DATE TIMESTAMP
CREATED_BY VARCHAR2(50)

UPDATED_DATE TIMESTAMP
UPDATED_BY VARCHAR2(50)

VERSION NUMBER

Golden Rules

✅ Use VARCHAR2 instead of CHAR.

✅ Specify appropriate lengths.

✅ Use NUMBER(19) for IDs.

✅ Use BigDecimal for money.

✅ Use TIMESTAMP for audit columns.

✅ Standardize timestamps in UTC.

✅ Use LocalDate for business dates.

✅ Use UUID only when necessary.

✅ Use composite keys only for junction tables.

✅ Add NOT NULL wherever possible.

✅ Index primary and foreign keys.

✅ Always include audit columns.


Final Recommendation Matrix

RequirementOracle TypeJava Type
Primary KeyNUMBER(19)Long
UUID KeyRAW(16)UUID
NameVARCHAR2(100)String
EmailVARCHAR2(200)String
AmountNUMBER(12,2)BigDecimal
Date OnlyDATELocalDate
TimestampTIMESTAMPLocalDateTime
Global TimestampTIMESTAMP WITH TZOffsetDateTime
Large TextCLOBString
FileBLOBbyte[]

A properly designed schema can remain stable for decades, while applications, frameworks, and APIs evolve over time. The database design decisions made today directly influence performance, maintainability, and scalability for years to come.

Leave a Reply

Your email address will not be published. Required fields are marked *