# 数据表格式 ## 表头 如下所示,加上文件中附加的`ptr of next table`,表头永远是`4k`对齐的,其中行类型列表的No.1自动成为主键,强制应用`unique`+`nonnull`类型修饰符;`data blocks`可以为任意数据,如索引,表项等。由于数据块均为定长,增加时直接添加或重用已删除区块,修改时直接覆盖,删除时直接在索引中移除该项,将块首地址附加到已删除块的链表即可。 ``` ┌──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┐ │ 0 - 7 │ 8 - 15 │ 16 -- 23 │ 24 -- 31 │ 32 -- 39 │ 40 -- 47 │ 48 -- 55 │ 56 -- 63 │ ├──────────┴──────────┼──────────┴──────────┴──────────┴──────────┴──────────┴──────────┤ │ table name length │ name of the table ( variable length ) │ ├─────────────────────┼──────────┬──────────┬──────────┬──────────┬──────────┬──────────┤ │ │ type of │ type of │ type of │ type of │ type of │ type of │ │ table column length │ column │ column │ column │ column │ column │ column │ │ │ No.1 │ No.2 │ No.3 │ No.4 │ No... │ No.N │ ├─────────────────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┤ │ index pointer of pk ( this pointer will never be zero ) │ ├───────────────────────────────────────────────────────────────────────────────────────┤ │ index pointer of column No.2 ( if it's zero, there is no index for this column ) │ ├───────────────────────────────────────────────────────────────────────────────────────┤ │ index pointer of column No... ( if it's zero, there is no index for this column ) │ ├───────────────────────────────────────────────────────────────────────────────────────┤ │ index pointer of column No.N ( if it's zero, there is no index for this column ) │ ├───────────────────────────────────────────────────────────────────────────────────────┤ │ index pointer of first foreign key ( if available ) │ ├───────────────────────────────────────────────────────────────────────────────────────┤ │ index pointer of second foreign key ( if available ) │ ├───────────────────────────────────────────────────────────────────────────────────────┤ │ data blocks ... │ └───────────────────────────────────────────────────────────────────────────────────────┘ ``` ## 数据区块 ### 索引 > 区块长度固定,但是不同索引类型有所不同 详见[索引格式](/api/index.md)。 ### 表项 > 区块长度固定,为`8+len(column1)+len(column2)+...+len(columnN)`字节 为方便遍历,数据表项以uint64的指针开头,代表下一项的地址。接下来按照[数据类型](/api/types.md)中规定的存储格式依次附加第一项、第二项直到第N项的值。 #### 表项的增加 优先附加到上一个表项末尾。如无法实现,则从空区块选取一个,或附加到整个文件末尾。 #### 表项的删除 需要更新[未被使用的对齐部分](/api/dbfile.md#空闲块)