loading

SQL Unique


SQL UNIQUE Constraint

To guarantee that every value in a column is unique, apply the UNIQUE constraint.

A column’s or a group of columns’ uniqueness is ensured by the UNIQUE and PRIMARY KEY constraints.

A UNIQUE constraint is inherently included in a PRIMARY KEY constraint.

On the other hand, only one PRIMARY KEY constraint and numerous unique constraints are allowed per table.


SQL UNIQUE Constraint on CREATE TABLE

When the “Persons” table is formed, the UNIQUE constraint on the “ID” column is created with the following SQL:

SQL Server / Oracle / MS Access:

----- EXM MUKAVA -----

MySQL:

----- EXM MUKAVA -----

Use the following SQL syntax to construct a unique constraint on multiple columns and to name the constraint:

MySQL / SQL Server / Oracle / MS Access:

----- EXM MUKAVA -----

SQL UNIQUE Constraint on ALTER TABLE

Once the table has been established, use the following SQL to create a UNIQUE constraint on the “ID” column:

MySQL / SQL Server / Oracle / MS Access:

				
					ALTER TABLE Persons
ADD UNIQUE (ID);
				
			

Use the following SQL syntax to construct a unique constraint on multiple columns and to name the constraint:

MySQL / SQL Server / Oracle / MS Access:

				
					ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);
				
			

DROP a UNIQUE Constraint

To remove a UNIQUE constraint, use the SQL code below:

MySQL:

				
					ALTER TABLE Persons
DROP INDEX UC_Person;
				
			

SQL Server / Oracle / MS Access:

				
					ALTER TABLE Persons
DROP CONSTRAINT UC_Person;
				
			
Share this Doc

SQL Unique

Or copy link

Explore Topic