CREATE TABLE foobar(
id SERIAL PRIMARY KEY,
user_id INTEGER, -- Missing foreign key constraints
amount DECIMAL(10,2)
);
CREATE TABLE foobar (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
amount DECIMAL(10,2)
);
CREATE TABLE questions (
id SERIAL PRIMARY KEY,
difficulty_level VARCHAR(20)
CHECK (difficulty_level IN (‘Easy’, ‘Medium’, ‘Hard’)),
tags JSONB DEFAULT ‘{}’::JSONB
);
CREATE TABLE difficulty_levels (
id SMALLINT PRIMARY KEY,
level_name TEXT UNIQUE
);
INSERT INTO difficulty_levels (id, level_name)
VALUES (1, 'Easy'), (2, 'Medium'), (3, 'Hard');
CREATE TABLE questions (
id SERIAL PRIMARY KEY,
difficulty_level SMALLINT REFERENCES difficulty_levels(id)
);
Data Type | Storage per Row | Index Efficiency | Flexibility | Lookup Performance |
---|---|---|---|---|
ENUM (1 byte) | ✅ Smaller (1 byte) | ❌ Worse (extra lookup) | ❌ Fixed set, hard to change | ❌ Text lookup overhead |
SMALLINT (2 bytes) | ❌ Slightly larger (2 bytes) | ✅ Faster (B-Tree optimized) |
✅ Can modify values easily | ✅ Direct integer comparison |
CREATE TABLE foobar
( id SERIAL PRIMARY KEY,
tag1 VARCHAR(50),
tag2 VARCHAR(50),
tag3 VARCHAR(50) );
CREATE TABLE questions (
id SERIAL PRIMARY KEY,
tags JSONB DEFAULT '{}'::JSONB
);
AI has undoubtedly made database schema generation more accessible, but it’s crucial to approach it with realistic expectations. While AI can assist in structuring schemas, human expertise remains irreplaceable in optimizing for space efficiency, indexing strategies, and query performance.
When designing a PostgreSQL database, avoiding ENUMs can prevent long-term migration and maintenance challenges, even though they might seem efficient in the short term.
On the other hand, MySQL provides more flexibility in storage choices, offering a wider range of options ( TinyINT, MediumINT ) to fine-tune performance based on specific needs.
Data Type | PostgreSQL Equivalent | Storage (Bytes) | Best Use Case |
---|---|---|---|
TINYINT | ❌ Not Available | 1 byte | Flags, small counters |
SMALLINT | SMALLINT | 2 bytes | Small numeric values |
MEDIUMINT | ❌ Not Available | 3 bytes | Compact storage for moderate ranges |
INT | INTEGER | 4 bytes | Default choice for numeric IDs |
BIGINT | BIGINT | 8 bytes | Large numeric values (e.g., timestamps) |
Ultimately, good database design is about trade-offs — balancing readability, scalability, and storage efficiency. AI can be a helpful assistant in this process, but the final decisions should always be informed by a deep understanding of how databases actually work.