0
0
MySQLquery~10 mins

UNIQUE constraints in MySQL - Interactive Code Practice

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

Complete the code to add a UNIQUE constraint to the 'email' column in the 'users' table.

MySQL
ALTER TABLE users ADD CONSTRAINT [1] UNIQUE (email);
Drag options to blanks, or click blank then click option'
Aprimary_key
Bunique_email
Cindex_email
Dforeign_key
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'primary_key' as the constraint name instead of a unique constraint name.
Using 'index_email' which is an index, not a constraint.
2fill in blank
medium

Complete the code to create a table 'products' with a UNIQUE constraint on the 'sku' column.

MySQL
CREATE TABLE products (id INT PRIMARY KEY, sku VARCHAR(50) [1] UNIQUE);
Drag options to blanks, or click blank then click option'
ANOT NULL
BNULL
CDEFAULT
DAUTO_INCREMENT
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL which allows multiple NULLs and breaks uniqueness.
Using AUTO_INCREMENT on a VARCHAR column.
3fill in blank
hard

Fix the error in the code to add a UNIQUE constraint on 'username' in the 'accounts' table.

MySQL
ALTER TABLE accounts ADD UNIQUE [1] (username);
Drag options to blanks, or click blank then click option'
Aunique_username
Busername
Cusername_unique
Dusername_key
Attempts:
3 left
💡 Hint
Common Mistakes
Using the column name 'username' as the constraint name causes confusion.
Using names that are not descriptive of the constraint purpose.
4fill in blank
hard

Fill both blanks to create a UNIQUE constraint on 'email' and 'phone' columns in 'contacts' table.

MySQL
ALTER TABLE contacts ADD CONSTRAINT [1] UNIQUE ([2]);
Drag options to blanks, or click blank then click option'
Aunique_contact
Bemail, phone
Cemail,phone
Dcontact_unique
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces after commas in column list causes syntax errors.
Using generic constraint names that don't describe the constraint.
5fill in blank
hard

Fill all three blanks to create a table 'employees' with a UNIQUE constraint on 'email' and 'employee_id'.

MySQL
CREATE TABLE employees (id INT PRIMARY KEY, [1] VARCHAR(100) NOT NULL, [2] INT NOT NULL, CONSTRAINT [3] UNIQUE (email, employee_id));
Drag options to blanks, or click blank then click option'
Aemail
Bemployee_id
Cunique_emp
Demp_email
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up column names or missing NOT NULL on unique columns.
Using constraint names that are not descriptive.