Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle 11g: Can I create a number column that stores only 1 byte?

I need a number column to serve as an indicator for something I am working on, but I don't want it to take up more than a single byte per record. If I use NUMBER(1), would this satisfy my requirement?

like image 294
oscilatingcretin Avatar asked Feb 17 '12 19:02

oscilatingcretin


1 Answers

A NUMBER(1) column will take up however much space it requires to store a 1 digit number. That is likely to be more than 1 byte (negative numbers will require 3 bytes, a 0 requires 1 byte, the numbers 1-9 require 2 bytes)

SQL> create table foo( col1 number(1) );

Table created.

SQL> insert into foo values( 1 );

1 row created.

SQL> insert into foo values( 9 );

1 row created.

SQL> insert into foo values( -7 );

1 row created.

SQL> select vsize(col1), col1 from foo;

VSIZE(COL1)       COL1
----------- ----------
          2          1
          2          9
          3         -7

A table with a VARCHAR2(1 BYTE) column, on the other hand, will use at most 1 byte per row of storage

SQL> create table bar( col1 varchar2(1) );

Table created.

SQL> insert into bar values( 'Y' );

1 row created.

SQL> insert into bar values( 'N' );

1 row created.

SQL> select vsize(col1), col1 from bar;

VSIZE(COL1) C
----------- -
          1 Y
          1 N
like image 61
Justin Cave Avatar answered Oct 24 '22 09:10

Justin Cave