Complete the code to define a foreign key relationship in Supabase SQL.
CREATE TABLE orders (id SERIAL PRIMARY KEY, user_id INTEGER [1] REFERENCES users(id));The FOREIGN KEY keyword defines a foreign key relationship linking user_id in orders to id in users.
Complete the code to create a one-to-many relationship between users and posts.
CREATE TABLE posts (id SERIAL PRIMARY KEY, author_id INTEGER [1] users(id));The REFERENCES keyword creates a foreign key pointing author_id in posts to id in users, establishing a one-to-many relationship.
Fix the error in the foreign key constraint syntax.
ALTER TABLE comments ADD CONSTRAINT fk_post_id FOREIGN KEY (post_id) [1] posts(id);The correct syntax uses REFERENCES to specify the referenced table and column in a foreign key constraint.
Fill both blanks to create a many-to-many relationship using a join table.
CREATE TABLE [1] (user_id INTEGER [2] users(id), role_id INTEGER REFERENCES roles(id));
The join table is commonly named user_roles and the foreign key uses REFERENCES to link user_id to users(id).
Fill all three blanks to define a composite primary key and foreign keys in a join table.
CREATE TABLE [1] (user_id INTEGER [2] users(id), role_id INTEGER REFERENCES roles(id), PRIMARY KEY ([3]));
The join table user_roles uses REFERENCES to link foreign keys and defines a composite primary key on user_id, role_id to ensure uniqueness.