|NO.Z.00035|——————————|BigDataEnd|——|Hadoop&OLAP_ClickHouse.V07|——|ClickHouse.v07|ClickHouse语法|CreateInsertAlter|DescribeCheck|

一、CREATE DATABASE
### --- CREATE DATABASE创建数据库

~~~     用于创建指定名称的数据库,语法如下:
~~~     CREATE DATABASE [IF NOT EXISTS] db_name
### --- 如果查询中存在IF NOT EXISTS,则当数据库已经存在时,该查询不会返回任何错误。

~~~     # 创建数据库
hadoop01 :) create database test;
~~~     # 查看创建的数据库
hadoop01 :) show databases;

┌─name───────────────────────────┐
│ test                           │
└────────────────────────────────┘
二、CREATE TABLE
### --- CREATE TABLE

~~~     # 对于创建表,语法如下:
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
    name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
    name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
    ...
) ENGINE = engine
~~~     # DEFAULT expr – 默认值,用法与SQL类似。

~~~     MATERIALIZED expr – 物化表达式,被该表达式指定的列不能被INSERT~~~     因为它总是被计算出来的。 对于INSERT而言,不需要考虑这些列。 
~~~     另外,在SELECT查询中如果包含星号,此列不会被查询。
~~~     ALIAS expr – 别名。
### --- 有三种方式创建表:

~~~     # 方式一:直接创建表
hadoop01 :) create table t1(id UInt16,name String) engine=TinyLog;
~~~     # 方式二:创建一个与其他表具有相同结构的表
~~~     CREATE TABLE [IF NOT EXISTS] [db.]table_name AS [db2.]name2 [ENGINE = engine]
~~~     可以对其指定不同的表引擎声明。如果没有表引擎声明,则创建的表将与db2.name2使用相同的表引擎。

hadoop01 :) create table t2 as t1 engine=Memory;
hadoop01 :) desc t2;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id   │ UInt16 │              │                    │         │                  │                │
│ name │ String │              │                    │         │                  │                │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
~~~     # 方式三:使用指定的引擎创建一个与SELECT子句的结果具有相同结构的表,并使用SELECT子句的结果填充它。

~~~     # 语法:
CREATE TABLE [IF NOT EXISTS] [db.]table_name ENGINE = engine AS SELECT ...

hadoop01 :) insert into t1 values(1,'zhangsan'),(2,'lisi'),(3,'wangwu');
hadoop01 :) create table t3 engine=TinyLog as select * from t1;
hadoop01 :) select * from t3;

┌─id─┬─name─────┐
│  1 │ zhangsan │
│  2 │ lisi     │
│  3 │ wangwu   │
└────┴──────────┘
三、INSERT INTO
### --- 主要用于向表中添加数据,基本格式如下:

~~~     # 语法格式:
INSERT INTO [db.]table [(c1, c2, c3)] VALUES (v11, v12, v13), (v21, v22, v23), ...
~~~     # 加载数据:
hadoop01 :) insert into t1 values(1,'zhangsan'),(2,'lisi'),(3,'wangwu');
~~~     # 查询数据:
hadoop01 :) select * from t1;

┌─id─┬─name─────┐
│  1 │ zhangsan │
│  2 │ lisi     │
│  3 │ wangwu   │
└────┴──────────┘
### --- 还可以使用select来写入数据:
~~~     ClickHouse不支持的修改数据的查询:
~~~     UPDATE, DELETE, REPLACE, MERGE, UPSERT, INSERTUPDATE。

~~~     # 语法格式:
INSERT INTO [db.]table [(c1, c2, c3)] SELECT ...
~~~     # 以select方式写入数据:
hadoop01 :) insert into t2 select * from t3;
~~~     # 查询数据:
hadoop01 :) select * from t2;
┌─id─┬─name─────┐
│  1 │ zhangsan │
│  2 │ lisi     │
│  3 │ wangwu   │
└────┴──────────┘
三、ALTER
### --- ALTER

~~~     ALTER只支持MergeTree系列,Merge和Distributed引擎的表,基本语法:
~~~     ALTER TABLE [db].name [ON CLUSTER cluster] ADD|DROP|MODIFY COLUMN ...
### --- 参数解析:

~~~     ADD COLUMN – 向表中添加新列
~~~     DROP COLUMN – 在表中删除列
~~~     MODIFY COLUMN – 更改列的类型
### --- 案例演示:

~~~     # 创建一个MergerTree引擎的表
hadoop01 :) create table mt_table (
            date Date, 
            id UInt8, 
            name String
            ) ENGINE=MergeTree(date, (id, name), 8192);
~~~     # 向表中插入一些值

hadoop01 :) insert into mt_table values ('2021-11-01', 1, 'zhangsan');
hadoop01 :) insert into mt_table values ('2021-11-01', 2, 'lisi');
hadoop01 :) insert into mt_table values ('2021-11-03', 3, 'wangwu');
~~~     # 在末尾添加一个新列age

hadoop01 :) alter table mt_table add column age UInt8;

hadoop01 :) desc mt_table;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ dateDate   │              │                    │         │                  │                │
│ id   │ UInt8  │              │                    │         │                  │                │
│ name │ String │              │                    │         │                  │                │
│ age  │ UInt8  │              │                    │         │                  │                │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
~~~     # 查看引擎表中的数据

hadoop01 :) select * from mt_table;

┌───────date─┬─id─┬─name─────┬─age─┐
│ 2021-11-01 │  1 │ zhangsan │   0 │
└────────────┴────┴──────────┴─────┘
┌───────date─┬─id─┬─name─┬─age─┐
│ 2021-11-01 │  2 │ lisi │   0 │
└────────────┴────┴──────┴─────┘
┌───────date─┬─id─┬─name───┬─age─┐
│ 2021-11-03 │  3 │ wangwu │   0 │
└────────────┴────┴────────┴─────┘
~~~     # 更改age列的类型

hadoop01 :) alter table mt_table modify column age UInt16;

hadoop01 :) desc mt_table;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ dateDate   │              │                    │         │                  │                │
│ id   │ UInt8  │              │                    │         │                  │                │
│ name │ String │              │                    │         │                  │                │
│ age  │ UInt16 │              │                    │         │                  │                │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
~~~     # 删除刚才创建age列

hadoop01 :) alter table mt_table drop column age;

hadoop01 :) desc mt_table;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ dateDate   │              │                    │         │                  │                │
│ id   │ UInt8  │              │                    │         │                  │                │
│ name │ String │              │                    │         │                  │                │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
四、DESCRIBE TABLE
### --- 查看表结构

~~~     # 查看表结构:
hadoop01 :) desc mt_table;

┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ date │ Date   │              │                    │         │                  │                │
│ id   │ UInt8  │              │                    │         │                  │                │
│ name │ String │              │                    │         │                  │                │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
五、CHECK TABLE
### --- CHECK TABLE

~~~     # 检查表中的数据是否损坏,他会返回两种结果:
~~~     0 – 数据已损坏
~~~     1 – 数据完整
~~~     该命令只支持Log,TinyLog和StripeLog引擎。

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(31)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示