Export data by using OUTFILE statements

2023-10-27 09:57:43  Updated

The SELECT INTO OUTFILE statement allows you to specify the fields to be exported and are preferred when primary key fields 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

This syntax is supported in OceanBase Database in MySQL mode. We recommend that you use OceanBase Database V2.2.40 and later in MySQL mode, where you can run commands by using MySQL Client and OBClient.

Notice

To export data from OceanBase Database, the client must be directly connected to the OceanBase Database instance.

Syntax

The following syntax is supported for a SELECT INTO OUTFILE statement:

SELECT [column_list] INTO OUTFILE {'/path/file' | 'oss://$PATH/$FILENAME/?host=$HOST&access_id=$ACCESSID&access_key=$ACCESSKEY'}
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [FROM  TABLENAME]
        [WHERE condition]
        [GROUP BY group_expression_list ]
        [HAVING condition]]
        [ORDER BY order_expression_list]
Parameter Required Description Example
SELECT [column_list] Yes The columns to be exported. To specify all data, use an asterisk (*). select ID,name,score
'/PATH/FILE' or 'oss://$PATH/$FILENAME/?host=$HOST&access_id=$ACCESSID&access_key=$ACCESSKEY' Yes The path of the exported file. Alibaba Cloud Object Storage Service (OSS) is supported.
Note
Alibaba Cloud OSS does not support files larger than 5 GB in size. When you export a file larger than 5 GB in size to Alibaba Cloud OSS, it is split into multiple smaller files.
into outfile '/home/admin/student.sql'
[{FIELDS | COLUMNS} [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char'] ] Yes Specifies the field format in the output file.
  • TERMINATED BY: specifies the separator between field values. For example, TERMINATED BY ',' indicates that field values are separated by commas (,).
  • enclosed by: specifies the symbol for enclosing field values. For example, ENCLOSED BY ' " ' indicates that field values are enclosed in double quotation marks ("), and ENCLOSED BY ' " ' OPTIONALLY indicates that only string values are enclosed in double quotation marks (").
  • escaped by: specifies the escape character. For example, ESCAPED BY '*' indicates that the asterisk (*) but not the backslash () is used as the escape character. The backslash () is the default escape character.
terminated by ',' enclosed by '"'
[LINES [STARTING BY 'string'] [TERMINATED BY 'string'] ] Yes LINES: specifies the characters to enclose each line in the output file.
  • STARTING BY: specifies the character to start each line.
  • TERMINATED BY: specifies the character to end each line.
lines terminated by '\n' indicates that a row ends with a line feed.
[FROM TABLENAME] [WHERE condition] [GROUP BY group_expression_list ] [HAVING condition]] [ORDER BY order_expression_list] Yes The table and content to be exported. WHERE: the query conditions. GROUP BY: the grouping attribute. ORDER BY: the sorting attribute. from student

Scenarios and examples

Data export example:

obclient> CREATE TABLE student (ID int primary key,name varchar(128),score int);
Query OK, 0 rows affected (0.04 sec)

obclient> INSERT INTO student VALUES(1,'lin',98),(2,'hei',90),(3,'ali',95);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

obclient> SELECT * FROM student;
+----+------+-------+
| ID | name | score |
+----+------+-------+
|  1 | lin  |    98 |
|  2 | hei  |    90 |
|  3 | ali  |    95 |
+----+------+-------+
3 rows in set (0.00 sec)

obclient> SELECT ID,name,score INTO OUTFILE '/home/admin/student.sql'
            FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
            LINES TERMINATED BY '\n' FROM student;
Query OK, 3 rows affected (0.01 sec)

Then you can check the information about the exported file in the /home/admin directory on your local device.

[root@***** /home/admin]cat student.sql
1,"lin",98
2,"hei",90
3,"ali",95

More information

You can use the LOAD DATA statement to import the files that are exported with OUTFILE. For more information, see Import data by using the LOAD DATA statement.

Contact Us