 |
|
EnterpriseDB: CHAR Data Type
Oracle Tips by Burleson Consulting
|
CHAR
EnterpriseDB also supports a CHAR data type. CHAR is a fixed length
data type. If you don't specify a size, it defaults to 1.
A
fixed length data type always uses all of its declared length. If
you declare a CHAR(50) and assign it a value, it will be padded to
50 characters with spaces. A VARCHAR2(50) does not pad. I don't
use CHAR
very
often. VARCHAR2 is much more useful to me as it only requires
storage for characters that are actually used.
To
declare a 50-character CHAR
variable,
you would simply enter the variable name followed by CHAR(50). This
variable or column would pad to 50 spaces when you add any data,
regardless of the data's size.
For
example, if I declare a variable:
v_data CHAR(50);
My
variable name is v_data, the data type is CHAR and the declared
length is 50. The actual length stored is 0 characters because the
variable is null. If we assign it a value:
v_data := 'LEWIS';
We
gave the variable v_data that value of LEWIS. LEWIS is 5 characters
but our variable now holds 50 characters: 5 characters for LEWIS
and 45 spaces. If v_data had been a VARCHAR2(50), its length would
only be 5.
The
only benefit of a CHAR
to
a
VARCHAR2 is that you always know exactly how long a CHAR column is
going to be. Either it will be NULL or it will be its declared
length. A VARCHAR2 will always either be NULL or it will be some
length less than or equal to its max length.
Synonyms for CHAR are:
*
CHARACTER
CLOB
In
Oracle, a CLOB data type is used to store very large text
documents. In Oracle 10g, on a 64-bit machine, a CLOB can store 128
terabytes of data. EnterpriseDB does not support the CLOB data type
directly. A CLOB declaration is transformed to the TEXT data type.
A TEXT field can be up to a gigabyte in size. TEXT works like a
VARCHAR2 but without the declared max size.
Synonyms for CLOB are:
* LONG
* LONG
VARCHAR
*
LONGTEXT
*
LVARCHAR
*
MEDIUMTEXT
* TEXT
If I
were to create a table with the text data types that we have
discussed, the syntax would look like this:
CREATE TABLE text_table (
name VARCHAR2(50),
address CHAR(50),
comments CLOB
);
Now
that we have a table, we can insert into it:
INSERT INTO text_table (name, address, comments)
VALUES ('Lewis', 'New Orleans', 'My Name is Lewis and My address
is New Orleans.');
If we selected the data back out from the table:
SELECT name, address, comments
FROM text_table;
I can use EnterpriseDB PSQL (which I spoke about in Chapter 2) to
run these commands.
edb=# CREATE TABLE text_table (
edb(# name VARCHAR2(50),
edb(# address CHAR(50),
edb(# comments CLOB
edb(# );
CREATE TABLE
edb=# INSERT INTO text_table (name, address, comments)
edb-# VALUES ('Lewis', 'New Orleans', 'My Name is Lewis and My
address is New
Orleans.');
INSERT 0 1
edb=# commit;
COMMIT
edb=# SELECT name, address, comments
edb-# FROM text_table;
name | address |
comments
-------+----------------------------------+--------------------------------------------- Lewis
| New Orleans | My Name is Lewis and My address
is New Orleans.
(1 row)
edb=#
I will use PSQL to run commands for the remainder of this chapter.
This
is an excerpt from the book "EnterpriseDB:
The Definitive Reference" by Rampant TechPress.