Tuple

简介: Tuple

元组类型有 1~n 个元素组成,每个元素之间允许设置不同的数据类型,且彼此之间要求兼容。元组同样支持类型推断,其推断依据仍然是以最小存储代价为原则。与数组类似,元组也可以使用两种方式定义,常规方式 tuple(T)

$ SELECT tuple(1, 'a', now()) as x, toTypeName(x);
┌─x─────────────────────────────┬─toTypeName(tuple(1, 'a', now()))─┐
│ (1,'a','2022-03-26 09:06:32') │ Tuple(UInt8, String, DateTime)   │
└───────────────────────────────┴──────────────────────────────────┘点击复制复制失败已复制


或者简写方式 (T)

$ SELECT (1, 2.0, null) as x, toTypeName(x);
┌─x──────────┬─toTypeName((1, 2., NULL))────────────────┐
│ (1,2,NULL) │ Tuple(UInt8, Float64, Nullable(Nothing)) │
└────────────┴──────────────────────────────────────────┘点击复制复制失败已复制


在定义表字段时,元组也需要指定明确的元素类型:

$ CREATE Tuple_TEST (
  c1 Tuple(String, Int8)
) ENGINE = Memory;点击复制复制失败已复制


元素类型和泛型的作用类似,可以进一步保障数据质量。在数据写入的过程中会进行类型检查。例如,写入 INSERT INTO Tuple_TEST VALUES( ('abc', 123) ) 是可行的,而写入 INSERT INTO Tuple_TEST VALUES(('abc', 'efg)) 则会报错。

目录
相关文章
|
5月前
|
编译器 C# 索引
元祖Tuple
`Tuple`和`ValueTuple`是.NET中的元组类型,`ValueTuple`是值类型,结构体,成员为可修改的字段,而`Tuple`是引用类型,成员为只读属性。微软推荐使用`ValueTuple`,因其性能更优并有语法支持,如简化的声明`(Type, Type,...)`,值相等比较,元素命名和解构赋值。元组常用于方法返回多个值。
|
5月前
|
索引 Python
tuple
tuple
32 2
|
12月前
8 Tuple
Tuple
50 0
|
C++
C++17使用std::apply和fold expression对tuple进行遍历
C++17使用std::apply和fold expression对std::tuple进行遍历
101 0
list和tuple的区别
了解list和tuple的相同点和不同点
96 0
TypeError: tuple indices must be integers, not tuple是怎么回事
TypeError: tuple indices must be integers, not tuple是怎么回事
251 0
|
索引 Python
pyhon tuple 元组
pyhon tuple 元组
|
安全 索引 Python
Python基础教程之list和tuple
Python基础教程之list和tuple
|
Python
Python 修改tuple元组提示:TypeError: ‘tuple‘ object does not support item assignment. 问题原因
Python 修改tuple元组提示:TypeError: ‘tuple‘ object does not support item assignment. 问题原因
1284 0
|
索引 Python
成功解决TypeError: tuple indices must be integers or slices, not str
成功解决TypeError: tuple indices must be integers or slices, not str