Skip to content
Advertisement

Update Primary Key Column Name in SQL

This code defines Category table in SQL:

CREATE TABLE [dbo].[Categories] 
(
    [CategoryID]             INT IDENTITY (1, 1) NOT NULL,
    [Name]                   NVARCHAR(450) NULL,
    [SubCategoryName]        NVARCHAR(MAX) DEFAULT ('') NULL,
    [FurtherSubCategoryName] NVARCHAR(MAX) NULL,

    CONSTRAINT [PK_dbo.Categories] 
        PRIMARY KEY CLUSTERED ([CategoryID] ASC),
    CONSTRAINT [AK_UniqueName] 
        UNIQUE NONCLUSTERED ([Name] ASC)
);

I need to change [CategoryID] to [ID] i.e., change its name. How can this be accomplished?

Thanks in advance.

Advertisement

Answer

For versions SQL Server 2008 and above; you may execute the following stored procedure

exec sp_rename 'Categories.CategoryID', 'Id', 'COLUMN';

Reference SQL Docs

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement