最近公司正在啟用TDengine作為物聯網實時數據的存儲數據庫,但作為國產開源軟件的發光體,目前這個數據庫的使用方式,我還不是很熟悉,特此記錄和總結一些使用技巧。會持續更新……
1.修改用戶密碼
taos 數據庫 `root`用戶的默認密碼為: taosdata,安裝好taos數據庫后,可以通過:alter user root pass `your password` 進行修改
2.登錄數據庫
taos -uroot -p123456 ;
//備注: 我root賬戶的密碼是123456,當我使用: taos -u root -p 123456;進行登錄會報錯。參數連在一起:taos -uroot -p123456 就可以執行,不知道為什么。
3.數據庫操作
#創建庫(如果不存在)keep 字段是指文件在表存儲的時間,默認是天:
create database if not exists mydb keep 365 days 10 blocks 4;
#使用庫:
use mydb;
#刪除庫:
drop database mydb;
#刪除庫(如果存在):
drop database if exists mydb;
#顯示所有數據庫:
show databases;
#修改數據庫文件壓縮標志位:
alter database mydb comp 2;
#修改數據庫副本數:
alter database mydb replica 2;
#修改數據文件保存的天數:
alter database mydb keep 365;
#修改數據寫入成功所需要的確認數:
alter database mydb quorum 2;
#修改每個VNODE (TSDB) 中有多少cache大小的內存塊:
alter database mydb blocks 100;
4.表操作
#創建表,創建表時timestamp 字段必須為第一個字段類型,為主鍵:
create table if not exists mytable(column_name timestamp, column_name int,……);
#根據超級表創建子表,這樣建表之后,子表會復制除去超級表里面的tags字段外的所有字段;
create table table_name using super_table tags (column_value,column_value……);
#刪除數據表
drop table if exists mytable;
#顯示當前數據庫下的所有數據表信息
show tables;
#顯示當前數據庫下的所有數據表信息
show tables like "%table_name%";
#獲取表的結構信息
describe mytable;
#表增加列
alter table mytable add column addfield int;
#表刪除列
alter table mytable drop column addfield;
5.超級表操作
#創建超級表
#創建STable, 與創建表的SQL語法相似,但需指定TAGS字段的名稱和類型。說明:
#1) TAGS 列的數據類型不能是timestamp類型;
#2) TAGS 列名不能與其他列名相同;
#3) TAGS 列名不能為預留關鍵字;
#4) TAGS 最多允許128個,可以0個,總長度不超過16k個字符
create table if not exists mysupertable (time timestamp, column_name int,……) tags (column_name nchar(50), column_name nchar(100),……);
#刪除超級表
drop table if exists super_table ;
#顯示當前數據庫下的所有超級表信息
show stables like "%super%";
#獲取超級表的結構信息
describe super_table ;
#超級表增加列
alter table super_table add column column_name int;
#超級表刪除列
alter table super_table drop column column_name;
#添加標簽
alter table super_table add tag column nchar(60);
#刪除標簽
alter table super_table drop tag tag_name;
#修改標簽名
alter table super_table change tag old_tag_name new_tag_name;
#修改子表標簽值(TAG)
alter table item_table_name set tag column_key = "value";
6. #解釋一下(超級表)super_table,(子表)sub_table,(標簽)Tag之間的關系
在物聯網中,假設我們現在有一個小區的電表設備需要聯網。那么電表就會存在張三家的電表,李四家的電表,張三家電表的電流和電壓,李四家的電流和電壓,以及王五等等家的設備信息。
那么,作為電表這個物聯設備,就可以設計成超級表super_table,這樣電表就有了張三的電表sub_table1,李四家的電表sub_table2,等等,電流和電壓就是超級表中定義表字段屬性,而電表所屬的業主名稱,小區地址可以存放在TAG。
這個場景中,我們就可以下如下創建語句
首先:創建電表超級表:super_table
create database mydb; use mydb; create table super_dianbiao(ts timestamp,dianya float,dianliu float) tags (yezhu_name nchar(15),xiaoqu_location nchar(50),menpai_num nchar(10));
其次:創建子表dianbiao……
create table dianbiao1001 using super_dianbiao tags('張三','東城小區','1-1101'); create table dianbiao1002 using super_dianbiao tags('李四','東城小區','1-1102');
最后:往子表中插入數據
insert into dianbiao1001 values(now,1.7,3.2);