Creating UNIQUE Constraints on Existing Tables : Unique « Constraints « SQL Server / T-SQL Tutorial






4>
5>
6>    CREATE TABLE Employees
7>    (
8>       EmployeeID       int           IDENTITY  NOT NULL,
9>       FirstName        varchar(25)             NOT NULL,
10>       MiddleInitial    char(1)                 NULL,
11>       LastName         varchar(25)             NOT NULL,
12>       Title            varchar(25)             NOT NULL,
13>       SSN              varchar(11)             NOT NULL,
14>       Salary           money                   NOT NULL,
15>       PriorSalary      money                   NOT NULL,
16>       LastRaise AS Salary - PriorSalary,
17>       HireDate         smalldatetime           NOT NULL,
18>       TerminationDate  smalldatetime           NULL,
19>       ManagerEmpID     int                     NOT NULL,
20>       Department       varchar(25)             NOT NULL
21>    )
22>    GO
1>
2>    ALTER TABLE Employees
3>       ADD CONSTRAINT AK_EmployeeSSN
4>       UNIQUE (SSN)
5>    GO
1>
2>    drop table Employees;
3>    GO








7.10.Unique
7.10.1.Adding a UNIQUE Constraint to an Existing Table
7.10.2.Unique constraints will also allow one NULL, whereas primary keys will not.
7.10.3.Unique Constraint: Enforce uniqueness on a column which is not the column used to primarily identify each row.
7.10.4.Unique constraints can be created when initially creating the table or added after.
7.10.5.Unique constraint
7.10.6.Creating a UNIQUE Constraints when creating a table
7.10.7.Creating UNIQUE Constraints on Existing Tables