Updated on 2022-08-17 GMT+08:00

Creating a Table

Database and Table Sharding

The following is an example when HASH is used for database sharding and MOD_HASH for table sharding:

CREATE TABLE tbpartition_tbl (
id int NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID',
name varchar(128),
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
DBPARTITION BY HASH(id)
TBPARTITION BY mod_hash(name) tbpartitions 8;

Database Sharding

The following is an example when HASH is used:

CREATE TABLE dbpartition_tbl (
id int NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID',
name varchar(128),
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
DBPARTITION BY HASH(id);

Creating a Broadcast Table

The following is an example statement:

CREATE TABLE broadcast_tbl (
id int NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID',
name varchar(128),
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
BROADCAST;

Creating a Table When Sharding Is not Used

The following is an example statement:

CREATE TABLE single(
id int NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID',
name varchar(128),
PRIMARY KEY(id)
);