MySQL Database Quiz Questions

Course Name:MySQL
Chapter Name:Chapter 8 - RDBMS Concepts
Lesson Content Link:Primary Key
Current Quiz Count:30
Progress
0%
Q1
True / False

MySQL, a table can have multiple primary keys.

Q2
True / False

primary key in MySQL automatically creates a unique index for the column.

Q3
True / False

MySQL, a primary key column cannot contain NULL values

Q4
True / False

MySQL, a composite primary key is a primary key made up of multiple columns.

Q5
True / False

You can alter a table to add a primary key after the table has been created in MySQL.

Q6
True / False

MySQL, using AUTO_INCREMENT with a primary key column automatically assigns a unique value to each new row.

Q7
True / False

MySQL, you can define a primary key constraint on a view.

Q8
True / False

MySQL, you can drop a primary key constraint using the DROP CONSTRAINT statement.

Q9
True / False

In MySQL, a primary key can consist of up to 16 columns.

Q10
True / False

In MySQL, a primary key is both a unique index and a clustered index by default.

Q11
Single Choice

What is the purpose of a primary key in a MySQL database?

Q12
Single Choice

Which of the following statements correctly creates a primary key in a MySQL table?

Q13
Single Choice

Can a MySQL table have more than one primary key?

Q14
Single Choice

How can you add a primary key to an existing MySQL table?

Q15
Single Choice

Which constraint automatically enforces uniqueness for a primary key in MySQL?

Q16
Single Choice

What is a composite primary key in MySQL?

Q17
Single Choice

How do you define a composite primary key when creating a table in MySQL?

Q18
Single Choice

What happens if you try to insert a duplicate value into a column defined as the primary key in MySQL?

Q19
Single Choice

In MySQL, how can you remove a primary key from a table?

Q20
Single Choice

Which statement about primary keys and foreign keys in MySQL is correct?

Q21
Multiple Choice

Which of the following SQL statements correctly creates a table named 'customers' with 'customer_id' as the primary key?

SQL Code
CREATE TABLE customers (
 customer_id INT AUTO_INCREMENT,
 first_name VARCHAR(50),
 last_name VARCHAR(50),
 PRIMARY KEY (customer_id)
);
Q22
Multiple Choice

Identify the correct SQL code that ensures a composite primary key for the 'order_items' table.

SQL Code
CREATE TABLE order_items (
 order_id INT,
 product_id INT,
 quantity INT,
 PRIMARY KEY (order_id, product_id)
);
Q23
Multiple Choice

Which SQL statement is used to add a primary key constraint to an existing table named 'employees'?

SQL Code
ALTER TABLE employees
ADD PRIMARY KEY (employee_id);
Q24
Multiple Choice

Choose the SQL code that correctly drops a primary key constraint from the 'orders' table.

SQL Code
ALTER TABLE orders
DROP PRIMARY KEY;
Q25
Multiple Choice

Which SQL code snippet demonstrates how to handle a duplicate primary key error in a table insertion?

SQL Code
INSERT INTO users (user_id, username)
VALUES (1, 'alice');
INSERT INTO users (user_id, username)
VALUES (1, 'bob')
ON DUPLICATE KEY UPDATE username = 'bob';
Q26
Multiple Choice

Select the SQL code that correctly implements a self-referencing primary key in a hierarchical table structure.

SQL Code
CREATE TABLE categories (
 category_id INT AUTO_INCREMENT,
 category_name VARCHAR(100),
 parent_category_id INT,
 PRIMARY KEY (category_id),
 FOREIGN KEY (parent_category_id) REFERENCES categories(category_id)
);
Q27
Multiple Choice

Identify the SQL code that ensures data integrity by preventing null values in a primary key column.

SQL Code
CREATE TABLE employees (
 employee_id INT NOT NULL AUTO_INCREMENT,
 first_name VARCHAR(50),
 last_name VARCHAR(50),
 PRIMARY KEY (employee_id)
);
Q28
Multiple Choice

Which SQL statement is used to enforce a unique primary key across two columns in the 'student_grades' table?

SQL Code
CREATE TABLE student_grades (
 student_id INT,
 subject_id INT,
 grade CHAR(2),
 PRIMARY KEY (student_id, subject_id)
);
Q29
Multiple Choice

Choose the SQL code that properly enforces referential integrity using a primary key and foreign key relationship.

SQL Code
CREATE TABLE departments (
 department_id INT AUTO_INCREMENT,
 department_name VARCHAR(100),
 PRIMARY KEY (department_id)
);
CREATE TABLE employees (
 employee_id INT AUTO_INCREMENT,
 employee_name VARCHAR(100),
 department_id INT,
 PRIMARY KEY (employee_id),
 FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
Q30
Multiple Choice

Which SQL code properly handles auto-incrementing a primary key for the 'projects' table?

SQL Code
CREATE TABLE projects (
 project_id INT AUTO_INCREMENT,
 project_name VARCHAR(100),
 start_date DATE,
 PRIMARY KEY (project_id)
);