Table

Table DDL commands are used to create, modify, query, and delete regular tables in a workspace.


Table Types

Singdata Lakehouse provides multiple table types. Use the following comparison when choosing:

Table TypeDescriptionUse Case
Regular Table (Table)Structured two-dimensional data, manually INSERT/UPDATE/DELETERaw data storage, ODS layer
Dynamic TableData objects that auto-incrementally refresh based on query definitionsDWD/DWS/ADS layers, metric aggregation
Materialized ViewSpecial views that pre-compute and store query resultsPre-computed query results, query rewriting
ViewVirtual table, no data stored, dynamically computed at query timeSimplifying complex queries, logical abstraction
External TableData stored in external systems; Lakehouse manages only metadataFederated queries, data lake access

Storage Format

Regular tables use Parquet columnar storage by default, with the following advantages:

  • Columnar storage: reads only the columns involved in the query, reducing I/O
  • Efficient compression: saves 50%–80% storage space compared to row-based storage
  • Vectorized execution: combined with the Lakehouse execution engine, aggregation and filter performance is better

Table Constraints

NOT NULL

Add NOT NULL to a column definition; the system validates that the column cannot be empty on write.

CREATE TABLE orders ( order_id BIGINT NOT NULL, amount DECIMAL(10, 2) NOT NULL, note STRING -- nullable );

PRIMARY KEY

The primary key constraint has two behavior modes; understand the differences when choosing:

ModeSyntaxDeduplication ScopeUse Case
ENABLE VALIDATE RELY (default)PRIMARY KEY (col)Both SQL writes and real-time writes deduplicateGeneral scenarios
DISABLE NOVALIDATE RELYPRIMARY KEY (col) DISABLE NOVALIDATE RELYOnly real-time writes deduplicate; SQL writes are not checkedTables written only via CDC real-time sync

-- Default mode: both SQL writes and real-time writes perform primary key deduplication CREATE TABLE customers ( customer_id BIGINT PRIMARY KEY, name STRING, updated_at TIMESTAMP ); -- DISABLE NOVALIDATE RELY: only real-time writes deduplicate; SQL writes do not check primary key uniqueness -- Suitable for tables written only via CDC real-time sync, not via INSERT SQL CREATE TABLE customers_cdc ( customer_id BIGINT, name STRING, updated_at TIMESTAMP, PRIMARY KEY (customer_id) DISABLE NOVALIDATE RELY );


This Chapter

PageDescription
CREATE TABLECreate a regular table with options for partitioning, bucketing, primary keys, and identity columns
PartitionPartition by time or other fields to accelerate partition pruning
BucketHash-bucket by column values to optimize JOIN and aggregation
Primary KeyDefine primary key constraints for CDC real-time deduplication writes
Identity ColumnColumns that auto-generate unique incrementing integer values
Generated ColumnColumns automatically computed from expressions on other columns
Default ValueDefault value definition when a column value is not specified on insert
CREATE...CLONEQuickly clone table structure (and optionally data) into a new table
ALTER TABLEModify table properties such as renaming, adding/modifying columns, and setting lifecycle
ALTER TABLE COLUMNAdd, rename, modify column types, or drop columns
DROP TABLEDelete a table and its data
RESTORE TABLERoll back a table to a historical version
UNDROP TABLERecover a deleted table (within the data retention period)
DESC TABLEView a table's column definitions, types, and constraints
DESC HISTORY TABLEView a table's list of historical versions
SHOW TABLESList all tables under the current schema
SHOW COLUMNSList all column information for a table
SHOW CREATE TABLEView the table creation statement
SHOW PARTITIONSList all partitions of a partitioned table
SHOW TABLES HISTORYList deleted tables (available for UNDROP)
ANALYZE TABLECollect table statistics to help the optimizer generate better execution plans
OPTIMIZEMerge small files to improve query performance

Common Operations

Create Table

-- Basic table creation CREATE TABLE IF NOT EXISTS public.orders ( order_id BIGINT, customer_id BIGINT, amount DECIMAL(10, 2), status STRING, created_at TIMESTAMP ); -- With partitioning (partition by day, recommended for time-series data) CREATE TABLE IF NOT EXISTS public.events ( event_id BIGINT, event_type STRING, user_id BIGINT, created_at TIMESTAMP ) PARTITIONED BY (days(created_at)); -- With primary key (CDC real-time write scenario) CREATE TABLE IF NOT EXISTS public.customers ( customer_id BIGINT PRIMARY KEY, name STRING, email STRING, updated_at TIMESTAMP );

Modify Table

-- Add column ALTER TABLE public.orders ADD COLUMN discount DECIMAL(5, 2); -- Rename table ALTER TABLE public.orders RENAME TO public.orders_v2; -- Set data retention period (days) ALTER TABLE public.orders SET data_retention_days = 7;

View Table

-- View table structure DESC TABLE public.orders; -- View table creation statement SHOW CREATE TABLE public.orders; -- List all tables SHOW TABLES; -- View partition list SHOW PARTITIONS public.events;

Delete and Restore

-- Delete table DROP TABLE IF EXISTS public.temp_orders; -- Restore deleted table UNDROP TABLE public.temp_orders; -- Roll back table to historical version RESTORE TABLE public.orders TO TIMESTAMP AS OF '2024-01-15 00:00:00';


DocumentDescription
SQL Commands OverviewCategorized navigation for all SQL commands
Regular Table (Object Model)Table storage format, type selection, and best practices
COPY INTO (Import)Batch import data from Volume or external storage
Time Travel GuideHistorical data queries and table rollback operations