Space used by nulls in database

前端 未结 6 1576
深忆病人
深忆病人 2020-12-03 13:32

If a column is null, does that affect the space used by the column? Is the space used fixed by the column definition? Does this vary from database to database. (I am mainly

6条回答
  •  无人及你
    2020-12-03 14:02

    SQL Server has a Bit to indicate NULL. There is no such bit used if the column is defined as NOT NULL

    VARCHAR uses variable length to store data (and thus has overhead of indicating how long the actual data is), whereas CHAR is fixed width.

    So on that basis a CHAR(1) NOT NULL is "shorter" than a VARCHAR(1) NOT NULL as VARCHAR needs a length indicator, and CHAR will always use just one byte.

    EDIT: Note that having a BIT field that allows NULL requires two bits to store it! I often see BIT fields where this has not been considered, don't need to store NULL but have not been set to NOT NULL so are wasting a bit unintentionally

提交回复
热议问题