Database Management
PostgreSQL
Subjective
Sep 25, 2025
Explain PostgreSQL constraint types and their usage.
Detailed Explanation
Constraint Types:
PRIMARY KEY:
• Unique identifier
• Cannot be NULL
• Automatically creates index
FOREIGN KEY:
• References another table
• Maintains referential integrity
• Supports CASCADE actions
UNIQUE:
• Ensures uniqueness
• Allows one NULL value
CHECK:
• Custom validation rules
• Boolean expressions
NOT NULL:
• Prevents NULL values
Example:
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(id) ON DELETE CASCADE,
amount DECIMAL CHECK (amount > 0),
email VARCHAR UNIQUE NOT NULL
);Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts