Tablestore SDK for PHP creates a mapping table for an existing table or search index to enable SQL access to data.
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, see Create a mapping table for a table and Create mapping tables for search indexes.
Usage notes
SQL query is supported in Tablestore SDK for PHP V5.1.0 and later. Make sure that a supported version is installed.
For the version history of Tablestore SDK for PHP, see Version history of Tablestore SDK for PHP.
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
SQLQueryRequest contains the following parameter.
|
Name |
Type |
Description |
|
query (required) |
String |
The |
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 `tableName` (`PK0` BIGINT(20),`PK1` VARCHAR(1024),`geo` MEDIUMTEXT,`boolean` BOOL,`array` MEDIUMTEXT,`double` DOUBLE,`text` MEDIUMTEXT,`keyword` MEDIUMTEXT,`nested` MEDIUMTEXT,`long` BIGINT(20),PRIMARY KEY(`PK0`,`PK1`)); statement to create a mapping table for the tableName table.
$request = array(
'query' => 'CREATE TABLE `tableName` (`PK0` BIGINT(20),`PK1` VARCHAR(1024),`geo` MEDIUMTEXT,`boolean` BOOL,`array` MEDIUMTEXT,`double` DOUBLE,`text` MEDIUMTEXT,`keyword` MEDIUMTEXT,`nested` MEDIUMTEXT,`long` BIGINT(20),PRIMARY KEY(`PK0`,`PK1`));',
);
$response = $otsClient->sqlQuery($request);
print json_encode($response, JSON_PRETTY_PRINT);
FAQ
The Table 'instance.table' doesn't exist error is returned when you create a mapping table
Cause: The specified data table or search index does not exist. The CREATE TABLE statement creates only a mapping table for an existing data table or search index. It does not create the data table or search index.
Solution: Check whether the data table or search index name is correct and make sure that the data table or search index exists. Then, execute the CREATE TABLE statement again.
The Table 'instance.table' already exists error is returned when you create a mapping table
Cause: The mapping table already exists. It was created by a CREATE TABLE statement.
Solution:
-
If you want to prevent an error when the mapping table already exists, add
IF NOT EXISTSto theCREATE TABLEstatement. -
If you want to change the schema of the mapping table, first execute a
DESCRIBEstatement to view the schema, and then use anALTER TABLEstatement to modify the mapping table.
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, List the names of mapping tables, Query the 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.