CREATE TABLE (Version 2.0)
Updated at:
Copy as MD
In AnalyticDB for MySQL, the CREATE TABLE statement is used to create standard tables.
Create a dimension table
Syntax
CREATE DIMENSION TABLE table_name (
column_name column_type [NOT NULL][DEFAULT 'default'][COMMENT 'comment'][, …],
[FULLTEXT INDEX index_name (column_name),]
primary key (column_name[, …])
)Parameters
- CREATE DIMENSION TABLE: The
DIMENSIONkeyword specifies that the table to be created is a dimension table. - column_type: The data type of the column. For more information about the data types that AnalyticDB for MySQL supports, see Data types.
- NOT NULL: An optional column property. A column defined with
NOT NULLcannot containNULLvalues. If this property is not defined,NULLvalues are allowed by default. - DEFAULT ‘default’: Optional. This property specifies a default value for the column.
- COMMENT ‘comment’: Optional. This property specifies a comment for the column.
- FULLTEXT INDEX: Optional. Creates a full-text index on the specified column. The name of the index is
index_name. The data type ofcolumn_namecan be Varchar or Clob. Varchar is the recommended data type. - primary key: Specifies the primary key. A composite primary key is supported.
Example
CREATE DIMENSION TABLE goods (
goods_id bigint comment 'Product ID',
price double comment 'Price',
class bigint comment 'Category',
name varchar comment 'Name',
update_time timestamp comment 'Update time',
FULLTEXT INDEX name_fulltext (name),
primary key (goods_id)
)Create a standard table
Syntax
CREATE TABLE table_name (
column_name column_type [NOT NULL][DEFAULT 'default'][COMMENT 'comment'][, …],
[FULLTEXT INDEX index_name (col_name),]
primary key (column_name[, … ])
)
PARTITION BY HASH KEY(column_name)
[PARTITION NUM N]
TABLEGROUP tablegroup_nameParameters
- column_type: The data type of the column. For more information about the data types that AnalyticDB for MySQL supports, see Data types.
- NOT NULL: An optional column property. A column defined with
NOT NULLcannot containNULLvalues. The default is to allowNULLvalues. - DEFAULT ‘default’: Optional. This property specifies a default value for the column.
- COMMENT ‘comment’: Optional. This property specifies a comment for the column.
- FULLTEXT INDEX: Optional. Creates a full-text index on the specified column. The name of the index is
index_name. The data type ofcolumn_namecan be Varchar or Clob. Varchar is the recommended data type. - primary key: Specifies the primary key. A composite primary key is supported. The primary key of a standard table must contain the partition key column.
- HASH KEY: Specifies the partition key column. For more information, see Plan and design hash partitions.
- PARTITION NUM: Optional. The number of hash partitions. The default value is 128.
- TABLEGROUP: Specifies the table group to which the table belongs.Important You must create a table group before you create a standard table. Otherwise, an error is reported when the `CREATE TABLE` statement is executed. For more information about how to create a table group, see Create a table group.
Example
Create a table named CUSTOMER in the
table_group table group.CREATE TABLE customer (
customer_id bigint NOT NULL COMMENT 'Customer ID',
customer_name varchar NOT NULL COMMENT 'Customer name',
phone_num bigint NOT NULL COMMENT 'Phone number',
city_name varchar NOT NULL COMMENT 'City',
sex int NOT NULL COMMENT 'Gender',
id_number varchar NOT NULL COMMENT 'ID card number',
home_address varchar NOT NULL COMMENT 'Home address',
office_address varchar NOT NULL COMMENT 'Office address',
age int NOT NULL COMMENT 'Age',
login_time timestamp NOT NULL COMMENT 'Logon time',
FULLTEXT INDEX address_fulltext (home_address),
PRIMARY KEY (customer_id,phone_num)
)
PARTITION BY HASH KEY (customer_id)
TABLEGROUP table_group
COMMENT 'Customer information table';Related articles
Is this page helpful?