Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Use Computed Column as Primary Key

$
0
0

I am trying to make my computed column (QuoteID) the primary key.  QuoteID is a concatenation of two fields as you can see below.  Below is how SQL Server 2008 scripts the CREATE TABLE without QuoteID being the primary key.  For some reason, I'm able to set QuoteID as the primary key in the designer but when I save it I get the message below, why?  I know you can have computed columns as a primary key according to MSDNhere.  How do I get this to save?

QuoteNumber is not nullable and is an identity field so it will never be null.

RevisionNumber has a default value of 1 so it will never be null.

How in the world will QuoteID ever be null?

USE [QuoteGenerator]
GO

/****** Object:  Table [dbo].[Quotes]    Script Date: 07/13/2013 19:25:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Quotes](
	[QuoteID]  AS (([QuoteNumber]+'-')+[RevisionNumber]) PERSISTED,
	[QuoteNumber] [int] IDENTITY(1,1) NOT NULL,
	[RevisionNumber] [tinyint] NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Quotes] ADD  CONSTRAINT [DF_Quotes_RevisionNumber]  DEFAULT ((1)) FOR [RevisionNumber]
GO

Thanks in advance


Ryan



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>