MySQL Column Attributes Cheat Sheet
1. Value Constraints
- NOT NULL: Disallows NULL values
- NULL: Allows NULL values (default)
- DEFAULT: Sets a default value if not specified
- CHECK: Enforces a condition (e.g., CHECK(age >= 18))
- UNIQUE: All values must be different
- PRIMARY KEY: Unique + NOT NULL combination to identify each row
2. Auto & Identity Handling
- AUTO_INCREMENT: Automatically generates sequential numbers
- ON UPDATE CURRENT_TIMESTAMP: Automatically updates timestamp when a row is modified
3. Type-Specific Modifiers
- UNSIGNED: Numeric values must be zero or positive
- ZEROFILL: Pads numbers with leading zeros (e.g., 0007)
- BINARY: Case-sensitive string comparison for CHAR/VARCHAR
- CHARACTER SET: Sets specific character set (e.g., utf8mb4)
- COLLATE: Defines how strings are compared
4. Metadata & Indexing
- COMMENT: Adds notes to the column
- INDEX / KEY: Creates an index on the column
- FULLTEXT: Enables full-text search indexing
- SPATIAL: For indexing spatial (geographic) data
5. Invisible Columns (MySQL 8.0+)
- INVISIBLE: Column hidden from SELECT * (unless explicitly called)
- VISIBLE: Default behavior - column appears in results
MySQL Column Attributes Cheat Sheet
6. Generated Columns
- GENERATED ALWAYS AS (...): Defines computed values
- VIRTUAL: Computed at query time (default)
- STORED: Computed once and saved like normal column