Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between BYTE and CHAR in column datatypes

In Oracle, what is the difference between :

CREATE TABLE CLIENT (  NAME VARCHAR2(11 BYTE),  ID_CLIENT NUMBER ) 

and

CREATE TABLE CLIENT (  NAME VARCHAR2(11 CHAR), -- or even VARCHAR2(11)  ID_CLIENT NUMBER ) 
like image 864
Guido Avatar asked Sep 17 '08 09:09

Guido


People also ask

What is the difference between byte and char in Oracle?

The CHAR data type can store a character string with the size from 1 to 2000 bytes. If you don't explicitly specify BYTE or CHAR followed the length , Oracle uses the BYTE by default. When you insert or update a fixed-length character string column, Oracle stores the characters as the fixed-length data.

Which is bigger byte or char?

The char type takes 1 byte of memory (8 bits) and allows expressing in the binary notation 2^8=256 values. The char type can contain both positive and negative values. The range of values is from -128 to 127.

How many bytes is a char in Oracle?

A single CHAR will take 1 byte.


2 Answers

Let us assume the database character set is UTF-8, which is the recommended setting in recent versions of Oracle. In this case, some characters take more than 1 byte to store in the database.

If you define the field as VARCHAR2(11 BYTE), Oracle can use up to 11 bytes for storage, but you may not actually be able to store 11 characters in the field, because some of them take more than one byte to store, e.g. non-English characters.

By defining the field as VARCHAR2(11 CHAR) you tell Oracle it can use enough space to store 11 characters, no matter how many bytes it takes to store each one. A single character may require up to 4 bytes.

like image 156
David Sykes Avatar answered Sep 28 '22 04:09

David Sykes


One has exactly space for 11 bytes, the other for exactly 11 characters. Some charsets such as Unicode variants may use more than one byte per char, therefore the 11 byte field might have space for less than 11 chars depending on the encoding.

See also http://www.joelonsoftware.com/articles/Unicode.html

like image 37
Matthias Kestenholz Avatar answered Sep 28 '22 04:09

Matthias Kestenholz