vijay1234
Starting Member
48 Posts |
Posted - 2014-06-12 : 08:50:46
|
Hi Fred,I Still want to have Unique Constraint on the column 'Name', but should allow NULL values & Duplicate entries. As the Datatype is declared varchar(100) NULL.I have altered to allow NULL values by creating one more computed column. It's working fine. But i need one more thing to allow duplicate records.Please find my code belowUSE chsdirectoryCreate Table Emp([Employee id] int not NULL constraint Emp_pk primary key clustered,[First Name] varchar(100) NULL,[Last Name] varchar(100) NULL,[Nick Name] varchar(100) NULL,[Social Security Number] int NULL)goAlter table Emp Add constraint ssn_unique UNIQUE ([Social Security Number]) Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(1,'Robert','Bates','Bob',111213422)Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(2,'Robert','Bates','Bob',121213422)Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(3,'Robert','William','Rob',131213422)Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(4,'Sonia','Keira','Sony',131413426)Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(5,'Mellisa','Brown','Mel',NULL)Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(6,'Sibey','Chikhs','Ciby',NULL)Alter table Emp Add MySSN as case when [Social Security Number] is NULL then [Employee id] else [Social Security Number] endgoAlter table Emp Add constraint ssn_unique1 UNIQUE ([MySSN] )goInsert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(7,'Harsha','Sree','Bang',NULL)Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(8,'Sunil','Kiran','Vij',NULL)Insert into Emp ([Employee id],[First Name],[Last Name],[Nick Name],[Social Security Number])values(9,'SonKirania','Keira','Sony',131413426)alter table Empdrop constraint ssn_unique alter table EmpADD Deleted bit NOT NULL default (0)select * from EmpRegards,Vijay |
|
|