02-database.md 4.6 KB
Newer Older
D
dingbo 已提交
1
---
2
sidebar_label: Database
3
title: Database
4
description: "create and drop database, show or change database parameters"
D
dingbo 已提交
5 6
---

7
## Create Database
D
dingbo 已提交
8 9 10 11 12 13

```
CREATE DATABASE [IF NOT EXISTS] db_name [KEEP keep] [DAYS days] [UPDATE 1];
```

:::info
14

15
1. KEEP specifies the number of days for which the data in the database will be retained. The default value is 3650 days, i.e. 10 years. The data will be deleted automatically once its age exceeds this threshold.
16
2. UPDATE specifies whether the data can be updated and how the data can be updated.
17 18 19
   1. UPDATE set to 0 means update operation is not allowed. The update for data with an existing timestamp will be discarded silently and the original record in the database will be preserved as is.
   2. UPDATE set to 1 means the whole row will be updated. The columns for which no value is specified will be set to NULL.
   3. UPDATE set to 2 means updating a subset of columns for a row is allowed. The columns for which no value is specified will be kept unchanged.
20 21
3. The maximum length of database name is 33 bytes.
4. The maximum length of a SQL statement is 65,480 bytes.
22 23 24 25 26 27 28 29 30 31 32 33 34 35
5. Below are the parameters that can be used when creating a database
   - cache: [Description](/reference/config/#cache)
   - blocks: [Description](/reference/config/#blocks)
   - days: [Description](/reference/config/#days)
   - keep: [Description](/reference/config/#keep)
   - minRows: [Description](/reference/config/#minrows)
   - maxRows: [Description](/reference/config/#maxrows)
   - wal: [Description](/reference/config/#wallevel)
   - fsync: [Description](/reference/config/#fsync)
   - update: [Description](/reference/config/#update)
   - cacheLast: [Description](/reference/config/#cachelast)
   - replica: [Description](/reference/config/#replica)
   - quorum: [Description](/reference/config/#quorum)
   - comp: [Description](/reference/config/#comp)
G
gccgdb1234 已提交
36
   - precision: [Description](/reference/config/#precision)
37
6. Please note that all of the parameters mentioned in this section are configured in configuration file `taos.cfg` on the TDengine server. If not specified in the `create database` statement, the values from taos.cfg are used by default. To override default parameters, they must be specified in the `create database` statement.
38
   
D
dingbo 已提交
39 40
:::

41
## Show Current Configuration
D
dingbo 已提交
42 43 44 45 46

```
SHOW VARIABLES;
```

47
## Specify The Database In Use
D
dingbo 已提交
48 49 50 51 52

```
USE db_name;
```

53
:::note
54
This way is not applicable when using a REST connection. In a REST connection the database name must be specified before a table or stable name. For e.g. to query the stable "meters" in database "test" the query would be "SELECT count(*) from test.meters"
55 56

:::
D
dingbo 已提交
57

58
## Drop Database
D
dingbo 已提交
59 60 61 62 63

```
DROP DATABASE [IF EXISTS] db_name;
```

64
:::note
65
All data in the database will be deleted too. This command must be used with extreme caution. Please follow your organization's data integrity, data backup, data security or any other applicable SOPs before using this command.
66 67 68

:::

69
## Change Database Configuration
D
dingbo 已提交
70

71
Some examples are shown below to demonstrate how to change the configuration of a database. Please note that some configuration parameters can be changed after the database is created, but some cannot. For details of the configuration parameters of database please refer to [Configuration Parameters](/reference/config/).
D
dingbo 已提交
72 73 74 75 76

```
ALTER DATABASE db_name COMP 2;
```

77
COMP parameter specifies whether the data is compressed and how the data is compressed.
D
dingbo 已提交
78 79 80 81 82

```
ALTER DATABASE db_name REPLICA 2;
```

83
REPLICA parameter specifies the number of replicas of the database.
D
dingbo 已提交
84 85 86 87 88

```
ALTER DATABASE db_name KEEP 365;
```

89
KEEP parameter specifies the number of days for which the data will be kept.
D
dingbo 已提交
90 91 92 93 94

```
ALTER DATABASE db_name QUORUM 2;
```

95
QUORUM parameter specifies the necessary number of confirmations to determine whether the data is written successfully.
D
dingbo 已提交
96 97 98 99 100

```
ALTER DATABASE db_name BLOCKS 100;
```

101
BLOCKS parameter specifies the number of memory blocks used by each VNODE.
D
dingbo 已提交
102 103 104 105 106

```
ALTER DATABASE db_name CACHELAST 0;
```

107
CACHELAST parameter specifies whether and how the latest data of a sub table is cached.
D
dingbo 已提交
108 109

:::tip
110
The above parameters can be changed using `ALTER DATABASE` command without restarting. For more details of all configuration parameters please refer to [Configuration Parameters](/reference/config/).
D
dingbo 已提交
111

112 113
:::

114
## Show All Databases
D
dingbo 已提交
115 116 117 118 119

```
SHOW DATABASES;
```

120
## Show The Create Statement of A Database
D
dingbo 已提交
121 122 123 124 125

```
SHOW CREATE DATABASE db_name;
```

126
This command is useful when migrating the data from one TDengine cluster to another. This command can be used to get the CREATE statement, which can be used in another TDengine instance to create the exact same database.