title: SQL Server Computed Column as Primary Key
body: <p>I've created a table with a computed column as the primary key.
  Table is created fine.And here is the script..</p>
  
  <pre><code>SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER ON
  GO
  
  SET ARITHABORT ON
  GO
  
  SET ANSI_PADDING ON
  GO
  
  CREATE TABLE [planning.A062].[RMAllocation](
      [Id] [int] IDENTITY(100,1) NOT NULL,
      [RMAllocatonId]  AS ('RMA_'+CONVERT([nvarchar](100),[Id])) PERSISTED NOT NULL,
      [RequsitionNo] [nvarchar](100) NULL,
      [RMDemandId] [nvarchar](104) NULL,
      [HierarchyId] [nvarchar](102) NULL,
      [Season] [nvarchar](50) NULL,
      [VendorSupplierNo] [nvarchar](100) NULL,
      [Year] [int] NULL,
      [Month] [int] NULL,
      [Week] [int] NULL,
      [Day] [int] NULL,
      [PlannedQty] [int] NULL,
      [ConfirmedQty] [int] NULL,
      [Status] [int] NULL,
      [CreatedBy] [int] NULL,
      [SyncId] [nvarchar](100) NULL,
      [CreatedOn] [datetime2](7) NULL,
      [UpdatedBy] [int] NULL,
      [UpdatedOn] [datetime2](7) NULL,
      [IsActive] [bit] NULL,
      [RecordDateTime] [datetime2](7) NULL,
   CONSTRAINT [PK_RMAllocation] PRIMARY KEY CLUSTERED 
  (
      [RMAllocatonId] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  
  GO
  
  SET ANSI_PADDING OFF
  GO
  </code></pre>
  
  <p>The problem is when I change this table (Add/edit a column ) using Designer View,it gives me the following error.</p>
  
  <p><img src="https://i.stack.imgur.com/m6vhq.jpg" alt="enter image description here"></p>
  
  <p>error </p>
  
  <pre><code>Unable to create index 'PK_RMAllocation'.  
  Cannot define PRIMARY KEY constraint on nullable column in table 'RMAllocation'.
  Could not create constraint. See previous errors.
  </code></pre>
  
  <p>When I use script to do modifications,it works. And Even I have declared the computed column as <code>NOT NULL</code>. How this happen??</p>