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
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)
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));
Explanation: The statement that can be used to apply the UNIQUE constraint to a column is –
CREATE TABLE TABLE_NAME ( column1 datatype UNIQUE, column2 datatype UNIQUE, ... columnn datatype UNIQUE));
The UNIQUE constraint is applied after the datatype of a column.
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;
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);
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;
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.