Toadd a primary key constraint after table creation, we use theALTER TABLEstatement.
Example Usage:
sql
ALTER TABLE Employees
ADD CONSTRAINT PK_Employees PRIMARY KEY (EmpID);
Thisaddsa primary key to the EmpID columnafter the table was created.
Why Other Options Are Incorrect:
Option B (CREATE TABLE) (Incorrect):Used for defining constraintsduringtable creation, not after.
Option C (UPDATE) (Incorrect):Modifiesrow values, not constraints.
Option D (INSERT INTO) (Incorrect):Used toadd datato a table, not modify constraints.
Thus, the correct answer isALTER TABLE, as itmodifies table structure to add a primary key constraint.
[Reference:SQL ALTER TABLE and Constraints., ]
Question 2
Which clause is used to specify the join columns when performing a join in MySQL?
Options:
A.
AS
B.
JOIN
C.
ON
D.
AND
Answer:
C
Explanation:
When performing aJOIN operationin MySQL, the ON clause specifies thejoining condition, defining which columns from both tables should be matched.
Example:
sql
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
JOIN Departments ON Employees.DepartmentID = Departments.ID;
Option A (Incorrect):AS is used foraliasing tables and columns, not for specifying join conditions.
Option B (Incorrect):JOIN defines the type of join (INNER JOIN, LEFT JOIN, etc.), butdoes not specify the columns.
Option C (Correct):The ON clause is used tospecify the join conditionbetween two tables.
Option D (Incorrect):AND is used in filtering conditions, not for joining tables.
[Reference:MySQL JOIN operations., ]
Question 3
Which type of join selects all the rows from both the left and right table, regardless of match?
Options:
A.
Full Join
B.
Outer Join
C.
Inner Join
D.
Cross Join
Answer:
A
Explanation:
AFull Join (FULL OUTER JOIN)selectsall records from both tables, filling in NULL values where there is no match. This ensures that no data is lost from either table.
Example Usage:
sql
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments ON Employees.DeptID = Departments.ID;
This query retrievesall employees and all departments, even if an employeehas no assigned departmentor a departmenthas no employees.
Types of Joins:
FULL OUTER JOIN (Correct Answer)→ Includesall rows from both tables, filling missing values with NULL.
LEFT JOIN (Incorrect)→ Includesall rows from the left tableandmatching rows from the right table.
RIGHT JOIN (Incorrect)→ Includesall rows from the right tableandmatching rows from the left table.
CROSS JOIN (Incorrect)→ Produces aCartesian product(each row from one table is combined with every row from another table).
Thus, the correct answer isFULL JOIN, whichensures that all rows from both tables appear in the result.