Updated on 2024-08-29 GMT+08:00

Creating a Table

A MariaDB data table consists of basic information, columns, generated columns, indexes, and foreign keys, among which generated columns, indexes, and foreign keys are optional. Configure these items as required.

Procedure

  1. Log in to the DAS console.
  2. Click in the upper left corner and select a region and project.
  3. Click in the upper left corner, and under Databases, click Data Admin Service.
  4. In the navigation pane on the left, choose Development Tool.

    You can also click Go to Development Tool on the overview page.

  5. Locate the DB instance that you want to log in to and click Log In in the Operation column.
  6. On the top menu bar, choose Database Management.
  7. Click Change on the right of the current database to switch to the database where you want to create a table.

    Figure 1 Switching to another database

  8. Click Create Table.

    Figure 2 Creating a table

  9. Configure basic information as follows:

    Enter a table name, specify the storage engine, character set, collation, and enter a comment in sequence. The table name is mandatory.
    Figure 3 Basic information

  10. (Optional) Configure advanced settings.

    Specify parameters based on service requirements.
    Figure 4 Advanced settings

    To learn more about partitioned tables, see the following note:

    Table partitioning is used to divide a large table into several small tables based on conditions. Different rows in the table can be allocated to different physical partitions. Creating a partitioned table is not recommended because there are many constraints on MariaDB partitioned tables.

    If you need to create a partitioned table, you can create one by referring to the following example. The supported partitioning methods are RANGE, LIST, COLUMNS, KEY, and HASH.

    For example, if you want to create partitioned table employees, enter the following content for Partition Definition in the Advanced Settings area:

    Creating a partitioned table:

    CREATE TABLE employees (
    id INT NOT NULL,
    fname VARCHAR(30),
    lname VARCHAR(30),
    hired DATE NOT NULL DEFAULT '1970-01-01',
    separated DATE NOT NULL DEFAULT '9999-12-31',
    job_code INT NOT NULL,
    store_id INT NOT NULL
    )
    PARTITION BY RANGE (store_id) (
    PARTITION p0 VALUES LESS THAN (6),
    PARTITION p1 VALUES LESS THAN (11),
    PARTITION p2 VALUES LESS THAN (16),
    PARTITION p3 VALUES LESS THAN (21)
    );

    Specifying a partition definition:

    PARTITION BY RANGE (store_id) (
    PARTITION p0 VALUES LESS THAN (6),
    PARTITION p1 VALUES LESS THAN (11),
    PARTITION p2 VALUES LESS THAN (16),
    PARTITION p3 VALUES LESS THAN (21)
    )

  11. Click Next.
  12. On the Column page, click Add.

    • Set Column Name, Type, Length, Nullable, Primary Key, Comment, and Extended Information as needed.
      • In the Type column, you can select only the parameters from the drop-down list box.
      • In the Length column, retain the default value. For some columns whose length is variable, you can change their lengths.
      • If Primary Key is selected, Nullable will be grayed out.
      • Auto Increment can be set for one column only. When it is selected, Primary Key must be selected, and no default values can be set.
    • If you do not need to add generated columns, indexes, or foreign keys, click Create at the bottom of the page. In the displayed SQL Preview dialog box, click Execute.
      Figure 5 Configuring field information
    • If you need to add generated columns, indexes, and foreign keys, click Next. Then, set the column name, type, length, nullable, primary key, expression, storage type, comment, and extended information. To continue to set an index or foreign key for the table, click Next.
      Figure 6 (Optional) Adding virtual columns
      Figure 7 (Optional) Adding indexes
      Figure 8 (Optional) Adding foreign keys

      When you create a foreign key, the type of columns in the referenced table must be the same as that of included columns, and must be the primary key or have a unique index.

  13. After the setting is complete, click Create.
  14. In the SQL Preview dialog box, click Execute.