SQL Questions and Answers – SQL Unique Constraint

This set of SQL Multiple Choice Questions & Answers (MCQs) focuses on “SQL Unique Constraint”.

1. In SQL, which of the following constraint can be used to ensure that all the values in a column are different?
a) PRIMARY KEY
b) UNIQUE
c) NOT NULL
d) CANDIDATE KEY
View Answer

Answer: b
Explanation: In SQL, the UNIQUE constraint can be used to ensure that all the values in a column are different. The NOT NULL constraint is used to reject any null values in columns. The PRIMARY KEY is a combination of both UNIQUE and NOT NULL constraints.

2. Which of the following statement can be used to apply the UNIQUE constraint to a column?
a)

CREATE TABLE TABLE_NAME (
column1 datatype UNIQUE,
column2 datatype UNIQUE,
...
columnn datatype UNIQUE));

b)

CREATE TABLE TABLE_NAME (
UNIQUE column1 datatype,
UNIQUE column2 datatype,
...
UNIQUE columnn datatype));

c)

advertisement
advertisement
CREATE TABLE TABLE_NAME UNIQUE (
column1 datatype,
column2 datatype,
...
columnn datatype));

d)

CREATE TABLE TABLE_NAME (
column1 datatype,
column2 datatype,
...
columnn datatype,
UNIQUE (column1, column2,,columnn));
View Answer
Answer: a
Explanation: The statement that can be used to apply the UNIQUE constraint to a column is –

advertisement
CREATE TABLE TABLE_NAME (
column1 datatype UNIQUE,
column2 datatype UNIQUE,
...
columnn datatype UNIQUE));

The UNIQUE constraint is applied after the datatype of a column.

advertisement

3. Which of the following statement can be used to apply the UNIQUE constraint to a column of an existing table?
a)

ALTER TABLE TABLE_NAME
ADD (column_name) UNIQUE;

b)

ALTER TABLE TABLE_NAME
UNIQUE (column_name);

c)

ALTER TABLE TABLE_NAME
ADD UNIQUE (column_name);

d)

ALTER TABLE TABLE_NAME
ADD UNIQUE;
View Answer
Answer: c
Explanation: The statement that can be used to apply the UNIQUE constraint to a column of an existing table is –

ALTER TABLE TABLE_NAME
ADD UNIQUE (column_name);

To add the UNIQUE constraint to a column of an existing column, it must be used with the ALTER command.

4. Which of the following statement can be used to apply the UNIQUE constraint to multiple columns of an existing table?
a)

ALTER TABLE TABLE_NAME
ADD CONSTRAINT UNIQUE (column1,column2,,columnn);

b)

ALTER TABLE TABLE_NAME
ADD constraint_name UNIQUE (column1, column2,,columnn);

c)

ALTER TABLE TABLE_NAME
CONSTRAINT constraint_name UNIQUE (column1,column2,,columnn);

d)

ALTER TABLE TABLE_NAME
ADD CONSTRAINT constraint_name UNIQUE (column1,column2,,columnn);
View Answer
Answer: d
Explanation: The statement that can be used to apply the UNIQUE constraint to multiple columns of an existing table is –

ALTER TABLE TABLE_NAME
ADD CONSTRAINT constraint_name UNIQUE (column1,column2,,columnn);

The syntax is almost same as for adding the constraint to a single column. We just have to add a constraint name, that is a name given to the constraint by the user. The name can be anything and is not specific.

5. Which of the following statement can be used to delete the UNIQUE constraint from a column?
a)

ALTER TABLE TABLE_NAME
DROP constraint_name;

b)

ALTER TABLE TABLE_NAME
DROP INDEX constraint_name;

c)

ALTER TABLE TABLE_NAME
DROP INDEX UNIQUE;

d)

ALTER TABLE TABLE_NAME
DROP UNIQUE;
View Answer
Answer: b
Explanation: The statement that can be used to delete the UNIQUE constraint from a column is –

ALTER TABLE TABLE_NAME
DROP INDEX constraint_name;

This statement will delete the UNIQUE constraint from any column that has it.
In the statement INDEX keyword is used only in case of MySQL and for rest, the CONSTRAINT keyword is used.

Sanfoundry Global Education & Learning Series – SQL.

To practice all areas of SQL, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.