Overview
Scenarios
Product Architecture
Instance Types
Compatibility Notes
Usage specification recommendations
CREATE TABLE not only defines the logical structure of data (columns, indexes, constraints), but also affects the storage and distribution policy of data in the distributed cluster to some extent.Table Type | Introduction | Scenarios |
Single table | Data is not split and is stored entirely on a single storage node. | Tables with small data volumes and low write concurrency requirements. |
Partition Table | Data is scattered based on the partition key and distributed across multiple storage nodes, enabling horizontal scaling. | Business tables with large data volumes, rapid growth, and requiring high-concurrency writes (such as orders and transaction records). |
Synchronized Table/Broadcast Table | A table type that fully replicates data to all storage nodes to enable local joins and enforces strong synchronous writes for all replicas (Note: Currently, synchronized tables must be used together with broadcast tables). | Suitable for scenarios with small data volumes (e.g., dictionaries/configurations), frequent reads and infrequent writes, and frequent join queries with large tables. |
CREATE TABLE The statement typically takes the following form:CREATE TABLE <table_name> (<create_definitions>) {table_options};
<table_name>: Required. The unique identifier of the table.<create_definitions> : Required. Includes column definitions, primary key constraints, and so on.{table_options} : Optional. Common options include COMMENT='...', CHARSET=utf8mb4, and possible custom syntax.<column_name> <data_type> {column_qualification}
<column_name>: The identifier of the column.<data_type>: Specifies the type of data that can be stored in this column (such as integers, characters, time, and so on).{column_qualification}: Optional column-level constraints (such as NOT NULL, DEFAULT) or generation policy.CREATE TABLE `warehouse`.`inventory_items` (`sku_id` bigint NOT NULL,`item_name` varchar(150),`storage_zone` enum('Cold_Chain', 'Dry_Goods', 'Hazardous', 'General'),`entry_time` datetime,`quantity` int,`unit_cost` decimal(18,4),PRIMARY KEY (`sku_id`)COMMENT='Product Inventory Core Table';
bigint (8 bytes) instead of int is to handle the potential billions of product categories in large warehouses.varchar(150) is a variable-length character string. If a product name is only 10 characters long, it occupies approximately 10 bytes of space (plus a length prefix), without forced space padding like char, making it ideal for storing text of varying lengths.enum enforces data compliance with predefined specifications, preventing inconsistencies like "ColdChain" and "cold_chain".int supports up to approximately 2.1 billion, which is typically more than sufficient for individual item inventory.decimal(18,4). Precision 18: supports extremely large total amounts. Scale 4: retains 4 decimal places (such as 12.5678).NULL values.INT, BIGINT, DECIMAL, VARCHAR) to minimize parsing overhead and maximize storage efficiency.NOT NULL constraint, UNIQUE KEY constraint and DEFAULT value, and so on.DEFAULT constraint. This way, when inserting data, you don't need to specify values for every column, and the database will automatically populate them.CREATE TABLE `warehouse`.`inventory_items` (`sku_id` bigint NOT NULL,`item_name` varchar(150),`storage_zone` enum('Cold_Chain', 'Dry_Goods', 'Hazardous', 'General'),-- Use DEFAULT CURRENT_TIMESTAMP to automatically populate the current timestamp`entry_time` datetime DEFAULT CURRENT_TIMESTAMP,`quantity` int,`unit_cost` decimal(18,4),PRIMARY KEY (`sku_id`)COMMENT='Product Inventory Core Table';
UNIQUE constraint.sku_id is the primary key, you may also require that the product name (item_name) must be unique across the entire warehouse. This can be rewritten as:CREATE TABLE `warehouse`.`inventory_items` (`sku_id` bigint NOT NULL,-- Add a UNIQUE constraint; inserting duplicate product names will result in an error`item_name` varchar(150) UNIQUE,`storage_zone` enum('Cold_Chain', 'Dry_Goods', 'Hazardous', 'General'),`entry_time` datetime,`quantity` int,`unit_cost` decimal(18,4),PRIMARY KEY (`sku_id`)COMMENT='Product Inventory Core Table';
NOT NULL constraint.quantity cannot be NULL either (it can default to 0). This can be rewritten as:CREATE TABLE `warehouse`.`inventory_items` (`sku_id` bigint NOT NULL,-- No duplicates or nulls`item_name` varchar(150) UNIQUE NOT NULL,`storage_zone` enum('Cold_Chain', 'Dry_Goods', 'Hazardous', 'General'),`entry_time` datetime DEFAULT CURRENT_TIMESTAMP,-- Cannot be NULL, and the default value is 0`quantity` int NOT NULL DEFAULT 0,`unit_cost` decimal(18,4),PRIMARY KEY (`sku_id`)COMMENT='Product Inventory Core Table';
ALTER TABLE ... PARTITION BY ... statement to convert it to a partitioned table online. The data will be automatically redistributed to other nodes in the cluster without downtime migration.Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback