All Products
Search
Document Center

ApsaraDB for SelectDB:Create a database and write data to the database

Last Updated:Jun 06, 2024

Before you write data to or query data from ApsaraDB for SelectDB, you must create a database for an ApsaraDB for SelectDB instance. This topic describes how to create a database for an ApsaraDB for SelectDB instance and write data to the database.

Prerequisites

You have connected to an ApsaraDB for SelectDB instance over the MySQL protocol. For more information, see Connect to an instance.

Usage notes

  • You can create up to 256 databases for an ApsaraDB for SelectDB instance.

  • ApsaraDB for SelectDB is compatible with standard SQL syntax. For more information, see CREATE-DATABASE.

Procedure

  1. Execute the CREATE DATABASE statement to create a database named test_db.

    CREATE DATABASE test_db;
  2. Execute the following statements to create a table named test_table in the test_db database.

    USE test_db;
    
    CREATE TABLE test_table
    (
        k1 TINYINT,
        k2 DECIMAL(10, 2) DEFAULT "10.05",
        k3 CHAR(10) COMMENT "string column",
        k4 INT NOT NULL DEFAULT "1" COMMENT "int column"
    )
    COMMENT "my first table"
    DISTRIBUTED BY HASH(k1) BUCKETS 16;
  3. Save the following sample data as a local file and name it data.csv.

    1,0.14,a1,20
    2,1.04,b2,21
    3,3.14,c3,22
    4,4.35,d4,23
  4. Execute the following statement to write the data to the test_table table by using Stream Load: Stream Load submits an import request over the HTTP protocol. In this example, the curl command is run on the on-premises device to submit an import request. For more information, see Import data by using Stream Load.

    curl --location-trusted -u admin:admin_123 -H "label:123" -H "column_separator:," -T data.csv http://host:port/api/test_db/test_table/_stream_load

    Parameters:

    host: the virtual private cloud (VPC) endpoint or public endpoint of the ApsaraDB for SelectDB instance. For more information about how to apply for a public endpoint, see Apply for or release a public endpoint.

    port: the HTTP port number of the ApsaraDB for SelectDB instance.

    Note

    You can view the endpoint and HTTP port number of an ApsaraDB for SelectDB instance on the instance details page in the ApsaraDB for SelectDB console.

  5. Execute the following statement to query data in the test_table table.

    SELECT * FROM test_table;

    The following results are returned:

    +------+------+------+------+
    | k1   | k2   | k3   | k4   |
    +------+------+------+------+
    |    1 | 0.14 | a1   |   20 |
    |    2 | 1.04 | b2   |   21 |
    |    3 | 3.14 | c3   |   22 |
    |    4 | 4.35 | d4   |   23 |
    +------+------+------+------+