Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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
reply
    Bookmark Topic Watch Topic
  • New Topic