Data types tell the database what kind of information to expect. Constraints make sure the data follows rules to keep it correct and useful.
Data types and constraints in Supabase
CREATE TABLE table_name ( column_name data_type CONSTRAINTS, ... );
Data types define the kind of data (e.g., text, number, date).
Constraints add rules like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY.
CREATE TABLE users ( id SERIAL PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, age INT CHECK (age >= 0) );
CREATE TABLE products ( product_id UUID PRIMARY KEY, name TEXT NOT NULL, price NUMERIC(10,2) CHECK (price > 0) );
This table stores employee data with rules: each employee has a unique ID, names and email cannot be empty, emails must be unique, hire date is required, and salary cannot be negative.
CREATE TABLE employees ( employee_id SERIAL PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, hire_date DATE NOT NULL, salary NUMERIC(8,2) CHECK (salary >= 0) );
Use the right data type to save space and improve speed.
Constraints help keep your data clean and prevent mistakes.
Always test your constraints by trying to insert bad data to see if they block it.
Data types define what kind of data each column holds.
Constraints enforce rules to keep data accurate and consistent.
Combining both helps build reliable and safe databases.
Practice
Solution
Step 1: Understand data types role
Data types define the kind of data a column can hold, like numbers or text.Step 2: Differentiate from other features
Access control, reports, and backups are unrelated to data types.Final Answer:
To specify what kind of data each column can store -> Option AQuick Check:
Data types = specify data kind [OK]
- Confusing data types with user permissions
- Thinking data types create reports
- Assuming data types handle backups
Solution
Step 1: Recall SQL column definition order
The correct order is: column name, data type, then constraints.Step 2: Match the syntax
username TEXT NOT NULLfollows this order:username TEXT NOT NULL.Final Answer:
username TEXT NOT NULL-> Option AQuick Check:
Column name + type + constraint = correct syntax [OK]
- Placing NOT NULL before the column name
- Swapping data type and constraint order
- Using incorrect keyword order
CREATE TABLE users (id SERIAL PRIMARY KEY, email TEXT UNIQUE NOT NULL, age INT);
What happens if you try to insert two rows with the same email?
Solution
Step 1: Understand UNIQUE constraint effect
The UNIQUE constraint prevents duplicate values in the email column.Step 2: Predict insert behavior
Inserting a duplicate email causes the second insert to fail with an error.Final Answer:
The second insert fails due to UNIQUE constraint violation -> Option CQuick Check:
UNIQUE prevents duplicates = second insert fails [OK]
- Assuming duplicates are allowed
- Thinking second insert overwrites first
- Ignoring constraint errors
CREATE TABLE products (id INT PRIMARY KEY, name TEXT NOT NULL, price TEXT);
What is the main issue with this definition?
Solution
Step 1: Check data type appropriateness
Price should be a numeric type (like INT or NUMERIC), not TEXT, to store numbers properly.Step 2: Validate other columns
id as INT with PRIMARY KEY and name as TEXT NOT NULL are valid.Final Answer:
The price column should not be TEXT for numeric values -> Option BQuick Check:
Price needs numeric type, not text [OK]
- Using TEXT for numeric columns
- Thinking PRIMARY KEY can't be INT
- Believing NOT NULL is invalid on text
- Each user has a unique ID
- Email must be unique and not empty
- Age must be a positive integer or NULL if unknown
Which table definition correctly applies data types and constraints?
Solution
Step 1: Check unique ID and email constraints
CREATE TABLE profiles (user_id UUID PRIMARY KEY, email TEXT UNIQUE NOT NULL, age INT CHECK (age > 0));
uses UUID PRIMARY KEY for unique ID and email TEXT UNIQUE NOT NULL, which is correct.Step 2: Validate age constraint
CREATE TABLE profiles (user_id UUID PRIMARY KEY, email TEXT UNIQUE NOT NULL, age INT CHECK (age > 0));
usesCHECK (age > 0)allowing NULL by default, which fits the requirement.Step 3: Review other options
CREATE TABLE profiles (user_id INT, email TEXT NOT NULL, age INT NOT NULL CHECK (age >= 0));
lacks PRIMARY KEY and UNIQUE on email, makes age NOT NULL; C adds NOT NULL to age preventing NULL values; D has syntax error in CHECK constraint.Final Answer:
CREATE TABLE profiles (user_id UUID PRIMARY KEY, email TEXT UNIQUE NOT NULL, age INT CHECK (age > 0)); -> Option DQuick Check:
UUID PK, UNIQUE email, age > 0 or NULL = correct [OK]
- Forgetting PRIMARY KEY on user_id
- Making age NOT NULL when it can be NULL
- Writing invalid CHECK constraints
Ready to prove it?
Pass the test and this pattern moves unknown
