Reputation: 31
Can we drop and recreate a composite primary key in a SQL Server table in production environment? Do we have take down the server for it, or can we do it live?
Because we have to add more columns to the primary key. If we done it in live what are the problems we have to face?
Upvotes: 1
Views: 13717
Reputation: 2550
You can remove primary key from table. This will also remove clustered index of that table if you haven't mentioned explicitly on other column.
To remove primary key run below query
-- Drop CHECK CONSTRAINT from the table
ALTER TABLE /*schema*/./*table*/
DROP CONSTRAINT /*constraint_name*/
GO
and to add primary key run below code
-- Add a new CHECK CONSTRAINT to the table
ALTER TABLE /*schema*/./*table*/
ADD CONSTRAINT /*contraint_name*/ /*constraint_type*/ (/*constraint_column_name*/ /*logical_expression*/)
GO
Upvotes: 3