Q1
True / FalseMySQL, a table can have multiple primary keys.
MySQL, a table can have only one primary key, which may consist of single or multiple columns.
Q2
True / Falseprimary key in MySQL automatically creates a unique index for the column.
When a primary key is defined, MySQL automatically creates a unique index for the primary key column(s).
Q3
True / FalseMySQL, a primary key column cannot contain NULL values
primary key column must contain unique values and cannot have NULL values, ensuring each record can be uniquely identified.
Q4
True / FalseMySQL, a composite primary key is a primary key made up of multiple columns.
composite primary key in MySQL is a primary key that consists of more than one column, used to uniquely identify a record.
Q5
True / FalseYou can alter a table to add a primary key after the table has been created in MySQL.
You can use the ALTER TABLE statement to add a primary key to an existing table in MySQL
Q6
True / FalseMySQL, using AUTO_INCREMENT with a primary key column automatically assigns a unique value to each new row.
The AUTO_INCREMENT attribute in MySQL automatically generates a unique value for each new row inserted into a table
Q7
True / FalseMySQL, you can define a primary key constraint on a view.
Primary keys can only be defined on tables, not views, as views do not store data themselves.
Q8
True / FalseMySQL, you can drop a primary key constraint using the DROP CONSTRAINT statement.
MySQL, you drop a primary key constraint using the ALTER TABLE statement with DROP PRIMARY KEY.
Q9
True / FalseIn MySQL, a primary key can consist of up to 16 columns.
In MySQL, a primary key can consist of up to 64 columns, as defined by the database constraints
Q10
True / FalseIn MySQL, a primary key is both a unique index and a clustered index by default.
In MySQL, a primary key is implicitly a unique index, and in InnoDB (the default storage engine), it is also a clustered index, meaning the table data is stored in the order of the primary key.
Q17
Single ChoiceHow do you define a composite primary key when creating a table in MySQL?
The correct syntax to define a composite primary key in MySQL is to list multiple columns within the PRIMARY KEY clause.
Q21
Multiple ChoiceWhich 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)
);
In MySQL, the PRIMARY KEY constraint uniquely identifies each record in the table. The correct SQL statement should define the primary key after specifying the column data types.
Q22
Multiple ChoiceIdentify 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)
);
A composite primary key consists of two or more columns used together as a unique identifier for the records. The code correctly defines a composite primary key for the 'order_items' table.
Q25
Multiple ChoiceWhich 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';
In MySQL, the 'ON DUPLICATE KEY UPDATE' clause allows you to specify how to handle duplicate key errors, such as updating an existing record if a duplicate primary key is detected.
Q26
Multiple ChoiceSelect 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)
);
A self-referencing primary key, also known as a recursive foreign key, is used in hierarchical table structures where a record in a table references another record in the same table. The code correctly implements this relationship in the 'categories' table.
Q27
Multiple ChoiceIdentify 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)
);
In MySQL, a primary key automatically implies NOT NULL, ensuring that no null values can be entered into the primary key column. The code snippet explicitly includes the NOT NULL constraint for clarity.
Q28
Multiple ChoiceWhich 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)
);
In MySQL, a composite primary key can be created using the PRIMARY KEY clause across two or more columns, ensuring that the combination of values is unique for each record.
Q29
Multiple ChoiceChoose 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)
);
Referential integrity in MySQL is maintained by linking two tables with a primary key in one table and a corresponding foreign key in another. This ensures consistency and accuracy of the data.
Q30
Multiple ChoiceWhich 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)
);
In MySQL, the AUTO_INCREMENT attribute is used with primary keys to automatically generate unique values for new records. This is essential for maintaining the integrity and uniqueness of the primary key column.