Torna al Thread
USE tempdb;
GO
CREATE TABLE dbo.Utenti(
UtenteID int NOT NULL IDENTITY(1, 1),
Operatore varchar(30) NOT NULL,
Nome varchar(30) NOT NULL,
Cognome varchar(30) NOT NULL,
CONSTRAINT PK_Utenti PRIMARY KEY(UtenteID),
CONSTRAINT UN_Operatore UNIQUE(Operatore)
);
GO
INSERT dbo.Utenti VALUES('Operatore 1', 'Lorenzo', 'Benaglia');
INSERT dbo.Utenti VALUES('Operatore 2', 'Luca', 'Bianchi');
INSERT dbo.Utenti VALUES('Operatore 3', 'Andrea', 'Montanari');
/* Questo comando genererà un errore di violazione del constraint UN_Operatore */
INSERT dbo.Utenti VALUES('Operatore 1', 'Gianluca', 'Hotz');
GO
/* Output:
Msg 2627, Level 14, State 1, Line 2
Violation of UNIQUE KEY constraint 'UN_Operatore'. Cannot insert duplicate key in object 'dbo.Utenti'.
The statement has been terminated.
*/
/* Vediamo... */
SELECT *
FROM dbo.Utenti;
GO
/* Output:
UtenteID Operatore Nome Cognome
----------- ------------- -------- ----------
1 Operatore 1 Lorenzo Benaglia
2 Operatore 2 Luca Bianchi
3 Operatore 3 Andrea Montanari
(3 row(s) affected)
*/
DROP TABLE dbo.Utenti;