Create a Typed Table
Part of the SQL Foundations section of Coddy's PostgreSQL journey. Lesson 4 of 22.
CREATE TABLE declares named columns and their types. A PRIMARY KEY identifies each row uniquely and rejects NULL. NOT NULL requires a value; CHECK rejects rows when its condition is false.
CREATE TABLE demo_bins (id integer PRIMARY KEY, label text NOT NULL, capacity integer CHECK (capacity >= 0));
SELECT * FROM demo_bins;The new table has three columns and no rows yet. Its constraints describe valid future rows. CHECK alone does not reject NULL, because an unknown check result is allowed; combine it with NOT NULL when a number is required.
Declare types and constraints when creating a table. A primary key is both unique and non-null.
Challenge
MediumCreate a table named packing_bins with id as integer primary key, label as required text, and capacity as a required nonnegative integer. Use inline column constraints in the taught order: type, NOT NULL when required, then CHECK. Finish by selecting id, label and capacity from the empty table.
The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.
Try it yourself
-- Write your SQL here.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in SQL Foundations
1Types and Casts
Convert with CastsExact Decimal DivisionBoolean ValuesCreate a Typed TableActive Price ReviewPractice on your own: PostgreSQL playground