
# 创建表
本章节主要介绍Doris创建表的SQL基本语法和使用说明。
#### 基本语法
```
CREATE TABLE [IF NOT EXISTS] [database.]table
(
column_definition_list,
[index_definition_list]
)
[engine_type]
[keys_type]
[table_comment]
[partition_info]
distribution_desc
[rollup_list]
[properties]
[extra_properties]
```
#### 使用示例
- 创建一个名为**table1** 的普通表：
  ```
  CREATE TABLE example_db.table1
  (
  k1 TINYINT,
  k2 DECIMAL(10, 2) DEFAULT "10.5",
  k3 CHAR(10) COMMENT "string column",
  k4 INT NOT NULL DEFAULT "1" COMMENT "int column"
  )
  COMMENT "table comment"
  DISTRIBUTED BY HASH(k1) BUCKETS 32;
  ```
  
- 创建一个名为**table2** 的分区表。使用**event_day** 列作为分区列，建立3个分区：p201706、p201707、p201708，取值为：
  - p201706：范围为\[最小值, 2017-07-01)
  
  - p201707：范围为\[2017-07-01, 2017-08-01)
  
  - p201708：范围为\[2017-08-01, 2017-09-01)
  
  
  每个分区使用**siteid**进行哈希分桶，桶数为10。创建表命令如下：
  ```
  CREATE TABLE table2
  (
  event_day DATE,
  siteid INT DEFAULT '10',
  citycode SMALLINT,
  username VARCHAR(32) DEFAULT '',
  pv BIGINT SUM DEFAULT '0'
  )
  AGGREGATE KEY(event_day, siteid, citycode, username)
  PARTITION BY RANGE(event_day)
  (
  PARTITION p201706 VALUES LESS THAN ('2017-07-01'),
  PARTITION p201707 VALUES LESS THAN ('2017-08-01'),
  PARTITION p201708 VALUES LESS THAN ('2017-09-01')
  )
  DISTRIBUTED BY HASH(siteid) BUCKETS 10
  PROPERTIES("replication_num" = "3");
  ```
  ![](https://support.huaweicloud.com/usermanual-cloudtable/public_sys-resources/note_3.0-zh-cn.png)
  - Doris创建表时限制至少指定3副本，以保证高可用。
  
  - 如果是非节点集群，则不指定副本。
  
  - 可以对Table增加上卷表（Rollup）以提高查询性能。
  
  - 表的列的Null属性默认为**true**，会对查询性能有一定的影响。
  
  - Doris表必须指定分桶列。
    

- 查看表内容：
  - 查看当前数据库中表。
    ```
    SHOW TABLES;
    ```
    ```
    +----------------------+
    | Tables_in_example_db |
    +----------------------+
    | table1               |
    | table2               |
    +----------------------+
    2 rows in set (0.01 sec)
    ```
    
  
  - 查看table1表结构。
    ```
    DESC table1;
    ```
    ```
    +----------+-------------+------+-------+---------+-------+
    | Field    | Type        | Null | Key   | Default | Extra |
    +----------+-------------+------+-------+---------+-------+
    | siteid   | int(11)     | Yes  | true  | 10      |       |
    | citycode | smallint(6) | Yes  | true  | N/A     |       |
    | username | varchar(32) | Yes  | true  |         |       |
    | pv       | bigint(20)  | Yes  | false | 0       | SUM   |
    +----------+-------------+------+-------+---------+-------+
    4 rows in set (0.00 sec)
    ```
    
  
  - 查看table2表结构。
    ```
    DESC table2;
    ```
    ```
    +-----------+-------------+------+-------+---------+-------+
    | Field     | Type        | Null | Key   | Default | Extra |
    +-----------+-------------+------+-------+---------+-------+
    | event_day | date        | Yes  | true  | N/A     |       |
    | siteid    | int(11)     | Yes  | true  | 10      |       |
    | citycode  | smallint(6) | Yes  | true  | N/A     |       |
    | username  | varchar(32) | Yes  | true  |         |       |
    | pv        | bigint(20)  | Yes  | false | 0       | SUM   |
    +-----------+-------------+------+-------+---------+-------+
    5 rows in set (0.00 sec)
    ```
    
   
 
