Overview

Defines/modifies DB schema (structure) in SQL

  • Create/drop tables, alter columns basically CRUD the tables
  • Changes are saved immediately.
  • Commands: CREATE, ALTER, TRUNCATE, DROP
-- Create the initial 'products' table
CREATE TABLE products (
    product_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2)
);
 
-- Add a new column for stock quantity
ALTER TABLE products ADD COLUMN stock_quantity INT;
 
--  Delete the data in the table, but keep the table (initialization)
TRUNCATE TABLE products;
 
-- Delete the 'products' table completely
DROP TABLE products;

CREATE

KeywordDescription
SERIALAuto-increment integer (PostgreSQL only)
VARCHAR(n)Stores a string up to n characters
NOT NULLValue must not be empty (required)
UNIQUEEnsures values are not duplicated
TIMESTAMPRepresents date and time
DEFAULTSets a default value if none is provided