Question 1
You want to create a 'Enrollments' table to link students to the courses they've enrolled in. Which of the following is the most appropriate and robust table definition for 'Enrollments'?
CREATE TABLE Enrollments (StudentID INT, CourseID INT,
PRIMARY KEY (StudentID, CourseID), FOREIGN KEY (StudentID)
REFERENCES Students(StudentID), FOREIGN KEY (CourseID)
REFERENCES Courses(CourseID));
CREATE TABLE Enrollments (StudentID INT, CourseID INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID));
CREATE TABLE Enrollments (StudentID INT, CourseID INT,
PRIMARY KEY (StudentID, CourseID));
CREATE TABLE Enrollments (EnrollmentID INT PRIMARY KEY,
StudentID INT, CourseID INT);
Question 2
Which of the following statements is true regarding the relationship between PRIMARY KEY and UNIQUE constraints in SQL?
A table can have multiple PRIMARY KEY constraints but only one UNIQUE constraint.
Both PRIMARY KEY and UNIQUE constraints allow multiple NULL values.
A PRIMARY KEY constraint is functionally equivalent to a UNIQUE constraint combined with a NOT NULL constraint.
A UNIQUE constraint automatically creates a clustered index, while a PRIMARY KEY creates a non-clustered index.
Question 3
You want to ensure that the price of any product is always greater than 0. Which of the following constraints is the most appropriate for this?
ALTER TABLE Products ADD CONSTRAINT
nn_price NOT NULL (Price);
ALTER TABLE Products ADD CONSTRAINT
unq_price UNIQUE (Price);
ALTER TABLE Products ADD CONSTRAINT
chk_price CHECK (Price > 0);
ALTER TABLE Products ADD CONSTRAINT
df_price DEFAULT 0.01 FOR Price;
Question 4
You want to ensure that every user has a unique email address, but some users might not have provided their email yet. Which of the following is the best way to define the 'Email' column?
Email VARCHAR(255) NOT NULL
Email VARCHAR(255) UNIQUE
Email VARCHAR(255) UNIQUE NOT NULL
Email VARCHAR(255) PRIMARY KEY
Question 5
The 'Employees' table has a 'DeptID' column that is a FOREIGN KEY referencing the 'DepartmentID' in the 'Departments' table. What happens if you try to delete a department that still has employees assigned to it?
The department and all its employees will be deleted.
The operation will fail due to a FOREIGN KEY constraint violation.
The department will be deleted, but the employees' 'DeptID' will remain unchanged, leading to orphaned records.
The department will be deleted, and the 'DeptID' for the affected employees will be set to NULL.
Question 6
Which of the following is NOT a valid use of a CHECK constraint?
Ensuring that a 'Gender' column can only contain 'Male', 'Female', or 'Other'.
Ensuring that a 'StartDate' is always before an 'EndDate' in the same row.
Ensuring that the sum of all values in a 'Salary' column does not exceed a certain amount.
Ensuring that a 'Discount' column is always between 0 and 1.
Question 7
In a table of 'Orders', you want to ensure that every order has a unique 'OrderNumber'. You also have an 'InvoiceNumber' which should also be unique, but not all orders have been invoiced yet. How would you define these columns?
OrderNumber INT PRIMARY KEY, InvoiceNumber INT PRIMARY KEY
OrderNumber INT NOT NULL, InvoiceNumber INT
OrderNumber INT UNIQUE, InvoiceNumber INT UNIQUE
OrderNumber INT PRIMARY KEY, InvoiceNumber INT UNIQUE
Question 8
You have a table with a 'Status' column that should only accept the values 'Active', 'Inactive', or 'Pending'. Which constraint should you use?
CREATE TABLE MyTable (Status VARCHAR(10) CHECK (Status IN ('Active', 'Inactive', 'Pending')));
CREATE TABLE MyTable (Status VARCHAR(10) NOT NULL);
CREATE TABLE MyTable (Status VARCHAR(10) DEFAULT 'Pending');
CREATE TABLE MyTable (Status VARCHAR(10) UNIQUE);
Question 9
You want to ensure that the bonus is never more than 20% of the salary. Which query would you use?
ALTER TABLE Employees ADD CONSTRAINT
unq_bonus UNIQUE (Bonus, Salary);
ALTER TABLE Employees ADD CONSTRAIN
chk_bonus CHECK (Bonus <= Salary * 0.2);
ALTER TABLE Employees ADD CONSTRAINT
chk_bonus CHECK (Bonus <= (SELECT Salary * 0.2
FROM Employees));
ALTER TABLE Employees ADD CONSTRAINT
fk_bonus FOREIGN KEY (Bonus)
REFERENCES Salaries(MaxBonus);
Question 10
You have a 'Products' table where each product has a unique 'ProductCode'. You also want to assign a unique 'SKU' to each product, but some products might not have an SKU assigned yet. Define the table.
CREATE TABLE Products (ProductCode VARCHAR(20) NOT NULL, SKU VARCHAR(20));
CREATE TABLE Products (ProductCode VARCHAR(20) UNIQUE, SKU VARCHAR(20) UNIQUE);
CREATE TABLE Products (ProductCode VARCHAR(20) PRIMARY KEY, SKU VARCHAR(20) UNIQUE);
CREATE TABLE Products (ProductCode VARCHAR(20) PRIMARY KEY, SKU VARCHAR(20) PRIMARY KEY);
There are 10 questions to complete.