This topic describes how to use the SELECT INTO OUTFILE statement to export data. The SELECT INTO OUTFILE statement allows you to specify the fields to be exported, and is preferred when the primary key columns do not need to be exported. You can use this statement in combination with the LOAD DATA INFILE statement to facilitate data import and export.
Background information
OceanBase Database is compatible with the SELECT INTO OUTFILE statement.
| Mode | Recommended OceanBase Database version | Recommended client |
|---|---|---|
| MySQL | V2.2.40 or later | MySQL client or OceanBase Client (OBClient) |
| Oracle | V2.2.40 or later | OBClient |
Notice
To import data into and export data from OceanBase Database, your client must be directly connected to your OceanBase Database instance.
Syntax
SELECT column_list_option
INTO OUTFILE file_route_option
[format_of_field_option]
[start_and_end_option]
FROM table_name_list
[WHERE where_conditions]
[GROUP BY group_by_list [HAVING having_search_conditions]]
[ORDER BY order_expression_list]
column_list_option:
column_name[,column_name]...
file_route_option:
'/path/file'
| 'oss://$PATH/$FILENAME/?host=$HOST&access_id=$ACCESS_ID&access_key=$ACCESSKEY'
format_of_field_option:
{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
start_and_end_option:
LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
| Parameter | Required? | Description | Example |
|---|---|---|---|
| column_list_option | Yes | The list of columns to be exported. To select all data, use an asterisk (*). column_name: the name of a column. |
SELECT col1,col2,col3 ... |
| file_route_option | Yes | The export path, namely the path to which the data is to be exported. Alibaba Cloud Object Storage Service (OSS) is supported.
NoteAlibaba Cloud OSS does not support files larger than 5 GB. When you export a file larger than 5 GB to Alibaba Cloud OSS, it is split into multiple smaller files. |
... INTO OUTFILE '/home/admin/student.sql' ... |
| format_of_field_option | No | The field format options for the exported file. You can use the FIELDS or COLUMNS clause to specify the format of each field in the output file. The options are described as follows:
|
... TERMINATED BY ',' ENCLOSED BY '"' ... |
| start_and_end_option | No | The start and end character options for exported data lines. LINES specifies the characters for enclosing each line in the output file.
|
... LINES TERMINATED BY '\n' ... indicates that a line ends with a line feed. |
| FROM table_name_list | Yes | The object from which data is selected. | ... FROM tbl1,tbl2 ... |
| WHERE where_conditions | No | The filter conditions. Only data that meets the conditions is returned in the query result. | ... WHERE col1 > 100 ... |
| GROUP BY group_by_list | No | Groups data by one or more columns. It is usually used in combination with aggregate functions.
NoteFor all columns following the |
... GROUP BY col1,col2 ... |
| HAVING having_search_conditions | No | Filters the grouped data. The HAVING clause is similar to the WHERE clause, but the HAVING clause can reference an aggregate function such as SUM() and AVG(). |
... HAVING SUM(col1) < 160 ... |
| ORDER BY order_expression_list | No | Sorts the query result by one or more columns in ascending (ASC) or descending (DESC) order. If you do not specify ASC or DESC, the default value ASC is used.
|
... ORDER BY col1,col2 DESC ... |
Examples
The following takes exporting data to a local device as an example to describe how to export data.
Create a table named
tbl1in thetestdatabase in themysql001tenant and insert data into the table.obclient [test]> CREATE TABLE tbl1(col1 INT PRIMARY KEY,col2 varchar(128),col3 INT); Query OK, 0 rows affected obclient [test]> INSERT INTO tbl1 VALUES(1,'one',80),(2,'two',90),(3,'three',100); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 obclient [test]> SELECT * FROM tbl1; +------+-------+------+ | col1 | col2 | col3 | +------+-------+------+ | 1 | one | 80 | | 2 | two | 90 | | 3 | three | 100 | +------+-------+------+ 3 rows in setSet the export path.
Set the system variable
secure_file_privto specify the path that can be accessed for file import or export.Notice
For security reasons, you can only connect to the database through a local socket to execute the SQL statement for setting
secure_file_priv. For more information, see secure_file_priv.Log in to the OBServer node to connect to.
[xxx@xxx /home/admin]# ssh admin@xxx.xxx.xxx.xxxRun the following command to connect to the
mysql001tenant through a local Unix socket:obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******Set the export path to
/home/admin.obclient [(none)]> SET GLOBAL secure_file_priv = "/home/admin"; Query OK, 0 rows affected
Reconnect to the database and execute the
SELECT INTO OUTFILEstatement to export data from thetbl1table. The name of the exported file istbl1.sql. The field values are separated with a comma (,). The values of the string type are enclosed within a pair of double quotation marks ("). The end symbol is a line feed.obclient [test]> SELECT * INTO OUTFILE '/home/admin/tbl1.sql' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM tbl1; Query OK, 3 rows affectedLog in to the OBServer node and check the information about the exported file in the
/home/admindirectory on your local device.[xxx@xxx /home/admin]# cat tbl1.sql 1,"one",80 2,"two",90 3,"three",100
More information
You can use the LOAD DATA statement to import the files that are exported by using SELECT INTO OUTFILE. For more information, see Use the LOAD DATA statement to import data.