MySQL Primary Key MCQ: Primary Key Ensures Data Integrity and Uniqueness in Database Tables

PROGRAMMING

3/20/20242 min read

eyeglasses and skeleton key on white book
eyeglasses and skeleton key on white book

Introduction to MySQL Primary Key

A primary key is a unique identifier for each record in a database table. It ensures that each row in a table is uniquely identified and helps in maintaining data integrity and enforcing data consistency. In MySQL, a primary key can be defined on one or more columns of a table.

MCQ Questions on MySQL Primary Key

Question 1:

What is a primary key in MySQL?

a) A key that uniquely identifies a record in a table

b) A key used for sorting data in a table

c) A key used for joining multiple tables

d) A key used for indexing columns in a table

Answer: a) A key that uniquely identifies a record in a table

Explanation: A primary key is a unique identifier for each record in a table.

Question 2:

How many primary keys can be defined in a table?

a) Only one

b) Multiple

c) None

d) Depends on the table structure

Answer: a) Only one

Explanation: In MySQL, a table can have only one primary key.

Question 3:

Can a primary key contain NULL values?

a) Yes

b) No

Answer: b) No

Explanation: A primary key cannot contain NULL values. It must have a value for each record.

Question 4:

What happens if a duplicate primary key is inserted?

a) The record is rejected

b) The primary key is automatically updated

c) The primary key is ignored

d) An error is thrown

Answer: d) An error is thrown

Explanation: MySQL throws an error if a duplicate primary key is inserted, as it violates the uniqueness constraint.

Question 5:

Can a primary key be modified?

a) Yes

b) No

Answer: b) No

Explanation: Once a primary key is defined, it cannot be modified. If required, the table structure needs to be altered.

Question 6:

What is the purpose of a primary key?

a) To enforce data consistency

b) To improve query performance

c) To ensure data integrity

d) All of the above

Answer: d) All of the above

Explanation: A primary key helps in enforcing data consistency, improving query performance, and ensuring data integrity.

Question 7:

Can a primary key be a composite key?

a) Yes

b) No

Answer: a) Yes

Explanation: In MySQL, a primary key can be a composite key, which means it can be defined on multiple columns.

Question 8:

What is the default index type for a primary key in MySQL?

a) B-tree

b) Hash

c) R-tree

d) None

Answer: a) B-tree

Explanation: The default index type for a primary key in MySQL is B-tree, which is suitable for most use cases.

Question 9:

Can a primary key be dropped?

a) Yes

b) No

Answer: a) Yes

Explanation: A primary key can be dropped using the ALTER TABLE statement in MySQL.

Question 10:

Is a primary key required for every table in MySQL?

a) Yes

b) No

Answer: b) No

Explanation: A primary key is not required for every table in MySQL. It depends on the specific requirements of the table and the data.