ClickHouse Create Table

API

For the full list of supported methods, see CreateTableQueryopen in new window.

db.NewCreateTable().

	Model(&strct).

	Table("table1"). // quotes table names
	TableExpr("table1"). // arbitrary unsafe expression
	ModelTableExpr("table1"). // overrides model table name

	IfNotExists().

	Partition("toDate(time)").
    Order("col1, col2").
    TTL("toDate(time) + INTERVAL 30 DAY DELETE").
    Setting("ttl_only_drop_parts = 1").

	Exec(ctx)

Example

To create a table, define a model and use CreateTableQueryopen in new window:

type Span struct {
	ch.CHModel `ch:"table:spans,insert:spans_buffer"`

	ID	 uint64
	Name string	   `ch:",lc"` // low cardinality column
	Time time.Time
}

res, err := db.NewCreateTable().Model((*Span)(nil)).
	TTL("time + INTERVAL 30 DAY DELETE").
	Partition("toDate(time)").
	Order("time").
	Setting("ttl_only_drop_parts = 1").
	Exec(ctx)

ResetModel

go-clickhouse provides ResetModel method to quickly drop and create tables:

err := db.ResetModel(ctx, (*Model1)(nil), (*Model2)(nil))
DROP TABLE IF EXISTS model1 CASCADE;
CREATE TABLE model1 (...);

DROP TABLE IF EXISTS model2 CASCADE;
CREATE TABLE model2 (...);
Last Updated: