Search⌘ K
AI Features

Answer: The UNIQUE Constraint

Explore how to apply and remove UNIQUE constraints on SQL table columns to ensure data uniqueness. Learn to modify tables with ALTER TABLE, add constraints during table creation, and understand best practices for enforcing unique data entries in your database.

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 INSERT INTO statement is followed by the table name, Employees, which will be modified. The VALUES clause specifies the values.

  • Line ...