IoTDB 入门教程 基础篇⑥——数据库SQL操作 | 数据库管理和数据读写

文章目录

  • 一、前文
  • 二、数据库管理
    • [2.1 创建数据库](#2.1 创建数据库)
    • [2.2 查询数据库](#2.2 查询数据库)
    • [2.3 删除数据库](#2.3 删除数据库)
  • 三、数据读写
    • [3.1 查询数据](#3.1 查询数据)
    • [3.2 新增数据](#3.2 新增数据)
    • [3.3 修改数据](#3.3 修改数据)
    • [3.4 删除数据](#3.4 删除数据)
  • 四、参考

一、前文

IoTDB入门教程------导读
本博文主要讲述数据库管理和数据读写

二、数据库管理

2.1 创建数据库

CREATE DATABASE root.ln

sql 复制代码
IoTDB> CREATE DATABASE root.test
Msg: The statement is executed successfully.

注意:必须是root.开头

这一点也是我一直吐槽的地方,为啥要限制必须root.开头呢?

全部都是root.开头不就等于全部都没有root.开头。意义何在呢。

sql 复制代码
IoTDB> CREATE DATABASE test
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 700: Error occurred while parsing SQL to physical plan: line 1:16 mismatched input 'test' expecting ROOT
IoTDB> CREATE DATABASE test.test
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 700: Error occurred while parsing SQL to physical plan: line 1:16 mismatched input 'test' expecting ROOT

注意:Database的父子节点都不能再设置 database

sql 复制代码
IoTDB> CREATE DATABASE root.test.aa
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 501: root.test has already been created as database
IoTDB> CREATE DATABASE root.test1.aa
Msg: The statement is executed successfully.

2.2 查询数据库

SHOW DATABASES;

sql 复制代码
IoTDB> SHOW DATABASES;
+----------------+----+-----------------------+---------------------+---------------------+
|        Database| TTL|SchemaReplicationFactor|DataReplicationFactor|TimePartitionInterval|
+----------------+----+-----------------------+---------------------+---------------------+
|      root.user1|null|                      1|                    1|            604800000|
|   root.test1.aa|null|                      1|                    1|            604800000|
|       root.test|null|                      1|                    1|            604800000|
|root.water_meter|null|                      1|                    1|            604800000|
+----------------+----+-----------------------+---------------------+---------------------+
Total line number = 4
It costs 0.010s

2.3 删除数据库

DELETE DATABASE root.ln

sql 复制代码
IoTDB> DELETE DATABASE root.user1
Msg: The statement is executed successfully.
IoTDB> SHOW DATABASES;
+----------------+----+-----------------------+---------------------+---------------------+
|        Database| TTL|SchemaReplicationFactor|DataReplicationFactor|TimePartitionInterval|
+----------------+----+-----------------------+---------------------+---------------------+
|   root.test1.aa|null|                      1|                    1|            604800000|
|       root.test|null|                      1|                    1|            604800000|
|root.water_meter|null|                      1|                    1|            604800000|
+----------------+----+-----------------------+---------------------+---------------------+
Total line number = 3
It costs 0.006s
sql 复制代码
IoTDB> DELETE DATABASE root.test1.aa
Msg: The statement is executed successfully.
IoTDB> SHOW DATABASES;
+----------------+----+-----------------------+---------------------+---------------------+
|        Database| TTL|SchemaReplicationFactor|DataReplicationFactor|TimePartitionInterval|
+----------------+----+-----------------------+---------------------+---------------------+
|       root.test|null|                      1|                    1|            604800000|
|root.water_meter|null|                      1|                    1|            604800000|
+----------------+----+-----------------------+---------------------+---------------------+
Total line number = 2
It costs 0.005s

三、数据读写

3.1 查询数据

select status from root.test.test

sql 复制代码
IoTDB> select status from root.test.test
+-----------------------------+---------------------+
|                         Time|root.test.test.status|
+-----------------------------+---------------------+
|2024-05-03T19:18:07.573+08:00|                  1.0|
|2024-05-03T19:18:19.825+08:00|                  2.0|
|2024-05-03T19:18:21.893+08:00|                  3.0|
+-----------------------------+---------------------+
Total line number = 3
It costs 0.127s

3.2 新增数据

  • 新增单条数据

INSERT INTO root.test.test(status) values(1)

sql 复制代码
IoTDB> INSERT INTO root.test.test(status) values(1)
Msg: The statement is executed successfully.
IoTDB> INSERT INTO root.test.test(status) values(2)
Msg: The statement is executed successfully.
IoTDB> INSERT INTO root.test.test(status) values(3)
Msg: The statement is executed successfully.
IoTDB> select status from root.test.test
+-----------------------------+---------------------+
|                         Time|root.test.test.status|
+-----------------------------+---------------------+
|2024-05-03T19:18:07.573+08:00|                  1.0|
|2024-05-03T19:18:19.825+08:00|                  2.0|
|2024-05-03T19:18:21.893+08:00|                  3.0|
+-----------------------------+---------------------+
Total line number = 3
It costs 0.127s
  • 新增多条数据

注意:新增多条数据,需要传入时间戳参数

INSERT INTO root.test.test(timestamp,status) values(1,4),(2,5),(3,6)

sql 复制代码
IoTDB> INSERT INTO root.test.test(timestamp,status) values(1,4),(2,5),(3,6)
Msg: The statement is executed successfully.
IoTDB> select status from root.test.test
+-----------------------------+---------------------+
|                         Time|root.test.test.status|
+-----------------------------+---------------------+
|1970-01-01T08:00:00.001+08:00|                  4.0|
|1970-01-01T08:00:00.002+08:00|                  5.0|
|1970-01-01T08:00:00.003+08:00|                  6.0|
|2024-05-03T19:18:07.573+08:00|                  1.0|
|2024-05-03T19:18:19.825+08:00|                  2.0|
|2024-05-03T19:18:21.893+08:00|                  3.0|
+-----------------------------+---------------------+
Total line number = 6
It costs 0.012s

3.3 修改数据

传入同样的时间戳,即可覆盖该时间戳的上一条数据

sql 复制代码
IoTDB> INSERT INTO root.test.test(timestamp,status) values(1,444)
Msg: The statement is executed successfully.
IoTDB> SELECT status FROM root.test.test
+-----------------------------+---------------------+
|                         Time|root.test.test.status|
+-----------------------------+---------------------+
|1970-01-01T08:00:00.001+08:00|                444.0|
|1970-01-01T08:00:00.002+08:00|                  5.0|
|1970-01-01T08:00:00.003+08:00|                  6.0|
|2024-05-03T19:18:07.573+08:00|                  1.0|
|2024-05-03T19:18:19.825+08:00|                  2.0|
|2024-05-03T19:18:21.893+08:00|                  3.0|
+-----------------------------+---------------------+
Total line number = 6
It costs 0.010s

3.4 删除数据

DELETE FROM root.test.test.status where time < 4

sql 复制代码
IoTDB> DELETE FROM root.test.test.status where time < 4
Msg: The statement is executed successfully.
IoTDB> SELECT status FROM root.test.test
+-----------------------------+---------------------+
|                         Time|root.test.test.status|
+-----------------------------+---------------------+
|2024-05-03T19:18:07.573+08:00|                  1.0|
|2024-05-03T19:18:19.825+08:00|                  2.0|
|2024-05-03T19:18:21.893+08:00|                  3.0|
+-----------------------------+---------------------+
Total line number = 3
It costs 0.014s

四、参考

SQL手册 | IoTDB Website

数据增删 | IoTDB Website

数据查询 | IoTDB Website

觉得好,就一键三连呗(点赞+收藏+关注)

相关推荐
leegong231111 小时前
PostgreSQL 初中级认证可以一起学吗?
数据库
秋野酱3 小时前
如何在 Spring Boot 中实现自定义属性
java·数据库·spring boot
weisian1513 小时前
Mysql--实战篇--@Transactional失效场景及避免策略(@Transactional实现原理,失效场景,内部调用问题等)
数据库·mysql
AI航海家(Ethan)3 小时前
PostgreSQL数据库的运行机制和架构体系
数据库·postgresql·架构
Amd7944 小时前
深入探讨索引的创建与删除:提升数据库查询效率的关键技术
数据结构·sql·数据库管理·索引·性能提升·查询优化·数据检索
Kendra9196 小时前
数据库(MySQL)
数据库·mysql
时光书签7 小时前
Mongodb副本集群为什么选择3个节点不选择4个节点
数据库·mongodb·nosql
人才程序员9 小时前
【C++拓展】vs2022使用SQlite3
c语言·开发语言·数据库·c++·qt·ui·sqlite
极客先躯9 小时前
高级java每日一道面试题-2025年01月23日-数据库篇-主键与索引有什么区别 ?
java·数据库·java高级·高级面试题·选择合适的主键·谨慎创建索引·定期评估索引的有效性
指尖下的技术9 小时前
Mysql面试题----MyISAM和InnoDB的区别
数据库·mysql