Data Definition Language (DDL) statements
DDL commands are used to create, manipulate, and modify objects in ScopeDB, such as databases, schemas, and tables.
CREATE DATABASE
Section titled “CREATE DATABASE”Creates a new database in the system.
Syntax
Section titled “Syntax”CREATE DATABASE [ IF NOT EXISTS ] <name>
DROP DATABASE
Section titled “DROP DATABASE”Removes a database from the system.
Syntax
Section titled “Syntax”DROP DATABASE [ IF EXISTS ] <name>
CREATE SCHEMA
Section titled “CREATE SCHEMA”Creates a new schema in the current database
Syntax
Section titled “Syntax”CREATE SCHEMA [ IF NOT EXISTS ] <name>
DROP SCHEMA
Section titled “DROP SCHEMA”Removes a schema from the current database.
Syntax
Section titled “Syntax”DROP SCHEMA [ IF EXISTS ] <name>
CREATE TABLE
Section titled “CREATE TABLE”Creates a new table in the current schema.
Syntax
Section titled “Syntax”CREATE TABLE [ IF NOT EXISTS ] <table_name> ( [ <col_name> <col_type> [ NULL | NOT NULL ] [ COMMENT '<string_literal>' ] ],*)[ COMMENT = '<string_literal>' ]WITH ( ['string_literal' = <literal>],*)
Examples
Section titled “Examples”CREATE TABLE t1 (a int)with ( 'storage.type' = 's3', 'storage.endpoint' = 'http://127.0.0.1:9000/', 'storage.bucket' = 'test-bucket', 'storage.region' = 'us-east-1', 'storage.access_key_id' = 'minioadmin', 'storage.secret_access_key' = 'minioadmin');
DROP TABLE
Section titled “DROP TABLE”Removes a table from the current schema.
Syntax
Section titled “Syntax”DROP TABLE [ IF EXISTS ] <name>
CREATE VIEW
Section titled “CREATE VIEW”Creates a new view in the current schema. A view is a virtual table based on the result set of a query. Views can help simplify complex queries and provide an abstraction layer over the underlying tables.
Syntax
Section titled “Syntax”CREATE [ OR REPLACE ] VIEW <name>AS <query>[ COMMENT = '<string_literal>' ]
Parameters
Section titled “Parameters”[ OR REPLACE ]
Section titled “[ OR REPLACE ]”Optional clause that allows you to replace an existing view if it already exists.
<name>
Section titled “<name>”The name of the view to create.
COMMENT
Section titled “COMMENT”Optional comment to describe the purpose of the view.
<query>
Section titled “<query>”The SELECT query that defines the view’s contents.
Examples
Section titled “Examples”-- Create a table to store user eventsCREATE TABLE user_events ( user_id int, event_time timestamp, status string, score int);
-- Insert sample dataVALUES (1, '2024-01-01T10:00:00Z', 'active', 85), (1, '2024-01-02T15:30:00Z', 'inactive', 75), (2, '2024-01-01T09:00:00Z', 'active', 90), (2, '2024-01-03T14:20:00Z', 'active', 95)INSERT INTO user_events;
-- Create a view that keeps only the latest record for each userCREATE VIEW latest_user_status ASFROM user_eventsDISTINCT ON user_idBY event_time DESC;
-- Create or replace a view with a commentCREATE OR REPLACE VIEW highest_user_scoresASFROM user_eventsDISTINCT ON user_idBY score DESCCOMMENT = 'Users highest achieved scores';
Usage Notes
Section titled “Usage Notes”- Views are read-only and cannot be used as targets for INSERT, UPDATE, or DELETE operations
- Views are useful for:
- Simplifying repeated queries
- Creating logical abstractions of the data
- Storing commonly used DISTINCT ON operations
DROP VIEW
Section titled “DROP VIEW”Removes a view from the current schema.
Syntax
Section titled “Syntax”DROP VIEW <name>
Parameters
Section titled “Parameters”<name>
Section titled “<name>”The name of the view to drop.
Examples
Section titled “Examples”-- Drop a viewDROP VIEW latest_user_status;
-- Drop a view if it existsDROP VIEW highest_user_scores;
SHOW TABLES
Section titled “SHOW TABLES”Lists the tables for which you have access privileges.
Syntax
Section titled “Syntax”SHOW TABLES
DESCRIBE TABLE
Section titled “DESCRIBE TABLE”Describes the columns in a table.
Syntax
Section titled “Syntax”DESCRIBE TABLE <name>
CREATE INDEX
Section titled “CREATE INDEX”Creates a new index on a table to improve query performance.
Syntax
Section titled “Syntax”CREATE <index_type> INDEX <name> ON <table_name> (<index_expression>)
<index_type>: smart | search
Parameters
Section titled “Parameters”-
<index_type>
: The type of index to create:SMART
: A general-purpose index that improves performance of queries involving the indexed expressionSEARCH
: A specialized index that optimizes performance of text search operations using thesearch
function
-
<name>
: The name of the index (must be unique within the table) -
<table_name>
: The name of the table to create the index on -
<index_expression>
:- For SMART indexes: Any valid expression that returns a deterministic value
- For SEARCH indexes: Must be a string column that will be used with the
search
function
Examples
Section titled “Examples”-- Create a table with different column typesCREATE TABLE logs ( id int, time timestamp, message string, level string, var variant,);
-- SMART index on a columnCREATE SMART INDEX idx_log_id ON logs (id);
-- SMART index on a timestamp expressionCREATE SMART INDEX idx_log_hour ON logs (trunc(time, unit => 'hour'));
-- SMART index on a computed expressionCREATE SMART INDEX idx_log_var_type ON logs (var['type']::string);
-- SEARCH index for text searching in log messagesCREATE SEARCH INDEX idx_log_message ON logs (message);
When to Use Each Index Type
Section titled “When to Use Each Index Type”-
Use
SMART
indexes when:- You frequently filter on specific columns or expressions
- You need to optimize queries that use range conditions or equality comparisons
- You want to improve performance of queries involving computed expressions(e.g. path access on variant columns like
var['type']::string
)
-
Use
SEARCH
indexes when:- You need to perform text search operations using the
search
function - You want to optimize queries that look for specific text patterns in string columns
- You need to perform text search operations using the
- Indexes improve query performance but require additional storage space
- Creating too many indexes can slow down write operations and increase query latency
- The query optimizer automatically determines when to use available indexes
DROP INDEX
Section titled “DROP INDEX”Removes an index from a table.
Syntax
Section titled “Syntax”DROP INDEX <name> ON <table_name>
Parameters
Section titled “Parameters”<name>
: The name of the index to drop<table_name>
: The name of the table containing the index
Examples
Section titled “Examples”-- Drop indexes created in the previous examplesDROP INDEX idx_log_id ON logs;DROP INDEX idx_log_message ON logs;
CREATE NODEGROUP
Section titled “CREATE NODEGROUP”Creates a new nodegroup.
Syntax
Section titled “Syntax”CREATE NODEGROUP <name>
DROP NODEGROUP
Section titled “DROP NODEGROUP”Removes a nodegroup.
Syntax
Section titled “Syntax”DROP NODEGROUP <name>
CREATE JOB
Section titled “CREATE JOB”Creates a new job.
Syntax
Section titled “Syntax”CREATE JOB <name>SCHEDULE = '<cronexpr>'NODEGROUP = '<comma-separated-nodegroups>'AS <statement>
Examples
Section titled “Examples”CREATE JOB archive_table_tSCHEDULE = '4 2 * * * Asia/Shanghai'NODEGROUP = 'background'AS DELETE FROM t;
DROP JOB
Section titled “DROP JOB”Removes a job.
Syntax
Section titled “Syntax”DROP JOB <name>
SHOW JOBS
Section titled “SHOW JOBS”Lists the jobs for which you have access privileges.
Syntax
Section titled “Syntax”SHOW JOBS
OPTIMIZE TABLE
Section titled “OPTIMIZE TABLE”Optimizing a table. Do compaction or purge historical data to save storage space and enhance query performance.
Syntax
Section titled “Syntax”OPTIMIZE TABLE <name>