Using DEFAULT Values and Expressions in PostgreSQL
📖 Scenario: You are creating a simple database table to store information about books in a library. Some columns should have default values to simplify data entry and ensure consistency.
🎯 Goal: Create a table called books with columns that use DEFAULT values and expressions to automatically fill in missing data.
📋 What You'll Learn
Create a table named
books with columns id, title, author, copies, and added_date.Set
id as a primary key with automatic increment.Set
copies to have a default value of 1.Set
added_date to have a default value of the current date.Insert a row without specifying
copies and added_date to see defaults in action.💡 Why This Matters
🌍 Real World
DEFAULT values help reduce errors and simplify data entry in real databases by automatically filling in common or expected values.
💼 Career
Understanding DEFAULT values is essential for database design and maintenance roles, ensuring data consistency and efficient workflows.
Progress0 / 4 steps