100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
SQL Mastery
35 minbeginner

CREATE TABLE, Data Types & Constraints

Every table you have queried began with a CREATE TABLE statement that defined its structure: the columns, their data types, and the rules that govern valid data. Schema design is where data quality is won or lost, because the structure you declare determines what the database will accept, reject, and guarantee about every row from that point forward, long before any query runs.

Choosing the right data type for each column is foundational. Types like INTEGER, TEXT, NUMERIC, BOOLEAN, and TIMESTAMPTZ tell the engine how to store, compare, and validate values, and a well-chosen type both saves space and prevents whole classes of invalid data. A column declared as a date simply cannot hold the string "yesterday", which stops bad data at the door.

Constraints go further, encoding business rules directly into the schema. PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE, and CHECK turn assumptions into enforced guarantees, so the database itself refuses to store data that violates them. Designing types and constraints deliberately is what makes a database trustworthy, turning integrity from something applications must remember into something the engine enforces automatically.

Analogy🏏Cricket
🏏 Think of it like cricket: Imagine the end of an IPL innings when the scorer announces the team's final total. Just as the scorer does not read out all one hundred and twenty individual deliveries but instead sums them into a single team score, SUM folds many row values into one number. Just as the commentator reports the highest individual score, the average partnership, and the number of wickets, MAX, AVG, and COUNT each summarise the innings differently. Just as a not-out batsman with no deliveries faced does not lower the team average, aggregates skip NULLs when computing. Just as the announcement is one figure no matter whether the innings had fifty deliveries or two hundred and fifty, an aggregate collapses any number of rows into a single result. This reveals why summaries answer the questions about the whole that individual deliveries cannot.
Lesson 27 of 35
0% complete