SQL Questions and Answers – SQL Primary Key

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

1. In SQL, which of the following constraint is used to uniquely identify each record in a table?
a) UNIQUE
b) PRIMARY KEY
c) NOT NULL
d) CHECK
View Answer

Answer: b
Explanation: In SQL, the PRIMARY KEY constraint is used to uniquely identify each record in a table. The column on which the PRIMARY KEY constraint has been applied, cannot accept any null or duplicate value. The CHECK constraint is used to limit the value range that can be placed in a column. The UINQUE and the NOT NULL constraint restricts duplicate and null entry in a column respectively.

2. In SQL, a table can have more than one primary key.
a) True
b) False
View Answer

Answer: b
Explanation: In SQL, a table can have only one primary key, although the primary key can consist of more than one column. The primary key made up of combination of more than one column is called as Composite primary key.

3. In SQL, the PRIMARY KEY constraint can be defined while creating a table and also while altering the table’s structure.
a) True
b) False
View Answer

Answer: a
Explanation: In SQL, the PRIMARY KEY constraint can be defined while creating a table and also while altering the table’s structure. While creating a table we define the primary key by the help of the CREATE command. While altering the table’s structure we use the ALTER command to define the primary key.

4. Which of the following statement can be used to apply the primary key constraint to a column?
a)

CREATE TABLE TABLE_NAME (
column1 datatype NOT NULL PRIMARY KEY, 
column2 datatype, 
...
columnn datatype);

b)

CREATE TABLE TABLE_NAME (
PRIMARY KEY column1 datatype NOT NULL, 
column2 datatype, 
...
columnn datatype);

c)

advertisement
advertisement
CREATE TABLE TABLE_NAME PRIMARY KEY (
column1 datatype NOT NULL, 
column2 datatype, 
...
columnn datatype);

d)

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

advertisement
CREATE TABLE TABLE_NAME (
column1 datatype NOT NULL PRIMARY KEY, 
column2 datatype, 
...
columnn datatype);

The PRIMARY KEY constraint is applied after the datatype of a column.

advertisement

5. Which of the following statement can be used to make a composite PRIMARY KEY?
a)

CREATE TABLE TABLE_NAME (
column1 datatype constraint_type,
column2 datatype constraint_type,
...
columnn datatype constraint_type,  
constraint_name PRIMARY KEY (column1, column2));

b)

CREATE TABLE TABLE_NAME (
column1 datatype constraint_type,
column2 datatype constraint_type,
...
columnn datatype constraint_type,  
CONSTRAINT constraint_name PRIMARY KEY);

c)

CREATE TABLE TABLE_NAME (
column1 datatype PRIMARY KEY,
column2 datatype PRIMARY KEY,
...
columnn datatype constraint_type);

d)

CREATE TABLE TABLE_NAME (
column1 datatype constraint_type,
column2 datatype constraint_type,
...
columnn datatype constraint_type,  
CONSTRAINT constraint_name PRIMARY KEY (column1, column2));
View Answer
Answer: d
Explanation: The statement that can be used to make a composite PRIMARY KEY in SQL is –

CREATE TABLE TABLE_NAME (
column1 datatype constraint_type,
column2 datatype constraint_type,
...
columnn datatype constraint_type,  
CONSTRAINT constraint_name PRIMARY KEY (column1, column2));

The columns that will be used to make the primary key are written within the PRIMARY KEY constraint. Constraint name is a name given to the primary key by the user.

6. Which of the following statement can be used to apply the PRIMARY KEY constraint to a column of an existing table?
a)

ALTER TABLE TABLE_NAME
ADD column_name PRIMARY KEY;

b)

ALTER TABLE TABLE_NAME
column_name ADD PRIMARY KEY;

c)

ALTER TABLE TABLE_NAME
ADD PRIMARY KEY (column_name);

d)

ALTER TABLE Persons
PRIMARY KEY (ID);
View Answer
Answer: c
Explanation: The statement that can be used to apply the PRIMARY KEY constraint to a column of an existing table is –

ALTER TABLE TABLE_NAME
ADD PRIMARY KEY (column_name);

We use the ALTER command to add the primary key constraint to an existing table.

7. Which of the following statement can be used to drop the PRIMARY KEY constraint form a table?
a)

ALTER TABLE TABLE_NAME
DROP PRIMARY KEY;

b)

ALTER TABLE TABLE_NAME
DROP PRIMARY KEY (column_name);

c)

ALTER TABLE TABLE_NAME
DELETE PRIMARY KEY;

d)

ALTER TABLE TABLE_NAME
DELETE PRIMARY KEY (column_name);
View Answer
Answer: a
Explanation: The statement that can be used to drop the primary key constraint from a table is –

ALTER TABLE Persons
DROP PRIMARY KEY;

We use the ALTER plus DROP command to delete the primary key constrain from the table. There is no need to specify the column at which the constraint has been applied.

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]

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.