How can I change primary key on SQL Azure

前端 未结 5 1581
无人共我
无人共我 2021-02-07 04:44

I am going to change the primary key on SQL Azure. But it throws an error when using Microsoft SQL Server Management Studio to generate the scripts. Because every tables on SQL

5条回答
  •  长情又很酷
    2021-02-07 05:27

    You can try the following scripts. Change it to suit for your table def.

    EXECUTE sp_rename N'[PK_MyTable]', N'[PK_MyTable_old]',  'OBJECT'
    
    CREATE TABLE [dbo].[Temp_MyTable](
    [id] [int] NOT NULL,
    [text] [text] NOT NULL CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED (
    [id] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON))
    
    INSERT INTO dbo.[Temp_MyTable] (Id, Text)
    SELECT Id, Text FROM dbo.MyTable
    
    drop table dbo.MyTable
    EXECUTE sp_rename N'Temp_MyTable', N'MyTable', 'OBJECT'
    

提交回复
热议问题