how to add unique constraint in sql code example
Example 1: sql server add unique constraint
-- Add a Unique constraint and deifine the constraint name
ALTER TABLE TableName
ADD CONSTRAINT TableName_ColumnName_Unique UNIQUE (ColumnName)
Example 2: how to add unique constraint in mysql table
ALTER TABLE mytbl ADD UNIQUE (columnName);
Example 3: how to drop a unique constraint in sql
SHOW CREATE TABLE [tableName]
-- Find the constraintID after CONSTRAINT 'constraintID'
ALTER TABLE [tableName] DROP FOREIGN KEY constraintID
Example 4: set column as unique in sql server
ALTER TABLE Persons
ADD UNIQUE (ID);
Example 5: unique constraint mssql
USE AdventureWorks2012;
GO
CREATE TABLE Production.TransactionHistoryArchive4
(
TransactionID int NOT NULL,
CONSTRAINT AK_TransactionID UNIQUE(TransactionID)
);
GO