02-database.md 3.4 KB
Newer Older
D
dingbo 已提交
1
---
2 3 4
sidebar_label: Database
title: Database Management
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 16 17 18 19 20 21 22

1. KEEP specifies the number of days for which the data in the database to be created will be kept, the default value is 3650 days, i.e. 10 years. The data will be deleted automatically once its age exceeds this threshold.
2. UPDATE specifies whether the data can be updated and how the data can be updated.
   1. UPDATE set to 0 means update operation is not allowed, the data with an existing timestamp will be dropped silently.
   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 part of columns for a row is allowed, the columns for which no value is specified will be kept as no change
3. The maximum length of database name is 33 bytes.
4. The maximum length of a SQL statement is 65,480 bytes.
5. For more parameters that can be used when creating a database, like cache, blocks, days, keep, minRows, maxRows, wal, fsync, update, cacheLast, replica, quorum, maxVgroupsPerDb, ctime, comp, prec, Please refer to [Configuration Parameters](/reference/config/).
D
dingbo 已提交
23 24 25

:::

26
## SHOW THE CURRENT CONFIGURATION
D
dingbo 已提交
27 28 29 30 31

```
SHOW VARIABLES;
```

32
## SPECIFY THE DEFAULT DATABASE IN FOLLOWING OPERATIONS
D
dingbo 已提交
33 34 35 36 37

```
USE db_name;
```

38 39 40 41
:::note
This way is not applicable when using a REST connection

:::
D
dingbo 已提交
42

43
## DROP DATABASE
D
dingbo 已提交
44 45 46 47 48

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

49 50 51 52 53 54
:::note
All data in the database will be deleted too. This command must be used with caution.

:::

## CHANGE DATABASE CONFIGURATION
D
dingbo 已提交
55

56
Some examples are shown below to demonstrate how to change the configuration of a database. Please be noted that some configuration parameters can be changed after the database is created, but some others can't, for details of the configuration parameters of database please refer to [Configuration Parameters](/reference/config/).
D
dingbo 已提交
57 58 59 60 61

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

62
COMP parameter specifies whether the data is compressed and how the data is compressed.
D
dingbo 已提交
63 64 65 66 67

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

68
REPLICA parameter specifies the number of replications of the database.
D
dingbo 已提交
69 70 71 72 73

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

74
KEEP parameter specifies the number of days for which the data will be kept.
D
dingbo 已提交
75 76 77 78 79

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

80
QUORUM parameter specifies the necessary number of confirmations to determine whether the data is written successfully.
D
dingbo 已提交
81 82 83 84 85

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

86
BLOCKS parameter specifies the number of memory blocks used by each VNODE.
D
dingbo 已提交
87 88 89 90 91

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

92
CACHELAST parameter specifies whether and how the latest data of a sub table is cached.
D
dingbo 已提交
93 94

:::tip
95
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 已提交
96

97 98 99
:::

## SHOW ALL THE DATABASES
D
dingbo 已提交
100 101 102 103 104

```
SHOW DATABASES;
```

105
## SHOW THE CREATE STATEMENT OF A DATABASE
D
dingbo 已提交
106 107 108 109 110

```
SHOW CREATE DATABASE db_name;
```

111
This command is useful when migrating the data from one TDengine cluster to another one. Firstly this command can be used to get the CREATE statement, which in turn can be used in another TDengine to create an exactly same database.