This topic describes how to create an external table by using SQL statements. It also covers the prerequisites, overview, and considerations for creating an external table, and provides examples.
Overview
An external table is a logical table object. It does not store data internally in the database but in an external storage system.
For more information about external tables, see Overview.
Prerequisites
Before you create an external table, make sure that the following conditions are met:
You have deployed an OceanBase cluster and created an Oracle tenant. For more information about how to deploy an OceanBase cluster, see Deployment overview.
You have connected to the Oracle tenant of OceanBase Database. For more information about how to connect to the database, see Overview of connection methods.
The current user has the
CREATE TABLEprivilege to create an external table. For more information about how to view the privileges of the current user, see View user privileges. If you do not have this privilege, contact the administrator to request it. For more information about how to grant privileges to a user, see Modify user privileges.
Considerations
An external table can only be queried, and DML operations are not supported.
When you query an external table, if the external file accessed by the external table has been deleted, the system does not return an error, but instead returns an empty result set.
If the external storage system that manages the file accessed by the external table becomes unavailable, an error is returned when you query the external table.
External tables store data in external data sources. Therefore, queries involve factors such as cross-network and file system access, which may affect query performance. Make sure to select an appropriate data source and optimize strategy when you create an external table to improve query efficiency.
Create an external table
Use the CREATE EXTERNAL TABLE statement to create an external table.
Define an external table name
When you create an external table, you must name it. We recommend that you use specific naming conventions or prefixes to distinguish external tables from regular tables when you name external tables. For example, you can add a suffix such as _csv to the name of an external table.
Here is an example:
When you create an external table that stores student information, you can name the table students_csv.
CREATE EXTERNAL TABLE students_csv external_options
Notice
The preceding SQL statement cannot be executed because no other attributes of the external table are specified.
Define columns
You cannot define constraints, such as DEFAULT, NOT NULL, UNIQUE, CHECK, PRIMARY KEY, and FOREIGN KEY, for columns of an external table.
The column types supported by external tables are the same as those supported by regular tables. For more information about the data types supported in Oracle mode of OceanBase Database, see Overview.
Define LOCATION
The LOCATION option specifies the path where the files of the external table are stored. Generally, the data files of an external table are stored in a dedicated directory, which can contain subdirectories. When you create an external table, the system automatically collects all files in the directory that you specified.
OceanBase Database supports the following two path formats:
Local path:
LOCATION = '[file://] local_file_path'Notice
In a scenario that uses the local path format, you must set the system variable
secure_file_privto specify an accessible path. For more information, see secure_file_priv.Remote path:
LOCATION = '{oss|cos}://$ACCESS_ID:$ACCESS_KEY@$HOST/remote_file_path'You must specify the
$ACCESS_ID,$ACCESS_KEY, and$HOSTparameters for accessing Alibaba Cloud OSS or Tencent Cloud COS. The sensitive access information is encrypted and stored in system tables of the database.
Notice
When you specify an object storage path, the parameters of the path are separated by the & character. Make sure that the values of the parameters you entered contain only uppercase and lowercase letters, numbers, and the following characters: /-_$+=. If you enter characters other than the preceding ones, the setting may fail.
Define FORMAT
The FORMAT option specifies the format of external files. The following parameters are supported:
TYPE: the type of external files. Currently, only the CSV file type is supported.LINE_DELIMITER: the line delimiter for CSV files. The default value isLINE_DELIMITER='\n'.FIELD_DELIMITER: the field delimiter for CSV files. The default value isFIELD_DELIMITER='\t'.ESCAPE: the escape character for CSV files, which can be only 1 byte in length. The default value isESCAPE ='\'.FIELD_OPTIONALLY_ENCLOSED_BY: the characters that enclose field values in CSV files. The default value is an empty string.ENCODING: the character set encoding format of files. For more information about the character sets supported in the Oracle mode, see Character sets. If this parameter is not specified, the default valueUTF8MB4takes effect.NULL_IF: the strings that are to be treated asNULL. The default value is an empty string.SKIP_HEADER: specifies to skip the file header and indicates the number of lines to skip.SKIP_BLANK_LINES: specifies whether to skip blank lines. The default value isFALSE, which indicates that blank lines are not skipped.TRIM_SPACE: specifies whether to remove leading and trailing spaces from fields. The default value isFALSE, which indicates that leading and trailing spaces are not removed.EMPTY_FIELD_AS_NULL: specifies whether to treat empty strings asNULL. The default value isFALSE, which indicates that empty strings are not treated asNULL.
(Optional) Define a pattern
The PATTERN option specifies a regular pattern string for filtering files in the LOCATION directory. For each file in the LOCATION directory, the file path is matched against the pattern string. If the file path matches the pattern string, the external table accesses the file. Otherwise, the external table skips the file. If this parameter is not specified, the external table accesses all files in the LOCATION directory. The external table stores the list of files that match the PATTERN in the database system table. When the external table scans for data, it accesses the external files based on the list. The file list can be automatically updated or manually updated.
Example
Notice
The commands in the following examples are desensitized. Replace the IP address with your real IP address when you verify the examples.
The following examples show how to create an external table when the external file is located locally and in OceanBase Database in Oracle mode. The procedure is as follows:
Prepare the external file.
Execute the following command to create a file named
test_tbl1.csvin the/home/admin/external_csvdirectory on the server that you will log in to later.[admin@xxx /home/admin/external_csv]# vi test_tbl1.csvThe content of the file is as follows:
1,'Emma' 2,'William' 3,'Olivia'Set the path of the imported file.
Notice
For security reasons, when you set the system variable
secure_file_priv, you can connect to the database only by using a local socket to execute the SQL statement that modifies the global variable. For more information, see secure_file_priv.Execute the following command to log in to the server where the OBServer node is located.
ssh admin@10.10.10.1Execute the following command to connect to the
oracle001tenant by using a local Unix socket.obclient -S /home/admin/oceanbase/run/sql.sock -usys@oracle001 -p******Execute the following SQL command to set the import path to
/home/admin/external_csv.SET GLOBAL secure_file_priv = "/home/admin/external_csv";
Reconnect to the
oracle001tenant.Here is an example:
obclient -h10.10.10.1 -P2881 -usys@oracle001 -p****** -AExecute the following SQL command to create an external table named
test_tbl1_csv.CREATE EXTERNAL TABLE test_tbl1_csv ( id INT, name VARCHAR(50) ) LOCATION = '/home/admin/external_csv' FORMAT = ( TYPE = 'CSV' FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY ='''' ) PATTERN = 'test_tbl1.csv';Execute the following SQL command to view the data in the external table
test_tbl1_csv.SELECT * FROM test_tbl1_csv;The return result is as follows:
+------+---------+ | ID | NAME | +------+---------+ | 1 | Emma | | 2 | William | | 3 | Olivia | +------+---------+ 3 rows in set
References
For more information about how to view and update external tables, see Manage external files.
