|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:43 AM
Points: 226,
Visits: 57
|
|
Hi,
I have a table called GN_CarParkingAreas Fields: 1. CarParkId int PK 2. CarParkNo varchar(10) not null 3. DevelopmentID int not null 4. Notes varchar(100) null
There are 10 records in the table.
Now I need to add a unique constraint in the table with UNIQUE(CarParkNo,DevelopmentID) There are rows in the table violating this. I need to check this constraint only for the new records and ignore the values existing in the table.
I tried ALTER TABLE GN_CarParkingAreas WITH NOCHECK ADD CONSTRAINT UC_CarParkNo UNIQUE (CarParkNo,DevelopmentID) GO but falied with error Msg 1505, Level 16, State 1, Line 2 The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.GN_CarParkingAreas' and the index name 'UC_CarParkNo'. The duplicate key value is (1, 40). Msg 1750, Level 16, State 0, Line 2 Could not create constraint. See previous errors. The statement has been terminated.
How can I do this? Please help.
Thanks In Advance, Anju Renjith.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Today @ 3:17 AM
Points: 151,
Visits: 1,035
|
|
Anju Renjith (1/29/2013) ... I need to check this constraint only for the new records and ignore the values existing in the table.
You can't have part of a table with constraint and part without. Create a new table with constrain for new data and you can eg: create a view that will hide for select that there are two tables.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 12:03 PM
Points: 412,
Visits: 862
|
|
It seems you have duplicate data in the columns and you will not able to create the unique index on a column with duplicate rows.
You might consider delete the duplicate data
Suppose,
Pk colmn, col1, col2
1 , 2, 40 2 , 2, 40
So the pk clustered index on pkcolumn is fine and unique cannot be created on col1 and col2..
Hope it helps.
--SQLFRNDZ
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 12:03 PM
Points: 412,
Visits: 862
|
|
Sorry, i haven't seen that line to ignore the current data ...and create a unique index..sorry that is not possible for the table with that data.
--SQLFRNDZ
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:43 AM
Points: 226,
Visits: 57
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 12:03 PM
Points: 412,
Visits: 862
|
|
If you want to create a index for certain search criteria then you could use filtered index(non clustered) for specific type of data.
may be helpful
here
--SQLFRNDZ
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:55 AM
Points: 13,381,
Visits: 25,165
|
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:43 AM
Points: 226,
Visits: 57
|
|
| Thanx all for ur quick response!!!
|
|
|
|