In Supabase, you want to create a computed column that automatically shows the full name by combining first and last names stored in a table. Which SQL statement correctly creates a generated column for this purpose?
CREATE TABLE users (
id SERIAL PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED
);Look for the correct syntax for generated columns in PostgreSQL, which Supabase uses.
Option D correctly uses the GENERATED ALWAYS AS syntax with STORED keyword and concatenates strings with ||. Option D uses invalid concatenation with +. Option D misses the STORED keyword which is required. Option D uses VIRTUAL which PostgreSQL does not support.
You have an existing table orders with columns price and quantity. You want to add a computed column total_cost that multiplies these two. Which ALTER TABLE statement correctly adds this generated column?
ALTER TABLE orders ADD COLUMN total_cost numeric GENERATED ALWAYS AS (price * quantity) STORED;
Remember that PostgreSQL requires the STORED keyword for generated columns.
Option A correctly adds a stored generated column. Option A misses the GENERATED ALWAYS and STORED keywords. Option A misses STORED. Option A uses VIRTUAL which is not supported in PostgreSQL.
You want to optimize read performance for a Supabase app that frequently queries user full names. You can either use a computed generated column or create a view that concatenates first and last names. Which approach is better for performance and why?
Think about whether the computed value is stored or calculated on the fly.
Computed generated columns store the result physically, so reads are faster. Views compute values on the fly each time, which can be slower. Option B is incorrect because views do not cache results automatically. Option B is false because views can concatenate strings.
In Supabase, you have a computed column that concatenates sensitive user data. What is a key security consideration when using computed generated columns?
Consider how database permissions apply to generated columns.
Computed columns are part of the table and follow the same access controls. They do not encrypt data automatically (B is false). They are stored physically if STORED, so (C) is false. Permissions are managed at the table level, so (D) is false.
In Supabase, you have a table with a computed generated column that concatenates two fields. What happens to the computed column when you update one of the base fields?
Think about how generated columns behave on data changes.
Generated columns are automatically recalculated on insert or update, so they always reflect current data. Options A, B, and C are incorrect because no manual action is needed to update the computed column.
