Question

I have a column of email addresses that I need to delete (for privacy reasons) but I can't simply set an empty string to that each row because there's a unique key constraint on that column.

What's the easiest way of wiping data from the email address column without deleting the unique key constraint?

Keep in mind, the table has a composite primary key of 2 columns (let's call these two columns CK1 and CK2).

Was it helpful?

Solution

Maybe just replace them with something random?

UPDATE [YourTable]
   SET [Email] = REPLACE(CONVERT(varchar(36), NEWID()), '-', '') + '@fake.com'

That will give you something like:

3931BB9646304A949D9F4EBA81E9E798@fake.com

Or if your table has an identity column, plug that inherently unique value into a new fake e-mail address:

UPDATE [YourTable]
   SET [Email] = 'email' + CONVERT(varchar(20), [Id]) + '@fake.com'

Or disable the constraint (without deleting it) before clearing out the column:

ALTER INDEX [YourUniqueIndexName] ON [YourTable] DISABLE;

Although that last one might alter the behavior of any application(s) writing to that table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top