Skip to Main Content
  • Questions
  • Oracle JDBC driver getDatabaseMetadata()["COLUMN_SIZE"] returns column size in bytes (not in characters) for "BYTE" length semantics

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sam.

Asked: September 29, 2023 - 10:26 pm UTC

Last updated: October 12, 2023 - 3:17 am UTC

Version: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Viewed 1000+ times

You Asked

Description of getDatabaseMetadata()["COLUMN_SIZE"] in Javadoc:
"The COLUMN_SIZE column specifies the column size for the given column.
For character data, this is the ***length in characters***."

Scenario:
- Check that database encoding is multiple bytes per character.
- Create column with the definition VARCHAR2(4 BYTE).
- Try to insert 4 characters each of which is encoded as multiple bytes.
- DBMS correctly generates ORA-12899: value too large for column (actual: 8, maximum: 4)
- Issue: Oracle JDBC driver getDatabaseMetadata()["COLUMN_SIZE"] returns 4, should return 1.

Steps to reproduce:
select * from NLS_DATABASE_PARAMETERS where PARAMETER = 'NLS_CHARACTERSET';
-- NLS_LENGTH_SEMANTICS = BYTE
-- NLS_CHARACTERSET = AL32UTF8

create table T {
 C VARCHAR2(4 BYTE)
);

insert into T(C) values ('1234');
-- OK

insert into T(C) values ('яяяя');
-- SQL Error: ORA-12899: value too large for column "T"."C" (actual: 8, maximum: 4)

Write Java program where you get getDatabaseMetadata()["COLUMN_SIZE"].
-- It returns 4, should return 1.

and Connor said...

Some information from , our NLS expert Sergiusz Wolicki:

It is enough if the characters are ASCII (0x00-0x7f). Note that any column length is formally a length constraint, not a capacity. 4 means “up to 4 characters”, not “at least 4 characters”. A VARCHAR2(32767 CHAR) column cannot store 32767 characters if they are not all ASCII.

With byte length semantics, byte length constraint always equals char length constraint.

We're not taking comments currently, so please try again later if you want to add a comment.

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database