There are three ways to generate a UNIQUE column on a table.
1. When creating the table definition. For example:
CREATE TABLE [dbo].[Factura] (
[FacturaId] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[ExamenId] [int] NOT NULL UNIQUE,
[Fecha] [datetime] NOT NULL
)
2. After creating the table as follows:
CREATE TABLE [dbo].[Factura] (
[FacturaId] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[ExamenId] [int] NOT NULL ,
[Fecha] [datetime] NOT NULL
) ON [PRIMARY]
ALTER TABLE Factura
ADD UNIQUE (ExamenId)
3. Or, like this:
ALTER TABLE Factura
ADD CONSTRAINT UN_examenId UNIQUE (ExamenId)
In the first and second case SQL Server will create a deafult label for the unique column, for example something like UQ__Factura__29572725 .
In the third case we are explicitly applying the label UN_examenId for our unique column.
Apparently there’s another method to create a unique column, but I haven’t tested it. It’s as follows:
CREATE TABLE [dbo].[Factura] (
[FacturaId] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[ExamenId] [int] NOT NULL CONSTRAINT UN_Factura UNIQUE ,
[Fecha] [datetime] NOT NULL
) ON [PRIMARY]