0
0
Supabasecloud~10 mins

Primary keys and foreign keys in Supabase - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a primary key for the 'users' table.

Supabase
CREATE TABLE users (id SERIAL [1] PRIMARY KEY, name TEXT);
Drag options to blanks, or click blank then click option'
AUNIQUE
BCHECK
CDEFAULT
DNOT NULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using UNIQUE alone does not prevent NULL values.
Forgetting to specify NOT NULL on primary key columns.
2fill in blank
medium

Complete the code to create a foreign key in the 'orders' table referencing 'users'.

Supabase
CREATE TABLE orders (order_id SERIAL PRIMARY KEY, user_id INTEGER [1] REFERENCES users(id));
Drag options to blanks, or click blank then click option'
AUNIQUE
BNOT NULL
CDEFAULT
DCHECK
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving foreign key columns nullable when they should not be.
Using UNIQUE instead of NOT NULL.
3fill in blank
hard

Fix the error in the foreign key constraint syntax.

Supabase
ALTER TABLE orders ADD CONSTRAINT fk_user FOREIGN KEY (user_id) [1] users(id);
Drag options to blanks, or click blank then click option'
AREFERENCES
BREFERENCING
CREFERENCE
DREFERS
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'REFERENCE' instead of 'REFERENCES'.
Misspelling the keyword causes syntax errors.
4fill in blank
hard

Fill both blanks to create a table with a composite primary key.

Supabase
CREATE TABLE enrollment (student_id INTEGER [1], course_id INTEGER [2], PRIMARY KEY (student_id, course_id));
Drag options to blanks, or click blank then click option'
ANOT NULL
BUNIQUE
CDEFAULT
DCHECK
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving one or both columns nullable.
Using UNIQUE instead of NOT NULL.
5fill in blank
hard

Fill all three blanks to create a foreign key with ON DELETE CASCADE and ON UPDATE CASCADE.

Supabase
ALTER TABLE orders ADD CONSTRAINT fk_user FOREIGN KEY (user_id) [1] users(id) ON DELETE [2] ON UPDATE [3];
Drag options to blanks, or click blank then click option'
AREFERENCES
BCASCADE
CRESTRICT
DSET NULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using RESTRICT or SET NULL instead of CASCADE for ON DELETE or ON UPDATE.
Misspelling REFERENCES.