Bird
0
0

How do you correctly add a UNIQUE constraint to the existing column user_id in the table members?

easy📝 Syntax Q3 of 15
SQL - Table Constraints
How do you correctly add a UNIQUE constraint to the existing column user_id in the table members?
AALTER TABLE members ADD CONSTRAINT unique_user_id UNIQUE (user_id);
BALTER TABLE members ADD UNIQUE user_id;
CALTER TABLE members MODIFY user_id UNIQUE;
DALTER TABLE members SET UNIQUE user_id;
Step-by-Step Solution
Solution:
  1. Step 1: Syntax for adding UNIQUE constraint

    To add a UNIQUE constraint to an existing column, use ALTER TABLE with ADD CONSTRAINT and specify UNIQUE on the column.
  2. Step 2: Evaluate options

    ALTER TABLE members ADD CONSTRAINT unique_user_id UNIQUE (user_id); uses correct syntax. ALTER TABLE members ADD UNIQUE user_id; is invalid syntax. ALTER TABLE members MODIFY user_id UNIQUE; is incorrect because MODIFY does not add constraints. ALTER TABLE members SET UNIQUE user_id; is not valid SQL.
  3. Final Answer:

    ALTER TABLE members ADD CONSTRAINT unique_user_id UNIQUE (user_id); -> Option A
  4. Quick Check:

    ALTER TABLE ADD CONSTRAINT UNIQUE (column) [OK]
Quick Trick: Use ADD CONSTRAINT with UNIQUE for existing columns [OK]
Common Mistakes:
MISTAKES
  • Using ADD UNIQUE without CONSTRAINT keyword
  • Trying to MODIFY column to add UNIQUE
  • Using SET UNIQUE which is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes