You can execute the CREATE TABLE statement to create a mapping table for an existing table or search index. When you create a mapping table, specify the field names and types, primary key information, and the execution engine you want to use for data queries based on your business requirements.
Tablestore does not automatically create SQL mapping tables for data tables. To access a data table by using SQL, you must first execute a CREATE TABLE statement to create a mapping table.
For more information about the syntax, field types, and parameters of the CREATE TABLE statement, see DDL operations.
Usage notes
SQL query is supported in Tablestore SDK for .NET V5.0.0 and later. Make sure that a supported SDK version is installed.
For more information, see Version history of Tablestore SDK for .NET.
Prerequisites
-
A client is initialized. For more information, see Initialize a Tablestore client.
-
If you want to create a mapping table for a search index, you must first create the search index.
Parameters
|
Parameter |
Description |
|
query |
The SQL statement. Configure this parameter based on the required feature. |
Examples
This topic describes how to create a mapping table for a table. If you want to create a mapping table for a search index, replace the SQL statement in the sample code with the SQL statement used to create a mapping table for a search index.
The following example shows how to use the CREATE TABLE `test_table` (`pk0` VARCHAR(1024), `pk1` BIGINT(20), `col0` MEDIUMTEXT, `col1` BIGINT(20), `col2` DOUBLE, `col3` BOOL, `col4` MEDIUMBLOB, `date_col` MEDIUMTEXT, `geo_col` MEDIUMTEXT, `col0_v1` MEDIUMTEXT, `col0_v2` MEDIUMTEXT, PRIMARY KEY(`pk0`,`pk1`)); statement to create a mapping table for the test_table table.
/// <summary>
/// Create a mapping table for a data table.
/// </summary>
/// <param name="otsClient"></param>
public static void CreateMappingTable(OTSClient otsClient)
{
string sqlCommand = @"CREATE TABLE `test_table` (
`pk0` VARCHAR(1024),
`pk1` BIGINT(20),
`col0` MEDIUMTEXT,
`col1` BIGINT(20),
`col2` DOUBLE,
`col3` BOOL,
`col4` MEDIUMBLOB,
`date_col` MEDIUMTEXT,
`geo_col` MEDIUMTEXT,
`col0_v1` MEDIUMTEXT,
`col0_v2` MEDIUMTEXT,
PRIMARY KEY(`pk0`,`pk1`)
);";
SQLQueryRequest sqlQueryRequest = new SQLQueryRequest(sqlCommand);
SQLQueryResponse sqlQueryResponse = otsClient.SQLQuery(sqlQueryRequest);
}
FAQ
What do I do if the "Table 'instancename.tablename' doesn't exist ?" error message is returned when I create a mapping table?
-
Possible cause
The table for which you want to create a mapping table does not exist.
-
Solution
You can execute the CREATE TABLE statement in SQL to create a mapping table only for an existing table or search index. Make sure that the table or search index for which you want to create a mapping table exists.
What do I do if the "Table 'instancename.tablename' already exist?" error message is returned when I create a mapping table?
-
Cause
A mapping table was already created for the table by a CREATE TABLE statement.
-
Solution
You can execute the
DESCRIBEstatement to query the schema of the mapping table. If the schema does not meet your business requirements, you can Update the attribute columns of a mapping table.
For common SQL query errors and solutions, see Troubleshoot common SQL query errors.
References
-
After you create a mapping table, you can update the attribute columns of the mapping table, query information of the mapping table, query the names of mapping tables, query the index information of the mapping table, and delete the mapping table. For more information, see Update attribute columns of a mapping table, Query information about a table, Query the names of mapping tables, Query index information about a table, and Delete a mapping table.
-
You can use the SELECT statement to query and analyze data in a mapping table. For more information, see Query data.