Search⌘ K
AI Features

Answer: The UNIQUE Constraint

Explore how to effectively use the UNIQUE constraint in SQL to ensure data integrity. This lesson teaches modifying tables with ALTER TABLE to add or remove UNIQUE constraints, applying them to single or multiple columns, and alternatives to ensure uniqueness during data insertion.

Solution

The solution is given below:

MySQL
/* Applying UNIQUE contraint on EmpName */
ALTER TABLE Employees ADD UNIQUE (EmpName);
/* Inserting a new record in the table */
INSERT INTO Employees VALUES (5, 'Susan Lee', 5000);
/* Retrieve the records in the table */
SELECT * FROM Employees;

Explanation

The explanation of the solution code is given below:

  • Line 2: The ALTER TABLE query modifies a table. ADD is used to add a constraint. UNIQUE takes in a column name as a parameter and applies a unique constraint on that column.

  • Line 5: The  ...