UNIQUE
UNIQUE is a constraint in SQL that ensures all values in a column are different, prohibiting duplicate entries in the specified column within the table.
Example
Output
Explanation
In the given example, the SQL UNIQUE constraint is used to prevent two entries from having the same ID in the ‘Employees’ table. Duplicate insertion for ID ‘1’ is attempted and results in the displayed error, since UNIQUE constraint ensures the uniqueness of the data in a column.
Example
Output
Explanation
In this example, a table named Employees
is created with columns ID
, Name
, and Dept
. The UNIQUE
constraint is added on ID
ensuring that no two records will have identical ID
values within this table. This enforcement of uniqueness prevents duplicate entries for the employee’s ID
. If an attempt is made to insert a duplicate ID
, the database will stop the action and throw an error. This can help to maintain the integrity of the data within the table.
Example
Output
The table ‘Employees’ has been created. No data output is expected from a CREATE TABLE statement in SQL Server.
Explanation
In this example, theUNIQUE
keyword is used to create a unique constraint for the Emp_ID
and Emp_Name
fields in the Employees
table. This constraint ensures that no two records have the same combination of Emp_ID
and Emp_Name
values, thereby enforcing data integrity in the Employees
table.
Example
Output
Explanation
In the example above, a table called Employees is created with a unique constraint on the ID field. As a result, when an attempt is made to insert a new record with an ID that already exists in the table (in this case, ID 4), an ORA-00001: unique constraint violated
error is displayed. The unique constraint ensures that no two records in the table have the same ID value.
Example
Output
Explanation
The UNIQUE keyword in SQLite enforces the uniqueness of the values in a column. No duplicate entries are permitted in the column where the UNIQUE keyword is included. In the example, when trying to insert a duplicate email address for a different employee, SQLite shows an error because of the UNIQUE constraint on the Email column.