This topic describes how to create a table in the ODC console.
Background information
OceanBase Developer Center (ODC) allows you to create a table on a GUI.
Create an internal table
The procedure comprises seven steps:
Specify the basic information.
Create a table.
Set indexes. (Optional)
Set constraints. (Optional)
Set partitioning rules. (Optional)
Verify the SQL statement.
Complete the table creation.
Create an external table
An external table is a logical table object. Its data is stored in an external storage system instead of the database. For more information about external tables, see Overview.
ODC V4.3.3 and later allow you to create an external table by executing SQL statements in the SQL window, and to map an external file to a database managed by ODC by specifying the file path. You can view the existing external tables in the resource tree of the SQL window. For more information, see Create an external table.
You can use the CREATE EXTERNAL TABLE
statement to create an external table.
External tables are read-only. You can query an external table, but cannot perform DML operations on external tables.
You need to specify the
secure_file_priv
variable before you create an external table in ODC by using a local file. To specify the variable, you can connect to OceanBase Database only by using a local Unix socket. ODC does not support this connection method, and you must use obclient to configure the connection. For more information, see secure_file_priv.
Create an external file.
Create an external table in the SQL window of ODC by executing SQL statements.
View the external table in the resource tree.
Procedure
Create a table in the physical database
In the following example, a table named employee
is created in the ODC console. The table contains the emp_no
,birthday
, name
, and gender
columns.
The data used in this topic are examples. You can replace them with actual data as needed.
Step 1: Specify the basic information
Log on to the ODC console and click the name of the desired connection to go to the corresponding connection management page. You can click Table in the left-side navigation pane to view tables. To create a table, click the plus sign (+) in the upper-right corner of the table list or click Create in the top navigation bar.
In the Basic Info section, set the Table Name and Description parameters.
NoteIn MySQL mode, you also need to set theDefault Character SetandDefault Collationparameters.
After you specify the basic information and go to the "Set columns" step, the basic information is submitted.
Step 2: Set columns
In ODC V4.2.3 and later, you can create a table that contains columns of spatial data types in OceanBase Database in MySQL mode or MySQL Database.
The following figure and table show the information that you need to specify when you add a column.
The following table describes operations that you can perform on the column setting page.
Operation | Description |
Toolbar operations | You can add or delete columns by using the top toolbar of the page. |
Click a row ID |
|
Right-click a row | You can right-click a row to select it, and then select Copy or Move Down from the context menu that appears. |
After you copy a row, you can paste the row by using theCommand + VorCtrl + Vshortcut keys.
The auxiliary editing section at the bottom of the page displays additional information about the selected column.
The basic information and column settings are required. The settings in other configuration steps are optional. After you specify the basic information and column settings, you can submit the settings and confirm the SQL statement to create the table.
Step 3: Set indexes
If a table contains a large amount of data, you can use indexes to accelerate data queries. An index is a data structure that pre-sorts the values of one or more columns in a table. By using indexes, you can directly locate records that meet the conditions.
The following figure and table show the information that you need to specify when you set an index.
The index setting page provides the following icons.
Icon | Description |
Create | Click this icon to create an index. |
Delete | Click this icon to delete the selected index. |
Step 4: Set constraints
Constraints are used to specify data rules for a table. A data operation that violates the constraints is terminated.
ODC supports the following four types of table constraints:
PRIMARY KEY constraint: defines a primary key to uniquely identify each row of data in the table. A PRIMARY KEY constraint can be a field or a group of fields. You can set only one PRIMARY KEY constraint for a table, and you cannot modify the PRIMARY KEY constraint after you configure the constraint.
UNIQUE constraint: ensures that the data in a field or a group of fields is unique in the table. You can set multiple UNIQUE constraints in one table.
FOREIGN KEY constraint: associates one or more columns in two tables. A FOREIGN KEY constraint is used to maintain the data consistency and integrity between associated tables. After you complete the setting of FOREIGN KEY constraints, you cannot create new constraints or modify existing constraints.
CHECK constraints: checks the data in the database based on the configured check rules when you edit the data. Data modification is allowed only after the check is passed.
OceanBase Database supports different constraints in MySQL and Oracle modes, and different constraints require different information. Therefore, you need to specify the required information based on the constraint you selected and the requirements on the page.
The constraint setting page provides the following icons.
Icon | Description |
Create | Click this icon to create a constraint. |
Delete | Click this icon to delete the selected constraint. |
Step 5: Set partitioning rules
You can partition a table that contains a large amount of data. After a table is partitioned, data in the table is stored in multiple tablespaces. The database does not scan the entire table for a query.
In MySQL mode, OceanBase Database supports the following partitioning methods: KEY, HASH, RANGE, RANGE COLUMNS, LIST, and LIST COLUMNS.
In Oracle mode, OceanBase Database supports the following three partitioning methods: List, Range, and Hash.
Step 6: Confirm the SQL statement
After you click Execute, you can view the statement on the SQL confirmation page. You can check and format the statement.
The syntax is as follows:
CREATE TABLE table_name (column_name column_type, column_name column_type,.......);
The following table describes the fields in the syntax.
Field | Description |
CREATE TABLE | The keyword that indicates the creation of a table with a specified name. You must have the permission to create tables. |
table_name | The name of the table. The table name must conform to the identifier naming rules. |
column_name column_type | The name and data type of each column in the table. Separate the tuples of multiple columns with commas (,). |
Step 7: Complete the table creation
Click Execute. After the table is created, the employee table appears in the table list in the left-side navigation pane.
You can click the More icon next to a table name in the table list in the left-side navigation pane, and select View Table Schema, View Table Data, Import, Export, Download, Mock Data, Open SQL Window, Copy, Delete, or Refresh from the context menu to manage or operate the table.
For more information, see Manage table attributes.
You can use the SELECT
statement to query data in the new table.
The syntax is as follows:
SELECT
column_name,
column_name
FROM
table_name [WHERE Clause] [LIMIT N] [ OFFSET M]
The following table describes the fields in the syntax.
Field | Description |
SELECT | The keyword that indicates the name of the statement. You can use the SELECT statement to read one or more records. |
column_name | The name of the column to be queried. You can use an asterisk (*) in the format of * to query all columns |
WHERE | The keyword of the condition clause. |
LIMIT | The number of records to be returned. |
OFFSET | The data offset from which the SELECT statement starts to query. The default offset is 0. |
Here is an example:
SELECT `emp_no`, `birthdate`, `name`, `gender` FROM `employee`;
Create an external table
The example in this section describes how to create an external table named employee
in the odc_test
database managed by ODC in the SQL window by mapping the specified file in Alibaba Cloud Object Storage Service (OSS) to the odc_test
database.
Create an external file.
Create a directory named
external table
on the Use Alibaba Cloud accounts to log on to the OSS console.Upload the
employee.csv
file to theexternal table
directory.Copy the path of the
employee
table, such ashttps://ob*********.oss-cn-shanghai.aliyuncs.com/doc/img/odc/433/external%20table/employee.csv
.
In the SQL window of ODC, execute the following SQL statement to create an external table named
employee
.CREATE EXTERNAL TABLE `employee` ( `emp_no` int(120), `birthday` date, `name` varchar(120) ) LOCATION = 'ob*********.oss-cn-shanghai.aliyuncs.com/doc/img/odc/433/500.sql-development/700.database-objects/100.web-odc-table-objects/employee/employee.csv' FORMAT = ( TYPE = 'CSV' FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY ='"' ENCODING = 'utf8mb4' ) PATTERN = 'employee.csv';
The
LOCATION
option specifies the path for storing the external table.Local location format:
LOCATION = '[file://] local_file_path'
. When you use the local location format, you must set the system variablesecure_file_priv
to specify an accessible path. For more information, see secure_file_priv.Remote location format:
LOCATION = '{oss|cos|S3}://$ACCESS_ID:$ACCESS_KEY@$HOST/remote_file_path'
. In this format,$ACCESS_ID
,$ACCESS_KEY
, and$HOST
are required for accessing Alibaba Cloud OSS, Tencent Cloud Object Storage (COS), or Amazon Simple Storage Service (S3).
The
FORMAT = ( TYPE = 'CSV'... )
option specifies the format of a CSV external file. The parameters are described as follows:TYPE
: the type of the external file.LINE_DELIMITER
: the line delimiter for the CSV file. The default value is'\n'
.FIELD_DELIMITER
: the field delimiter for the CSV file. The default value is'\t'
.ESCAPE
: the escape character for the CSV file, which can be only 1 byte in length. The default value is'\'
.FIELD_OPTIONALLY_ENCLOSED_BY
: the characters that enclose the field values in the CSV file. By default, this parameter is left empty.ENCODING
: the character set encoding used by the file. For more information about all character sets supported in MySQL mode, see Character sets. If this parameter is not specified, the default valueUTF8MB4
takes effect.NULL_IF
: the strings to be treated asNULL
values. By default, this parameter is left empty.SKIP_HEADER
: specifies to skip the file header, and specifies the number of lines to skip.SKIP_BLANK_LINES
: specifies whether to skip blank lines. The default value isFALSE
, which specifies not to skip blank lines.TRIM_SPACE
: specifies whether to remove leading and trailing spaces from fields in the file. The default value isFALSE
, which specifies not to remove leading and trailing spaces from fields in the file.EMPTY_FIELD_AS_NULL
: specifies whether to treat empty strings asNULL
values. The default value isFALSE
, which specifies not to treat empty strings asNULL
values.
The
PATTERN
option specifies a regular pattern string for filtering files in theLOCATION
directory. For each file in the directory specified byLOCATION
, if the file path matches the pattern string, the external table accesses the file. Otherwise, the external table skips the file. By default, if this parameter is not specified, all files in the directory specified byLOCATION
are accessible. The external table stores the list of files that match the string specified byPATTERN
in the system table of the database. During a scan, the external table accesses external files based on this list.
In the left-side navigation pane, view the
employee
table in the external table directory.