表命令
Commands to create, alter, or delete schemas in TimescaleDB are identical to those in PostgreSQL. Schema commands should be made to the hypertable name, and any changes are propagated to all chunks belonging to that hypertable.
TimescaleDB中创建、修改和删除数据库对象集合的命令与PostgreSQL相同。Schema命令必须执行到hypertable上,hypertable中所有的区块都将继承命令所作出的所有修改。
创建一个Hypertable
创建一个hypertable需要以下两个步骤.
Create a standard table (PostgreSQL docs).
创建一个标准表
CREATE TABLE conditions (
time TIMESTAMPTZ NOT NULL,
location TEXT NOT NULL,
temperature DOUBLE PRECISION NULL
);
Then, execute the TimescaleDB
create_hypertable
command on this newly created table (API docs).对新创建的表执行TimescaleDB
create_hypertable
命令。
WARNING:You can only convert a plain PostgreSQL table into a hypertable if it is currently empty. Otherwise, the create_hypertable command will throw an error. If you need to migrate data from an existing table to a hypertable, follow these migration instructions instead.
警告:该命令只能将空表转化为hypertable,如果表里有数据,该命令将会报错。如果需要将现有表中的数据迁移到hypertable中,请按照以下步骤进行迁移。
修改一个Hypertable
You can execute standard
ALTER TABLE
commands against the hypertable (PostgreSQL docs).
执行ALTER TABLE
命令可修改一个hypertable。
ALTER TABLE conditions
ADD COLUMN humidity DOUBLE PRECISION NULL;
TimescaleDB will then automatically propagate these schema changes to the chunks that constitute this hypertable.
TimescaleDB会自动将这些集合变化传递给该hypertable的所有区块。
WARNING:Altering a table's schema is quite efficient provided that its default value is set to NULL. If its default is a non-null value, TimescaleDB will need to fill in this value into all rows (of all chunks) belonging to this hypertable.
警告:将表集合的默认值设置为NULL,修改起来会更加高效。如果默认值不为空,TimescaleDB需要将该值复制到hypertable(所有区块)的所有数据行中。
删除一个Hypertable
It's just the standard
DROP TABLE
command, where TimescaleDB will correspondingly delete all chunks belonging to the hypertable.
TimescaleDB可执行DROP TABLE
命令删除hypertable的所有区块。
DROP TABLE conditions;