I have an issue related to data in sql server. In my database some of the constraint were not enabled i.e. they were not checked , After some time working on it we found this issue that a parent rows can be deleted without deleting child, which was an issue. I enabled all the constraint in the database using query
ALTER TABLE tbl_name CHECK CONSTRAINT ALL
above query was executed on all the tables of that database without any error . But my concern is whether it will work or not , if it will work on the existing data then what will happen to that data whose parent table data has been deleted.
I want to know is there any way such that I can validate such data data whose parent record doesn’t exist in the entire database. There are about 270 constraint containing FOREIGN KEY AND UNIQUE KEY . I don’t want to go for manual option.
Please help me out.
only re-enables the constraints. Importantly, the constraints are not checked against the existing data in the database (nor are they trusted by the optimizer). If you want that to occur, you need to specify
WITH CHECKas well:(And yes, the word
CHECKappears twice)If you execute this, and there are orphaned child rows (or other invalid constraints), then the
ALTER TABLEwill fail with an error message. There’s nothing SQL Server can do to fix this issue – it’s for you to decide whether to a) remove the orphaned rows, or b) to re-create, in some manner, a suitable parent row for them.