当前位置:网站首页>【clickhouse专栏】基础数据类型说明
【clickhouse专栏】基础数据类型说明
2022-06-13 07:35:00 【字母哥博客】
本文是clickhouse专栏第五篇,更多内容请关注本号历史文章!
一、数据类型表
clickhouse内置了很多的column数据类型,可以通过查询system.data_type_families这张表获取版本的所有支持的数据类型。下文中第一列是字段类型,第二列表示该类型的字段类型是否区分大小写(1表示不区分大小写,Date和date都是有效的数据类型名称),第三类是该字段类型的别名。
peer1 :) SELECT * FROM system.data_type_families;┌─name────────────────────────────┬─case_insensitive─┬─alias_to────┐│ Polygon │ 0 │ ││ Ring │ 0 │ ││ Point │ 0 │ ││ MultiPolygon │ 0 │ ││ IPv6 │ 0 │ ││ IntervalSecond │ 0 │ ││ IPv4 │ 0 │ ││ UInt32 │ 0 │ ││ IntervalYear │ 0 │ ││ IntervalQuarter │ 0 │ ││ IntervalMonth │ 0 │ ││ Int64 │ 0 │ ││ IntervalDay │ 0 │ ││ IntervalHour │ 0 │ ││ UInt256 │ 0 │ ││ Int16 │ 0 │ ││ LowCardinality │ 0 │ ││ AggregateFunction │ 0 │ ││ Nothing │ 0 │ ││ Decimal256 │ 1 │ ││ Tuple │ 0 │ ││ Array │ 0 │ ││ Enum16 │ 0 │ ││ IntervalMinute │ 0 │ ││ FixedString │ 0 │ ││ String │ 0 │ ││ DateTime │ 1 │ ││ Map │ 0 │ ││ UUID │ 0 │ ││ Decimal64 │ 1 │ ││ Nullable │ 0 │ ││ Enum │ 0 │ ││ Int32 │ 0 │ ││ UInt8 │ 0 │ ││ Date │ 1 │ ││ Decimal32 │ 1 │ ││ UInt128 │ 0 │ ││ Float64 │ 0 │ ││ SimpleAggregateFunction │ 0 │ ││ Nested │ 0 │ ││ DateTime64 │ 1 │ ││ Int128 │ 0 │ ││ Decimal128 │ 1 │ ││ Int8 │ 0 │ ││ Decimal │ 1 │ ││ Int256 │ 0 │ ││ IntervalWeek │ 0 │ ││ UInt64 │ 0 │ ││ Enum8 │ 0 │ ││ DateTime32 │ 1 │ ││ UInt16 │ 0 │ ││ Float32 │ 0 │ ││ INET6 │ 1 │ IPv6 ││ INET4 │ 1 │ IPv4 ││ BINARY │ 1 │ FixedString ││ NATIONAL CHAR VARYING │ 1 │ String ││ BINARY VARYING │ 1 │ String ││ NCHAR LARGE OBJECT │ 1 │ String ││ NATIONAL CHARACTER VARYING │ 1 │ String ││ NATIONAL CHARACTER LARGE OBJECT │ 1 │ String ││ NATIONAL CHARACTER │ 1 │ String ││ NATIONAL CHAR │ 1 │ String ││ CHARACTER VARYING │ 1 │ String ││ LONGBLOB │ 1 │ String ││ MEDIUMTEXT │ 1 │ String ││ TEXT │ 1 │ String ││ TINYBLOB │ 1 │ String ││ VARCHAR2 │ 1 │ String ││ CHARACTER LARGE OBJECT │ 1 │ String ││ DOUBLE PRECISION │ 1 │ Float64 ││ LONGTEXT │ 1 │ String ││ NVARCHAR │ 1 │ String ││ INT1 UNSIGNED │ 1 │ UInt8 ││ VARCHAR │ 1 │ String ││ CHAR VARYING │ 1 │ String ││ MEDIUMBLOB │ 1 │ String ││ NCHAR │ 1 │ String ││ CHAR │ 1 │ String ││ SMALLINT UNSIGNED │ 1 │ UInt16 ││ TIMESTAMP │ 1 │ DateTime ││ FIXED │ 1 │ Decimal ││ TINYTEXT │ 1 │ String ││ NUMERIC │ 1 │ Decimal ││ DEC │ 1 │ Decimal ││ TINYINT UNSIGNED │ 1 │ UInt8 ││ INTEGER UNSIGNED │ 1 │ UInt32 ││ INT UNSIGNED │ 1 │ UInt32 ││ CLOB │ 1 │ String ││ MEDIUMINT UNSIGNED │ 1 │ UInt32 ││ BOOL │ 1 │ Int8 ││ SMALLINT │ 1 │ Int16 ││ INTEGER SIGNED │ 1 │ Int32 ││ NCHAR VARYING │ 1 │ String ││ INT SIGNED │ 1 │ Int32 ││ TINYINT SIGNED │ 1 │ Int8 ││ BIGINT SIGNED │ 1 │ Int64 ││ BINARY LARGE OBJECT │ 1 │ String ││ SMALLINT SIGNED │ 1 │ Int16 ││ MEDIUMINT │ 1 │ Int32 ││ INTEGER │ 1 │ Int32 ││ INT1 SIGNED │ 1 │ Int8 ││ BIGINT UNSIGNED │ 1 │ UInt64 ││ BYTEA │ 1 │ String ││ INT │ 1 │ Int32 ││ SINGLE │ 1 │ Float32 ││ FLOAT │ 1 │ Float32 ││ MEDIUMINT SIGNED │ 1 │ Int32 ││ BOOLEAN │ 1 │ Int8 ││ DOUBLE │ 1 │ Float64 ││ INT1 │ 1 │ Int8 ││ CHAR LARGE OBJECT │ 1 │ String ││ TINYINT │ 1 │ Int8 ││ BIGINT │ 1 │ Int64 ││ CHARACTER │ 1 │ String ││ BYTE │ 1 │ Int8 ││ BLOB │ 1 │ String ││ REAL │ 1 │ Float32 │└─────────────────────────────────┴──────────────────┴─────────────┘二、基础数据类型
clickhouse和传统的数据库一样,提供了基础的数据类型,这一部分就简单的介绍一下,相信学过编程语言的同学对这一部分并不陌生。
整数类型
整数类型,IntN随着N的增大,整型数值的范围扩大。U表示unsigned无符号,无符号就是没有负数符号,只能存储大于等于0的树。
- 有符号整型(IntN):Int8、Int16、Int32、Int64、Int128、Int256。数值范围是【-2^(N-1) ~ 2^(N-1)-1】,比如:Int8(-128到127)
- 无符号整型(UIntN):UInt8、UInt16、UInt32、UInt64、UInt128、UInt256。数值范围是【0 ~ 2^N-1】,比如:UInt8(0到255)
浮点类型
单精度浮点数Float32,学过C、java、mysql的朋友把它当作float数据类型使用即可。从小数点后第8位起会发生精度丢失。
双精度浮点数Float64,学过C、java、mysql的朋友把它当作double数据类型使用即可。从小数点后第17位起会发生精度丢失。
Decimal类型
有的时候Float32、Float64无法满足计算精度的要求,我们就需要使用Decimal数据类型。ClickHouse 提供了 Decimal32、Decimal64 和 Decimal128 三种精度的Decimal。 在定义表字段的类型时,可以通过两种形式声明:简写方式有 Decimal32(S)、Decimal64(S)、Decimal128(S) 三种,原生方式为 Decimal(P, S),表示该定点数的整数位加上小数位的总长度最大为 P,其中小数位长度最多为 S。
参考下面的SQL及输出结果进行理解和学习
SELECT toDecimal64(2, 3) AS x, --数值为2,精度为3,所以2.000 toTypeName(x) AS xtype, toDecimal32(2, 2) AS y, --数值为2,精度为2,所以2.00 toTypeName(y) as ytype, x + y AS a, --2.000 + 2.00,加减法取最大精度。所以4.000 toTypeName(a) AS atype, x * y AS b, --2.000*2.00,乘法的精度值相加(2+3=5)。所以4.00000 toTypeName(b) AS btype, x / y AS c, --2.000/2.00,被除数2.000精度是3,结果精度等于被除数精度。所以1.000 toTypeName(c) AS ctype;下图为上面的SQL的执行结果 
普通String类型
clickhouse 中的String类型并没有长度限制,所以它可以代替传统关系型数据库中的所有以字符形式存在的数据类型,如:CHAR、VARCHAR、CLOB 等等。
FixedString类型
FixedString(N)表示固定长度为N的数据类型,可以使用toFixedString函数将String转换成FixedString。下文中的SQL虽然zimug长度是5,但使用FixedString(6)长度为6,实际存储内容是zimug\0。
peer1 :) select toFixedString('zimug', 6), length(toFixedString('zimug', 6));┌─toFixedString('zimug', 6)─┬─length(toFixedString('zimug', 6))─┐│ zimug │ 6 │└───────────────────────────┴───────────────────────────────────┘UUID
UUID是比较常见的数据唯一值,ClickHouse把它作为一种数据类型。UUID数据格式如:6fb875ae-75b9-4643-a146-5a1de7c717b4,使用方式如下:
使用示例如下:
-- 建表时创建UUID数据类型字段CREATE TABLE test_uuid (id UUID, content String) ENGINE=TinyLog;-- 向表中插入数据INSERT INTO test_uuid SELECT generateUUIDv4(), 'test uuid 1';-- 插入的时候不为UUID赋值,UUID字段使用0填充INSERT INTO test_uuid (content) VALUES ('test uuid 2'); 日期类型
日期数据类型有三种:DateTime、DateTime64和Date,并且支持使用字符串的方式insert写入日期格式。另外还有一种日期类型TIMESTAMP ,其使用方法和DateTime类型一摸一样,只是对DateTime数据类型起了一个别名。
- DateTime精确到秒,如:'2022-06-12 06:06:06'
- DateTime64精确到亚秒,如:'2022-06-12 06:06:06.000'
- Date不包含时间信息,精确到天,如:'2022-06-12'
布尔类型
ClickHouse布尔数据类型BOOLEAN 。其实际存储就是使用UInt8 类型,取值限制为0或 1。
推荐阅读
边栏推荐
- An example of CSRF attack defense in web application scenarios
- First graphical interface (modified version)
- C language: how to give an alias to a global variable?
- redis-3. Redis list, set, hash, sorted_ set、skiplist
- MySQL row column conversion (updated version)
- Three handshakes and four waves of TCP protocol and why------ One two pandas
- 理财产品连续几天收益都是零是怎么回事?
- MySQL query timeout control
- String source code analysis
- Redis learning journey -- do you know the data types of redis?
猜你喜欢

Index push down (ICP) for mysql5.6

Problems encountered during commissioning of C # project

Considerations for using redis transactions

Redis learning journey - persistence

量化框架backtrader之一文讀懂Analyzer分析器
![[MySQL change master error] slave is not configured or failed to initialize properly](/img/1d/0d0442eba0eca3902a1ff5fd71c40c.jpg)
[MySQL change master error] slave is not configured or failed to initialize properly

Redis' underlying data structure -- SDS

21 | 面向流水线的指令设计(下):奔腾4是怎么失败的?

Upgrade the project of log4j to log4j2

量化框架backtrader之一文读懂Analyzer分析器
随机推荐
Classification of databases
redis-1. Install redis with pictures and texts
Sharp weapon tcpdump
8. process status and transition
Deploy RDS service
21 | 面向流水线的指令设计(下):奔腾4是怎么失败的?
Mui mixed development - when updating the download app, the system status bar displays the download progress
How app inventor accesses resource files in assets directory
Consistency under distributed
[hard copy] core differences among dirty reading, non repeatable reading and unreal reading scenarios
An example of CSRF attack defense in web application scenarios
全志V3S环境编译开发流程
Database outline
mysql中时间字段 比较时间大小
P6154 游走(记忆化搜索
Awk use
RT thread simulator lvgl control: slider control
在排序数组中查找元素的第一个和最后一个位置
Find the first and last positions of elements in a sorted array
MySQL summary