What is the major difference between Varchar2 and char

前端 未结 7 583
独厮守ぢ
独厮守ぢ 2020-11-22 14:33

Creating Table:

CREATE TABLE test (
charcol    CHAR(10),
varcharcol VARCHAR2(10));

SELECT LENGTH(charcol), LENGTH(varcharcol) FROM test;


        
相关标签:
7条回答
  • 2020-11-22 15:21

    CHAR type has fixed size, so if you say it is 10 bytes, then it always stores 10 bytes in the database and it doesn't matter whether you store any text or just empty 10 bytes

    VARCHAR2 size depends on how many bytes you are actually going to store in the database. The number you specify is just the maximum number of bytes that can be stored (although 1 byte is minimum)

    You should use CHAR when dealing with fixed length strings (you know in advance the exact length of string you will be storing) - database can then manipulate with it better and faster since it knows the exact lenght

    You should use VARCHAR2 when you don't know the exact lenght of stored strings.

    Situation you would use both may be:

    name VARCHAR2(255),
    zip_code CHAR(5) --if your users have only 5 place zip codes
    
    0 讨论(0)
提交回复
热议问题