Advanced CREATE TABLE Topics
1. Temporary Tables
-- Session-scoped temporary table
CREATE TEMPORARY TABLE session_cache (
id SERIAL PRIMARY KEY,
data JSON NOT NULL
) ON COMMIT PRESERVE ROWS;
-- Transaction-scoped temporary table
CREATE TEMPORARY TABLE temp_results (
calculation_id INT,
result DECIMAL(16,4)
) ON COMMIT DELETE ROWS;
-
Visibility: Only visible to the creating
session
-
Lifespan: Session-bound or transaction-bound
-
Storage: Typically in memory or tempdb space
-
Indexing: Can have indexes but with
performance tradeoffs
2. CTAS Patterns
-- Basic CTAS with data copy
CREATE TABLE customer_archive AS
SELECT * FROM customers
WHERE status = 'inactive';
-- Schema-only copy with enhanced structure
CREATE TABLE optimized_orders AS
SELECT order_id, customer_id, total_amount
FROM orders
WHERE 1=0 WITH NO DATA;
-- Cross-database CTAS
CREATE TABLE reporting.sales_summary AS
SELECT product_id, SUM(quantity)
FROM operational.sales
GROUP BY product_id;
-
Data Loading: Can include or exclude data
-
Schema Inference: Derives structure from
query
-
Performance: Often faster than CREATE +
INSERT
-
Limitations: May not copy constraints/indexes
3. Table Cloning Techniques
-- Oracle complete clone
CREATE TABLE employees_clone
AS SELECT * FROM employees
WHERE 1=1;
-- PostgreSQL LIKE clause
CREATE TABLE new_logs (LIKE logs INCLUDING ALL);
-- SQL Server full schema copy
SELECT * INTO orders_backup
FROM orders
WHERE 1=0;
-
Schema-Only: Clone structure without data
-
Deep Clone: Copy constraints, indexes,
defaults
-
Partial Clone: Select specific
columns/features
-
Storage Parameters: May or may not be
inherited
4. System Catalog Impact
-- PostgreSQL catalog queries
SELECT * FROM pg_class
WHERE relname = 'new_table';
-- SQL Server metadata
SELECT * FROM sys.tables t
JOIN sys.columns c ON t.object_id = c.object_id
WHERE t.name = 'CustomerData';
-
Metadata Storage: How table definitions are
recorded
-
Dependencies: Views, functions, and triggers
referencing the table
-
Statistics: Initial statistics collection
behavior
-
DDL Triggers: System-wide triggers on table
creation
5. DDL Triggers
-- SQL Server DDL trigger
CREATE TRIGGER audit_table_creations
ON DATABASE
FOR CREATE_TABLE
AS
BEGIN
INSERT INTO ddl_audit_log
SELECT EVENTDATA(), SYSTEM_USER, GETDATE();
END;
-- Oracle event trigger
CREATE TRIGGER validate_table_name
BEFORE CREATE ON SCHEMA
BEGIN
IF ORA_DICT_OBJ_TYPE = 'TABLE' THEN
-- Validate naming conventions
END IF;
END;
-
Event Capture: Logging table creation events
-
Prevention: Blocking invalid table creations
- Security: Enforcing naming standards
-
Performance Impact: Overhead of trigger
execution
6. Cross-Database Syntax Comparison
/* MySQL vs PostgreSQL vs SQL Server */
-- Auto-increment variations:
-- MySQL
CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY);
-- PostgreSQL
CREATE TABLE t1 (id SERIAL PRIMARY KEY);
-- SQL Server
CREATE TABLE t1 (id INT IDENTITY(1,1) PRIMARY KEY);
-- Oracle
CREATE TABLE t1 (id NUMBER GENERATED ALWAYS AS IDENTITY);
-
Data Type Mappings: Equivalent types across
platforms
-
Constraint Syntax: Different keyword
requirements
-
Storage Parameters: Engine-specific table
options
-
Default Behaviors: Nullability, character
sets, collations