Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

defining multi-column primary key

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to structure a CREATE TABLE statement such that three columns together will serve as the primary key. in SQL - The Complete Reference, p. 315 states:
"A PRIMARY KEY constraint can appear as a column constraint or a table constraint. If the primary key consists of a single column, the column constraint may be more convenient. If it consists of multiple columns, it should be specified as a table constraint."
I do not know how to define the table constraint in my CREATE.
Here is the SQL:
CREATE TABLE [dbo].[ContractInformation] (
[OwnerID] [int] NOT NULL,
[ClaimNumber] [varchar] (20) NOT NULL,
[ContractNetworkCode] [varchar] (3) NOT NULL,
[DateStamp] [datetime] NOT NULL ,
[SeqNum] [int] NULL ,
[NetworkCode] [varchar] (3) NULL ,
[Used] [varchar] (1) NULL ,
[DateSent] [datetime] NULL ,
[DateReceived] [datetime] NULL ,
[RepricedStatus] [int] NULL ,
[PPOBilledDate] [datetime] NULL ,
[PayerBilledDate] [datetime] NULL ,
[ReadyToBill] [varchar] (1) NULL ,
[ParticipatingPPO] [varchar] (1) NULL
) ON [PRIMARY]
GO
adding a single primary key is easy:
CREATE TABLE [dbo].[ContractInformation] (
[OwnerID] [int] NOT NULL PRIMARY KEY,
[ClaimNumber] [varchar] (20) NOT NULL,
[ContractNetworkCode] [varchar] (3) NOT NULL,
......
if I just add "PRIMARY KEY" in the same way to each of those first 3 columns, it fails, saying I can't create more than one primary key.
Anybody out there know how to write this? I'm hoping it will work in both SQL-Server and Oracle.
 
Peter Lyons
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, since I wasn't just sitting around waiting, I did find one way of accomplishing this. In case you're interested: http://www.1tehnicka.hr/estuff/books/0-672/0-672-30832-0/ch15.htm#E69E205
I found I can define a multi-column UNIQUE key by executing the following statement:
CREATE TABLE [dbo].[ContractInformation] (
[OwnerID] [int] NOT NULL,
[ClaimNumber] [varchar] (20) NOT NULL,
[ContractNetworkCode] [varchar] (3) NOT NULL,
[DateStamp] [datetime] NOT NULL ,
[SeqNum] [int] NULL ,
[NetworkCode] [varchar] (3) NULL ,
[Used] [varchar] (1) NULL ,
[DateSent] [datetime] NULL ,
[DateReceived] [datetime] NULL ,
[RepricedStatus] [int] NULL ,
[PPOBilledDate] [datetime] NULL ,
[PayerBilledDate] [datetime] NULL ,
[ReadyToBill] [varchar] (1) NULL ,
[ParticipatingPPO] [varchar] (1) NULL,
CONSTRAINT UK_OwnerID_ClaimNumber_ContractNetworkCode UNIQUE (OwnerID, ClaimNumber, ContractNetworkCode)
) ON [PRIMARY]
GO
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic