This set of SQL Multiple Choice Questions & Answers (MCQs) focuses on “SQL Check Constraint”.
1. In SQL, which of the following constraint is used to check the value entered in a table on the basis of a specified condition?
a) NOT NULL
b) UNIQUE
c) CHECK
d) DEFAULT
View Answer
Explanation: In SQL, the CHECK constraint can be used to check the value entered in a table on the basis of a specified condition. The condition is specified by the user and is used to filter out the incoming data.
2. In SQL, a view can also have a check constraint.
a) True
b) False
View Answer
Explanation: In SQL, views cannot have a check constraint. A view is only a copy of a table in database, so it cannot have its own constraint. The check constraint defined on a table must refer to only columns in that table.
3. We can use the CHECK constraint on a column as well as on a table level.
a) True
b) False
View Answer
Explanation: In SQL, we can use the CHECK constraint on a column level as well as on a table level. A column level check constraint is used to put constrainton a single column. The table level check constraint is used to put constraint on multiple columns.
4. Which of the following statement can be used to apply the column level CHECK constraint in SQL?
a)
CREATE TABLE TABLE_NAME CHECK ( column1 datatype (condition), column2 datatype (condition), ... columnn datatype (condition));
b)
CREATE TABLE TABLE_NAME ( column1 datatype CHECK (condition), column2 datatype CHECK (condition), ... columnn datatype CHECK (condition));
c)
CREATE TABLE TABLE_NAME ( column1 datatype, column2 datatype, ... columnn datatype, CHECK (column1, column2) (condition));
d)
CREATE TABLE TABLE_NAME ( CHECK (column1 datatype condition), CHECK (column2 datatype condition), ... CHECK (columnn datatype condition));
Explanation: The statement that can be used to apply the column level check constraint in SQL is –
CREATE TABLE TABLE_NAME ( column1 datatype CHECK (condition), column2 datatype CHECK (condition), ... columnn datatype CHECK (condition));
The CHECK keyword followed by the condition is used after the datatype of the column on which the constraint is to be applied.
5. Which of the following statement can be used to apply the table level CHECK constraint in SQL?
a)
CREATE TABLE table_nameCHECK (condition1 AND condition2) ( column1 datatype CONSTRAINT, column2 datatype CONSTRAINT, ... columnn datatype CONSTRAINT);
b)
CREATE TABLE TABLE_NAME ( column1 datatype CONSTRAINT, column2 datatype CONSTRAINT, ... columnn datatype CONSTRAINT, CONSTRAINT CHECK (condition1 AND condition2));
c)
CREATE TABLE TABLE_NAME ( column1 datatype CONSTRAINT, column2 datatype CONSTRAINT, ... columnn datatype CONSTRAINT, CHECK (condition1 AND condition2));
d)
CREATE TABLE TABLE_NAME ( column1 datatype CONSTRAINT, column2 datatype CONSTRAINT, ... columnn datatype CONSTRAINT, CONSTRAINT constraint_name CHECK (condition1 AND condition2));
Explanation: The statement that can be used to apply the table level check constraint in SQL is –
CREATE TABLE TABLE_NAME ( column1 datatype CONSTRAINT, column2 datatype CONSTRAINT, ... columnn datatype CONSTRAINT, CONSTRAINT constraint_name CHECK (condition1 AND condition2));
The CHECK constraint is added at the last of the statement by the help of the CONSTRAINT keyword. Constraint name is any name the user gives to the constraint. Condition 1 is applied on the column1, Condition 2 is applied on the column2 and so on.
6. Which of the following statement can be used to apply the CHECK constraint on an existing table?
a)
ALTER TABLE TABLE_NAME ADD CONSTRAINT constraint_name CHECK (condition);
b)
ALTER TABLE TABLE_NAME ADD CONSTRAINT CHECK (condition);
c)
ALTER TABLE TABLE_NAME ADD CHECK (condition);
d)
ALTER TABLE TABLE_NAME ADD constraint_name CHECK (condition);
Explanation: The statement that can be used to apply the CHECK constraint on an existing table is –
ALTER TABLE TABLE_NAME ADD CONSTRAINT constraint_name CHECK (condition);
The ALTER command is used with the ADD and CONSTRAINT keyword to apply the CHECK constraint to an existing table or column. Constraint name is any name given to the constraint by the user.
7. Which of the following statement can be used to delete the CHECK constraint from an existing table?
a)
ALTER TABLE TABLE_NAME DROP constraint_name;
b)
ALTER TABLE TABLE_NAME DELETE CHECK constraint_name;
c)
ALTER TABLE TABLE_NAME DROP CHECK constraint_name;
d)
ALTER TABLE TABLE_NAME DELETE constraint_name;
Explanation: The statement that can be used to delete the CHECK constraint from an existing table is
ALTER TABLE TABLE_NAME DROP CHECK constraint_name;
The ALTER command is used with the DROP keyword to delete the CHECK constraint from an existing table. The constraint name of the CHECK constraint that needs to be deleted will be mentioned in the statement.
8. Consider the underlying statement and answer the question that follows –
CREATE TABLE employee ( Eid VARCHAR(10) PRIMARY KEY, Age INT CHECK (Age BETWEEN 21 AND 40));
What is the range of age of the employees that can be entered in the table employee?
a) 21 ≤ Age < 40
b) 21 < Age ≤ 40
c) 21 < Age < 40
d) 21 ≤ Age ≤ 40
View Answer
Explanation: In the above created table employee there is a CHECK constraint present on the column Age. This constraint only accepts the age when it is entered within the specified limit. Since, we used the BETWEEN operator, it includes both the lower limit and the upper limit in the range specified.
Sanfoundry Global Education & Learning Series – SQL.
To practice all areas of SQL, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]