Today I will present short notes about popular string data types in SQL and PLSQL.
CHAR
it is used for storing fix length character strings. String values will be space/blank padded before stored on disk. If this type is used to store variable length strings, it will waste a lot of disk space.Example
CREATE TABLE CHAR_TAB (CHAR_COL CHAR (10));
INSERT INTO CHAR_TAB (CHAR_COL)
VALUES ('Mahmoud');
COMMIT;
Let's now select data and lenght of data and dump of data in table CHAR_TAB
SELECT CHAR_COL, LENGTH (CHAR_COL), DUMP (CHAR_COL) FROM CHAR_TAB;
The output will be like
As We noticed in result that length is 10 characters inspire of I entered 'Mahmoud' which has only 7 characters.
We also noticed that in dump he padded at latest ASCII code three times number 32 which is ASCII code of space.
VARCHAR
VARCHAR work as varchar2 but this type shouldn't use as it is reserved for future usage.Example
CREATE TABLE VARCHAR_TAB (VARCHAR_COL VARCHAR (10));
INSERT INTO
VARCHAR
_TAB (
VARCHAR
_COL) VALUES ('Mahmoud'); COMMIT;
Let's now select data and length of data and dump of data in table CHAR_TAB
SELECT
VARCHAR
_COL, LENGTH (
VARCHAR
_COL
), DUMP (
VARCHAR
_COL
) FROM
VARCHAR
_TAB;
The output will be like
As we noticed that it stored 7 characters in spite of the length of column in table is 10 characters.
VARCHAR2
It is used to store variable length character strings. The string value's length will be stored on disk with the value itself.Example
CREATE TABLE VARCHAR2_TAB (VARCHAR2_COL VARCHAR2 (10));
INSERT INTO
VARCHAR2
_TAB (
VARCHAR2
_COL) VALUES ('Mahmoud'); COMMIT;
Let's now select data and length of data and dump of data in table CHAR_TAB
SELECT
VARCHAR
2_COL, LENGTH (
VARCHAR2
_COL
), DUMP (
VARCHAR2
_COL
)
FROM
VARCHAR2
_TAB;
The output will be like
Thanks
No comments:
Post a Comment