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

DDL Syntax Overview

Data definition language (DDL) is used to define or modify an object in a database, such as a table, an index, or a view.

GaussDB does not support DDL operations when the CN is incomplete. For example, if a CN in cluster is faulty, creating a database or a table will fail.

Defining a Role

A role is used to manage permissions. For database security, management and operation permissions can be granted to different roles. For details about related SQL statements, see Table 1.

Table 1 SQL statements for defining a role

Function

SQL Statement

Creating a role

CREATE ROLE

Altering role attributes

ALTER ROLE

Dropping a role

DROP ROLE

Dropping the database objects owned by a database role

DROP OWNED

Defining a User

A user is used to log in to a database. Different permissions can be granted to users for managing data accesses and operations of the users. For details about related SQL statements, see Table 2.

Table 2 SQL statements for defining a user

Function

SQL Statement

Creating a user

CREATE USER

Altering user attributes

ALTER USER

Dropping a user

DROP USER

Defining a CMK

Client master keys (CMKs) are used to encrypt column encryption keys (CEKs) for the encrypted database feature. CMK definition includes creating and deleting a CMK. For details about related SQL statements, see Table 3.

Table 3 SQL statements for defining a CMK

Function

SQL Statement

Creating a CMK

CREATE CLIENT MASTER KEY

Deleting a CMK

DROP CLIENT MASTER KEY

Defining a CEK

CEKs are used to encrypt data for the encrypted database feature. You can create a CEK, change the client master key specified by a CEK, and delete a CEK. For details about related SQL statements, see Table 3.

Table 4 SQL statements for defining a CEK

Function

SQL Statement

Creating a CEK

CREATE COLUMN ENCRYPTION KEY

Changing the client master key specified by a CEK

7.14.173-ALTER COLUMN ENCRYPTION KEY

Deleting a CEK

DROP COLUMN ENCRYPTION KEY

Defining a Database

A database is the warehouse for organizing, storing, and managing data. Defining a database includes: creating a database, altering the database attributes, and dropping the database. For details about related SQL statements, see Table 5.

Table 5 SQL statements for defining a database

Function

SQL Statement

Creating a database

CREATE DATABASE

Altering database attributes

ALTER DATABASE

Dropping a Database

DROP DATABASE

Defining a Schema

A schema is the set of a group of database objects and is used to control the access to the database objects. For details about related SQL statements, see Table 6.

Table 6 SQL statements for defining a schema

Function

SQL Statement

Creating a schema

CREATE SCHEMA

Altering schema attributes

ALTER SCHEMA

Dropping a schema

DROP SCHEMA

Defining a Tablespace

A tablespace is used to manage data objects and corresponds to a catalog on a disk. For details about related SQL statements, see Table 7.

Table 7 SQL statements for defining a tablespace

Function

SQL Statement

Creating a tablespace

CREATE TABLESPACE

Altering tablespace attributes

ALTER TABLESPACE

Dropping a tablespace

DROP TABLESPACE

Defining a Table

A table is a special data structure in a database and is used to store data objects and relationship between data objects. For details about related SQL statements, see Table 8.

Table 8 SQL statements for defining a table

Function

SQL Statement

Creating a table

CREATE TABLE

Altering table attributes

ALTER TABLE

Renaming one or more tables

RENAME TABLE

Dropping a table

DROP TABLE

Creating a table from the results of a query

CREATE TABLE AS

Defining a Partitioned Table

A partitioned table is a logical table used to improve query performance and does not store data (data is stored in ordinary tables). For details about related SQL statements, see Table 9.

Table 9 SQL statements for defining a partitioned table

Function

SQL Statement

Creating a partitioned table

CREATE TABLE PARTITION

Create a partition

ALTER TABLE PARTITION

Altering partitioned table attributes

ALTER TABLE PARTITION

Deleting a partition

ALTER TABLE PARTITION

Dropping a partitioned table

DROP TABLE

Defining an Index

An index indicates the sequence of values in one or more columns in a database table. It is a data structure that improves the speed of data access to specific information in a database table. For details about related SQL statements, see Table 10.

Table 10 SQL statements for defining an index

Function

SQL Statement

Creating an index

CREATE INDEX

Altering index attributes

ALTER INDEX

Dropping an index

DROP INDEX

Rebuilding an index

REINDEX

Creating a global secondary index in a specified table

CREATE GLOBAL INDEX

Defining a Stored Procedure

A stored procedure is a set of SQL statements for achieving specific functions and is stored in the database after compiling. Users can specify a name and provide parameters (if necessary) to execute the stored procedure. For details about related SQL statements, see Table 11.

Table 11 SQL statements for defining a stored procedure

Function

SQL Statement

Creating a stored procedure

CREATE PROCEDURE

Dropping a stored procedure

DROP PROCEDURE

Defining a Function

In GaussDB, a function is similar to a stored procedure, which is a set of SQL statements. The function and stored procedure are used the same. For details about related SQL statements, see Table 12.

Table 12 SQL statements for defining a function

Function

SQL Statement

Creating a function

CREATE FUNCTION

Modifying attributes of a function or recompiling the function

ALTER FUNCTION

Dropping a function

DROP FUNCTION

Defining a Cursor

To process SQL statements, the stored procedure thread assigns a memory segment to store context association. Cursors are handles or pointers to context regions. With a cursor, the stored procedure can control alterations in context areas. For details, see Table 13.

Table 13 SQL statements for defining a cursor

Function

SQL Statement

Creating a cursor

CURSOR

DECLARE

Moving a cursor

MOVE

Closing a cursor

CLOSE

Defining a Resource Pool

A resource pool is a system catalog used by the resource load management module to specify attributes related to resource management, such as Cgroups. For details about related SQL statements, see Table 14.

Table 14 SQL statements for defining a resource pool

Function

SQL Statement

Creating a resource pool

CREATE RESOURCE POOL

Altering resource attributes

ALTER RESOURCE POOL

Dropping a resource pool

DROP RESOURCE POOL

Defining an Aggregate Function

Table 15 SQL statements for defining an aggregate function

Function

SQL Statement

Creating an aggregate function

CREATE AGGREGATE

Modifying an aggregate function

ALTER AGGREGATE

Deleting an aggregate function

DROP AGGREGATE

Defining a Plug-in Extension

Table 16 SQL statements for defining a plug-in extension

Function

SQL Statement

Creating an extension

CREATE EXTENSION

The extended function is for internal use only. You are advised not to use it.

Defining a Data Type

Table 17 SQL statements for defining a data type

Function

SQL Statement

Creating a data type

CREATE TYPE

Modifying a data type

ALTER TYPE

Deleting a data type

DROP TYPE

Defining a Database Link

A database link can be used to remotely operate a database object. For details about corresponding SQL statements, see Table 18.

Table 18 DATABASE LINK-related SQL statements

Function

SQL Statement

Creating a database link

CREATE DATABASE LINK

Modifying a database link

ALTER DATABASE LINK

Deleting a database link

DROP DATABASE LINK

Defining an Audit Policy

Table 19 SQL statements for defining an audit policy

Function

SQL Statement

Creating a unified audit policy

CREATE AUDIT POLICY

Modifying a unified audit policy

ALTER AUDIT POLICY

Deleting an audit policy

DROP AUDIT POLICY

Setting the nodeis_active Column of a Specified Node

ALTER COORDINATOR describes how to change the value of the nodeis_active column of a specified node in the pgxc_node system catalog.

Defining a Directory

Table 20 SQL statements for defining a directory

Function

SQL Statement

Creating a directory

CREATE DIRECTORY

Modifying attributes of a directory

ALTER DIRECTORY

Deleting a specified directory

DROP DIRECTORY

Defining a Foreign Data Wrapper

Table 21 SQL statements related to the foreign data wrapper

Function

SQL Statement

Creating a foreign data wrapper

CREATE FOREIGN DATA WRAPPER

Modifying a foreign data wrapper

ALTER FOREIGN DATA WRAPPER

Deleting a foreign data wrapper

DROP FOREIGN DATA WRAPPER

SQL Statements Related to the gs_global_config System Catalog

Table 22 SQL statements related to the gs_global_config system catalog

Function

SQL Statement

Adding and modifying parameter values in the gs_global_config system catalog

ALTER GLOBAL CONFIGURATION

Deleting parameter values from the gs_global_config system catalog

DROP GLOBAL CONFIGURATION

Inserting one or more weak passwords into the gs_global_config system catalog

CREATE WEAK PASSWORD DICTIONARY

Clearing all weak passwords in the gs_global_config system catalog

DROP WEAK PASSWORD DICTIONARY

Defining a User Group

Table 23 SQL statements for defining a user group

Function

SQL Statement

Creating a user group

CREATE GROUP

Modifying attributes of a user group

ALTER GROUP

Deleting a user group

DROP GROUP

Defining a Procedural Language

Table 24 SQL statements for defining a procedural language

Function

SQL Statement

Defining a new procedural language

CREATE LANGUAGE

Modifying the definition of a procedural language

ALTER LANGUAGE

Deleting a procedural language

DROP LANGUAGE

Defining a Masking Policy

Table 25 SQL statements for defining a masking policy

Function

SQL Statement

Creating a masking policy

CREATE MASKING POLICY

Modifying a masking policy

ALTER MASKING POLICY

Deleting a masking policy

DROP MASKING POLICY

Defining a Materialized View

Table 26 SQL statements for defining a materialized view

Function

SQL Statement

Creating a complete-refresh materialized view

CREATE MATERIALIZED VIEW

Creating a fast-refresh materialized view

CREATE INCREMENTAL MATERIALIZED VIEW

Modifying multiple auxiliary attributes of an existing materialized view

ALTER MATERIALIZED VIEW

Forcibly deleting an existing materialized view from the database

DROP MATERIALIZED VIEW

Refreshing a materialized view in complete refresh mode

REFRESH MATERIALIZED VIEW

Refreshing a materialized view in fast refresh mode

REFRESH INCREMENTAL MATERIALIZED VIEW

Defining a Node

Table 27 SQL statements for defining a node

Function

SQL Statement

Creating a cluster node

CREATE NODE

Modifying the definition of an existing node

ALTER NODE

Deleting a node

DROP NODE

Defining a Node Group

Table 28 SQL statements for defining a node group

Function

SQL Statement

Creating a cluster node group

CREATE NODE GROUP

Modifying information about an existing node group

ALTER NODE GROUP

Deleting a node group

DROP NODE GROUP

Defining a Resource Label

Table 29 SQL statements for defining a resource label

Function

SQL Statement

Creating a resource label

CREATE RESOURCE LABEL

Modifying a resource label

ALTER RESOURCE LABEL

Deleting a resource label

DROP RESOURCE LABEL

Defining a Row-Level Security Policy

Table 30 SQL statements for defining a row-level security policy

Function

SQL Statement

Creating a row-level security policy for a table

CREATE ROW LEVEL SECURITY POLICY

Modifying an existing row-level security policy

ALTER ROW LEVEL SECURITY POLICY

Deleting a row-level security policy from a table

DROP ROW LEVEL SECURITY POLICY

Defining a Sequence

Table 31 SQL statements for defining a sequence

Function

SQL Statement

Adding a sequence to the current database

CREATE SEQUENCE

Modifying parameters of an existing sequence

ALTER SEQUENCE

Deleting a sequence from the current database

DROP SEQUENCE

Defining a Foreign Server

Table 32 SQL statements for defining a foreign server

Function

SQL Statement

Defining a new foreign server

CREATE SERVER

Adding, modifying, and deleting parameters of an existing server

ALTER SERVER

Deleting a data server

DROP SERVER

Defining a Synonym Object

Table 33 SQL statements for defining a synonym object

Function

SQL Statement

Creating a synonym object

CREATE SYNONYM

Modifying the owner of a synonym object

ALTER SYNONYM

Deleting a specified synonym object

DROP SYNONYM

Defining a Trigger

Table 34 SQL statements for defining a trigger

Function

SQL Statement

Creating a trigger

CREATE TRIGGER

Renaming a trigger

ALTER TRIGGER

Deleting a trigger

DROP TRIGGER

Defining a View

Table 35 SQL statements for defining a view

Function

SQL Statement

Creating a view

CREATE VIEW

Modifying the auxiliary attributes of a view

ALTER VIEW

Forcibly deleting a view from the database

DROP VIEW

Collecting Statistics

For details about how to collect statistics related to the contents of ordinary tables in the database, see ANALYZE | ANALYSE.

Creating a Synchronization Point

For details about how to create a synchronization point between nodes in a new cluster, see CREATE BARRIER.

Creating an Encoding Conversion Task

For details about how to define a new conversion between two character set encodings, see CREATE CONVERSION.

Defining a Model

Table 36 SQL statements for defining a model

Function

SQL Statement

Training a machine learning model and saving the model

CREATE MODEL

Deleting a model that has been trained and saved

DROP MODEL

Defining a Security Label

Table 37 SQL statements for defining a security label

Function

SQL Statement

Creating a security label

CREATE SECURITY LABEL

Applying, updating, or canceling a security label

SECURITY LABEL ON

Deleting a security label

DROP SECURITY LABEL

Importing a Database/Table

Table 38 SQL statements for data import

Function

SQL Statement

Preparation phase for importing a database

IMPDP DATABASE CREATE

Execution phase of importing a database

IMPDP RECOVER

Preparation phase for importing a table

IMPDP TABLE PREPARE

Execution phase of importing a table

IMPDP TABLE

Clearing a Recycle Bin

GaussDB provides statements for clearing a recycle bin. For details, see PURGE.

Clustering a Table

GaussDB supports statements for clustering a table based on an index. For details, see CLUSTER.

Defining an Object Comment

GaussDB supports statements for defining or modifying an object comment. For details, see COMMENT.

Creating a Table Based on Query Results

GaussDB supports statements for creating a table based on query results and inserting queried data into the new table. For details, see SELECT INTO.

Restoring a Table to an Earlier State

GaussDB allows you to restore a table to an earlier state in the event of a manual operation or application error. For details, see TIMECAPSULE TABLE.

Clearing Table Data

GaussDB supports statements for quickly deleting all rows from a table. For details, see TRUNCATE.

Recycling Storage Space

GaussDB supports statements for recycling storage space occupied by deleted rows in a table or B-Tree index. For details, see VACUUM.