数据类型
以下是ClickHouse支持的主要数据类型列表,每种数据类型都附带一个示例建表语句、使用场景、与MySQL对应的数据类型以及一个INSERT语句示例:
整数类型
Int8,Int16,Int32,Int64UInt8,UInt16,UInt32,UInt64示例建表语句:
CREATE TABLE int_example
(
col_int32 Int32,
col_uint64 UInt64
) ENGINE = MergeTree()
ORDER BY col_int32;使用场景:存储整数数据,如ID、计数等。
MySQL对应数据类型:
TINYINT,SMALLINT,INT,BIGINT,UNSIGNED TINYINT,UNSIGNED SMALLINT,UNSIGNED INT,UNSIGNED BIGINTINSERT语句示例:
INSERT INTO int_example (col_int32, col_uint64) VALUES (42, 100);
浮点数类型
Float32,Float64示例建表语句:
CREATE TABLE float_example
(
col_float32 Float32,
col_float64 Float64
) ENGINE = MergeTree()
ORDER BY col_float64;使用场景:存储浮点数数据,如金额、温度等。
MySQL对应数据类型:
FLOAT,DOUBLEINSERT语句示例:
INSERT INTO float_example (col_float32, col_float64) VALUES (3.14, 6.28);
定点数类型
Decimal32,Decimal64,Decimal128示例建表语句:
CREATE TABLE decimal_example
(
col_decimal32 Decimal32(9, 2),
col_decimal64 Decimal64(18, 4)
) ENGINE = MergeTree()
ORDER BY col_decimal64;使用场景:需要固定小数位数的高精度数值,如货币数据。
MySQL对应数据类型:
DECIMAL,DECIMAL,DECIMALINSERT语句示例:
INSERT INTO decimal_example (col_decimal32, col_decimal64) VALUES (123.45, 9876.5432);
日期和时间类型
Date,DateTime,DateTime64示例建表语句:
CREATE TABLE datetime_example
(
col_date Date,
col_datetime DateTime
) ENGINE = MergeTree()
ORDER BY col_datetime;使用场景:存储日期和时间数据,如订单时间、日志时间等。
MySQL对应数据类型:
DATE,DATETIME,DATETIMEINSERT语句示例:
INSERT INTO datetime_example (col_date, col_datetime) VALUES ('2023-08-10', '2023-08-10 14:30:00');
字符串类型
String,FixedString(n)示例建表语句:
CREATE TABLE string_example
(
col_string String,
col_fixed_string FixedString(10)
) ENGINE = MergeTree()
ORDER BY col_string;使用场景:存储可变长度字符串和固定长度字符串。
MySQL对应数据类型:
VARCHAR,CHARINSERT语句示例:
INSERT INTO string_example (col_string, col_fixed_string) VALUES ('Hello', 'World');
枚举类型
Enum8,Enum16示例建表语句:
CREATE TABLE enum_example
(
col_enum8 Enum8('A' = 1, 'B' = 2),
col_enum16 Enum16('X' = 10, 'Y' = 20)
) ENGINE = MergeTree()
ORDER BY col_enum16;使用场景:存储枚举值,如状态、类型等。
MySQL对应数据类型:
ENUMINSERT语句示例:
INSERT INTO enum_example (col_enum8, col_enum16) VALUES ('A', 'Y');
UUID类型
UUID示例建表语句:
CREATE TABLE uuid_example
(
col_uuid UUID
) ENGINE = MergeTree()
ORDER BY col_uuid;使用场景:存储唯
一标识符。
   MySQL对应数据类型:CHAR(36)
INSERT语句示例:
INSERT INTO uuid_example (col_uuid) VALUES ('550e8400-e29b-41d4-a716-446655440000');
IPv4和IPv6类型
IPv4,IPv6示例建表语句:
CREATE TABLE ip_example
(
col_ipv4 IPv4,
col_ipv6 IPv6
) ENGINE = MergeTree()
ORDER BY col_ipv6;使用场景:存储IPv4和IPv6地址。
MySQL对应数据类型:
VARCHAR(45)INSERT语句示例:
INSERT INTO ip_example (col_ipv4, col_ipv6) VALUES ('192.168.1.1', '2001:0db8:85a3:0000:0000:8a2e:0370:7334');
数组类型
Array(T)示例建表语句:
CREATE TABLE array_example
(
col_array Array(Int32)
) ENGINE = MergeTree()
ORDER BY col_array;使用场景:存储数组数据,如标签、多选项等。
MySQL对应数据类型:无直接对应
INSERT语句示例:
INSERT INTO array_example (col_array) VALUES ([1, 2, 3]);
元组类型
- `Tuple(T1, T2, ...)`
示例建表语句:
```sql
CREATE TABLE tuple_example
(
    col_tuple Tuple(String, Int32, Float64)
) ENGINE = MergeTree()
ORDER BY col_tuple;
```
使用场景:存储多种类型的值的组合。
MySQL对应数据类型:无直接对应
INSERT语句示例:
```sql
INSERT INTO tuple_example (col_tuple) VALUES (('Alice', 25, 5.6));
```
Nullable类型
- `Nullable(T)`
示例建表语句:
```sql
CREATE TABLE nullable_example2
(
    col_nullable Nullable(Int32),
    col_nullable_string String,
    id int
) ENGINE = MergeTree()
ORDER BY id;
```
使用场景:存储允许为空的字段。
MySQL对应数据类型:无直接对应
INSERT语句示例:
```sql
INSERT INTO nullable_example2 (col_nullable, col_nullable_string) VALUES (NULL, 'Some Value');
INSERT INTO nullable_example2 (col_nullable, col_nullable_string) VALUES (NULL, NULL);
select * from nullable_example2;
┌─col_nullable─┬─col_nullable_string─┬─id─┐
│         ᴺᵁᴸᴸ │ Some Value          │  0 │
└──────────────┴─────────────────────┴────┘
┌─col_nullable─┬─col_nullable_string─┬─id─┐
│         ᴺᵁᴸᴸ │                     │  0 │
└──────────────┴─────────────────────┴────┘
```
AggregateFunction类型
- `AggregateFunction(arg_types, return_type)`
示例建表语句:
```sql
-- 请注意,这只是示例,实际使用时需要定义自己的聚合函数
CREATE TABLE aggregate_example
(
    col_aggregate AggregateFunction(Int32, UInt64)
) ENGINE = MergeTree()
ORDER BY col_aggregate;
```
使用场景:存储自定义的聚合函数的结果。
MySQL对应数据类型:无直接对应
INSERT语句示例:不适用于此类型数据
请根据实际需要,使用适当的数据类型来创建表。这些示例建表语句仅为演示,实际表的字段和设计应根据数据存储需求进行定制。